public void testBSMOperatorConsistency() { //("Testing consistency of BSM operators..."); Vector grid = new Vector(10); double price = 20.0; double factor = 1.1; for (int i = 0; i < grid.size(); i++) { grid[i] = price; price *= factor; } double dx = Math.Log(factor); double r = 0.05; double q = 0.01; double sigma = 0.5; BSMOperator refer = new BSMOperator(grid.size(), dx, r, q, sigma); DayCounter dc = new Actual360(); Date today = Date.Today; Date exercise = today + new Period(2, TimeUnit.Years); double residualTime = dc.yearFraction(today, exercise); SimpleQuote spot = new SimpleQuote(0.0); YieldTermStructure qTS = Utilities.flatRate(today, q, dc); YieldTermStructure rTS = Utilities.flatRate(today, r, dc); BlackVolTermStructure volTS = Utilities.flatVol(today, sigma, dc); GeneralizedBlackScholesProcess stochProcess = new GeneralizedBlackScholesProcess( new Handle <Quote>(spot), new Handle <YieldTermStructure>(qTS), new Handle <YieldTermStructure>(rTS), new Handle <BlackVolTermStructure>(volTS)); BSMOperator op1 = new BSMOperator(grid, stochProcess, residualTime); PdeOperator <PdeBSM> op2 = new PdeOperator <PdeBSM>(grid, stochProcess, residualTime); double tolerance = 1.0e-6; Vector lderror = refer.lowerDiagonal() - op1.lowerDiagonal(); Vector derror = refer.diagonal() - op1.diagonal(); Vector uderror = refer.upperDiagonal() - op1.upperDiagonal(); for (int i = 2; i < grid.size() - 2; i++) { if (Math.Abs(lderror[i]) > tolerance || Math.Abs(derror[i]) > tolerance || Math.Abs(uderror[i]) > tolerance) { Assert.Fail("inconsistency between BSM operators:\n" + i + " row:\n" + "expected: " + refer.lowerDiagonal()[i] + ", " + refer.diagonal()[i] + ", " + refer.upperDiagonal()[i] + "\n" + "calculated: " + op1.lowerDiagonal()[i] + ", " + op1.diagonal()[i] + ", " + op1.upperDiagonal()[i]); } } lderror = refer.lowerDiagonal() - op2.lowerDiagonal(); derror = refer.diagonal() - op2.diagonal(); uderror = refer.upperDiagonal() - op2.upperDiagonal(); for (int i = 2; i < grid.size() - 2; i++) { if (Math.Abs(lderror[i]) > tolerance || Math.Abs(derror[i]) > tolerance || Math.Abs(uderror[i]) > tolerance) { Assert.Fail("inconsistency between BSM operators:\n" + i + " row:\n" + "expected: " + refer.lowerDiagonal()[i] + ", " + refer.diagonal()[i] + ", " + refer.upperDiagonal()[i] + "\n" + "calculated: " + op2.lowerDiagonal()[i] + ", " + op2.diagonal()[i] + ", " + op2.upperDiagonal()[i]); } } }
public void testBSMOperatorConsistency() { //("Testing consistency of BSM operators..."); Vector grid = new Vector(10); double price = 20.0; double factor = 1.1; for (int i = 0; i < grid.size(); i++) { grid[i] = price; price *= factor; } double dx = Math.Log(factor); double r = 0.05; double q = 0.01; double sigma = 0.5; BSMOperator refer = new BSMOperator(grid.size(), dx, r, q, sigma); DayCounter dc = new Actual360(); Date today = Date.Today; Date exercise = today + new Period(2, TimeUnit.Years); double residualTime = dc.yearFraction(today, exercise); SimpleQuote spot = new SimpleQuote(0.0); YieldTermStructure qTS = Utilities.flatRate(today, q, dc); YieldTermStructure rTS = Utilities.flatRate(today, r, dc); BlackVolTermStructure volTS = Utilities.flatVol(today, sigma, dc); GeneralizedBlackScholesProcess stochProcess = new GeneralizedBlackScholesProcess( new Handle<Quote>(spot), new Handle<YieldTermStructure>(qTS), new Handle<YieldTermStructure>(rTS), new Handle<BlackVolTermStructure>(volTS)); BSMOperator op1 = new BSMOperator(grid, stochProcess, residualTime); PdeOperator<PdeBSM> op2 = new PdeOperator<PdeBSM>(grid, stochProcess, residualTime); double tolerance = 1.0e-6; Vector lderror = refer.lowerDiagonal() - op1.lowerDiagonal(); Vector derror = refer.diagonal() - op1.diagonal(); Vector uderror = refer.upperDiagonal() - op1.upperDiagonal(); for (int i = 2; i < grid.size() - 2; i++) { if (Math.Abs(lderror[i]) > tolerance || Math.Abs(derror[i]) > tolerance || Math.Abs(uderror[i]) > tolerance) { Assert.Fail("inconsistency between BSM operators:\n" + i + " row:\n" + "expected: " + refer.lowerDiagonal()[i] + ", " + refer.diagonal()[i] + ", " + refer.upperDiagonal()[i] + "\n" + "calculated: " + op1.lowerDiagonal()[i] + ", " + op1.diagonal()[i] + ", " + op1.upperDiagonal()[i]); } } lderror = refer.lowerDiagonal() - op2.lowerDiagonal(); derror = refer.diagonal() - op2.diagonal(); uderror = refer.upperDiagonal() - op2.upperDiagonal(); for (int i = 2; i < grid.size() - 2; i++) { if (Math.Abs(lderror[i]) > tolerance || Math.Abs(derror[i]) > tolerance || Math.Abs(uderror[i]) > tolerance) { Assert.Fail("inconsistency between BSM operators:\n" + i + " row:\n" + "expected: " + refer.lowerDiagonal()[i] + ", " + refer.diagonal()[i] + ", " + refer.upperDiagonal()[i] + "\n" + "calculated: " + op2.lowerDiagonal()[i] + ", " + op2.diagonal()[i] + ", " + op2.upperDiagonal()[i]); } } }