} //Calculate european values with mutil thread public void Calculate_asian() { long M = opt.NumberOfSimulation; Greeks[] greeks = new Greeks[cores]; Thread[] caller = new Thread[cores]; long[] m = new long[cores]; for (int i = 0; i < cores; i++) { m[i] = M / cores; if (i == (cores - 1)) { m[i] = M - i * m[0]; } opt.NumberOfSimulation = m[i]; greeks[i] = new Greeks(opt); caller[i] = new Thread(new ThreadStart(greeks[i].Calculate_Asian)); caller[i].Start(); } for (int j = 0; j < cores; j++) { caller[j].Join(); } double[] Price = new double[M]; for (int i = 0; i < cores; i++) { for (int j = 0; j < m[i]; j++) { Price[i * m[0] + j] = greeks[i].Asian_Prices[j]; } } for (int i = 0; i < M; i++) { Asian_finalPrice += Price[i] / M; } for (int j = 0; j < cores; j++) { Asian_DeltaT += ((double)m[j] / M) * greeks[j].Asian_Delta(); Asian_VegaT += ((double)m[j] / M) * greeks[j].Asian_Vega(); Asian_GammaT += ((double)m[j] / M) * greeks[j].Asian_Gamma(); Asian_ThetaT += ((double)m[j] / M) * greeks[j].Asian_Theta(); Asian_RhoT += ((double)m[j] / M) * greeks[j].Asian_Rho(); } double std = 0.0; for (int i = 0; i < M; i++) { std += (Price[i] - Asian_finalPrice) * (Price[i] - Asian_finalPrice); } Asian_TOET = Math.Sqrt(std / (M - 1) / M); //Get the stderror. } //Calculate asian values with mutil thread
} //Calculate lookback values with mutil thread public void Calculate_range() { long M = opt.NumberOfSimulation; Greeks[] greeks = new Greeks[cores]; Thread[] caller = new Thread[cores]; long[] m = new long[cores]; for (int i = 0; i < cores; i++) { m[i] = M / cores; if (i == (cores - 1)) { m[i] = M - i * m[0]; } opt.NumberOfSimulation = m[i]; greeks[i] = new Greeks(opt); caller[i] = new Thread(new ThreadStart(greeks[i].Calculate_Range)); caller[i].Start(); } for (int j = 0; j < cores; j++) { caller[j].Join(); } double[] Price = new double[M]; for (int i = 0; i < cores; i++) { for (int j = 0; j < m[i]; j++) { Price[i * m[0] + j] = greeks[i].Range_Prices[j]; } } for (int i = 0; i < M; i++) { Range_finalPrice += Price[i] / M; } for (int j = 0; j < cores; j++) { Range_DeltaT += ((double)m[j] / M) * greeks[j].Range_Delta(); Range_VegaT += ((double)m[j] / M) * greeks[j].Range_Vega(); Range_GammaT += ((double)m[j] / M) * greeks[j].Range_Gamma(); Range_ThetaT += ((double)m[j] / M) * greeks[j].Range_Theta(); Range_RhoT += ((double)m[j] / M) * greeks[j].Range_Rho(); } double std = 0.0; for (int i = 0; i < M; i++) { std += (Price[i] - Range_finalPrice) * (Price[i] - Range_finalPrice); } Range_TOET = Math.Sqrt(std / (M - 1) / M); }