/// <summary> /// the whole process to calculate /// </summary> /// <param name="S">Initial Stock Price</param> /// <param name="r">Risk Free Rate</param> /// <param name="T">Maturity</param> /// <param name="nv">parameter in model</param> /// <param name="k">parameter in model</param> /// <param name="theta">parameter in model</param> /// <param name="rho">parameter in model</param> /// <param name="sigma">parameter in model</param> /// <param name="N">Number Of Time Steps</param> /// <param name="n">Number Of Trials</param> /// <returns>the price of look back option</returns> public double CalculateLookbackCallOptionPrice(double S, double r, double T, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || T <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } if (2.0 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { double Priceitself = 0.0; double[] callprice = new double[n]; for (int i = 0; i < n; i++) { callprice[i] = Math.Exp(-r * T) * (GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma /*, tau*/, N, n)[N] - GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma, N, n).Min()); } for (int j = 0; j < n; j++) { Priceitself = Priceitself + callprice[j]; } return(Priceitself / n); } }
/// <summary> /// For how to calculate the European option price by MC /// </summary> /// <param name="S">Initial Stock Price</param> /// <param name="r">Risk Free Rate</param> /// <param name="T">Maturity</param> /// <param name="nv">parameter in model</param> /// <param name="k">parameter in model</param> /// <param name="theta">parameter in model</param> /// <param name="rho">parameter in model</param> /// <param name="sigma">parameter in model</param> /// <param name="N">Number Of Time Steps</param> /// <param name="n">Number Of Trials</param> /// <returns>the price we generating from Monte Carlo method</returns> public double CalculateEuropeanCallOptionPrice(double S, double K, double r, double T, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || K <= 0 || T <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } if (2 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { double[] callprice = new double[n]; for (int i = 0; i < n; i++) { callprice[i] = GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma, N, n)[N]; } double Price = 0.0; int j = 0; double[] aaa = new double[n]; for (j = 0; j < n; j++) { Price = Price + Math.Exp(-r * T) * Math.Max(callprice[j] - K, 0); aaa[j] = Math.Max(callprice[j] - K, 0); } return(Price / n); } }
public double CalculateAsianPutOptionPrice(double S, double K, double r, IEnumerable <double> T, double exerciseT, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || K <= 0 || exerciseT <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } CheckAsianOptionInputs(T, exerciseT); if (2.0 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { int M = T.Count(); double tau = exerciseT / N; double[] givenprice = new double[M]; double[] callprice = new double[n]; double[] path = new double[N]; int number; double priceitself; double price; double Price; for (int u = 0; u < n; u++) { path = GeneratePath.Generateallpath(S, r, exerciseT, nv, k, theta, rho, sigma /*, tau*/, N, n); for (int i = 0; i < M; i++) { number = (int)(T.ElementAt(i) / tau); givenprice[i] = path[number]; } priceitself = givenprice.Sum(); price = Math.Exp(-r * exerciseT) * Math.Max(K - priceitself / M, 0); callprice[u] = price; } Price = callprice.Sum(); return(Price / n); } }
/// <summary> /// how to calculate the price by using the Monte Carlo and payoff formula /// </summary> /// <param name="S">Initial Stock Price</param> /// <param name="r">Risk Free Rate</param> /// <param name="T">Maturity</param> /// <param name="K">Strike Price</param> /// <param name="nv">parameter in model</param> /// <param name="k">parameter in model</param> /// <param name="theta">parameter in model</param> /// <param name="rho">parameter in model</param> /// <param name="sigma">parameter in model</param> /// <param name="N">Number Of Time Steps</param> /// <param name="n">Number Of Trials</param> /// <returns>calculate the price from the payoff formula</returns> public double CalculateRainbowOptionPrice(double S, double K, double r, double T, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || T <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } if (2.0 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { double priceitself; double[] price = new double[n]; for (int j = 0; j < n; j++) { price[j] = Math.Exp(-r * T) * Math.Max(GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma, N, n).Max(), K); } priceitself = price.Sum(); return(priceitself / n); } }