CustomFundingCurveReconciler.java

  1. package org.drip.sample.funding;

  2. import java.util.List;

  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.definition.Turn;
  5. import org.drip.analytics.support.*;
  6. import org.drip.function.r1tor1.QuadraticRationalShapeControl;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.param.creator.MarketParamsBuilder;
  9. import org.drip.param.period.*;
  10. import org.drip.param.valuation.*;
  11. import org.drip.product.creator.*;
  12. import org.drip.product.rates.*;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.spline.basis.PolynomialFunctionSetParams;
  15. import org.drip.spline.params.*;
  16. import org.drip.spline.stretch.*;
  17. import org.drip.state.curve.DiscountFactorDiscountCurve;
  18. import org.drip.state.discount.*;
  19. import org.drip.state.estimator.LatentStateStretchBuilder;
  20. import org.drip.state.identifier.*;
  21. import org.drip.state.inference.*;

  22. /*
  23.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  24.  */

  25. /*!
  26.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  27.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  28.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  29.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  30.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  31.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  32.  *
  33.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  34.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  35.  *      credit, commodity, equity, FX, and structured products.
  36.  *  
  37.  *      https://lakshmidrip.github.io/DROP/
  38.  *  
  39.  *  DROP is composed of three modules:
  40.  *  
  41.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  42.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  43.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  44.  *
  45.  *  DROP Analytics Core implements libraries for the following:
  46.  *  - Fixed Income Analytics
  47.  *  - Asset Backed Analytics
  48.  *  - XVA Analytics
  49.  *  - Exposure and Margin Analytics
  50.  *
  51.  *  DROP Portfolio Core implements libraries for the following:
  52.  *  - Asset Allocation Analytics
  53.  *  - Transaction Cost Analytics
  54.  *
  55.  *  DROP Numerical Core implements libraries for the following:
  56.  *  - Statistical Learning
  57.  *  - Numerical Optimizer
  58.  *  - Spline Builder
  59.  *  - Algorithm Support
  60.  *
  61.  *  Documentation for DROP is Spread Over:
  62.  *
  63.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  64.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  65.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  66.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  67.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  68.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  69.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  70.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  71.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  72.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  73.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  74.  *
  75.  *  Licensed under the Apache License, Version 2.0 (the "License");
  76.  *      you may not use this file except in compliance with the License.
  77.  *  
  78.  *  You may obtain a copy of the License at
  79.  *      http://www.apache.org/licenses/LICENSE-2.0
  80.  *  
  81.  *  Unless required by applicable law or agreed to in writing, software
  82.  *      distributed under the License is distributed on an "AS IS" BASIS,
  83.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84.  *  
  85.  *  See the License for the specific language governing permissions and
  86.  *      limitations under the License.
  87.  */

  88. /**
  89.  * <i>CustomFundingCurveReconciler</i> demonstrates the multi-stretch transition custom Funding curve
  90.  * construction, turns application, discount factor extraction, and calibration quote recovery. It shows the
  91.  * following steps:
  92.  *  
  93.  * <br><br>
  94.  *  <ul>
  95.  *      <li>
  96.  *          Setup the linear curve calibrator.
  97.  *      </li>
  98.  *      <li>
  99.  *          Setup the cash instruments and their quotes for calibration.
  100.  *      </li>
  101.  *      <li>
  102.  *          Setup the cash instruments stretch latent state representation - this uses the discount factor
  103.  *              quantification metric and the "rate" manifest measure.
  104.  *      </li>
  105.  *      <li>
  106.  *          Setup the swap instruments and their quotes for calibration.
  107.  *      </li>
  108.  *      <li>
  109.  *          Setup the swap instruments stretch latent state representation - this uses the discount factor
  110.  *              quantification metric and the "rate" manifest measure.
  111.  *      </li>
  112.  *      <li>
  113.  *          Calibrate over the instrument set to generate a new overlapping latent state span instance.
  114.  *      </li>
  115.  *      <li>
  116.  *          Retrieve the "cash" stretch from the span.
  117.  *      </li>
  118.  *      <li>
  119.  *          Retrieve the "swap" stretch from the span.
  120.  *      </li>
  121.  *      <li>
  122.  *          Create a discount curve instance by converting the overlapping stretch to an exclusive
  123.  *              non-overlapping stretch.
  124.  *      </li>
  125.  *      <li>
  126.  *          Compare the discount factors and their monotonicity emitted from the discount curve, the
  127.  *              non-overlapping span, and the "swap" stretch across the range of tenor predictor ordinates.
  128.  *      </li>
  129.  *      <li>
  130.  *          Cross-Recovery of the Cash Calibration Instrument "Rate" metric across the different curve
  131.  *              construction methodologies.
  132.  *      </li>
  133.  *      <li>
  134.  *          Cross-Recovery of the Swap Calibration Instrument "Rate" metric across the different curve
  135.  *              construction methodologies.
  136.  *      </li>
  137.  *      <li>
  138.  *          Create a turn list instance and add new turn instances.
  139.  *      </li>
  140.  *      <li>
  141.  *          Update the discount curve with the turn list.
  142.  *      </li>
  143.  *      <li>
  144.  *          Compare the discount factor implied the discount curve with and without applying the turns
  145.  *              adjustment.
  146.  *      </li>
  147.  *  </ul>
  148.  *  
  149.  * <br><br>
  150.  *  <ul>
  151.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  152.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  153.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  154.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/funding/README.md">Funding Curve Builder</a></li>
  155.  *  </ul>
  156.  * <br><br>
  157.  *
  158.  * @author Lakshmi Krishnamurthy
  159.  */

  160. public class CustomFundingCurveReconciler {

  161.     /*
  162.      * Construct the Array of Deposit Instruments from the given set of parameters
  163.      *
  164.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  165.      */

  166.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  167.         final JulianDate dtEffective,
  168.         final String strCurrency,
  169.         final int[] aiDay)
  170.         throws Exception
  171.     {
  172.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  173.         ComposableFloatingUnitSetting cfus = new ComposableFloatingUnitSetting (
  174.             "3M",
  175.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  176.             null,
  177.             ForwardLabel.Create (
  178.                 strCurrency,
  179.                 "3M"
  180.             ),
  181.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  182.             0.
  183.         );

  184.         CompositePeriodSetting cps = new CompositePeriodSetting (
  185.             4,
  186.             "3M",
  187.             strCurrency,
  188.             null,
  189.             1.,
  190.             null,
  191.             null,
  192.             null,
  193.             null
  194.         );

  195.         CashSettleParams csp = new CashSettleParams (
  196.             0,
  197.             strCurrency,
  198.             0
  199.         );

  200.         for (int i = 0; i < aiDay.length; ++i) {
  201.             aDeposit[i] = new SingleStreamComponent (
  202.                 "DEPOSIT_" + aiDay[i],
  203.                 new Stream (
  204.                     CompositePeriodBuilder.FloatingCompositeUnit (
  205.                         CompositePeriodBuilder.EdgePair (
  206.                             dtEffective,
  207.                             dtEffective.addBusDays (
  208.                                 aiDay[i],
  209.                                 strCurrency
  210.                             )
  211.                         ),
  212.                         cps,
  213.                         cfus
  214.                     )
  215.                 ),
  216.                 csp
  217.             );

  218.             aDeposit[i].setPrimaryCode (aiDay[i] + "D");
  219.         }

  220.         return aDeposit;
  221.     }

  222.     /*
  223.      * Construct the Array of Swap Instruments from the given set of parameters
  224.      *
  225.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  226.      */

  227.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  228.         final JulianDate dtEffective,
  229.         final String strCurrency,
  230.         final String[] astrMaturityTenor)
  231.         throws Exception
  232.     {
  233.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  234.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  235.             2,
  236.             "Act/360",
  237.             false,
  238.             "Act/360",
  239.             false,
  240.             strCurrency,
  241.             true,
  242.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  243.         );

  244.         ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  245.             "6M",
  246.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  247.             null,
  248.             ForwardLabel.Create (
  249.                 strCurrency,
  250.                 "6M"
  251.             ),
  252.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  253.             0.
  254.         );

  255.         ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  256.             "6M",
  257.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  258.             null,
  259.             0.,
  260.             0.,
  261.             strCurrency
  262.         );

  263.         CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  264.             2,
  265.             "6M",
  266.             strCurrency,
  267.             null,
  268.             -1.,
  269.             null,
  270.             null,
  271.             null,
  272.             null
  273.         );

  274.         CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  275.             2,
  276.             "6M",
  277.             strCurrency,
  278.             null,
  279.             1.,
  280.             null,
  281.             null,
  282.             null,
  283.             null
  284.         );

  285.         CashSettleParams csp = new CashSettleParams (
  286.             0,
  287.             strCurrency,
  288.             0
  289.         );

  290.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  291.             List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  292.                 dtEffective,
  293.                 "6M",
  294.                 astrMaturityTenor[i],
  295.                 null
  296.             );

  297.             List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  298.                 dtEffective,
  299.                 "6M",
  300.                 astrMaturityTenor[i],
  301.                 null
  302.             );

  303.             Stream floatingStream = new Stream (
  304.                 CompositePeriodBuilder.FloatingCompositeUnit (
  305.                     lsFloatingStreamEdgeDate,
  306.                     cpsFloating,
  307.                     cfusFloating
  308.                 )
  309.             );

  310.             Stream fixedStream = new Stream (
  311.                 CompositePeriodBuilder.FixedCompositeUnit (
  312.                     lsFixedStreamEdgeDate,
  313.                     cpsFixed,
  314.                     ucasFixed,
  315.                     cfusFixed
  316.                 )
  317.             );

  318.             FixFloatComponent irs = new FixFloatComponent (
  319.                 fixedStream,
  320.                 floatingStream,
  321.                 csp
  322.             );

  323.             irs.setPrimaryCode ("IRS." + astrMaturityTenor[i] + "." + strCurrency);

  324.             aIRS[i] = irs;
  325.         }

  326.         return aIRS;
  327.     }

  328.     /*
  329.      * This sample demonstrates the multi-stretch transition custom discount curve construction, turns
  330.      *  application, discount factor extraction, and calibration quote recovery. It shows the following
  331.      *  steps:
  332.      *  - Setup the linear curve calibrator.
  333.      *  - Setup the cash instruments and their quotes for calibration.
  334.      *  - Setup the cash instruments stretch latent state representation - this uses the discount factor
  335.      *      quantification metric and the "rate" manifest measure.
  336.      *  - Setup the swap instruments and their quotes for calibration.
  337.      *  - Setup the swap instruments stretch latent state representation - this uses the discount factor
  338.      *      quantification metric and the "rate" manifest measure.
  339.      *  - Calibrate over the instrument set to generate a new overlapping latent state span instance.
  340.      *  - Retrieve the "cash" stretch from the span.
  341.      *  - Retrieve the "swap" stretch from the span.
  342.      *  - Create a discount curve instance by converting the overlapping stretch to an exclusive
  343.      *      non-overlapping stretch.
  344.      *  - Compare the discount factors and their monotonicity emitted from the discount curve, the
  345.      *      non-overlapping span, and the "swap" stretch across the range of tenor predictor ordinates.
  346.      *  - Cross-Recovery of the Cash Calibration Instrument "Rate" metric across the different curve
  347.      *      construction methodologies.
  348.      *  - Cross-Recovery of the Swap Calibration Instrument "Rate" metric across the different curve
  349.      *      construction methodologies.
  350.      *  - Create a turn list instance and add new turn instances.
  351.      *  - Update the discount curve with the turn list.
  352.      *  - Compare the discount factor implied the discount curve with and without applying the turns
  353.      *      adjustment.
  354.      *
  355.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  356.      */

  357.     private static final void SplineLinearDiscountCurve (
  358.         final JulianDate dtSpot,
  359.         final String strCurrency,
  360.         final SegmentCustomBuilderControl scbc)
  361.         throws Exception
  362.     {
  363.         /*
  364.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  365.          */

  366.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  367.             dtSpot,
  368.             strCurrency,
  369.             new int[] {
  370.                 1, 2, 7, 14, 30, 60
  371.             }
  372.         );

  373.         double[] adblDepositQuote = new double[] {
  374.             0.0013, 0.0017, 0.0017, 0.0018, 0.0020, 0.0023
  375.         };

  376.         /*
  377.          * Construct the Deposit Instrument Set Stretch Builder
  378.          */

  379.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  380.             "DEPOSIT",
  381.             aDepositComp,
  382.             "ForwardRate",
  383.             adblDepositQuote
  384.         );

  385.         /*
  386.          * Construct the Array of EDF Instruments and their Quotes from the given set of parameters
  387.          */

  388.         SingleStreamComponent[] aEDFComp = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  389.             dtSpot,
  390.             8,
  391.             strCurrency
  392.         );

  393.         double[] adblEDFQuote = new double[] {
  394.             0.0027, 0.0032, 0.0041, 0.0054, 0.0077, 0.0104, 0.0134, 0.0160
  395.         };

  396.         /*
  397.          * Construct the EDF Instrument Set Stretch Builder
  398.          */

  399.         LatentStateStretchSpec edfStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  400.             "EDF",
  401.             aEDFComp,
  402.             "ForwardRate",
  403.             adblEDFQuote
  404.         );

  405.         /*
  406.          * Construct the Array of Swap Instruments and their Quotes from the given set of parameters
  407.          */

  408.         FixFloatComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  409.             dtSpot,
  410.             strCurrency,
  411.             new java.lang.String[] {
  412.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  413.             }
  414.         );

  415.         double[] adblSwapQuote = new double[] {
  416.             0.0166, 0.0206, 0.0241, 0.0269, 0.0292, 0.0311, 0.0326, 0.0340, 0.0351, 0.0375, 0.0393, 0.0402, 0.0407, 0.0409, 0.0409
  417.         };

  418.         /*
  419.          * Construct the Swap Instrument Set Stretch Builder
  420.          */

  421.         LatentStateStretchSpec swapStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  422.             "SWAP",
  423.             aSwapComp,
  424.             "SwapRate",
  425.             adblSwapQuote
  426.         );

  427.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {depositStretch, edfStretch, swapStretch};

  428.         /*
  429.          * Set up the Linear Curve Calibrator using the following parameters:
  430.          *  - Cubic Exponential Mixture Basis Spline Set
  431.          *  - Ck = 2, Segment Curvature Penalty = 2
  432.          *  - Quadratic Rational Shape Controller
  433.          *  - Natural Boundary Setting
  434.          */

  435.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  436.             scbc,
  437.             BoundarySettings.NaturalStandard(),
  438.             MultiSegmentSequence.CALIBRATE,
  439.             null,
  440.             null
  441.         );

  442.         ValuationParams valParams = new ValuationParams (
  443.             dtSpot,
  444.             dtSpot,
  445.             strCurrency
  446.         );

  447.         /*
  448.          * Calibrate over the instrument set to generate a new overlapping latent state span instance
  449.          */

  450.         org.drip.spline.grid.OverlappingStretchSpan ors = lcc.calibrateSpan (
  451.             aStretchSpec,
  452.             1.,
  453.             valParams,
  454.             null,
  455.             null,
  456.             null
  457.         );

  458.         /*
  459.          * Retrieve the "Deposit" stretch from the span
  460.          */

  461.         MultiSegmentSequence mssDeposit = ors.getStretch ("DEPOSIT");

  462.         /*
  463.          * Retrieve the "swap" stretch from the span
  464.          */

  465.         MultiSegmentSequence mssSwap = ors.getStretch ("SWAP");

  466.         /*
  467.          * Create a discount curve instance by converting the overlapping stretch to an exclusive
  468.          *  non-overlapping stretch.
  469.          */

  470.         MergedDiscountForwardCurve dfdc = new DiscountFactorDiscountCurve (
  471.             strCurrency,
  472.             ors
  473.         );

  474.         /*
  475.          * Compare the discount factors and their monotonicity emitted from the discount curve, the
  476.          * non-overlapping span, and the Deposit stretch across the range of tenor predictor ordinates.
  477.          */

  478.         System.out.println ("\n\t----------------------------------------------------------------");

  479.         System.out.println ("\t     DEPOSIT DF            DFDC     STRETCH           LOCAL");

  480.         System.out.println ("\t----------------------------------------------------------------");

  481.         for (int iX = (int) mssDeposit.getLeftPredictorOrdinateEdge(); iX <= (int) mssDeposit.getRightPredictorOrdinateEdge();
  482.             iX += 0.1 * (mssDeposit.getRightPredictorOrdinateEdge() - mssDeposit.getLeftPredictorOrdinateEdge())) {
  483.             try {
  484.                 System.out.println ("\tDeposit [" + new JulianDate (iX) + "] = " +
  485.                     FormatUtil.FormatDouble (dfdc.df (iX), 1, 8, 1.) + " || " +
  486.                         ors.getContainingStretch (iX).name() + " || " +
  487.                             FormatUtil.FormatDouble (mssDeposit.responseValue (iX), 1, 8, 1.) + " | " +
  488.                                 mssDeposit.monotoneType (iX));
  489.             } catch (java.lang.Exception e) {
  490.                 e.printStackTrace();
  491.             }
  492.         }

  493.         /*
  494.          * Compare the discount factors and their monotonicity emitted from the discount curve, the
  495.          * non-overlapping span, and the "swap" stretch across the range of tenor predictor ordinates.
  496.          */

  497.         System.out.println ("\n\t----------------------------------------------------------------");

  498.         System.out.println ("\t     SWAP DF            DFDC     STRETCH            LOCAL");

  499.         System.out.println ("\t----------------------------------------------------------------");

  500.         for (int iX = (int) mssSwap.getLeftPredictorOrdinateEdge(); iX <= (int) mssSwap.getRightPredictorOrdinateEdge();
  501.                 iX += 0.05 * (mssSwap.getRightPredictorOrdinateEdge() - mssSwap.getLeftPredictorOrdinateEdge())) {
  502.                 System.out.println ("\tSwap [" + new JulianDate (iX) + "] = " +
  503.                     FormatUtil.FormatDouble (dfdc.df (iX), 1, 8, 1.) + " || " +
  504.                         ors.getContainingStretch (iX).name() + " || " +
  505.                             FormatUtil.FormatDouble (mssSwap.responseValue (iX), 1, 8, 1.) + " | " +
  506.                                 mssSwap.monotoneType (iX));
  507.         }

  508.         System.out.println ("\tSwap [" + dtSpot.addTenor ("60Y") + "] = " +
  509.             FormatUtil.FormatDouble (dfdc.df (dtSpot.addTenor ("60Y")), 1, 8, 1.));

  510.         /*
  511.          * Cross-Recovery of the Deposit Calibration Instrument "Rate" metric across the different curve
  512.          *  construction methodologies.
  513.          */

  514.         System.out.println ("\n\t----------------------------------------------------------------");

  515.         System.out.println ("\t     DEPOSIT INSTRUMENTS CALIBRATION RECOVERY");

  516.         System.out.println ("\t----------------------------------------------------------------");

  517.         for (int i = 0; i < aDepositComp.length; ++i)
  518.             System.out.println ("\t[" + aDepositComp[i].maturityDate() + "] = " +
  519.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (valParams, null,
  520.                     MarketParamsBuilder.Create (dfdc, null, null, null, null, null, null),
  521.                         null, "Rate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  522.         /*
  523.          * Cross-Recovery of the Swap Calibration Instrument "Rate" metric across the different curve
  524.          *  construction methodologies.
  525.          */

  526.         System.out.println ("\n\t----------------------------------------------------------------");

  527.         System.out.println ("\t     SWAP INSTRUMENTS CALIBRATION RECOVERY");

  528.         System.out.println ("\t----------------------------------------------------------------");

  529.         for (int i = 0; i < aSwapComp.length; ++i)
  530.             System.out.println ("\t[" + aSwapComp[i].maturityDate() + "] = " +
  531.                 FormatUtil.FormatDouble (aSwapComp[i].measureValue (valParams, null,
  532.                     MarketParamsBuilder.Create (dfdc, null, null, null, null, null, null),
  533.                         null, "CalibSwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblSwapQuote[i], 1, 6, 1.));

  534.         /*
  535.          * Create a turn list instance and add new turn instances
  536.          */

  537.         TurnListDiscountFactor tldc = new TurnListDiscountFactor();

  538.         tldc.addTurn (
  539.             new Turn (
  540.                 dtSpot.addTenor ("5Y").julian(),
  541.                 dtSpot.addTenor ("40Y").julian(),
  542.                 0.001
  543.             )
  544.         );

  545.         /*
  546.          * Update the discount curve with the turn list.
  547.          */

  548.         dfdc.setTurns (tldc);

  549.         /*
  550.          * Compare the discount factor implied the discount curve with and without applying the turns
  551.          *  adjustment.
  552.          */

  553.         System.out.println ("\n\t-------------------------------");

  554.         System.out.println ("\t     SWAP DF            DFDC");

  555.         System.out.println ("\t-------------------------------");

  556.         for (int iX = (int) mssSwap.getLeftPredictorOrdinateEdge(); iX <= (int) mssSwap.getRightPredictorOrdinateEdge();
  557.                 iX += 0.05 * (mssSwap.getRightPredictorOrdinateEdge() - mssSwap.getLeftPredictorOrdinateEdge())) {
  558.                 System.out.println ("\tSwap [" + new JulianDate (iX) + "] = " +
  559.                     FormatUtil.FormatDouble (dfdc.df (iX), 1, 8, 1.));
  560.         }

  561.         System.out.println ("\t-------------------------------");
  562.     }

  563.     public static final void main (
  564.         final String[] astrArgs)
  565.         throws Exception
  566.     {
  567.         /*
  568.          * Initialize the Credit Analytics Library
  569.          */

  570.         EnvManager.InitEnv ("");

  571.         /*
  572.          * Construct the segment Custom builder using the following parameters:
  573.          *  - Cubic Exponential Mixture Basis Spline Set
  574.          *  - Ck = 2, Segment Curvature Penalty = 2
  575.          *  - Quadratic Rational Shape Controller
  576.          */

  577.         SegmentCustomBuilderControl prbpPolynomial = new SegmentCustomBuilderControl (
  578.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  579.             new PolynomialFunctionSetParams (4),
  580.             SegmentInelasticDesignControl.Create (
  581.                 2,
  582.                 2
  583.             ),
  584.             new ResponseScalingShapeControl (
  585.                 true,
  586.                 new QuadraticRationalShapeControl (0.)
  587.             ),
  588.             null
  589.         );

  590.         /*
  591.          * Run the full spline linear discount curve builder sample.
  592.          */

  593.         SplineLinearDiscountCurve (
  594.             DateUtil.Today(),
  595.             "USD",
  596.             prbpPolynomial
  597.         );

  598.         EnvManager.TerminateEnv();
  599.     }
  600. }