private string printParamsReport(SDE sde0, SDE sde1, bool myWay)
        {
            string res = "";
            double alpha0 = sde0.alpha, beta0 = sde0.beta, gamma0 = sde0.gamma, delta0 = sde0.delta;
            double alpha1 = sde1.alpha, beta1 = sde1.beta, gamma1 = sde1.gamma, delta1 = sde1.delta;

            if (myWay)
            {
                beta1  *= alpha0 / alpha1;
                gamma1 *= alpha0 / alpha1;
                delta1 *= alpha0 / alpha1;
                alpha1 *= alpha0 / alpha1;
            }
            res += printParamReport(alpha0, alpha1, "Alpha");
            res += printParamReport(beta0, beta1, "Beta");
            res += printParamReport(gamma0, gamma1, "Gamma");
            res += printParamReport(delta0, delta1, "Delta");
            double sqerror = Math.Sqrt((
                                           Math.Pow(alpha1 - alpha0, 2) +
                                           Math.Pow(beta1 - beta0, 2) +
                                           Math.Pow(gamma1 - gamma0, 2) +
                                           Math.Pow(delta1 - delta0, 2)
                                           ) / 4);

            res += "Squared error: " + sqerror;

            return(res + Environment.NewLine + Environment.NewLine);
        }
 // Default constructor
 public MCSolver(BlockingCollection <double> q, int id, SDE sde, int NSteps, int NSimulations, double initialValue)
 {
     m_queue = q;
     m_id    = id;
     mySDE   = sde;
     N       = NSteps;
     S_0     = initialValue;
     NSim    = NSimulations;
     coun    = 0;
 }
        private void GetMeasurements(object sender, RoutedEventArgs e)
        {
            sde0 = new SDE(
                double.Parse(textBoxAlpha.Text),
                double.Parse(textBoxBeta.Text),
                double.Parse(textBoxGamma.Text),
                double.Parse(textBoxDelta.Text),
                double.Parse(textBoxX0.Text),
                double.Parse(textBoxY0.Text)
                );

            int    n      = int.Parse(textBoxN.Text);
            double stdDev = double.Parse(textBoxStdDev.Text);

            bool   myWay = (bool)checkBoxMyWay.IsChecked;
            string wayName;

            if (myWay)
            {
                wayName = "Rays";
                sde0.Rays(dt);
            }
            else
            {
                wayName = "OLSO";
                sde0.OSLO(dt);
            }

            textBlockrRes.Text = wayName + " squared error: " + sde0.GetAverageSquaredError() + Environment.NewLine;

            var exactSol = sde0.getSolution;

            plot.drawLine(wayName + " exact sol", exactSol, Brushes.Green);

            plot.drawPoints("Exact eq", sde0.GetEquilibriumPoint(), Brushes.Green);

            plot.drawPoints("Initial point", new double[] { sde0.x0, sde0.y0 }, Brushes.Green);

            measurements = Generator.getMeasurements(exactSol, stdDev, n);
            plot.drawPoints("Meas", measurements, Brushes.LightGreen);
        }
        private void Infer(object sender, RoutedEventArgs e)
        {
            bool  myWay = (bool)checkBoxMyWay.IsChecked;
            Brush color;

            try
            {
                double[] inferedParams;
                if (myWay)
                {
                    inferedParams       = Model.FirstIntegralInfer(measurements);
                    sde1                = new SDE(inferedParams[0], inferedParams[1], inferedParams[2], inferedParams[3], inferedParams[4]);
                    textBlockrRes.Text += printParamsReport(sde0, sde1, myWay);
                    color               = Brushes.Orange;
                }
                else
                {
                    inferedParams       = Model.numericalMethodInfer(measurements);
                    sde1                = new SDE(inferedParams[0], inferedParams[1], inferedParams[2], inferedParams[3], sde0.x0, sde0.y0);
                    textBlockrRes.Text += printParamsReport(sde0, sde1, myWay);
                    color               = Brushes.Blue;
                }
                if (myWay)
                {
                    sde1.Rays(dt);
                }
                else
                {
                    sde1.OSLO(dt);
                }
                var predictedSol = sde1.getSolution;
                plot.drawLine("Infered sol", predictedSol, color);

                plot.drawPoints("Infered eq 1", sde1.GetEquilibriumPoint(), color);
            }
            catch (Exception)
            {
                MessageBox.Show("Failed");
            }
        }
Beispiel #5
0
    public static void Main()
    {
        // Ask number of producers
        Console.Write("Number of producers: ");
        int nrProducers = Int32.Parse(Console.ReadLine());

        // Ask number of consumers
        Console.Write("Number of consumers: ");
        int nrConsumers = Int32.Parse(Console.ReadLine());

        OptionData myOption;

        myOption.K    = 65.0;
        myOption.T    = 0.25;
        myOption.r    = 0.08;
        myOption.sig  = 0.3;
        myOption.type = -1;     // Put -1, Call +1

        SDE mySDE;

        mySDE.data = myOption;

        SDE mySDE2 = mySDE;

        mySDE2.data.type = 1;

        // Portfolio of options
        Vector <SDE> optArr = new Vector <SDE>(2);

        optArr[optArr.MinIndex]     = mySDE;
        optArr[optArr.MinIndex + 1] = mySDE2;

        int NT   = 200;
        int NSim = 200000;

        // The number of numbers in the collection at any time. Can
        // experiment to test load balancing.
        int N = NT * NSim * nrConsumers;

        // Create queue
        BlockingCollection <double> q = new BlockingCollection <double>(N);

        // Create and start producers
        List <RngGenerator> producers = new List <RngGenerator>(nrProducers);

        for (int i = 0; i < nrProducers; i++)
        {
            RngGenerator generator = new RngGenerator(q, i, N);
            generator.Start();
            producers.Add(generator);
        }
        Console.WriteLine("Producers started...");

        // Create and start consumers
        double          V0        = 60.0;
        List <MCSolver> consumers = new List <MCSolver>(nrConsumers);

        for (int i = optArr.MinIndex; i <= optArr.MaxIndex; i++)
        {
            MCSolver solver = new MCSolver(q, i, optArr[i], NT, NSim, V0); // Euler
            solver.Start();
            consumers.Add(solver);
        }
        Console.WriteLine("Consumers started...");

        // Wait till all consumers ar finished.
        foreach (MCSolver solver in consumers)
        {
            solver.Join();
        }
        Console.WriteLine("Consumers finished...");

        // Stop all producers.
        foreach (RngGenerator generator in producers)
        {
            generator.Stop();
        }
        Console.WriteLine("Producers stopped");
    }
Beispiel #6
0
        static void Main(string[] args)
        {
            int    experimentsNum = ReadInt("number of experiments");
            int    sampleSize     = ReadInt("size of sample");
            double stdDev         = ReadDouble("stdDev");
            bool   myWay          = ReadInt("myWay") == 1;
            int    medianIndex    = (int)Math.Floor((double)((experimentsNum - 1) / 2));

            string report;

            int      total = 5 * 5 * 5 * 5 * experimentsNum;
            int      cnt   = 0;
            DateTime t0    = DateTime.Now;

            for (double alpha = 0.5; alpha <= 2.5; alpha += 0.5)
            {
                for (double beta = 0.5; beta <= 2.5; beta += 0.5)
                {
                    for (double gamma = 0.5; gamma <= 2.5; gamma += 0.5)
                    {
                        for (double delta = 0.5; delta <= 2.5; delta += 0.5)
                        {
                            List <double> results = new List <double>();
                            for (int k = 0; k < experimentsNum; k++)
                            {
                                report = sampleSize + ";";
                                double[] parameters = Generator.getRandomInitPoint(alpha, beta, gamma, delta);

                                SDE sde1, sde0 = new SDE(
                                    alpha,
                                    beta,
                                    gamma,
                                    delta,
                                    parameters[0],
                                    parameters[1]
                                    );

                                report +=
                                    alpha + ";" +
                                    beta + ";" +
                                    gamma + ";" +
                                    delta + ";" +
                                    parameters[0] + ";" +
                                    parameters[1];

                                if (myWay)
                                {
                                    sde0.Rays(dt);
                                }
                                else
                                {
                                    sde0.OSLO(dt);
                                }

                                var exactSol     = sde0.getSolution;
                                var measurements = Generator.getMeasurements(exactSol, stdDev, sampleSize);

                                try
                                {
                                    double[] inferedParams;

                                    if (myWay)
                                    {
                                        inferedParams = Model.FirstIntegralInfer(measurements);
                                        sde1          = new SDE(inferedParams[0], inferedParams[1], inferedParams[2], inferedParams[3], inferedParams[4]);
                                    }
                                    else
                                    {
                                        inferedParams = Model.numericalMethodInfer(measurements);
                                        sde1          = new SDE(inferedParams[0], inferedParams[1], inferedParams[2], inferedParams[3], sde0.x0, sde0.y0);
                                    }

                                    double alpha0 = sde0.alpha, beta0 = sde0.beta, gamma0 = sde0.gamma, delta0 = sde0.delta;
                                    double alpha1 = sde1.alpha, beta1 = sde1.beta, gamma1 = sde1.gamma, delta1 = sde1.delta;
                                    if (myWay)
                                    {
                                        beta1  *= alpha0 / alpha1;
                                        gamma1 *= alpha0 / alpha1;
                                        delta1 *= alpha0 / alpha1;
                                        alpha1 *= alpha0 / alpha1;
                                    }

                                    double sqerror = Math.Sqrt((
                                                                   Math.Pow(alpha1 - alpha0, 2) +
                                                                   Math.Pow(beta1 - beta0, 2) +
                                                                   Math.Pow(gamma1 - gamma0, 2) +
                                                                   Math.Pow(delta1 - delta0, 2)
                                                                   ) / 4);

                                    report += ";" + sqerror;
                                    results.Add(sqerror);
                                }
                                catch (Exception)
                                {
                                    report += ";failed";
                                }
                                WriteToFile(report, "Report.csv");
                                cnt++;
                                Console.Clear();
                                Console.WriteLine(Math.Round(((double)cnt / total * 100), 2) + "%");
                                DateTime t1          = DateTime.Now;
                                int      fullSeconds = (int)(t1.Subtract(t0).TotalMilliseconds / cnt * (total - cnt) / 1000);
                                int      fullMinutes = fullSeconds / 60;
                                int      seconds     = fullSeconds % 60;
                                int      hours       = fullMinutes / 60;
                                int      minutes     = fullMinutes % 60;
                                Console.WriteLine((hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds);
                            }
                            results.Sort();
                            double median;
                            if (results.Count >= medianIndex + 1)
                            {
                                median = results[medianIndex];
                            }
                            else
                            {
                                median = -1;
                            }
                            WriteToFile(median.ToString(), "HeatMap.txt");
                        }
                    }
                }
            }

            Console.Write("Finished!!");
            Console.ReadKey();
        }