Пример #1
0
  @Override
  public org.drip.state.estimator.PredictorResponseWeightConstraint generateCalibPRLC(
      final org.drip.param.valuation.ValuationParams valParams,
      final org.drip.param.pricer.PricerParams pricerParams,
      final org.drip.param.definition.ComponentMarketParams mktParams,
      final org.drip.param.valuation.QuotingParams quotingParams,
      final org.drip.state.representation.LatentStateMetricMeasure lsmm) {
    if (null == valParams
        || valParams.valueDate() >= getMaturityDate().getJulian()
        || null == lsmm
        || !(lsmm instanceof org.drip.analytics.rates.RatesLSMM)
        || !org.drip.analytics.rates.DiscountCurve.LATENT_STATE_DISCOUNT.equalsIgnoreCase(
            lsmm.getID())) return null;

    if (org.drip.analytics.rates.DiscountCurve.QUANTIFICATION_METRIC_FORWARD_RATE.equalsIgnoreCase(
            lsmm.getQuantificationMetric())
        || org.drip.analytics.rates.DiscountCurve.QUANTIFICATION_METRIC_DISCOUNT_FACTOR
            .equalsIgnoreCase(lsmm.getQuantificationMetric())) {
      if (null == mktParams) return null;

      org.drip.analytics.rates.DiscountCurve dc = mktParams.getDiscountCurve();

      if (null == dc) return null;

      org.drip.state.estimator.PredictorResponseWeightConstraint prwc =
          new org.drip.state.estimator.PredictorResponseWeightConstraint();

      double dblDirtyCV100 = 0.;
      boolean bFirstPeriod = true;

      try {
        for (org.drip.analytics.period.CashflowPeriod period : _lsCouponPeriod) {
          if (null == period) continue;

          double dblPayDate = period.getPayDate();

          if (valParams.valueDate() > dblPayDate) continue;

          double dblPeriodDCF = period.getCouponDCF();

          if (bFirstPeriod) {
            bFirstPeriod = false;

            dblPeriodDCF -= period.getAccrualDCF(valParams.valueDate());
          }

          double dblPeriodCV100 = dblPeriodDCF * dc.df(dblPayDate) * getNotional(dblPayDate);

          dblDirtyCV100 += dblPeriodCV100;

          if (!prwc.addPredictorResponseWeight(dblPayDate, dblPeriodCV100 * _dblNotional))
            return null;
        }

        return prwc.updateValue(-1. * dblDirtyCV100 * _dblNotional * lsmm.getMeasureQuoteValue())
            ? prwc
            : null;
      } catch (java.lang.Exception e) {
        e.printStackTrace();

        return null;
      }
    }

    return null;
  }
  private static final void CDSAPISample() throws Exception {
    JulianDate dtStart = JulianDate.Today();

    /*
     * Flat Discount Curve
     */

    DiscountCurve dc = DiscountCurveBuilder.CreateFromFlatRate(dtStart, "USD", 0.05);

    /*
     * Flat Credit Curve
     */

    CreditCurve cc = CreditCurveBuilder.FromFlatHazard(dtStart.getJulian(), "CC", "USD", 0.02, 0.4);

    /*
     * Component Market Parameters built from the Discount and the Credit Curves
     */

    ComponentMarketParams cmp = ComponentMarketParamsBuilder.MakeCreditCMP(dc, cc);

    /*
     * Create an SNAC CDS
     */

    CreditDefaultSwap cds = CDSBuilder.CreateSNAC(dtStart, "5Y", 0.1, "CC");

    /*
     * Valuation Parameters
     */

    ValuationParams valParams =
        ValuationParams.CreateValParams(dtStart, 0, "", Convention.DR_ACTUAL);

    /*
     * Standard Credit Pricer Parameters (check javadoc for details)
     */

    PricerParams pricerParams = PricerParams.MakeStdPricerParams();

    System.out.println(
        "Acc Start       Acc End     Pay Date    Index   Spread   Cpn DCF    Pay01    Surv01");

    System.out.println(
        "---------      ---------    ---------   ------  ------   -------- --------- --------");

    /*
     * CDS Coupon Cash Flow
     */

    for (CashflowPeriodCurveFactors p : cds.getCouponFlow(valParams, pricerParams, cmp))
      System.out.println(
          JulianDate.fromJulian(p.getAccrualStartDate())
              + FIELD_SEPARATOR
              + JulianDate.fromJulian(p.getAccrualEndDate())
              + FIELD_SEPARATOR
              + JulianDate.fromJulian(p.getPayDate())
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(p.getIndexRate(), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(p.getSpread(), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(p.getCouponDCF(), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dc.df(p.getPayDate()), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(cc.getSurvival(p.getPayDate()), 1, 4, 1.));

    System.out.println(
        "Loss Start     Loss End      Pay Date      Cpn    Notl     Rec    EffDF    StartSurv  EndSurv");

    System.out.println(
        "----------     --------      --------      ---    ----     ---    -----    ---------  -------");

    /*
     * CDS Loss Cash Flow
     */

    for (LossPeriodCurveFactors dp : cds.getLossFlow(valParams, pricerParams, cmp))
      System.out.println(
          JulianDate.fromJulian(dp.getStartDate())
              + FIELD_SEPARATOR
              + JulianDate.fromJulian(dp.getEndDate())
              + FIELD_SEPARATOR
              + JulianDate.fromJulian(dp.getPayDate())
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dp.getCouponDCF(), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dp.effectiveNotional(), 1, 0, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dp.effectiveRecovery(), 1, 2, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dp.effectiveDF(), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dp.startSurvival(), 1, 4, 1.)
              + FIELD_SEPARATOR
              + FormatUtil.FormatDouble(dp.endSurvival(), 1, 4, 1.));
  }
Пример #3
0
  @Override
  public org.drip.quant.calculus.WengertJacobian calcQuoteDFMicroJack(
      final java.lang.String strQuote,
      final org.drip.param.valuation.ValuationParams valParams,
      final org.drip.param.pricer.PricerParams pricerParams,
      final org.drip.param.definition.ComponentMarketParams mktParams,
      final org.drip.param.valuation.QuotingParams quotingParams) {
    if (null == valParams
        || valParams.valueDate() >= _dblMaturity
        || null == strQuote
        || null == mktParams
        || null == mktParams.getDiscountCurve()) return null;

    if ("Rate".equalsIgnoreCase(strQuote) || "SwapRate".equalsIgnoreCase(strQuote)) {
      org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapMeasures =
          value(valParams, pricerParams, mktParams, quotingParams);

      if (null == mapMeasures) return null;

      double dblDirtyDV01 = mapMeasures.get("DirtyDV01");

      double dblParSwapRate = mapMeasures.get("SwapRate");

      try {
        org.drip.quant.calculus.WengertJacobian wjSwapRateDFMicroJack = null;

        org.drip.analytics.rates.DiscountCurve dc = mktParams.getDiscountCurve();

        for (org.drip.analytics.period.CashflowPeriod p : _lsCouponPeriod) {
          double dblPeriodPayDate = p.getPayDate();

          if (dblPeriodPayDate < valParams.valueDate()) continue;

          org.drip.quant.calculus.WengertJacobian wjPeriodFwdRateDF =
              dc.getForwardRateJack(p.getStartDate(), p.getEndDate());

          org.drip.quant.calculus.WengertJacobian wjPeriodPayDFDF =
              dc.jackDDFDQuote(dblPeriodPayDate);

          if (null == wjPeriodFwdRateDF || null == wjPeriodPayDFDF) continue;

          double dblForwardRate = dc.libor(p.getStartDate(), p.getEndDate());

          double dblPeriodPayDF = dc.df(dblPeriodPayDate);

          if (null == wjSwapRateDFMicroJack)
            wjSwapRateDFMicroJack =
                new org.drip.quant.calculus.WengertJacobian(1, wjPeriodFwdRateDF.numParameters());

          double dblPeriodNotional = getNotional(p.getStartDate(), p.getEndDate());

          double dblPeriodDCF = p.getCouponDCF();

          for (int k = 0; k < wjPeriodFwdRateDF.numParameters(); ++k) {
            double dblPeriodMicroJack =
                (dblForwardRate - dblParSwapRate) * wjPeriodPayDFDF.getFirstDerivative(0, k)
                    + dblPeriodPayDF * wjPeriodFwdRateDF.getFirstDerivative(0, k);

            if (!wjSwapRateDFMicroJack.accumulatePartialFirstDerivative(
                0, k, dblPeriodNotional * dblPeriodDCF * dblPeriodMicroJack / dblDirtyDV01))
              return null;
          }
        }

        return wjSwapRateDFMicroJack;
      } catch (java.lang.Exception e) {
        e.printStackTrace();
      }
    }

    return null;
  }
  private static final Map<String, ForwardCurve> xM6MBasisSample(
      final JulianDate dtSpot,
      final String strCurrency,
      final DiscountCurve dc,
      final int iTenorInMonths,
      final String[] astrxM6MFwdTenor,
      final double[] adblxM6MBasisSwapQuote)
      throws Exception {
    System.out.println("------------------------------------------------------------");

    System.out.println(" SPL =>              n=4               |         |         |");

    System.out.println("---------------------------------------|  LOG DF |  LIBOR  |");

    System.out.println(" MSR =>  RECALC  |  REFEREN |  DERIVED |         |         |");

    System.out.println("------------------------------------------------------------");

    /*
     * Construct the 6M-xM float-float basis swap.
     */

    FloatFloatComponent[] aFFC =
        MakexM6MBasisSwap(dtSpot, strCurrency, astrxM6MFwdTenor, iTenorInMonths);

    String strBasisTenor = iTenorInMonths + "M";

    ValuationParams valParams = new ValuationParams(dtSpot, dtSpot, strCurrency);

    /*
     * Calculate the starting forward rate off of the discount curve.
     */

    double dblStartingFwd = dc.forward(dtSpot.julian(), dtSpot.addTenor(strBasisTenor).julian());

    /*
     * Set the discount curve based component market parameters.
     */

    CurveSurfaceQuoteSet mktParams =
        MarketParamsBuilder.Create(dc, null, null, null, null, null, null);

    Map<String, ForwardCurve> mapForward = new HashMap<String, ForwardCurve>();

    /*
     * Construct the shape preserving forward curve off of Quartic Polynomial Basis Spline.
     */

    ForwardCurve fcxMQuartic =
        ScenarioForwardCurveBuilder.ShapePreservingForwardCurve(
            "QUARTIC_FWD" + strBasisTenor,
            ForwardLabel.Create(strCurrency, strBasisTenor),
            valParams,
            null,
            mktParams,
            null,
            MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
            new PolynomialFunctionSetParams(5),
            aFFC,
            "ReferenceParBasisSpread",
            adblxM6MBasisSwapQuote,
            dblStartingFwd);

    mapForward.put(" QUARTIC_FWD" + strBasisTenor, fcxMQuartic);

    /*
     * Set the discount curve + quartic polynomial forward curve based component market parameters.
     */

    CurveSurfaceQuoteSet mktParamsQuarticFwd =
        MarketParamsBuilder.Create(dc, fcxMQuartic, null, null, null, null, null, null);

    int i = 0;
    int iFreq = 12 / iTenorInMonths;

    /*
     * Compute the following forward curve metrics for each of cubic polynomial forward, quartic
     * 	polynomial forward, and KLK Hyperbolic tension forward curves:
     * 	- Reference Basis Par Spread
     * 	- Derived Basis Par Spread
     *
     * Further compare these with a) the forward rate off of the discount curve, b) the LIBOR rate, and
     * 	c) Input Basis Swap Quote.
     */

    for (String strMaturityTenor : astrxM6MFwdTenor) {
      double dblFwdEndDate = dtSpot.addTenor(strMaturityTenor).julian();

      double dblFwdStartDate =
          dtSpot.addTenor(strMaturityTenor).subtractTenor(strBasisTenor).julian();

      FloatFloatComponent ffc = aFFC[i++];

      CaseInsensitiveTreeMap<Double> mapQuarticValue =
          ffc.value(valParams, null, mktParamsQuarticFwd, null);

      System.out.println(
          " "
              + strMaturityTenor
              + " =>  "
              + FormatUtil.FormatDouble(fcxMQuartic.forward(strMaturityTenor), 2, 2, 100.)
              + "  |  "
              + FormatUtil.FormatDouble(mapQuarticValue.get("ReferenceParBasisSpread"), 2, 2, 1.)
              + "  |  "
              + FormatUtil.FormatDouble(mapQuarticValue.get("DerivedParBasisSpread"), 2, 2, 1.)
              + "  |  "
              + FormatUtil.FormatDouble(
                  iFreq * java.lang.Math.log(dc.df(dblFwdStartDate) / dc.df(dblFwdEndDate)),
                  1,
                  2,
                  100.)
              + "  |  "
              + FormatUtil.FormatDouble(dc.libor(dblFwdStartDate, dblFwdEndDate), 1, 2, 100.)
              + "  |  ");
    }

    return mapForward;
  }