Float comparison in java to recognize patterns - java

I am writing a program that identifies patterns in stock market data and I am trying to identify the following short term pattern:
if the the low value is less than the open value by at least 3 and the close value is within 2 of the open value.
I am reading in the values from a CSV file in the following format but without the headers:
Open High Low Close
353.4 359.2 347.7 349
351.4 354.08 349.1 353.1
350.1 354 349.3 350.2
352.4 353.28 348.7 349.8
345.7 352.3 345.7 351.5
The values are stored in float arraylists called closePrice, openPrice, lowPrice. I am calculating the
This is the code I have wrote to try and identify the pattern within the data.
for(int i = 0; i < closePrice.size(); i ++)
{
//Difference between opening price and the price low
float priceDrop = Math.abs(openPrice.get(i) - lowPrice.get(i));
//Difference between opening price and close price (regardless of positive or negative)
float closingDiff = Math.abs(openPrice.get(i) - closePrice.get(i));
float dropTolerance = 3.0f;
float closingTolerance = 2.0f;
if( (priceDrop > dropTolerance) || (closingDiff < closingTolerance) )
{
System.out.println("price drop = " + priceDrop + " closing diff = " + closingDiff);
System.out.println("Hangman pattern" + "\n");
}
}
So what it should do is test if the price drops more than 3 and then the closing price is within 2 of the opening price however when I run the program it seems to let everything bypass the if statement. My output is:
price drop = 5.6999817 closing diff = 4.399994
Hangman pattern
price drop = 2.2999878 closing diff = 1.7000122
Hangman pattern
price drop = 0.8000183 closing diff = 0.1000061
Hangman pattern
Is it because I am comparing floats? Any help would be appreciated.

It looks like you've confused the AND operator and the OR operator.
You state you only want to output if both conditions are met, but your code says you will output if either condition is met.

Related

How to get exact chance of three variables?

I am struggling to calculate the estimated chance of user to get an item. He have got 15 tries(which may vary), chance to get into group of items and then chance to get exact item. I can see two ways of doing it, but none is perfect. Please take a look:
int userTries = 15;//Variable holding number of how many tries user has to get an item
double groupChance = 17;//Chance for user to get any item from the group
double itemChance = 80;//Chance for user to get specific item from the group
double simpleChance = groupChance * itemChance * userTries / 100.0;
int correctionTries = 10000;
int totalPassed = 0;
for (int i = 0;i < correctionTries;i++)
{
for (int x = 0;x < userTries;x++)
{
//Rnd.chance is checking if input chance was rolled, if chance is 17 then this method will return true in 17 tries out of 100
if (Rnd.chance(groupChance))
if (Rnd.chance(itemChance))
{
totalPassed++;
break;
}
}
}
double iterationChance = (double) totalPassed / (double) correctionTries * 100.0;
System.out.println("simple=" + simpleChance + " iteration=" + iterationChance);
When groupChance and itemChance are low(like 1 & 1), then simpleChance gives very good results, but when chances are high(like 17 & 80) then they vary a lot from iteration result. The problem with iteration solution is that when one I increase one of the chances, result actually be lower because of bad luck in calculated chances. I could increase correctionTries to solve that issue, but chance will be different when calculating same values once again and it also would have significant impact to performance.
Do you know any way to calculate that chance with low performance impact and good estimation that stays the same after calculating it once again?
I assume that the groupChance and itemChance are probabilities (in percent) to get into the specific group and to get the specific item in the group..
If so, then the probability to get this specific Item is groupChance/100 * itemChance/100 = 0.17*0.8 = 0.136 = 13.6%
not clear either what simpleChance should be => to get the specific item at least once after 15 tries?? exactly once after 15 tries?? to get it 15 times in a row?
if you want to get it 15 times in a row, then the chance is (groupChance/100 * itemChance/100 ) ^ userTries = 0.000000000000101
if you want to get it at least once after 15 tries, then the chance is 1 - ( 1 - groupChance/100 * itemChance/100 ) ^ userTries = 0.88839

Slow spark application - java

I am trying to create a spark application that takes a dataset of lat, long, timestamp points and increases the cell count if they are inside a grid cell. The grid is comprised of 3d cells with lon,lat and time as the z-axis.
Now I have completed the application and it does what its supposed to, but it takes hours to scan the whole dataset(~9g). My cluster is comprised of 3 nodes with 4 cores,8g ram each and I am currently using 6 executors with 1 core and 2g each.
I am guessing that I can optimize the code quite a bit but is there like a big mistake in my code that results in this delay?
//Create a JavaPairRDD with tuple elements. For each String line of lines we split the string
//and assign latitude, longitude and timestamp of each line to sdx,sdy and sdt. Then we check if the data point of
//that line is contained in a cell of the centroids list. If it is then a new tuple is returned
//with key the latitude, Longitude and timestamp (split by ",") of that cell and value 1.
JavaPairRDD<String, Integer> pairs = lines.mapToPair(x -> {
String sdx = x.split(" ")[2];
String sdy = x.split(" ")[3];
String sdt = x.split(" ")[0];
double dx = Double.parseDouble(sdx);
double dy = Double.parseDouble(sdy);
int dt = Integer.parseInt(sdt);
List<Integer> t = brTime.getValue();
List<Point2D.Double> p = brCoo.getValue();
double dist = brDist.getValue();
int dur = brDuration.getValue();
for(int timeCounter=0; timeCounter<t.size(); timeCounter++) {
for ( int cooCounter=0; cooCounter < p.size(); cooCounter++) {
double cx = p.get(cooCounter).getX();
double cy = p.get(cooCounter).getY();
int ct = t.get(timeCounter);
String scx = Double.toString(cx);
String scy = Double.toString(cy);
String sct = Integer.toString(ct);
if (dx > (cx-dist) && dx <= (cx+dist)) {
if (dy > (cy-dist) && dy <= (cy+dist)) {
if (dt > (ct-dur) && dt <= (ct+dur)) {
return new Tuple2<String, Integer>(scx+","+scy+","+sct,1);
}
}
}
}
}
return new Tuple2<String, Integer>("Out Of Bounds",1);
});
Try to use mapPartitions it's more fast see this exapmle link; other thing to do is to put this part of code outside the loop timeCounter
One of the biggest factors that may contribute to costs in running a Spark map like this relates to data access outside of the RDD context, which means driver interaction. In your case, there are at least 4 accessors of variables where this occurs: brTime, brCoo, brDist, and brDuration. It also appears that you're doing some line parsing via String#split rather than leveraging built-ins. Finally, scx, scy, and sct are all calculated for each loop, though they're only returned if their numeric counterparts pass a series of checks, which means wasted CPU cycles and extra GC.
Without actually reviewing the job plan, it's tough to say whether the above will make performance reach an acceptable level. Check out your history server application logs and see if there are any stages which are eating up your time - once you've identified a culprit there, that's what actually needs optimizing.
I tried mappartitionstopair and also moved the calculations of scx,scy and sct so that they are calculated only if the point passes the conditions. The speed of the application has improved dramatically only 17 minutes! I believe that the mappartitionsopair was the biggest factor. Thanks a lot Mks and bsplosion!

How come my program freezes up when I click calculate?

Here is my code:
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
int intInitialInvest = Integer.parseInt(this.txtInputInitialInvest.getText());
int intAnnualInterest = Integer.parseInt(this.txtInputAnnualInterest.getText());
int intEndingValue = Integer.parseInt(this.txtInputEndingValue.getText());
double dblAnnualPercent = intAnnualInterest/100;
int count = 0;
while (intInitialInvest < intEndingValue){
intInitialInvest += (intInitialInvest * dblAnnualPercent);
count += 1;
}
this.lblOutputYears.setText("The number of years required is " + count);
}
This program is supposed to calculate how many years (which is count) it takes for example for a cd with a value of $2000 to become $5000 with an annual interest rate of 8%. This should then return 12. What I did was create a while loop which runs until the $2000 turn into $5000 or more from interest which is expressed by intInitialinvest += (intInitialInvest * dblAnnualPercent);
Every time I run the program by clicking the "Calculate" button, the program freezes and doesn't do anything then I have to go into task manager to close it.
Be careful with integer divisions:
double dblAnnualPercent = intAnnualInterest/100;
causes the value of dblAnnualPercent to be 0.0, and thus you run into an infinite loop. You perform an integer division (e.g 8/100=0) then convert to double (0.0, not 0.05 as you would have expected).
double dblAnnualPercent = intAnnualInterest/100.;
should fix your bug.
Hint: add assertions, run your problem with assertions enabled.
assert(dblAnnualPercent > 0.);
would have saved you (assuming you run your program with -ea).
But also try to solve your problem without a loop. There is a closed form solution to your problem, using math instead of loops... that solution is one line only.
If intInitialInvest=0 or dblAnnualPercent=0 and intEndingValue > 0 you'll loop forever.
while (intInitialInvest < intEndingValue){
intInitialInvest += (intInitialInvest * dblAnnualPercent);
count += 1;
}
You have to test your values before you enter the loop, especially as you seem to read these values from some input. This is a possible attack vector, even when you assert on these values, as your algorithm breaks, when someone feeds input that makes intInitialInvest=0 or intAnnualInterest<100.

Java: calculating velocity of a skydiver

In Java, I am trying to implement the following equation for calculating the current velocity of a skydiver not neglecting air resistance.
v(t) = v(t-∆t) + (g - [(drag x crossArea x airDensity) / (2*mass)] *
v[(t-∆t)^2] ) * (∆t)
My problem is that I am not sure how to translate "v(t - ∆t)" into a code. Right now I have this method below, where as you can see I am using the method within itself to find the previous velocity. This has continued to result in a stack overflow error message, understandably.
(timeStep = ∆t)
public double calculateVelocity(double time){
double velocity;
velocity = calculateVelocity(time - timeStep)
+ (acceleration - ((drag * crossArea * airDensity)
/ (2 * massOfPerson))
* (calculateVelocity(time - timeStep)*(time * timeStep)))
* timeStep;
}
return velocity;
}
I am calling the above method in the method below. Assuming that the ending time = an int, will be the user input but written this way to be dynamic.
public void assignVelocitytoArrays(){
double currentTime = 0;
while(currentTime <= endingTime){
this.vFinal = calculateVelocity(currentTime);
currentTime += timeStep;
}
}
I would like to figure this out on my own, could someone give me a general direction? Is using a method within itself the right idea or am I completely off track?
The formula you want to implement is the recursive representation of a sequence, mathematiacally speaking.
Recursive sequences need a starting point, e.g.
v(0) = 0 (because a negative time does not make sense)
and a rule to calculate the next elements, e.g.
v(t) = v(t-∆t) + (g - [(drag x crossArea x airDensity) / (2*mass)] * v[(t-∆t)^2] ) * (∆t)
(btw: are you sure it has to be v([t-∆t]^2) instead of v([t-∆t])^2?)
So your approach to use recursion (calling a function within itself) to calculate a recursive sequence is correct.
In your implementation, you only forgot one detail: the starting point. How should your program know that v(0) is not defined be the rule, but by a definite value? So you must include it:
if(input value == starting point){
return starting point
}
else{
follow the rule
}
On a side note: you seem to be creating an ascending array of velocities. It would make sense to use the already calculated values in the array instead of recursion, so you don't have to calculate every step again and again.
This only works if you did indeed make a mistake in the rule.
double[] v = new double[maxTime/timeStep];
v[0] = 0; //starting point
for(int t = 1; t < maxSteps; t++){
v[t] = v[t-1] + (g - [(drag x crossArea x airDensity) / (2*mass)] * v[t-1]^2 ) * (∆t)
}

Error with BigDecimal calculation regarding User Input

I have this idea for my assignment where I wanted a cash register system to calculate the total for an item when the user enters it's cost price and quantity of said item.
That seemed to work, but then led my main problem - I wanted to let the user type the letter "T" after say, 10 transactions, to find out the total takings for the day.
I tried to use a for loop with the BigDecimal math class within the calculations etc.
I have errors on the words 'valueOf' within my calculations & Eclipse keeps trying to change my values to 'long' & i'm pretty sure that's not right.
My explanation isnt amazing so i'll give you the code i wrote and place comments next to where my errors are ..
try{
Scanner in = new Scanner (System.in);
String t = "T";
int count;
for (count = 1;count<=10;count++){
System.out.println("\n\nValue of Item " + count + " :");
BigDecimal itemPrice = in.nextBigDecimal();
System.out.println("Quantity of item " + count + " :");
BigDecimal itemQuantity = in.nextBigDecimal();
BigDecimal itemTotal = (BigDecimal.valueOf(itemPrice).multiply // error here
(BigDecimal.valueOf(itemQuantity))); // error here
System.out.println("\nTotal for item(s): £" + itemTotal);
count++;
while (t == "T"){
BigDecimal amountOfItems = (BigDecimal.valueOf(itemTotal).divide // error here
(BigDecimal.valueOf(itemQuantity))); // error here
BigDecimal totalTakings = (BigDecimal.valueOf(itemTotal).multiply // error here
(BigDecimal.valueOf(amountOfItems))); // error here
System.out.println("The Total Takings For Today is £" + totalTakings + " " );
}
}
}
}
}
Like I said, the 'red lines' that eclipse uses to show there is an error are only under the words, "valueOf" within my BigDecimal calculations.
Any help would be great because i'm tearing my hair out !!!!
Thanx,
Vinnie.
There's no method BigDecimal.valueOf(BigDecimal). itemPrice and itemQuantity are already BigDecimal values - you don't need any conversion:
BigDecimal itemTotal = itemPrice.multiply(itemQuantity);
EDIT: Okay, so the above solves your immediate problem, but you've also got:
while (t == "T")
{
// Loop that never changes the value of t
}
There are two issues with this:
The loop will always either execute forever, not execute at all, or keep going until an exception is thrown, because the loop condition can never change as you're not changing the value of t. My guess is you want t = System.in.readLine() at some point...
You're comparing two string references here whereas I suspect you want to compare their values, e.g.
while (t.equals("T"))

Categories

Resources