public void ValueOptionMonteCarloIterative(int numberOfSimulations) { MonteCarloSimulation monteCarloSimulation = new MonteCarloSimulation(AnnualVolatility, RiskFreeRate, StockPrice, ValuationDate, Maturity); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < numberOfSimulations; i++) { monteCarloSimulation.AddMonteCarloPath(); } watch.Stop(); Console.WriteLine($"Monte carlo simulation took {watch.Elapsed}"); MonteCarloSimulation = monteCarloSimulation; MonteCarloValue = monteCarloSimulation.GetValueAtDate(Maturity, PayOffFunction); }
private void ValueOptionMonteCarloParallel(int numberOfSimulations) { MonteCarloSimulation monteCarloSimulation = new MonteCarloSimulation(AnnualVolatility, RiskFreeRate, StockPrice, ValuationDate, Maturity); Stopwatch watch = new Stopwatch(); watch.Start(); var paths = new ConcurrentBag <SortedDictionary <DateTime, double> >(); Parallel.For(0, numberOfSimulations, i => { paths.Add(monteCarloSimulation.SimulatePath()); }); watch.Stop(); monteCarloSimulation.MonteCarloPaths = paths.ToList(); Console.WriteLine($"Monte carlo simulation parallel took {watch.Elapsed}"); MonteCarloSimulationParallel = monteCarloSimulation; MonteCarloValueParallel = monteCarloSimulation.GetValueAtDate(Maturity, PayOffFunction); }