public void testSqrt() { //BOOST_MESSAGE("Testing matricial square root..."); setup(); Matrix m = MatrixUtilitites.pseudoSqrt(M1, MatrixUtilitites.SalvagingAlgorithm.None); Matrix temp = m * Matrix.transpose(m); double error = norm(temp - M1); double tolerance = 1.0e-12; if (error > tolerance) { QAssert.Fail("Matrix square root calculation failed\n" + "original matrix:\n" + M1 + "pseudoSqrt:\n" + m + "pseudoSqrt*pseudoSqrt:\n" + temp + "\nerror: " + error + "\ntolerance: " + tolerance); } }
public void testHighamSqrt() { //BOOST_MESSAGE("Testing Higham matricial square root..."); setup(); Matrix tempSqrt = MatrixUtilitites.pseudoSqrt(M5, MatrixUtilitites.SalvagingAlgorithm.Higham); Matrix ansSqrt = MatrixUtilitites.pseudoSqrt(M6, MatrixUtilitites.SalvagingAlgorithm.None); double error = norm(ansSqrt - tempSqrt); double tolerance = 1.0e-4; if (error > tolerance) { QAssert.Fail("Higham matrix correction failed\n" + "original matrix:\n" + M5 + "pseudoSqrt:\n" + tempSqrt + "should be:\n" + ansSqrt + "\nerror: " + error + "\ntolerance: " + tolerance); } }
protected double mcReferenceValue(CmsCoupon cpn1, CmsCoupon cpn2, double cap, double floor, Handle <SwaptionVolatilityStructure> vol, double correlation) { List <double> acc = new List <double>(); int samples = 1000000; Matrix Cov = new Matrix(2, 2); Cov[0, 0] = vol.currentLink().blackVariance(cpn1.fixingDate(), cpn1.index().tenor(), cpn1.indexFixing()); Cov[1, 1] = vol.currentLink().blackVariance(cpn2.fixingDate(), cpn2.index().tenor(), cpn2.indexFixing()); Cov[0, 1] = Cov[1, 0] = Math.Sqrt(Cov[0, 0] * Cov[1, 1]) * correlation; Matrix C = MatrixUtilitites.pseudoSqrt(Cov, MatrixUtilitites.SalvagingAlgorithm.None); List <double> atmRate = new InitializedList <double>(2), adjRate = new InitializedList <double>(2), volShift = new InitializedList <double>(2); Vector avg = new Vector(2); atmRate[0] = cpn1.indexFixing(); atmRate[1] = cpn2.indexFixing(); adjRate[0] = cpn1.adjustedFixing; adjRate[1] = cpn2.adjustedFixing; if (vol.currentLink().volatilityType() == VolatilityType.ShiftedLognormal) { volShift[0] = vol.currentLink().shift(cpn1.fixingDate(), cpn1.index().tenor()); volShift[1] = vol.currentLink().shift(cpn2.fixingDate(), cpn2.index().tenor()); avg[0] = Math.Log((adjRate[0] + volShift[0]) / (atmRate[0] + volShift[0])) - 0.5 * Cov[0, 0]; avg[1] = Math.Log((adjRate[1] + volShift[1]) / (atmRate[1] + volShift[1])) - 0.5 * Cov[1, 1]; } else { avg[0] = adjRate[0]; avg[1] = adjRate[1]; } InverseCumulativeNormal icn = new InverseCumulativeNormal(); SobolRsg sb_ = new SobolRsg(2, 42); Vector w = new Vector(2), z = new Vector(2); for (int i = 0; i < samples; ++i) { List <double> seq = sb_.nextSequence().value; w[0] = icn.value(seq[0]); w[1] = icn.value(seq[1]); z = C * w + avg; for (int j = 0; j < 2; ++j) { if (vol.currentLink().volatilityType() == VolatilityType.ShiftedLognormal) { z[j] = (atmRate[j] + volShift[j]) * Math.Exp(z[j]) - volShift[j]; } } acc.Add(Math.Min(Math.Max(z[0] - z[1], floor), cap)); } return(acc.Average()); } // mcReferenceValue