I am a bit of a newbie at Java and I have a program that generates 52 different stock prices that can range anywhere from 0 to approximately 2000. My program also generates N number of different stock price "paths". I want to find out what the product of these 52 stock prices is. I have decided to use BigDecimal.
BigDecimal[] productOfStock1 = new BigDecimal[N+1];
for(int k = 1; k <= N; k++){
productOfStock1[k] = BigDecimal.ONE;
}
for(int k = 1; k <= N; k++){
for(int i = 1; i <= n; i++){
if (i == 1){
stockPrice[k][i] = stockZero*Math.pow(e, form + sigma*(randomno.nextGaussian()));
}
else {
stockPrice[k][i] = stockPrice[k][i-1]*Math.pow(e, form + sigma*(randomno.nextGaussian()));
}
productOfStock1[k] = productOfStock1[k].multiply(BigDecimal.valueOf(stockPrice[k][i]));
System.out.println("Stock at [" + i + "] for N = " + N + " and path number " + k + " is " + stockPrice[k][i]);
}
System.out.println("Product : " + productOfStock1[k]);
The problem that I ran into was that when I copy the values of stockPrice[1][i] for i = 1, ..., 52 into excel and calculate the product, I get a different value from the BigDecimal value from my code. Any help is appreciated.