Factoring out noise in an algorithm - java

I essentially have a bunch of data objects which map timestamps in milliseconds to float values. I'm looking to essentially find the peak/max of the data in a given range. I've been essentially using something like this:
float previousValue = 0;
for (int i = 0; i < data.size(); i++) {
MyData value = data.get(i);
if (value.getData() < previousValue) {
// found the peak!
break;
} else {
previousValue = value.getData();
}
}
The only problem with this algorithm is that it doesn't account for noise. Essentially, I could have values like this:
[0.1025, 0.3000, 0.3025, 0.3500, 0.3475, 0.3525, 0.1025]
The actual peak is at 0.3525, but my algorithm above would see it as 0.3500, as it comes first. Due to the nature of my calculations, I can't just do max() on the array and find out the largest value, I need to find the largest value that comes first before it falls.
How can I find the top of my peak, while accounting for some variance in noise?

There are two issues:
filtering out the noise;
finding the peak.
It seems like you already have a solution for 2, and need to solve 1.
To filter out the noise, you need some kind of low-pass filter. A moving average is one such filter. For example, exponential moving average is very easy to implement and should work well.
In summary: put your series through the filter, and then apply the peak finding algorithm.

an easier method to find a single peak (or the highest value) in an array (any numeric array: int, double) is to loop through the array and set a variable to the highest value...
Example: (all examples use a float array called "data")
float highest = 0; //use a number equal to or below the lowest possible value
for (int i = 0; i < data.length; i++){
if (data[i] > highest){
highest = data[i];
}
}
to find multiple peaks in noisy data filtering some of the noise out I used this method:
boolean[] isPeak = new boolean[20]; // I am looking for 20 highest peaks
float[] filter = new float[9]; // the range to which I want to define a peak is 9
float[] peaks = new float[20]; // again the 20 peaks I want to find
float lowpeak = 100; // use a value higher than the highest possible value
// first we start the filter cycling through the data
for (int i = 0; i < data.length; i++){
for (int a = filter.length-1; a > 0; a--){
filter[a] = filter[a-1];
}
filter[0] = data[1]
// now we check to see if the filter detects a peak
if (filter[4]>filter[0] && filter[4]>filter[1] && filter[4]>filter[2] &&
filter[4]>filter[3] && filter[4]>filter[5] && filter[4]>filter[6] &&
filter[4]>filter[7] && filter[4]>filter[8]){
// now we find the lowest peak
for (int x = 0; x < peaks.lengt-1; x++){
if (peaks[x] < lowpeak){
lowpeak = peaks[x];
}
}
// now we check to see if the peak is above the lowest peak
for (int x = 0; x < peaks.length; x++){
if (peaks[x] > lowpeak && peaks[x] != peaks[x+1]){
for (int y = peaks.length-1; y > 0 && !isPeak[y]; y--){
peaks[y] = peaks[y-1];
}
peaks[0] = filter[4];
}
}
}
}
this may not be the most efficient way to do this but it gets the job done!

Related

Smallest element in largest row

I came across this problem in class and I'm stuck on it. I did plenty of research but I'm not being able to fix my code.
I need to create a matrix and find the smallest value in the row of the largest value (I believe this element is called minimax). I'm trying to do with a simple 3 x 3 matrix. What I have so far:
Scanner val = new Scanner(System.in);
int matrizVal[][] = new int[3][3];
for (int a = 0; a < matrizVal.length; a++) {
for (int b = 0; b < matrizVal.length; b++) {
System.out.print("(" + a + ", " + b + "): ");
matrizVal[a][b] = val.nextInt();
}
}
int largest = matrizVal[0][0];
int largestrow = 0;
int arr[] = new int[2];
for (int row = 0; row < matrizVal.length; row++){
for (int col = 0; col < matrizVal.length; col++){
if (largest < matrizVal[row][col]){
largest = matrizVal[row][col];
largestrow = row;
}
}
}
To find the so called minimax element I decided to create a for each loop and get all the values of largestrow except the largest one.
for (int i : matrizVal[largestrow]){
if (i != largest){
System.out.print(i);
}
}
Here's where I'm stuck! I'd simply like to 'sort' this integer and take the first value and that'd be the minimax. I'm thinking about creating an array of size [matrizVal.length - 1], but not sure if it's gonna work.
I did a lot of research on the subject but nothing seems to help. Any tips are welcome.
(I don't think it is but I apologize if it's a duplicate)
Given the code you have provided, matrizVal[largestrow] should be the row of the matrix that contains the highest valued element.
Given that your task is to extract the smallest value in this array, there are a number of options.
If you want to simply extract the minimum value, a naive approach would go similarly to how you determined the maximum value, just with one less dimension.
For example:
int min = matrizVal[largestrow][0];
for (int i = 0; i < matrizVal.length; i++) {
if (matrizVal[largestrow][i] < min) {
min = matrizVal[largestrow][i];
}
}
// min will be the target value
Alternatively, if you want to sort the array such that the first element of the array is always the smallest, first ensure that you're making a copy of the array so as to avoid mutating the original matrix. Then feel free to use any sorting algorithm of your choice. Arrays.sort() should probably suffice.
You can simplify your approach by scanning each row for the maximum and minimum values in that row and then deciding what to do with those values based on the maximum value found in previous rows. Something like this (untested) should work:
int largestValue = Integer.MIN_VALUE;
int smallestValue = 0; // anything, really
for (int[] row : matrizVal) {
// First find the largest and smallest value for this row
int largestRowValue = Integer.MIN_VALUE;
int smallestRowValue = Integer.MAX_VALUE;
for (int val : row) {
smallestRowValue = Math.min(smallestRowValue, val);
largestRowValue = Math.max(largestRowValue, val);
}
// now check whether we found a new highest value
if (largestRowValue > largestValue) {
largestValue = largestRowValue;
smallestValue = smallestRowValue;
}
}
This doesn't record the row index, since it didn't sound like you needed to find that. If you do, then replace the outer enhanced for loop with a loops that uses an explicit index (as with your current code) and record the index as well.
I wouldn't bother with any sorting, since that (1) destroys the order of the original data (or introduces the expense of making a copy) and (2) has higher complexity than a one-time scan through the data.
You may want to consider a different alternative using Java 8 Stream :
int[] maxRow = Arrays.stream(matrizVal).max(getCompertator()).get();
int minValue = Arrays.stream(maxRow).min().getAsInt();
where getCompertator() is defined by:
private static Comparator<? super int[]> getCompertator() {
return (a1, a2)->
Integer.compare(Arrays.stream(a1).max().getAsInt(),
Arrays.stream(a2).max().getAsInt()) ;
}
Note that it may not give you the (undefined) desired output if two rows include the same highest value .

Time complexity on iterative and recursive solution

I'm trying to solve the following problem:
I feel like I've given it a lot of thoughts and tried a lot of stuff. I manage to solve it, and produce correct values but the problem is that it isn't time efficient enough. It completes 2 out of the Kattis tests and fails on the 3 because of the time limit 1 second was exceeded. There is noway for me to see what the input was that they tested with I'm afraid.
I started out with a recursive solution and finished that. But then I realised that it wasn't time efficient enough so I instead tried to switch to an iterative solution.
I start with reading input and add those to an ArrayList. And then I call the following method with target as 1000.
public static int getCorrectWeight(List<Integer> platesArr, int target) {
/* Creates two lists, one for storing completed values after each iteration,
one for storing new values during iteration. */
List<Integer> vals = new ArrayList<>();
List<Integer> newVals = new ArrayList<>();
// Inserts 0 as a first value so that we can start the first iteration.
int best = 0;
vals.add(best);
for(int i=0; i < platesArr.size(); i++) {
for(int j=0; j < vals.size(); j++) {
int newVal = vals.get(j) + platesArr.get(i);
if (newVal <= target) {
newVals.add(newVal);
if (newVal > best) {
best = newVal;
}
} else if ((Math.abs(target-newVal) < Math.abs(target-best)) || (Math.abs(target-newVal) == Math.abs(target-best) && newVal > best)) {
best = newVal;
}
}
vals.addAll(newVals);
}
return best;
}
My question is, is there some way that I can reduce the time complexity on this one for large number of data?
The main problem is that the size of vals and newVals can grow very quickly, as each iteration can double their size. You only need to store 1000 or so values which should be manageable. You're limiting the values but because they're stored in an ArrayList, it ends up with a lot of duplicate values.
If instead, you used a HashSet, then it should help the efficiency a lot.
You only need to store a DP table of size 2001 (0 to 2000)
Let dp[i] represent if it is possible to form ikg of weights. If the weight goes over the array bounds, ignore it.
For example:
dp[0] = 1;
for (int i = 0; i < values.size(); i++){
for (int j = 2000; j >= values[i]; j--){
dp[j] = max(dp[j],dp[j-values[i]);
}
}
Here, values is where all the original weights are stored. All values of dp are to be set to 0 except for dp[0].
Then, check 1000 if it is possible to make it. If not, check 999 and 1001 and so on.
This should run in O(1000n + 2000) time, since n is at most 1000 this should run in time.
By the way, this is a modified knapsack algorithm, you might want to look up some other variants.
If you think too generally about this type of problem, you may think you have to check all possible combinations of input (each weight can be included or excluded), giving you 2n combinations to test if you have n inputs. This is, however, rather beside the point. Rather, the key here is that all weights are integers, and that the goal is 1000.
Let's examine corner cases first, because that limits the search space.
If all weights are >= 1000, pick the smallest.
If there is at least one weight < 1000, that is always better than any weight >= 2000, so you can ignore any weight >= 1000 for combination purposes.
Then, apply dynamic programming. Keep a set (you got HashSet as suggestion from other poster, but BitSet is even better since the maximum value in it is so small) of all combinations of the first k inputs, and increase k by combining all previous solutions with the k+1'th input.
When you have considered all possibilities, just search the bit vector for the best response.
static int count() {
int[] weights = new int[]{900, 500, 498, 4};
// Check for corner case to limit search later
int min = Integer.MAX_VALUE;
for (int weight : weights) min = Math.min(min, weight);
if (min >= 1000) {
return min;
}
// Get all interesting combinations
BitSet combos = new BitSet();
for (int weight : weights) {
if (weight < 1000) {
for (int t = combos.previousSetBit(2000 - weight) ; t >= 0; t = combos.previousSetBit(t-1)) {
combos.set(weight + t);
}
combos.set(weight);
}
}
// Pick best combo
for (int distance = 0; distance <= 1000; distance++) {
if (combos.get(1000 + distance)) {
return 1000 + distance;
}
if (combos.get(1000 - distance)) {
return 1000 - distance;
}
}
return 0;
}

Trying to determine smallest amount and position in a Java array

I am new to the site, and new to programming in general, so please be gentle. I'm doing an assignment and I've built everything it needs, but I'm having problems with one particular part.
So the code below is just the for loop part of the overall program that I'm having trouble with. I created an array to store the user's quarterly input. When I go to determine which quarter had the largest rainfall, the code works no problem.
Where I'm having problems however is when I try to determine the quarter with the least amount. I basically set them both up the same way, but it doesn't work. I'm sure I must have a logic problem somewhere in there, but I've racked my brain trying to figure it out. Any help would be greatly appreciated. I'm sure its something small and stupid that I'm doing.
double largest=0;
for (int k = 0; k < quarterlyArray.length; k++) { // Determines the largest quarterly rainfall.
if ( quarterlyArray[k] >= largest ) {
largest = quarterlyArray[k];
}
}
for (int k = 0; k < quarterlyArray.length; k++) { // Determines in which quarter had largest rainfall.
if (quarterlyArray[k] == largest) {
System.out.println("Quarter " + (k+1) + " saw the most rain this year with " + largest + " inches.");
}
}
double smallest = 0;
for (int p = (quarterlyArray.length-1); p == 0; p--) { // This is not working right now.
if (quarterlyArray[p] < quarterlyArray [quarterlyArray.length-1] || quarterlyArray[p] < smallest) {
quarterlyArray[p] = smallest;
}
}
for (int l = quarterlyArray.length; l == 0; l--) { // Nor this.
if (quarterlyArray[l] == smallest) {
System.out.println("Quarter " + (l+1) + " saw the least rain this year with " + smallest + " inches.");
}
}
A few things that I notice, that are contributing to your problem:
for (int p = (quarterlyArray.length-1); p == 0; p--) {
The p == 0 is the termination expression. Your loop will only iterate while this condition is true. Your loop will not execute because the condition is false on the first iteration.
for (int l = quarterlyArray.length; l == 0; l--) {
Same problem in this loop, too. Look at your condition in your first for loop - k < quarterlyArray.length. When this is false, your loop will terminate.
quarterlyArray[p] = smallest
You are modifying the array rather than your local variable smallest.
You also need to be careful about bounds of the arrays. Your loop sets l to the array length - int l = quarterlyArray.length. If you then try to index into this array using quarterlyArray[l] you would get an ArrayIndexOutOfBoundsException because Java arrays (and most programming languages I would say) are 0-based.
Another thing to point out is how you are calculating both the min and max. Say, for example, that your quarterlyArray contains all negative values. What would largest be? Well, none of the items in the array are greater than 0, so largest would be 0. That would be incorrect because that value is not in the array.
In your first case, it looks like you are starting with an element that you know will always be lower than the elements in the array. This works out fine if you know all the elements will be greater than that. Your double largest = 0 will be changed to the maximum value of the array based on the constraints of the problem you are solving.
A better way to approach this is to start with a known element from the array. If you simply change your largest to be the first/last element from the array, and then go through the rest, you will always come out with the desired result.
double largest = quarterlyArray[0];
for (int k = 1; k < quarterlyArray.length; k++) { // Determines the largest quarterly rainfall.
if ( quarterlyArray[k] >= largest ) {
largest = quarterlyArray[k];
}
}
Notice how all I changed was the initialization of largest, and int k = 1. You can do the same thing for smallest. In this case, I start from the end of the array.
double smallest = quarterlyArray[quarterlyArray.length - 1];
for (int p = quarterlyArray.length - 2; p >= 0; p--) {right now.
if (quarterlyArray[p] < smallest) {
smallest = quarterlyArray[p];
}
}
Another note for after you get through this, is how could do both in 1 for loop?
Start the last loop form quarterlyArray.length-1.Change to
for (int l = quarterlyArray.length-1; l >= 0; l--)
The last element of the array would be at quarterlyArray.length-1 not at quarterlyArray.length
And l == 0(termination expression) should be l >= 0 because when the termination expression evaluates to false, the loop terminates.
Your loop for the smallest is all wrong - you are setting the array values, not finding the smallest.
Do it all in one loop:
double largest=Double.MIN_VALUE;
double smallest = Double.MAX_VALUE;
for (int k = 0; k < quarterlyArray.length; k++) {
if ( quarterlyArray[k] > largest ) {
largest = quarterlyArray[k];
}
if ( quarterlyArray[k] < smallest ) {
smallest = quarterlyArray[k];
}
}
You can also refine this in the manner shown by #mkobit to make it a bit faster:
double largest=quarterlyArray[0];
double smallest = largets;
for (int k = 1; k < quarterlyArray.length; k++) {
if ( quarterlyArray[k] > largest ) {
largest = quarterlyArray[k];
} else if ( quarterlyArray[k] < smallest ) {
smallest = quarterlyArray[k];
}
}
Of course, you should first check to ensure that quarterlyArray has a length greater than zero.

Java: Parallelise loop and merge results for calculating entropy

I have an algorithm that does the following:
Given I have an array array of length n It's goal is to merge certain elements based on some condition (it this case entropy). It calculates the entropy e_all of the entire array and calculates the entropy e_merged of the array where element n and n+1 are merged. It does that for each pair of adjacent elements. The pair where the difference in e_all - e_merged is greatest are marged. If there is a merge, the algorithm is applied again on the new array with length n-1.
As you can see, this takes in the worst case n^2 - 1 iterations and if n is big it might take minutes or even hours to complete.
Therefore I was wondering how I can parallelise this algorithms. Basically it should be able calculate the entropies on i cores and when all the elements are evaluated the results should be merged and a conclusion can be drawn.
How can I do such a thing? Which kinds of code pieces or idea's must I implement for it to work this way? Or is there a better way?
public double[] applyAlgorithm(double[] array) {
boolean merging = false;
for (int i = 0; i < array.length - 1; i++) {
double[] entropy = getEntropy(array); // returns list of entropy for all adjacent intervals
int idx = 0;
double max = Double.NEGATIVE_INFINITY;
for (int j = 0; j < entropy.length; j++) {
if (entropy[j] > max) {
max = entropy[j];
idx = j;
}
}
if (max > 0) {
array = mergeAdjacentIntervals(array, idx); //merge intervals that have the max entropy, if the entropy is > 0
merging = true;
break;
}
}
if (merging) {
array = applyAlgorithm(array);
}
return array;
}
private double[] getEntropy(double[] array) {
double[] entropy = new double[array.length - 1];
double[] tempArray = new double[array.length - 1];
double baseEntropy = calculateEntropy(array);
for (int i = 0; i < entropy.length; i++) {
tempArray = mergeAdjacentIntervals(array, idx);
entropy[i] = baseEntropy - calculateEntropy(tempArray);
}
return entropy;
}

Dynamic Programming?

Im struggling how to find the maximum amount of dollars that you can achieve with a specified limit on the number of transactions using Dynamic Programming
This not an elegant solution but it will work for this particular problem (I'm guessing we have the same professor).
The logic is that for each V[n][c] we want to find the highest value possible for each unit of currency, and in order to do this we must calculate the maximum value out of 6 vales.
There are 6 values because there are 3 currencies, and each of those currencies has two possible ways that it can be converted into the target currency.
In this case since there are only 2 exchanges I simply do two statements rather than another loop. This is represented by the 0 in the array: rates[0][i][c]
I hope this helps!
for (int n = 1; n <= numberOfTransactions; n++) {
for (int c = 0; c < numberOfcurrencies; c++) {
double max = Double.NEGATIVE_INFINITY;
double temp;
for (int i = 0; i < numberOfcurrencies;i++) {
temp = rates[0][i][c]*V[n-1][i];
if (temp > max)
max = temp;
temp = rates[1][i][c]*V[n-1][i];
if (temp > max)
max = temp;
}
V[n][c] = max;
}
}

Categories

Resources