コード例 #1
0
        public static void Task2()
        {
            // Task 2
            // Variance Process Values
            Console.WriteLine("Task 2");
            double Kappa = 1.5768;
            double Theta = 0.0398;
            double Sigma = 0.5751;
            double V0    = 0.0175;
            double Rho   = -0.5711;

            // Heston Model Params
            double InitialStockPrice = 100;
            double RiskFreeRate      = 0.025;

            // Option Params
            double     StrikePrice = 100;
            PayoffType Type        = PayoffType.Call;

            int[] Maturity = new int[5] {
                1, 2, 3, 4, 15
            };

            VarianceProcessParameters varParams =
                new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho);


            HestonModelParameters hestonModel =
                new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams);

            // ************Task 2.1 Print****************
            System.Console.WriteLine("*********************");
            HestonModelParamPrint(varParams, hestonModel);

            //Prepare csv
            var    csv     = new StringBuilder();
            String newLine = string.Format("K, T, Price");

            csv.AppendLine(newLine);

            for (int i = 0; i < 5; i++)
            {
                EuropeanOption euOption =
                    new EuropeanOption(StrikePrice, Type, Maturity[i]);
                double price = Heston.HestonEuropeanOptionPrice(hestonModel, euOption);

                System.Console.WriteLine("K={0}, T={1}, C={2}", StrikePrice,
                                         Maturity[i], price);
                newLine = string.Format("{0}, {1}, {2}", StrikePrice, Maturity[i], price);
                csv.AppendLine(newLine);
            }
            //Write to csv
            File.WriteAllText(@"./task2.csv", csv.ToString());
            System.Console.WriteLine("*********************");
            System.Console.WriteLine("\n\n");
        }
コード例 #2
0
        public static object HestonOneOptionPrice(double underlying,
                                                  double riskFreeRate,
                                                  double kappa,
                                                  double theta,
                                                  double sigma,
                                                  double rho,
                                                  double v0,
                                                  double maturity,
                                                  double strike,
                                                  string type,
                                                  [ExcelArgument(Description = "Timeout set to 1 min if not specified")]
                                                  int timeOutInMinutes)
        {
            try
            {
                if (ExcelDnaUtil.IsInFunctionWizard())
                {
                    return(null);
                }

                if (timeOutInMinutes.Equals(0))
                {
                    timeOutInMinutes = 1;
                }

                var task = Task.Run(() => Heston.HestonEuropeanOptionPrice(new HestonParametersGrading(underlying, riskFreeRate, kappa, theta, sigma, rho, v0),
                                                                           new EuropeanOptionGrading(maturity, strike, GetTypeOfVanillaEuropean(type))));
                if (task.Wait(TimeSpan.FromMinutes(timeOutInMinutes)))
                {
                    return(task.Result);
                }
                else
                {
                    throw new Exception($"Timed out ({timeOutInMinutes} mins)");
                }
            }
            catch (Exception e)
            {
                return("HestonOneOptionPrice: unknown error: " + e.Message);
            }
        }
コード例 #3
0
        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");
        }
コード例 #4
0
        public static void Task4()
        {
            // Variance Process Values
            Console.WriteLine("Task 4");
            // 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    = 1;

            VarianceProcessParameters varParams =
                new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho);

            HestonModelParameters hestonModel =
                new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams);

            // ************Task 4 Print****************
            System.Console.WriteLine("*********************");
            HestonModelParamPrint(varParams, hestonModel);

            EuropeanOption euOption =
                new EuropeanOption(StrikePrice, Type, Maturity);

            double priceForm = Heston.HestonEuropeanOptionPrice(hestonModel, euOption);

            System.Console.WriteLine("K={0}, T={1}, refPrice={2}", StrikePrice,
                                     Maturity, priceForm);

            //Prepare csv
            var    csv     = new StringBuilder();
            String newLine = string.Format("Trials, Time Steps, relError");

            csv.AppendLine(newLine);

            for (int i = 3; i < 5; i++)
            {
                // MC Simulation Params
                int      NumberOfTrials = (int)Math.Pow(10, i);
                double[] factor         = new double[3] {
                    0.5, 1, 2
                };
                for (int j = 0; j < 3; j++)
                {
                    int NumberOfTimeSteps = (int)Math.Ceiling(factor[j] * 365 * Maturity);
                    MonteCarloSettings monteCarloSettings =
                        new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps);

                    double price = Heston.HestonEuropeanOptionPriceMC(hestonModel, euOption, monteCarloSettings);
                    System.Console.WriteLine("K={0}, T={1}, #Trials={2}, #TimeSteps={3}, rel_error={4}", StrikePrice,
                                             Maturity, NumberOfTrials, NumberOfTimeSteps, Math.Abs(priceForm - price) / priceForm);
                    newLine = string.Format("{0}, {1}, {2}", NumberOfTrials, NumberOfTimeSteps, Math.Abs(priceForm - price) / priceForm);
                    csv.AppendLine(newLine);
                }
            }

            //Write to csv
            File.WriteAllText(@"./task4.csv", csv.ToString());

            System.Console.WriteLine("*********************");
            System.Console.WriteLine("\n\n");
        }
コード例 #5
0
        public static void Task3()
        {
            // Variance Process Values
            Console.WriteLine("Task 3");
            // 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 = new double[5] {
                1, 2, 3, 4, 15
            };

            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("K, T, Price, refPrice");

            csv.AppendLine(newLine);

            for (int i = 0; i < 5; i++)
            {
                // MC Simulation Params
                int NumberOfTrials    = (int)1e5;
                int NumberOfTimeSteps = (int)Math.Ceiling(365 * Maturity[i]);

                EuropeanOption euOption =
                    new EuropeanOption(StrikePrice, Type, Maturity[i]);

                MonteCarloSettings monteCarloSettings =
                    new MonteCarloSettings(NumberOfTrials, NumberOfTimeSteps);
                double priceForm = Heston.HestonEuropeanOptionPrice(hestonModel, euOption);

                double price = Heston.HestonEuropeanOptionPriceMC(hestonModel, euOption, monteCarloSettings);
                System.Console.WriteLine("K={0}, T={1}, C_MC={2}, C_form={3}", StrikePrice,
                                         Maturity[i], price, priceForm);

                newLine = string.Format("{0}, {1}, {2}, {3}", StrikePrice, Maturity[i], price, priceForm);
                csv.AppendLine(newLine);
            }

            //Write to csv
            File.WriteAllText(@"./task3.csv", csv.ToString());

            System.Console.WriteLine("*********************");
            System.Console.WriteLine("\n\n");
        }