CapletStrippingDirectGlobalWithPenalty { public LeastSquareResults solveForVol(final double[][] capVols) { ArgumentChecker.noNulls(capVols, "capVols"); DoubleMatrix1D capVolVec = toVector(capVols, _nCapsTotal); int nObs = capVolVec.getNumberOfElements(); double[] errors = new double[nObs]; Arrays.fill(errors, DEFAULT_ERROR); int nNodes = _nodalSurfaceProvider.getNumberOfNodes(); double[] guess = new double[nNodes]; Arrays.fill(guess, 0.7); final Function1D<DoubleMatrix1D, Boolean> allowed = new Function1D<DoubleMatrix1D, Boolean>() { @Override public Boolean evaluate(final DoubleMatrix1D x) { double[] temp = x.getData(); int m = temp.length; for (int i = 0; i < m; i++) { if (temp[i] < 0) { return false; } } return true; } }; try { return NLLSWP.solve(capVolVec, new DoubleMatrix1D(errors), _capVols, _capVolsGrad, new DoubleMatrix1D(guess), _penalty, allowed); } catch (final Exception e) { NonLinearLeastSquareWithPenalty lqWithSvd = new NonLinearLeastSquareWithPenalty(); return lqWithSvd.solve(capVolVec, new DoubleMatrix1D(errors), _capVols, _capVolsGrad, new DoubleMatrix1D(guess), _penalty, allowed); } } CapletStrippingDirectGlobalWithPenalty(final List<CapFloor>[] caps, final MulticurveProviderInterface curves, final CapletVolatilityNodalSurfaceProvider nodalSurfaceProvider); @SuppressWarnings("unchecked")  CapletStrippingDirectGlobalWithPenalty(final List<CapFloor>[] caps, final MulticurveProviderInterface curves, final CapletVolatilityNodalSurfaceProvider nodalSurfaceProvider,
      final double lambdaStrike, final double lambdaTime); LeastSquareResults solveForVol(final double[][] capVols); LeastSquareResults solveForVol(final double[][] capVols, final double[][] errors, final double[][] guess); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test(enabled = false) public void MarketDataTestWithATM() { final double[] capStrikes = getStrikes(); final int nStrikes = capStrikes.length; final int nCapEndTimes = getCapEndTimes().length; final List<CapFloor> atmCaps = getATMCaps(); final List<CapFloor>[] caps = new List[nStrikes + nCapEndTimes]; final double[][] capVols = new double[nStrikes + nCapEndTimes][]; final int[] atmPos = new int[nCapEndTimes]; int k = 0; for (int i = 0; i < nStrikes; ++i) { while (k < nCapEndTimes && capStrikes[i] > atmCaps.get(k).getStrike()) { caps[i + k] = new ArrayList<>(1); capVols[i + k] = new double[1]; caps[i + k].add(atmCaps.get(k)); capVols[i + k][0] = getAtmVols()[k]; atmPos[k] = i + k; ++k; } caps[i + k] = getCaps(i); capVols[i + k] = getCapVols(i); if (i == nStrikes - 1 && k != nCapEndTimes) { while (k < nCapEndTimes) { caps[i + k] = new ArrayList<>(1); capVols[i + k] = new double[1]; caps[i + k].add(atmCaps.get(k)); capVols[i + k][0] = getAtmVols()[k]; atmPos[k] = i + k; ++k; } } } final double[] allCapStrikes = new double[nStrikes + nCapEndTimes]; for (int i = 0; i < nStrikes + nCapEndTimes; ++i) { allCapStrikes[i] = caps[i].get(0).getStrike(); } final int sample = nStrikes + nCapEndTimes - 1; final int nCapsSample = caps[sample].size(); final CapFloorIbor[] payments = caps[sample].get(nCapsSample - 1).getPayments(); final int nPayments = payments.length; final double[] fixingTimes = new double[nPayments]; final double[] PaymentTimes = new double[nPayments]; for (int i = 0; i < nPayments; ++i) { fixingTimes[i] = payments[i].getFixingTime(); PaymentTimes[i] = payments[i].getPaymentTime(); } final CapletVolatilityNodalSurfaceProvider provider = new CapletVolatilityNodalSurfaceProvider(allCapStrikes, fixingTimes); final CapletStrippingDirectGlobalWithPenalty cpst = new CapletStrippingDirectGlobalWithPenalty(caps, getYieldCurves(), provider); LeastSquareResults res = cpst.solveForVol(capVols); int l = 0; System.out.print("\t"); for (int j = 0; j < nPayments; ++j) { System.out.print(PaymentTimes[j] + "\t"); } System.out.print("\n"); for (int i = 0; i < nStrikes + nCapEndTimes; ++i) { if (l < nCapEndTimes && i == atmPos[l]) { ++l; } else { System.out.print(allCapStrikes[i] + "\t"); for (int j = 0; j < nPayments; ++j) { System.out.print(res.getFitParameters().getData()[i * nPayments + j] + "\t"); } System.out.print("\n"); } } System.out.println(res.getChiSq()); DoubleMatrix1D resVec = res.getFitParameters(); double[][] resMatrix = new double[nStrikes + nCapEndTimes][nPayments]; for (int i = 0; i < nStrikes + nCapEndTimes; ++i) { for (int j = 0; j < nPayments; ++j) { resMatrix[i][j] = resVec.getEntry(i * nPayments + j); } } for (int i = 0; i < nStrikes + nCapEndTimes; ++i) { final int nCaps = caps[i].size(); for (int j = 0; j < nCaps; ++j) { CapFloor cf = caps[i].get(j); CapFloorPricer pr = new CapFloorPricer(cf, getYieldCurves()); final int nP = pr.getNumberCaplets(); final double[] vols = Arrays.copyOf(resMatrix[i], nP); System.out.println(capVols[i][j] + "\t" + pr.impliedVol(vols)); } } }
@Test(enabled = false) public void MarketDataTestExcATM() { final double[] capStrikes = getStrikes(); final int nStrikes = capStrikes.length; final List<CapFloor>[] caps = new List[nStrikes]; final double[][] capVols = new double[nStrikes][]; int k = 0; for (int i = 0; i < nStrikes; ++i) { caps[i] = getCaps(i); capVols[i] = getCapVols(i); } final int sample = 5; final int nCapsSample = caps[sample].size(); final CapFloorIbor[] payments = caps[sample].get(nCapsSample - 1).getPayments(); final int nPayments = payments.length; final double[] fixingTimes = new double[nPayments]; for (int i = 0; i < nPayments; ++i) { fixingTimes[i] = payments[i].getFixingTime(); } final CapletVolatilityNodalSurfaceProvider provider = new CapletVolatilityNodalSurfaceProvider(capStrikes, fixingTimes); final CapletStrippingDirectGlobalWithPenalty cpst = new CapletStrippingDirectGlobalWithPenalty(caps, getYieldCurves(), provider); LeastSquareResults res = cpst.solveForVol(capVols); System.out.print("\t"); for (int j = 0; j < nPayments; ++j) { System.out.print(fixingTimes[j] + "\t"); } System.out.print("\n"); for (int i = 0; i < nStrikes; ++i) { System.out.print(capStrikes[i] + "\t"); for (int j = 0; j < nPayments; ++j) { System.out.print(res.getFitParameters().getData()[i * nPayments + j] + "\t"); } System.out.print("\n"); } System.out.println(res.getChiSq()); DoubleMatrix1D resVec = res.getFitParameters(); double[][] resMatrix = new double[nStrikes][nPayments]; for (int i = 0; i < nStrikes; ++i) { for (int j = 0; j < nPayments; ++j) { resMatrix[i][j] = resVec.getEntry(i * nPayments + j); } } for (int i = 0; i < nStrikes; ++i) { final int nCaps = caps[i].size(); for (int j = 0; j < nCaps; ++j) { CapFloor cf = caps[i].get(j); CapFloorPricer pr = new CapFloorPricer(cf, getYieldCurves()); final int nP = pr.getNumberCaplets(); final double[] vols = Arrays.copyOf(resMatrix[i], nP); System.out.println(capVols[i][j] + "\t" + pr.impliedVol(vols)); } } }