private ColumnVector EulerMEthode2(Grid grid, ColumnVector volterra, ColumnVector Z_Brownian)//Not Used !! { int Nbstep = 20; Func <double, double> epsilon = t => Math.Pow(0.235, 2); var VIXFutures = new ColumnVector(grid.get_timeNmbrStep() / 50); for (int k = 1; k <= volterra.Count() / 50; k++) { int N_k = k * 50; Grid secondGrid = new Grid(grid.t(N_k), grid.t(N_k) + Delta, Nbstep); //3- approximate the continuous-time process V^T by the discrete-time version V^T~ defined via the forward Euler scheme int N_tau = secondGrid.get_timeNmbrStep(); // Volterra V^T double[] volterraT = new double[N_tau + 1]; volterraT[0] = volterra[N_k - 1]; for (int j = 1; j <= N_tau; j++) { double v = 0.0; double[] v2 = new double[volterra.Count() / 50]; for (int i = 1; i <= N_k; i++) { v += (Z_Brownian[i] - Z_Brownian[i - 1]) / Math.Pow((secondGrid.t(j) - grid.t(i - 1)), -H + 0.5); } volterraT[j] = (v); } double VIX_future_i = VIX_Calculate(epsilon, secondGrid, volterraT); VIXFutures[k - 1] += VIX_future_i; } return(VIXFutures); }
public Plot(ColumnVector x_i, ColumnVector y_i, ColumnVector y_i2, string title, string xName, string yName, string curveName) { InitializeComponent(); GraphPane myPane = zedGraphControl1.GraphPane; myPane.Title.Text = title; myPane.XAxis.Title.Text = xName; myPane.YAxis.Title.Text = yName; PointPairList VIX_fut_Prices_LogN = new PointPairList(); PointPairList VIX_fut_Prices_LogN2 = new PointPairList(); for (int i = 0; i < x_i.Count(); i++) { VIX_fut_Prices_LogN.Add(x_i[i], y_i[i]); VIX_fut_Prices_LogN2.Add(x_i[i], y_i2[i]); //VIX_fut_Prices_TrCholesky.Add(x_i[i], model.TruncatedCholeskyBergomi(x_i[i], epsilon_0)); } LineItem teamACurve = myPane.AddCurve(curveName, VIX_fut_Prices_LogN, Color.Red, SymbolType.None); LineItem teamACurve2 = myPane.AddCurve(curveName, VIX_fut_Prices_LogN2, Color.Blue, SymbolType.None); zedGraphControl1.AxisChange(); }
public Plot(ColumnVector x_i, ColumnVector y_i, string title, string xName, string yName, string curveName) { InitializeComponent(); GraphPane myPane = zedGraphControl1.GraphPane; myPane.Title.Text = title; myPane.XAxis.Title.Text = xName; myPane.YAxis.Title.Text = yName; PointPairList VIX_fut_Prices_LogN = new PointPairList(); rBergomiVIXfuture model = new rBergomiVIXfuture(); Func <double, double> epsilon_0 = t => Math.Pow(0.235, 2);// *Math.Pow(1 + t, 0.5); for (int i = 0; i < x_i.Count(); i++) { VIX_fut_Prices_LogN.Add(x_i[i], y_i[i]); //VIX_fut_Prices_TrCholesky.Add(x_i[i], model.TruncatedCholeskyBergomi(x_i[i], epsilon_0)); } LineItem teamACurve = myPane.AddCurve(curveName, VIX_fut_Prices_LogN, Color.Red, SymbolType.None); zedGraphControl1.AxisChange(); }
public Plot(ColumnVector x_i, ColumnVector y_i, ColumnVector y_i2, ColumnVector y_i3) { InitializeComponent(); GraphPane myPane = zedGraphControl1.GraphPane; myPane.Title.Text = "VIX Futures"; myPane.XAxis.Title.Text = "T"; myPane.YAxis.Title.Text = "VIX futures"; PointPairList VIX_fut_Prices_LogN = new PointPairList(); PointPairList VIX_fut_Prices_LogN2 = new PointPairList(); PointPairList VIX_fut_Prices_LogN3 = new PointPairList(); for (int i = 0; i < x_i.Count(); i++) { VIX_fut_Prices_LogN.Add(x_i[i], y_i[i]); VIX_fut_Prices_LogN2.Add(x_i[i], y_i2[i]); VIX_fut_Prices_LogN3.Add(x_i[i], y_i3[i]); //VIX_fut_Prices_TrCholesky.Add(x_i[i], model.TruncatedCholeskyBergomi(x_i[i], epsilon_0)); } LineItem teamACurve = myPane.AddCurve("Log Normal", VIX_fut_Prices_LogN, Color.Red, SymbolType.None); LineItem teamACurve2 = myPane.AddCurve("Hybrid + Euler", VIX_fut_Prices_LogN2, Color.Blue, SymbolType.None); LineItem teamACurve3 = myPane.AddCurve("Truncated Cholesky", VIX_fut_Prices_LogN3, Color.Green, SymbolType.None); zedGraphControl1.AxisChange(); }
static void Main(string[] args) { //Ploting results Graph //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Graph()); rBergomiVIXfuture model = new rBergomiVIXfuture(); Func <double, double> epsilon_0 = t => Math.Pow(0.235, 2);// *Math.Sqrt(1 + t); double T = 2.0; //// the Gri t_0 ..... t_{100} = T int n = 500; Grid grid = new Grid(0, T, (int)Math.Abs(T * n)); //lognormal double vixfuture_LogNormal = model.VIXfuture_LogNormal(grid.t(35), epsilon_0); // //Mc with hybrid Scheme ColumnVector vixfuture_Hybrid = model.VIXfuture_HybridMethod(T, epsilon_0); ColumnVector newvixfutures = new ColumnVector(vixfuture_Hybrid.Count()); ColumnVector newvixlognorm = new ColumnVector(vixfuture_Hybrid.Count()); ColumnVector newvixtruncated = new ColumnVector(vixfuture_Hybrid.Count()); ColumnVector ti = new ColumnVector(vixfuture_Hybrid.Count()); int perdiod = 100; for (int i = 0; i < vixfuture_Hybrid.Count(); i++) { int N_i = (i + 1) * perdiod; ti[i] = grid.t((N_i)); newvixfutures[i] = vixfuture_Hybrid[i]; newvixtruncated[i] = model.VIXfuture_TruncatedChlsky(ti[i], epsilon_0); newvixlognorm[i] = model.VIXfuture_LogNormal(ti[i], epsilon_0); } Application.Run(new Plot(ti, newvixlognorm, newvixfutures, newvixtruncated)); //Application.Run(new Plot(ti, newvixfutures,newvixlognorm, "Volterra Simulation in hybrid Method", "T + Delta ", "V", "volterra process")); //Application.Run(new Plot(ti, newvixlognorm, "Volterra Simulation in hybrid Method", "T + Delta ", "V", "volterra process")); // //Mc with trancated Cholesky double TrancatedMethode_MCstdDeviation; double vixfuture_trancated = model.VIXfuture_TruncatedChlsky(T, epsilon_0, out TrancatedMethode_MCstdDeviation); //S&P Option Pricing VIXOption option = new VIXOption(VIXOption.OptionType.Call, 1.0, 100); double optionPrice = model.PriceVIXOption(option, epsilon_0, 100); } //Main
public void simulateFFT(out ColumnVector Z, out ColumnVector volterra) { int n_T = grid.get_timeNmbrStep(); //Z_i defined in A.1 Z = new ColumnVector(n_T); RectangularMatrix Z_k = new RectangularMatrix(2, n_T); volterra = new ColumnVector(n_T); //DateTime starttestM = DateTime.Now; //for (int zz = 1; zz <= 1.0E4; zz++) //{ // SimulateCorrelated(Z, Z_k); //} //TimeSpan timeExecutiontestM = DateTime.Now - starttestM; SimulateCorrelated(Z, Z_k);//Simulate 3 Correlated Gaussians Z_i; Z_k_{1,i}; Z_k_{2,i} i:= 0, ..., n_T //Gamma for Convolution double[] Zdouble = Z.ToArray(); double[] convolution; DateTime starttest = DateTime.Now; //for (int zz = 1; zz <= 1.0E4; zz++) // alglib.convr1d(Gamma.ToArray(), n_T, Z.ToArray(), n_T, out convolution2); //TimeSpan timeExecutiontest = DateTime.Now - starttest; alglib.convr1d(Gamma.ToArray(), n_T, Z.ToArray(), n_T, out convolution); //DateTime starttestM = DateTime.Now; //for (int zz = 1; zz <= 1.0E5; zz++) //{ //} //TimeSpan timeExecutiontestM = DateTime.Now - starttestM; //var convolutionM = new Double1DConvolution(Z, Gamma.Count()); //DoubleVector convolution = convolutionM.Convolve(Gamma); for (int i = 1; i <= volterra.Count(); i++) { double v = convolution[i - 1]; for (int k = 1; k < Math.Min(i, kappa); k++) { v += Z_k[k - 1, i - k]; } volterra[i - 1] = v; } }
public ColumnVector VIXfuture_HybridMethod(double T, Func <double, double> epsilon) { //Result ColumnVector VIXFutures; //For Exécution Time DateTime start = DateTime.Now; //Input: int kappa = 2; // the Gri t_0 ..... t_{100} = T int n = 500; Grid grid = new Grid(0, T, (int)Math.Abs(T * n)); int n_T = grid.get_timeNmbrStep(); n_tH = Math.Pow(n_T, H - 0.5); //optimizeMC //The second Grid t_0=T, t_1 ..... t_N = T + Delta int Nbstep = 20; //Correlation : SymmetricMatrix correl = MakeCorrel(n, grid); SquareMatrix choleskyCorrel = correl.CholeskyDecomposition().SquareRootMatrix(); int period = 100; VIXFutures = new ColumnVector(grid.get_timeNmbrStep() / period); //-------------------------------------------------------------------------------------------------------------------------------------------------------------- //-----------------------------------MC---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------------------------------------------- var MCnbofSimulation = 3.0E4; HybridScheme hybridscheme = new HybridScheme(choleskyCorrel, grid, kappa, H); for (int mc = 1; mc <= MCnbofSimulation; mc++) { // 1-simulation of volterra Hybrid Scheme ColumnVector Z; ColumnVector volterra; ColumnVector Z_Brownian; hybridscheme.simulateFFT(out Z, out volterra); //2 - extract the path of the Brownian motion Z driving the Volterra process Z_Brownian = ExtractBrownian(kappa, n_T, Z, volterra); //Grid secondGrid2 = new Grid(grid.t(n_T), grid.t(n_T) + Delta, 20); //ColumnVector volterraT2 = EulerMEthode(grid, secondGrid2, n_T, volterra, Z_Brownian); //double VIX_future_i2 = VIX_Calculate(epsilon, secondGrid2, volterraT2); //VIXFutures[0] += VIX_future_i2; //DateTime starttestM = DateTime.Now; //for (int zz = 1; zz <= 1.0E4; zz++) //{ // hybridscheme.simulateFFT(out Z, out volterra); //} //TimeSpan timeExecutiontestM = DateTime.Now - starttestM; //DateTime starttest = DateTime.Now; //for (int zz = 1; zz <= 1.0E4; zz++) //{ // for (int i = 1; i <= volterra.Count() / period; i++) // { // int N_i2 = i * period; // //3- approximate the continuous-time process V^T by the discrete-time version V^T~ defined via the forward Euler scheme // double[] volterraT2 = EulerMEthode(grid, secondGrid, N_i2, volterra, Z_Brownian); // //double VIX_future_i2 = VIX_Calculate(epsilon, secondGrid, volterraT2); // //VIXFutures[i - 1] += VIX_future_i2; // } //} //TimeSpan timeExecutiontest = DateTime.Now - starttest; for (int i = 1; i <= volterra.Count() / period; i++) { int N_i = i * period; Grid secondGrid = new Grid(grid.t(N_i), grid.t(N_i) + Delta, Nbstep); //3- approximate the continuous-time process V^T by the discrete-time version V^T~ defined via the forward Euler scheme double[] volterraT = EulerMEthode(grid, secondGrid, N_i, volterra, Z_Brownian); double VIX_future_i = VIX_Calculate(epsilon, secondGrid, volterraT); VIXFutures[i - 1] += VIX_future_i; } }// ENd of MC //MC Mean for (int i = 0; i < VIXFutures.Count(); i++) { VIXFutures[i] /= MCnbofSimulation; } TimeSpan timeExecution = DateTime.Now - start; return(VIXFutures); }
/// <summary> /// /// </summary> /// <param name="pop"></param> /// <param name="muBest"></param> /// <param name="muWorst"></param> public void ReEstimate(RectangularMatrix pop, double muBest, double muWorst) { if (pop.ColumnCount != p.Mu) { throw new ApplicationException(""); } int n = p.N; fitnessHistory[gen % fitnessHistory.Count()] = muBest; // needed for divergence check ColumnVector oldmean = new ColumnVector(mean.Dimension); for (int i = 0; i < mean.Dimension; i++) { oldmean[i] = mean [i]; } double[] BDz = new double[n]; // valarray<double> /* calculate xmean and rgBDz~N(0,C) */ for (int i = 0; i < n; ++i) { mean[i] = 0.0; for (int j = 0; j < pop.ColumnCount; ++j) { mean[i] += p.Weights[j] * (pop.Column(j))[i]; } BDz[i] = Math.Sqrt(p.MuEff) * (mean[i] - oldmean[i]) / sigma; } ColumnVector tmp = new ColumnVector(n); // [vector<double>] /* calculate z := D^(-1) * B^(-1) * rgBDz into rgdTmp */ for (int i = 0; i < n; ++i) { double sum = 0.0; for (int j = 0; j < n; ++j) { sum += B[j, i] * BDz[j]; } tmp[i] = sum / d[i]; } /* cumulation for sigma (ps) using B*z */ for (int i = 0; i < n; ++i) { double sum = 0.0; for (int j = 0; j < n; ++j) { sum += B[i, j] * tmp[j]; } ps[i] = (1.0 - p.CCumSig) * ps[i] + Math.Sqrt(p.CCumSig * (2.0 - p.CCumSig)) * sum; } /* calculate norm(ps)^2 */ double psxps = ps.Sum(x => x * x); double chiN = Math.Sqrt((double)p.N) * (1.0 - 1.0 / (4.0 * p.N) + 1.0 / (21.0 * p.N * p.N)); /* cumulation for covariance matrix (pc) using B*D*z~N(0,C) */ bool isHsig = Math.Sqrt(psxps) / Math.Sqrt(1.0 - Math.Pow(1.0 - p.CCumSig, 2.0 * gen)) / chiN < 1.5 + 1.0 / (p.N - 0.5); double hsig = (isHsig) ? 1.0 : 0.0; //pc = (1.0 - p.ccumcov) * pc + hsig * Math.Sqrt(p.ccumcov * (2.0 - p.ccumcov)) * BDz; pc = pc.Select(x => x * (1.0 - p.CCumCov)).Zip(BDz.Select(x => x * Math.Sqrt(p.CCumCov * (2.0 - p.CCumCov)) * hsig), (lhs, rhs) => lhs + rhs).ToArray(); /* stop initial phase (MK, this was not reachable in the org code, deleted) */ /* remove momentum in ps, if ps is large and fitness is getting worse */ if (gen >= fitnessHistory.Count()) { // find direction from muBest and muWorst (muBest == muWorst handled seperately double direction = (muBest < muWorst) ? -1.0 : 1.0; int now = gen % fitnessHistory.Count(); int prev = (gen - 1) % fitnessHistory.Count(); int prevprev = (gen - 2) % fitnessHistory.Count(); bool fitnessWorsens = (muBest == muWorst) || // <- increase norm also when population has converged (this deviates from Hansen's scheme) ((direction * fitnessHistory[now] < direction * fitnessHistory[prev]) && (direction * fitnessHistory[now] < direction * fitnessHistory[prevprev])); if (psxps / p.N > 1.5 + 10.0 * Math.Sqrt(2.0 / p.N) && fitnessWorsens) { double tfac = Math.Sqrt((1 + Math.Max(0.0, Math.Log(psxps / p.N))) * p.N / psxps); ps = ps.Select(x => x * tfac).ToArray(); psxps *= tfac * tfac; } } /* update of C */ /* Adapt_C(t); not used anymore */ if (p.CCov != 0.0) { //flgEigensysIsUptodate = 0; /* update covariance matrix */ for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { C[i, j] = (1 - p.CCov) * C[i, j] + p.CCov * (1.0 / p.MuCov) * pc[i] * pc[j] + (1 - hsig) * p.CCumCov * (2.0 - p.CCumCov) * C[i, j]; /*C[i][j] = (1 - p.ccov) * C[i][j] + sp.ccov * (1./sp.mucov) * (rgpc[i] * rgpc[j] + (1-hsig)*sp.ccumcov*(2.-sp.ccumcov) * C[i][j]); */ for (int k = 0; k < p.Mu; ++k) { /* additional rank mu update */ C[i, j] += p.CCov * (1 - 1.0 / p.MuCov) * p.Weights[k] * ((pop.Column(k))[i] - oldmean[i]) * ((pop.Column(k))[j] - oldmean[j]) / sigma / sigma; // * (rgrgx[index[k]][i] - rgxold[i]) // * (rgrgx[index[k]][j] - rgxold[j]) // / sigma / sigma; } } } } /* update of sigma */ sigma *= Math.Exp(((Math.Sqrt(psxps) / chiN) - 1.0) / p.Damp); /* calculate eigensystem, must be done by caller */ //cmaes_UpdateEigensystem(0); /* treat minimal standard deviations and numeric problems * Note that in contrast with the original code, some numerical issues are treated *before* we * go into the eigenvalue calculation */ treatNumericalIssues(muBest, muWorst); gen++; // increase generation }