public double[] calculate(Value temp, double[,] matrix, string p, bool isant, PayoffDelegate2 payoff)//calculate all the greeks, temp is all values, matrix is random number matrix, payoff is Payoff function { double[] result = new double[5]; double s = temp.S, k = temp.K, r = temp.r, e = temp.e, t = temp.T; double a, b, c, dd = 0.01; temp.S = (1 + dd) * s; a = diff(temp, matrix, p, isant, payoff)[0]; temp.S = s; temp.S = (1 - dd) * s; b = diff(temp, matrix, p, isant, payoff)[0]; result[0] = greek(a, b, s, "delta"); temp.S = s; // f.BeginInvoke(f.myDelegate); c = diff(temp, matrix, p, isant, payoff)[0]; temp.K = (1 + dd) * k; a = diff(temp, matrix, p, isant, payoff)[0]; temp.K = k; temp.K = (1 - dd) * k; b = diff(temp, matrix, p, isant, payoff)[0]; result[1] = greek(a, b, c, "gamma"); temp.K = k; // f.BeginInvoke(f.myDelegate); temp.e = (1 + dd) * e; a = diff(temp, matrix, p, isant, payoff)[0]; temp.e = e; temp.e = (1 - dd) * e; b = diff(temp, matrix, p, isant, payoff)[0]; result[2] = greek(a, b, e, "vega"); temp.e = e; // f.BeginInvoke(f.myDelegate); temp.T = (1 + dd) * t; a = diff(temp, matrix, p, isant, payoff)[0]; temp.T = t; temp.T = (1 - dd) * t; b = diff(temp, matrix, p, isant, payoff)[0]; result[3] = -greek(a, b, t, "theta"); temp.T = t; // f.BeginInvoke(f.myDelegate); temp.r = (1 + dd) * r; a = diff(temp, matrix, p, isant, payoff)[0]; temp.r = r; temp.r = (1 - dd) * r; b = diff(temp, matrix, p, isant, payoff)[0]; result[4] = greek(a, b, r, "rho"); temp.r = r; // f.BeginInvoke(f.myDelegate); return result; }
private double[] diff(Value temp, double[,] matrix, string s, bool isant, PayoffDelegate2 payoff)//calculate the greeks, matrix is the random matrix generated before { double dt = temp.T / (temp.step - 1); double[] result = new double[2]; /*for (int i = 0; i < temp.trial; i++) { newmatrix[i, 0] = temp.S; for (int j = 1; j < temp.step; j++) newmatrix[i, j] = newmatrix[i, j - 1] * Math.Exp((temp.r - temp.e * temp.e * 0.5) *dt + (temp.e * Math.Sqrt(dt) * matrix[i, j])); }*/ result = payoff(temp, matrix, isant, s); return result; }