internal static global::System.Runtime.InteropServices.HandleRef getCPtr(LocalConstantVol obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(LocalConstantVol obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public void testFdmHestonVarianceMesher() { //Testing FDM Heston variance mesher... using (SavedSettings backup = new SavedSettings()) { Date today = new Date(22, 2, 2018); DayCounter dc = new Actual365Fixed(); Settings.Instance.setEvaluationDate(today); HestonProcess process = new HestonProcess(new Handle <YieldTermStructure>(Utilities.flatRate(0.02, dc)), new Handle <YieldTermStructure>(Utilities.flatRate(0.02, dc)), new Handle <Quote>(new SimpleQuote(100.0)), 0.09, 1.0, 0.09, 0.2, -0.5); FdmHestonVarianceMesher mesher = new FdmHestonVarianceMesher(5, process, 1.0); List <double> locations = mesher.locations(); double[] expected = new double[] { 0.0, 6.652314e-02, 9.000000e-02, 1.095781e-01, 2.563610e-01 }; double tol = 1e-6; double diff; for (int i = 0; i < locations.Count; ++i) { diff = Math.Abs(expected[i] - locations[i]); if (diff > tol) { QAssert.Fail("Failed to reproduce Heston variance mesh" + "\n calculated: " + locations[i] + "\n expected: " + expected[i] + "\n difference " + diff + "\n tolerance: " + tol); } } LocalVolTermStructure lVol = new LocalConstantVol(today, 2.5, dc); FdmHestonLocalVolatilityVarianceMesher constSlvMesher = new FdmHestonLocalVolatilityVarianceMesher(5, process, lVol, 1.0); double expectedVol = 2.5 * mesher.volaEstimate(); double calculatedVol = constSlvMesher.volaEstimate(); diff = Math.Abs(calculatedVol - expectedVol); if (diff > tol) { QAssert.Fail("Failed to reproduce Heston local volatility variance estimate" + "\n calculated: " + calculatedVol + "\n expected: " + expectedVol + "\n difference " + diff + "\n tolerance: " + tol); } double alpha = 0.01; LocalVolTermStructure leverageFct = new ParableLocalVolatility(today, 100.0, alpha, dc); FdmHestonLocalVolatilityVarianceMesher slvMesher = new FdmHestonLocalVolatilityVarianceMesher(5, process, leverageFct, 0.5, 1, 0.01); double initialVolEstimate = new FdmHestonVarianceMesher(5, process, 0.5, 1, 0.01).volaEstimate(); // double vEst = leverageFct.currentLink().localVol(0, 100) * initialVolEstimate; // Mathematica solution // N[Integrate[ // alpha*((100*Exp[vEst*x*Sqrt[0.5]] - 100)^2 + 25)* // PDF[NormalDistribution[0, 1], x], {x , // InverseCDF[NormalDistribution[0, 1], 0.01], // InverseCDF[NormalDistribution[0, 1], 0.99]}]] double leverageAvg = 0.455881 / (1 - 0.02); double volaEstExpected = 0.5 * (leverageAvg + leverageFct.localVol(0, 100)) * initialVolEstimate; double volaEstCalculated = slvMesher.volaEstimate(); if (Math.Abs(volaEstExpected - volaEstCalculated) > 0.001) { QAssert.Fail("Failed to reproduce Heston local volatility variance estimate" + "\n calculated: " + calculatedVol + "\n expected: " + expectedVol + "\n difference " + Math.Abs(volaEstExpected - volaEstCalculated) + "\n tolerance: " + tol); } } }