/// <summary> /// Price a European option in the Heston model using the Monte-Carlo method. Accuracy will depend on number of time steps and samples /// </summary> /// <param name="parameters">Object implementing IHestonModelParameters interface, containing model parameters.</param> /// <param name="europeanOption">Object implementing IEuropeanOption interface, containing the option parameters.</param> /// <param name="monteCarloSimulationSettings">An object implementing IMonteCarloSettings object and containing simulation settings.</param> /// <returns>Option price</returns> public static double HestonEuropeanOptionPriceMC(IHestonModelParameters parameters, IEuropeanOption europeanOption, IMonteCarloSettings monteCarloSimulationSettings) { try { // Create Monte Carlo EU option object EuropeanOptionMC euOptionMC = new EuropeanOptionMC((HestonModelParameters)parameters, (MonteCarloSettings)monteCarloSimulationSettings, (EuropeanOption)europeanOption); return(euOptionMC.Price(Environment.ProcessorCount)); } catch (Exception ex) { throw ex; } }
public void Task_2_3_15() { // Variance Process Values double Kappa = 2; double Theta = 0.06; double Sigma = 0.4; double V0 = 0.04; double Rho = 0.5; // Heston Model Params double InitialStockPrice = 100; double RiskFreeRate = 0.1; // Option Params double StrikePrice = 100; PayoffType Type = PayoffType.Call; double Maturity = 15; // MC Simulation Params int NumberOfTrials = (int)1e5; int NumberOfTimeSteps = (int)Math.Ceiling(365 * Maturity); VarianceProcessParameters varParams = new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho); HestonModelParameters hestonModel = new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams); EuropeanOption euOption = new EuropeanOption(StrikePrice, Type, Maturity); MonteCarloSettings monteCarloSettings = new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps); EuropeanOptionMC euOptionMC = new EuropeanOptionMC(hestonModel, monteCarloSettings, euOption); Assert.AreEqual(78.4306, euOptionMC.Price(1), 1e-1); }
public static void TaskThreadScaling() { // Variance Process Values Console.WriteLine("Task Thread Scaling"); // Variance Process Values double Kappa = 2.0; double Theta = 0.06; double Sigma = 0.4; double V0 = 0.04; double Rho = 0.5; // Heston Model Params double InitialStockPrice = 100; double RiskFreeRate = 0.1; // Option Params double StrikePrice = 100; PayoffType Type = PayoffType.Call; double Maturity = 3; VarianceProcessParameters varParams = new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho); HestonModelParameters hestonModel = new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams); // ************Task 3 Print**************** System.Console.WriteLine("*********************"); HestonModelParamPrint(varParams, hestonModel); //Prepare csv var csv = new StringBuilder(); String newLine = string.Format("Number of Trials, cores, time"); csv.AppendLine(newLine); int[] cores = new int[5] { 1, 2, 4, 8, 16 }; for (int i = 0; i < 5; i++) { // MC Simulation Params int NumberOfTrials = (int)1e6; int NumberOfTimeSteps = (int)Math.Ceiling(365 * Maturity); EuropeanOption euOption = new EuropeanOption(StrikePrice, Type, Maturity); MonteCarloSettings monteCarloSettings = new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps); double priceForm = Heston.HestonEuropeanOptionPrice(hestonModel, euOption); // Create Monte Carlo EU option object EuropeanOptionMC euOptionMC = new EuropeanOptionMC(hestonModel, monteCarloSettings, euOption); var stopwatch = new Stopwatch(); stopwatch.Start(); double price = euOptionMC.Price(cores[i]); stopwatch.Stop(); long elapsed_time = stopwatch.ElapsedMilliseconds; System.Console.WriteLine("K={0}, T={1}, cores={2}, C_MC={3}, C_form={4}, time={5}", StrikePrice, Maturity, cores[i], price, priceForm, elapsed_time); newLine = string.Format("{0}, {1}, {2}", NumberOfTrials, cores[i], elapsed_time); csv.AppendLine(newLine); } //Write to csv File.WriteAllText(@"./taskThreadScaling.csv", csv.ToString()); System.Console.WriteLine("*********************"); System.Console.WriteLine("\n\n"); }