示例#1
0
        //Runs monte carlo for the "other values" in Task 2.1 using the answer designed for Task 2.2
        static void doTask22(SSVIparametrisasion ssvi, int timesteps, int nrofpaths)
        {
            Console.WriteLine("\nTask 2.2 compared to task 2.1");

            double price = 0;

            double[]  K  = new double[] { 102.5313, 105.1271, 103.8212, 80, 80, 90, 120, 90 }; //Values to test
            double[]  T  = new double[] { 1, 2, 1.5, 1, 2, 1, 1, 2 };                          //Compare with Task 2.1
            Stopwatch sw = new Stopwatch();                                                    //for measuring how long this simulation takes

            sw.Start();
            for (int i = 0; i < T.Length; ++i)
            {
                if (i < 6) //Call options
                {
                    price = ssvi.MonteCarloEuropeanCall(T[i], K[i], nrofpaths, timesteps);
                    Console.WriteLine("For K = {0}, T = {1}, Call option price = {2}", K[i], T[i], Math.Round(price, 2));
                }
                else  //Put options
                {
                    price = ssvi.MonteCarloEuropeanPut(T[i], K[i], nrofpaths, timesteps);
                    Console.WriteLine("For K = {0}, T = {1}, Put option price = {2}", K[i], T[i], Math.Round(price, 2));
                }
            }
            sw.Stop();

            Console.WriteLine("\nTime with parallel for loops elapsed = {0}", sw.Elapsed);
        }
示例#2
0
        //Compare performance of monte carlo wtih parallel for loops and regular loops
        static void doTaskExtra(SSVIparametrisasion ssvi, int timesteps, int nrofpaths)
        {
            Console.WriteLine("\nCompare speed of parallel Monte Carlo and regular Monte Carlo");
            double price;                                      //option price

            double[] K = new double[] { 80, 80, 90, 120, 90 }; //Values to test
            double[] T = new double[] { 1, 2, 1, 1, 2 };       //Compare with Task 2.1

            for (int pths = 100; pths <= nrofpaths; pths *= 10)
            {
                Stopwatch sw = new Stopwatch(); //Object to time this method
                Console.WriteLine("\nRunning European option price monte carlo simulation with {0} paths using regular loops", pths);
                sw.Start();
                for (int i = 0; i < T.Length; ++i)
                {
                    if (i < 3) //Call options
                    {
                        price = ssvi.MonteCarloEuropeanCallSlow(T[i], K[i], pths, timesteps);
                    }
                    else  //Put options
                    {
                        price = ssvi.MonteCarloEuropeanPutSlow(T[i], K[i], pths, timesteps);
                    }
                }
                sw.Stop();
                Console.WriteLine("\nTask 2.2 completed with number of paths {0}", pths);
                Console.WriteLine("Time with regular loops elapsed = {0}", sw.Elapsed);
                sw = new Stopwatch(); //Object to time this method
                Console.WriteLine("\nRunning European option price monte carlo simulation with {0} paths using parallel loops", pths);
                sw.Start();
                for (int i = 0; i < T.Length; ++i)
                {
                    if (i < 3) //Call options
                    {
                        price = ssvi.MonteCarloEuropeanCall(T[i], K[i], pths, timesteps);
                    }
                    else  //Put options
                    {
                        price = ssvi.MonteCarloEuropeanPut(T[i], K[i], pths, timesteps);
                    }
                }
                sw.Stop();
                Console.WriteLine("\nTask 2.2 completed with number of pths {0}", pths);
                Console.WriteLine("Time with parallel loops elapsed = {0}", sw.Elapsed);
            }
        }