private double CalculateAsianOptionPrice(double S0, double[] T, double maturity, Func <double, double> payoffFn) { int nMonitorTimes = T.Length; double[][] stockPaths = MonteCarloPaths.ParallelGenerateMcPaths(N, M, maturity, r, S0, ssviParams); // size N x nMonitorTimes // the is the average for Asian option price double averageAlongPath; // this is the MC average double averagePayoff = 0; for (int n = 0; n < N; ++n) { averageAlongPath = 0; for (int i = 0; i < nMonitorTimes; ++i) { int index = (int)Math.Round((T[i] / maturity) * (M - 1)); averageAlongPath += stockPaths[n][index]; } averageAlongPath *= 1.0 / nMonitorTimes; averagePayoff += payoffFn(averageAlongPath); } averagePayoff *= 1.0 / N; return(Math.Exp(-r * maturity) * averagePayoff); }
private double CalculateBarrierOptionPrice(double S0, double maturity, string DownOrUp, string InOrOut, double barrier, Func <double, double> payoffFn) { double[][] stockPaths = MonteCarloPaths.ParallelGenerateMcPaths(N, M, maturity, r, S0, ssviParams); double payoff = 0; for (int n = 0; n < N; ++n) { bool barrierAchieved = false; for (int m = 0; m < M; ++m) { if (DownOrUp == "D") { if (stockPaths[n][m] <= barrier) { barrierAchieved = true; } } else if (DownOrUp == "U") { if (stockPaths[n][m] >= barrier) { barrierAchieved = true; } } else { throw new ArgumentException("Barrier type must be either D or U"); } } if (barrierAchieved) { if (InOrOut == "I") { payoff += payoffFn(stockPaths[n][M - 1]); } else if (InOrOut != "O") { throw new ArgumentException("InOrOut must be I or O."); } } else { if (InOrOut == "O") { payoff += payoffFn(stockPaths[n][M - 1]); } else if (InOrOut != "I") { throw new ArgumentException("InOrOut must be I or O."); } } } return(Math.Exp(-r * maturity) * payoff / N); }
private double CalculateEurOptionPrice(double S0, double T, Func <double, double> payoffFn) { double[][] stockPaths = MonteCarloPaths.ParallelGenerateMcPaths(N, M, T, r, S0, ssviParams); double Price = 0; for (int n = 0; n < N; ++n) { Price += payoffFn(stockPaths[n][M - 1]); } return(Math.Exp(-r * T) * (Price / N)); }
public static void Main(string[] args) { //ConstrainedMinimizationTest.RunTest(); //SsviTests.RunVolatilityTest(); // MonteCarloTests.RunEurOptionPricing(); double[] ssviParams = new double[] { Math.Sqrt(0.1), 1, 0.7, 0.2, 0.3 }; double r = 0.025; double S0 = 100; double T = 1; int N = 100000; int M = 128; Stopwatch timer = Stopwatch.StartNew(); double[,] paths = MonteCarloPaths.GenerateMcPaths(N, M, T, r, S0, ssviParams); timer.Stop(); TimeSpan serial_length = timer.Elapsed; Console.WriteLine("Serial elapsed time = " + serial_length.ToString("mm\\:ss\\.ff")); Stopwatch timer1 = Stopwatch.StartNew(); double[][] paths1 = MonteCarloPaths.ParallelGenerateMcPaths(N, M, T, r, S0, ssviParams); timer1.Stop(); TimeSpan serial_length1 = timer1.Elapsed; Console.WriteLine("Parallel elapsed time = " + serial_length1.ToString("mm\\:ss\\.ff")); //using (System.IO.StreamWriter file = new System.IO.StreamWriter("Paths.csv")) //{ // for (int i = 0; i < paths.GetLength(0); ++i) // { // for (int j = 0; j < paths.GetLength(1); ++j) // file.Write(paths[i, j].ToString() + ","); // file.Write(Environment.NewLine); // } //} Console.WriteLine("All done."); Console.ReadKey(); }
public double CalculateLookbackOptionPrice(double S0, double maturity) { double[][] stockPaths = MonteCarloPaths.ParallelGenerateMcPaths(N, M, maturity, r, S0, ssviParams); double payoff = 0; for (int n = 0; n < N; ++n) { double minValue = double.MaxValue; for (int m = 0; m < M; ++m) { if (stockPaths[n][m] < minValue) { minValue = stockPaths[n][m]; } } payoff += stockPaths[n][M - 1] - minValue; } return(Math.Exp(-r * maturity) * payoff / N); }
public static void RunPathBuildTest() { double[] ssviParams = new double[] { Math.Sqrt(0.1), 1, 0.7, 0.2, 0.3 }; double r = 0.025; double S0 = 100; double T = 1; int N = 6; int M = 4; double[,] paths = MonteCarloPaths.GenerateMcPaths(N, M, T, r, S0, ssviParams); int rLength = paths.GetLength(0); int cLength = paths.GetLength(1); for (int i = 0; i < rLength; ++i) { for (int j = 0; j < cLength; ++j) { Console.Write("{0}, ", paths[i, j]); } Console.WriteLine(); } }