Beispiel #1
0
 public object DeepClone()
 {
     Model clone = new Model(old_price, H, output, output2, mt_MAPrice, Count, old_volatilities);
     clone.old_volatilities = this.old_volatilities;
     clone.mean_reversion_rate = this.mean_reversion_rate;
     clone.mean_reversion_level = this.mean_reversion_level;
     clone.dt = this.dt;
     clone.variance_variance = this.variance_variance;
     clone.risk_free_rate = this.risk_free_rate;
     return clone;
 }
Beispiel #2
0
        private void Hoskin(int n, double L, double H, bool cum, double startPoint, double volatiles)
        {
            int m = n;// (int)Math.Pow(n, 2);
            double[] phi=new double[m];
            double[] cov = new double[m];
            double[] psi = new double[m];
            double[] output = new double[m];
            double[] output2 = new double[m];
            while (true)
            {
            double v = 1;
            output[0]=snorm();
            phi[0]=0;
            for (int i = 0; i < m; i++)
                cov[i]=covariance(i, H);

            /* simulation */
            for (int i = 1; i < m; i++)
            {
                phi[i - 1] = cov[i];
                for (int j = 0; j < i - 1; j++)
                {
                    psi[j] = phi[j];
                    phi[i - 1] -= psi[j] * cov[i - j - 1];
                }
                phi[i - 1] /= v;
                for (int j = 0; j < i - 1; j++)
                {
                    phi[j] = psi[j] - phi[i - 1] * psi[i - j - 2];
                }
                v *= (1 - phi[i - 1] * phi[i - 1]);

                output[i] = 0;
                for (int j = 0; j < i; j++)
                {
                    output[i] += phi[j] * output[i - j - 1];
                }
                output[i] += Math.Sqrt(v) * snorm();
            }

            /* rescale to obtain a sample of size 2^(*n) on [0,L] */
            double scaling = Math.Pow(L / m, H);
            for (int i = 0; i < m; i++)
            {
                output2[i] = scaling * (output[i]);
                if (cum && i > 0)
                {
                    output2[i] += output[i - 1];
                }
            }

            //Start the stopwatch
            Stopwatch s = new Stopwatch();
            s.Start();

            //Set up the Model
            List<double>outputv=new List<double>();
            List<double>outputv2=new List<double>();
            for (int i = 0; i < m; i++)
            {
                outputv.Add(output[i]);
                outputv2.Add(output2[i]);
            }

            Model model = new Model(startPoint, H, outputv, outputv2, startPoint + 0.01000, m, volatiles);

            //Set up the GA and run it
            Int32 threads = 10;
            GA ga = new GA(model,threads, 0.8, 0.0005, 1000);

                ga.Go();
                //===============================================================================================
                //In this example we have set the model, progress reporter delegate (which is optional), number
                //of threads, crossover rate, mutation rate, population size, number of generations
                //===============================================================================================

                //Get the results
                s.Stop();
                Console.WriteLine();
                Console.WriteLine("Optimisation run finished in " + s.ElapsedMilliseconds.ToString() + " ms.");
                double[] bestGenes; double bestFitness;
                ga.GetBest(out bestGenes, out bestFitness);
                if (bestFitness > 0.1)
                {
                    double old_price = startPoint;//clo->stock_price;
                    double old_volatilities = volatiles; //bestGenes[0];//clo->variance;
                    double mean_reversion_rate = bestGenes[0] / 10000000;
                    double mean_reversion_level = bestGenes[1] / 1000000;
                    double dt = bestGenes[2] / 10000000;
                    double variance_variance = bestGenes[3] / 10000000;
                    double risk_free_rate = bestGenes[4] / 10000000;

                    for (int i = 0; i < n; i++)
                    {
                        double new_volatilities = old_volatilities +
                               mean_reversion_rate * (mean_reversion_level - old_volatilities) * dt +
                               variance_variance * output[i];

                        double new_price = old_price +
                               risk_free_rate * old_price * dt +
                               new_volatilities * old_price * output2[i]; //These are already scaled via hoskin */
                        Ticks.Add(new_price);
                        old_price = new_price;
                        old_volatilities = new_volatilities;
                    }

                    break;
                }
            }
              /**/

            /*  for (int i = 0; i < n; i++)
              {
                /*  double new_volatilities = old_volatilities +
                               mean_reversion_rate * (mean_reversion_level - old_volatilities) * dt +
                               variance_variance * output[i];

                  double new_price = old_price +
                              risk_free_rate * old_price * dt +
                              new_volatilities * old_price * output2[i]; //These are already scaled via hoskin */
                  /*double new_volatilities = old_volatilities + variance_variance*output[i];
                  double new_price = old_price + new_volatilities * output2[i];
                  Ticks.Add(new_price);
                  old_price = new_price;
                  old_volatilities = new_volatilities;
              }*/
        }