JurisdictionOTCIndexSwaps.java

  1. package org.drip.sample.floatfloat;

  2. import java.util.*;

  3. import org.drip.analytics.date.*;
  4. import org.drip.analytics.support.*;
  5. import org.drip.market.otc.*;
  6. import org.drip.numerical.common.*;
  7. import org.drip.param.creator.*;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.valuation.*;
  10. import org.drip.product.creator.SingleStreamComponentBuilder;
  11. import org.drip.product.definition.*;
  12. import org.drip.product.fx.ComponentPair;
  13. import org.drip.product.rates.*;
  14. import org.drip.service.env.EnvManager;
  15. import org.drip.spline.basis.PolynomialFunctionSetParams;
  16. import org.drip.spline.stretch.*;
  17. import org.drip.state.creator.*;
  18. import org.drip.state.discount.*;
  19. import org.drip.state.estimator.LatentStateStretchBuilder;
  20. import org.drip.state.forward.ForwardCurve;
  21. import org.drip.state.identifier.ForwardLabel;
  22. import org.drip.state.inference.*;

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

  26. /*!
  27.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  28.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  29.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  30.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  31.  * Copyright (C) 2015 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>JurisdictionOTCIndexSwaps</i> demonstrates the Construction and Usage of the Jurisdiction Standard OTC
  90.  * Float-Float Swaps.
  91.  *  
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  96.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/floatfloat/README.md">Float Float Swap Analytics</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class JurisdictionOTCIndexSwaps {

  104.     private static final FixFloatComponent OTCFixFloat (
  105.         final JulianDate dtSpot,
  106.         final String strCurrency,
  107.         final String strMaturityTenor,
  108.         final double dblCoupon)
  109.     {
  110.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  111.             strCurrency,
  112.             "ALL",
  113.             strMaturityTenor,
  114.             "MAIN"
  115.         );

  116.         return ffConv.createFixFloatComponent (
  117.             dtSpot,
  118.             strMaturityTenor,
  119.             dblCoupon,
  120.             0.,
  121.             1.
  122.         );
  123.     }

  124.     private static final FloatFloatComponent OTCFloatFloat (
  125.         final JulianDate dtSpot,
  126.         final String strCurrency,
  127.         final String strDerivedTenor,
  128.         final String strMaturityTenor,
  129.         final double dblBasis)
  130.     {
  131.         FloatFloatSwapConvention ffConv = IBORFloatFloatContainer.ConventionFromJurisdiction (strCurrency);

  132.         return ffConv.createFloatFloatComponent (
  133.             dtSpot,
  134.             strDerivedTenor,
  135.             strMaturityTenor,
  136.             dblBasis,
  137.             1.
  138.         );
  139.     }

  140.     private static final ComponentPair OTCComponentPair (
  141.         final JulianDate dtSpot,
  142.         final String strCurrency,
  143.         final String strDerivedTenor,
  144.         final String strMaturityTenor,
  145.         final double dblReferenceFixedCoupon,
  146.         final double dblDerivedFixedCoupon,
  147.         final double dblDerivedStreamBasis)
  148.     {
  149.         FloatFloatSwapConvention ffConv = IBORFloatFloatContainer.ConventionFromJurisdiction (strCurrency);

  150.         return ffConv.createFixFloatComponentPair (
  151.             dtSpot,
  152.             strDerivedTenor,
  153.             strMaturityTenor,
  154.             dblReferenceFixedCoupon,
  155.             dblDerivedFixedCoupon,
  156.             dblDerivedStreamBasis,
  157.             1.
  158.         );
  159.     }

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

  165.     private static final CalibratableComponent[] DepositFromMaturityDays (
  166.         final JulianDate dtEffective,
  167.         final int[] aiDay,
  168.         final int iNumFutures,
  169.         final String strCurrency)
  170.         throws Exception
  171.     {
  172.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFutures];

  173.         for (int i = 0; i < aiDay.length; ++i)
  174.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  175.                 dtEffective,
  176.                 dtEffective.addBusDays (
  177.                     aiDay[i],
  178.                     strCurrency
  179.                 ),
  180.                 ForwardLabel.Create (
  181.                     strCurrency,
  182.                     aiDay[i] + "D"
  183.                 )
  184.             );

  185.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  186.             dtEffective,
  187.             iNumFutures,
  188.             strCurrency
  189.         );

  190.         for (int i = aiDay.length; i < aiDay.length + iNumFutures; ++i)
  191.             aCalibComp[i] = aEDF[i - aiDay.length];

  192.         return aCalibComp;
  193.     }

  194.     /*
  195.      * Construct the Array of Swap Instruments from the given set of parameters
  196.      *
  197.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  198.      */

  199.     private static final FixFloatComponent[] SwapFromMaturityTenor (
  200.         final JulianDate dtSpot,
  201.         final String strCurrency,
  202.         final String[] astrMaturityTenor,
  203.         final double[] adblCoupon)
  204.         throws Exception
  205.     {
  206.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  207.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  208.             aIRS[i] = OTCFixFloat (
  209.                 dtSpot,
  210.                 strCurrency,
  211.                 astrMaturityTenor[i],
  212.                 adblCoupon[i]
  213.             );

  214.         return aIRS;
  215.     }

  216.     /*
  217.      * Construct the discount curve using the following steps:
  218.      *  - Construct the array of cash instruments and their quotes.
  219.      *  - Construct the array of swap instruments and their quotes.
  220.      *  - Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  221.      *
  222.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  223.      */

  224.     private static final MergedDiscountForwardCurve MakeDC (
  225.         final JulianDate dtSpot,
  226.         final String strCurrency)
  227.         throws Exception
  228.     {
  229.         /*
  230.          * Construct the array of Deposit instruments and their quotes.
  231.          */

  232.         CalibratableComponent[] aDepositComp = DepositFromMaturityDays (
  233.             dtSpot,
  234.             new int[] {
  235.             },
  236.             0,
  237.             strCurrency
  238.         );

  239.         double[] adblDepositQuote = new double[] {
  240.         };

  241.         /*
  242.          * Construct the array of Swap instruments and their quotes.
  243.          */

  244.         double[] adblSwapQuote = new double[] {
  245.             0.0009875,   //  9M
  246.             0.00122,     //  1Y
  247.             0.00223,     // 18M
  248.             0.00383,     //  2Y
  249.             0.00827,     //  3Y
  250.             0.01245,     //  4Y
  251.             0.01605,     //  5Y
  252.             0.02597      // 10Y
  253.         };

  254.         String[] astrSwapManifestMeasure = new String[] {
  255.             "SwapRate",     //  9M
  256.             "SwapRate",     //  1Y
  257.             "SwapRate",     // 18M
  258.             "SwapRate",     //  2Y
  259.             "SwapRate",     //  3Y
  260.             "SwapRate",     //  4Y
  261.             "SwapRate",     //  5Y
  262.             "SwapRate"      // 10Y
  263.         };

  264.         CalibratableComponent[] aSwapComp = SwapFromMaturityTenor (
  265.             dtSpot,
  266.             strCurrency,
  267.             new java.lang.String[] {
  268.                 "9M",
  269.                 "1Y",
  270.                 "18M",
  271.                 "2Y",
  272.                 "3Y",
  273.                 "4Y",
  274.                 "5Y",
  275.                 "10Y"
  276.             },
  277.             adblSwapQuote
  278.         );

  279.         /*
  280.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  281.          */

  282.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  283.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  284.             new ValuationParams (
  285.                 dtSpot,
  286.                 dtSpot,
  287.                 strCurrency
  288.             ),
  289.             aDepositComp,
  290.             adblDepositQuote,
  291.             null,
  292.             aSwapComp,
  293.             adblSwapQuote,
  294.             astrSwapManifestMeasure,
  295.             false
  296.         );
  297.     }

  298.     /*
  299.      * Construct an array of float-float swaps from the corresponding reference (6M) and the derived legs.
  300.      *
  301.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  302.      */

  303.     private static final FloatFloatComponent[] OTCFloatFloat (
  304.         final JulianDate dtSpot,
  305.         final String strCurrency,
  306.         final String[] astrMaturityTenor,
  307.         final int iTenorInMonths)
  308.         throws Exception
  309.     {
  310.         FloatFloatComponent[] aFFC = new FloatFloatComponent[astrMaturityTenor.length];

  311.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  312.             aFFC[i] = OTCFloatFloat (
  313.                 dtSpot,
  314.                 strCurrency,
  315.                 iTenorInMonths + "M",
  316.                 astrMaturityTenor[i],
  317.                 0.
  318.             );

  319.         return aFFC;
  320.     }

  321.     /*
  322.      * Construct an array of fix-float component pairs from the corresponding reference (6M) and the derived legs.
  323.      *
  324.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  325.      */

  326.     private static final ComponentPair[] OTCComponentPair (
  327.         final JulianDate dtSpot,
  328.         final String strCurrency,
  329.         final String[] astrMaturityTenor,
  330.         final int iTenorInMonths,
  331.         final CurveSurfaceQuoteContainer csqs)
  332.         throws Exception
  333.     {
  334.         if (null == astrMaturityTenor || 0 == astrMaturityTenor.length) return null;

  335.         ComponentPair[] aFFCP = new ComponentPair[astrMaturityTenor.length];

  336.         ValuationParams valParams = new ValuationParams (
  337.             dtSpot,
  338.             dtSpot,
  339.             strCurrency
  340.         );

  341.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  342.             ComponentPair cp = OTCComponentPair (
  343.                 dtSpot,
  344.                 strCurrency,
  345.                 iTenorInMonths + "M",
  346.                 astrMaturityTenor[i],
  347.                 0.,
  348.                 0.,
  349.                 0.
  350.             );

  351.             double dblReferenceFixedCoupon = cp.referenceComponent().measureValue (
  352.                 valParams,
  353.                 null,
  354.                 csqs,
  355.                 null,
  356.                 "FairPremium"
  357.             );

  358.             double dblDerivedFixedCoupon = cp.derivedComponent().measureValue (
  359.                 valParams,
  360.                 null,
  361.                 csqs,
  362.                 null,
  363.                 "FairPremium"
  364.             );

  365.             aFFCP[i] = OTCComponentPair (
  366.                 dtSpot,
  367.                 strCurrency,
  368.                 iTenorInMonths + "M",
  369.                 astrMaturityTenor[i],
  370.                 dblReferenceFixedCoupon,
  371.                 dblDerivedFixedCoupon,
  372.                 0.
  373.             );
  374.         }

  375.         return aFFCP;
  376.     }

  377.     private static final ForwardCurve MakeFloatFloatFC (
  378.         final JulianDate dtSpot,
  379.         final String strCurrency,
  380.         final MergedDiscountForwardCurve dc,
  381.         final int iTenorInMonths,
  382.         final String[] astrxM6MFwdTenor,
  383.         final double[] adblxM6MBasisSwapQuote,
  384.         final boolean bDisplay)
  385.         throws Exception
  386.     {
  387.         if (bDisplay) {
  388.             System.out.println ("------------------------------------------------------------");

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

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

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

  392.             System.out.println ("------------------------------------------------------------");
  393.         }

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

  397.         FloatFloatComponent[] aFFC = OTCFloatFloat (
  398.             dtSpot,
  399.             strCurrency,
  400.             astrxM6MFwdTenor,
  401.             iTenorInMonths
  402.         );

  403.         String strBasisTenor = iTenorInMonths + "M";

  404.         ValuationParams valParams = new ValuationParams (
  405.             dtSpot,
  406.             dtSpot,
  407.             strCurrency
  408.         );

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

  412.         double dblStartingFwd = dc.forward (
  413.             dtSpot.julian(),
  414.             dtSpot.addTenor (strBasisTenor).julian()
  415.         );

  416.         /*
  417.          * Set the discount curve based component market parameters.
  418.          */

  419.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  420.             dc,
  421.             null,
  422.             null,
  423.             null,
  424.             null,
  425.             null,
  426.             null
  427.         );

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

  431.         ForwardCurve fcxMQuartic = ScenarioForwardCurveBuilder.ShapePreservingForwardCurve (
  432.             "QUARTIC_FWD" + strBasisTenor,
  433.             ForwardLabel.Create (
  434.                 strCurrency,
  435.                 strBasisTenor
  436.             ),
  437.             valParams,
  438.             null,
  439.             mktParams,
  440.             null,
  441.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  442.             new PolynomialFunctionSetParams (4),
  443.             aFFC,
  444.             "DerivedParBasisSpread",
  445.             adblxM6MBasisSwapQuote,
  446.             dblStartingFwd
  447.         );

  448.         if (bDisplay) {

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

  452.             CurveSurfaceQuoteContainer mktParamsQuarticFwd = MarketParamsBuilder.Create (
  453.                 dc,
  454.                 fcxMQuartic,
  455.                 null,
  456.                 null,
  457.                 null,
  458.                 null,
  459.                 null,
  460.                 null
  461.             );

  462.             int iFreq = 12 / iTenorInMonths;

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

  472.             for (int i = 0; i < astrxM6MFwdTenor.length; ++i) {
  473.                 FloatFloatComponent ffc = aFFC[i];
  474.                 String strMaturityTenor = astrxM6MFwdTenor[i];

  475.                 int iFwdEndDate = dtSpot.addTenor (strMaturityTenor).julian();

  476.                 int iFwdStartDate = dtSpot.addTenor (strMaturityTenor).subtractTenor (strBasisTenor).julian();

  477.                 CaseInsensitiveTreeMap<Double> mapQuarticValue = ffc.value (
  478.                     valParams,
  479.                     null,
  480.                     mktParamsQuarticFwd,
  481.                     null
  482.                 );

  483.                 System.out.println (" " + strMaturityTenor + " =>  " +
  484.                     FormatUtil.FormatDouble (fcxMQuartic.forward (strMaturityTenor), 2, 2, 100.) + "  |  " +
  485.                     FormatUtil.FormatDouble (mapQuarticValue.get ("ReferenceParBasisSpread"), 2, 2, 1.) + "  |  " +
  486.                     FormatUtil.FormatDouble (mapQuarticValue.get ("DerivedParBasisSpread"), 2, 2, 1.) + "  |  " +
  487.                     FormatUtil.FormatDouble (iFreq * java.lang.Math.log (dc.df (iFwdStartDate) / dc.df (iFwdEndDate)), 1, 2, 100.) + "  |  " +
  488.                     FormatUtil.FormatDouble (dc.libor (iFwdStartDate, iFwdEndDate), 1, 2, 100.) + "  |  "
  489.                 );
  490.             }
  491.         }

  492.         return fcxMQuartic;
  493.     }

  494.     private static final ForwardCurve MakeComponentPairFC (
  495.         final JulianDate dtSpot,
  496.         final String strCurrency,
  497.         final MergedDiscountForwardCurve dc,
  498.         final int iTenorInMonths,
  499.         final String[] astrComponentPairTenor,
  500.         final double[] adblComponentPairQuote,
  501.         final boolean bDisplay)
  502.         throws Exception
  503.     {
  504.         ValuationParams valParams = new ValuationParams (
  505.             dtSpot,
  506.             dtSpot,
  507.             strCurrency
  508.         );

  509.         /*
  510.          * Set the discount curve based component market parameters.
  511.          */

  512.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  513.             dc,
  514.             null,
  515.             null,
  516.             null,
  517.             null,
  518.             null,
  519.             null,
  520.             null
  521.         );

  522.         org.drip.product.fx.ComponentPair[] aComponentPair = OTCComponentPair (
  523.             dtSpot,
  524.             strCurrency,
  525.             astrComponentPairTenor,
  526.             iTenorInMonths,
  527.             mktParams
  528.         );

  529.         /*
  530.          * Construct the Float-Float Component Set Stretch Builder
  531.          */

  532.         FloatFloatSwapConvention ffConv = IBORFloatFloatContainer.ConventionFromJurisdiction (strCurrency);

  533.         LatentStateStretchSpec fixFloatCPStretch = LatentStateStretchBuilder.ComponentPairForwardStretch (
  534.             "FIXFLOATCP",
  535.             aComponentPair,
  536.             valParams,
  537.             mktParams,
  538.             adblComponentPairQuote,
  539.             ffConv.basisOnDerivedComponent(),
  540.             ffConv.basisOnDerivedStream()
  541.         );

  542.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  543.             fixFloatCPStretch
  544.         };

  545.         /*
  546.          * Set up the Linear Curve Calibrator using the following parameters:
  547.          *  - Cubic Exponential Mixture Basis Spline Set
  548.          *  - Ck = 2, Segment Curvature Penalty = 2
  549.          *  - Quadratic Rational Shape Controller
  550.          *  - Natural Boundary Setting
  551.          */

  552.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  553.             new org.drip.spline.params.SegmentCustomBuilderControl (
  554.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  555.                 new PolynomialFunctionSetParams (5),
  556.                 org.drip.spline.params.SegmentInelasticDesignControl.Create (
  557.                     2,
  558.                     2
  559.                 ),
  560.                 new org.drip.spline.params.ResponseScalingShapeControl (
  561.                     true,
  562.                     new org.drip.function.r1tor1.QuadraticRationalShapeControl (0.)
  563.                 ),
  564.                 null
  565.             ),
  566.             BoundarySettings.NaturalStandard(),
  567.             MultiSegmentSequence.CALIBRATE,
  568.             null,
  569.             null
  570.         );

  571.         /*
  572.          * Construct the Shape Preserving Forward Curve by applying the linear curve calibrator to the array
  573.          *  of Deposit and Swap Stretches.
  574.          */

  575.         ForwardCurve fcDerived = ScenarioForwardCurveBuilder.ShapePreservingForwardCurve (
  576.             lcc,
  577.             aStretchSpec,
  578.             aComponentPair[0].derivedComponent().forwardLabel().get ("DERIVED"),
  579.             valParams,
  580.             null,
  581.             mktParams,
  582.             null,
  583.             dc.libor (
  584.                 dtSpot,
  585.                 iTenorInMonths + "M"
  586.             )
  587.         );

  588.         /*
  589.          * Set the discount curve + cubic polynomial forward curve based component market parameters.
  590.          */

  591.         mktParams.setForwardState (fcDerived);

  592.         if (bDisplay) {
  593.             System.out.println ("\n\t----------------------------------------------------------------");

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

  595.             /*
  596.              * Cross-Comparison of the Fix-Float Component Pair "DerivedParBasisSpread" metric.
  597.              */

  598.             if (null != aComponentPair && null != adblComponentPairQuote) {
  599.                 System.out.println ("\t----------------------------------------------------------------");

  600.                 System.out.println ("\t     FIX-FLOAT COMPONENT PAIR QUOTE RECOVERY");

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

  602.                 for (int i = 0; i < aComponentPair.length; ++i)
  603.                     System.out.println ("\t[" + aComponentPair[i].effective() + " - " + aComponentPair[i].maturity() + "] = " +
  604.                         FormatUtil.FormatDouble (aComponentPair[i].derivedComponent().measureValue (valParams, null, mktParams, null, "DerivedParBasisSpread"), 1, 2, 1.) + " | " +
  605.                             FormatUtil.FormatDouble (aComponentPair[i].derivedComponent().measureValue (valParams, null, mktParams, null, "ReferenceParBasisSpread"), 1, 2, 1.) + " | " +
  606.                                 FormatUtil.FormatDouble (adblComponentPairQuote[i], 1, 2, 10000.) + " | " +
  607.                                     FormatUtil.FormatDouble (fcDerived.forward (aComponentPair[i].maturity()), 1, 4, 100.) + "% | " +
  608.                                         FormatUtil.FormatDouble (dc.libor (aComponentPair[i].maturity().subtractTenor ("3M"), iTenorInMonths + "M"), 1, 4, 100.) + "%");
  609.             }

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

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

  612.             System.out.println ("\t\tFIX-FLOAT COMPONENT PAIR RUNS");

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

  614.             System.out.println ("\tL -> R:");

  615.             System.out.println ("\t\tCurrency");

  616.             System.out.println ("\t\tFloat-Float Effective");

  617.             System.out.println ("\t\tFloat-Float Maturity");

  618.             System.out.println ("\t\tDerived Component Derived Stream Par Basis Spread");

  619.             System.out.println ("\t\tDerived Component Reference Stream Par Basis Spread");

  620.             System.out.println ("\t---------------------------------------------------------");
  621.         }

  622.         return fcDerived;
  623.     }

  624.     private static final ForwardCurve MakeFloatFloatFC (
  625.         final String strCurrency,
  626.         final MergedDiscountForwardCurve dc,
  627.         final boolean bDisplay)
  628.         throws Exception
  629.     {
  630.         /*
  631.          * Build and run the sampling for the 3M-6M Tenor Basis Swap from its instruments and quotes.
  632.          */

  633.         if (bDisplay) {
  634.             System.out.println ("\n------------------------------------------------------------");

  635.             System.out.println ("-------------------    3M-6M Basis Swap    -----------------");
  636.         }

  637.         ForwardCurve fc3M = MakeFloatFloatFC (
  638.             dc.epoch(),
  639.             strCurrency,
  640.             dc,
  641.             3,
  642.             new String[] {
  643.                 "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  644.             },
  645.             new double[] {
  646.                 0.00186,    //  1Y
  647.                 0.00127,    //  2Y
  648.                 0.00097,    //  3Y
  649.                 0.00080,    //  4Y
  650.                 0.00067,    //  5Y
  651.                 0.00058,    //  6Y
  652.                 0.00051,    //  7Y
  653.                 0.00046,    //  8Y
  654.                 0.00042,    //  9Y
  655.                 0.00038,    // 10Y
  656.                 0.00035,    // 11Y
  657.                 0.00033,    // 12Y
  658.                 0.00028,    // 15Y
  659.                 0.00022,    // 20Y
  660.                 0.00020,    // 25Y
  661.                 0.00018     // 30Y
  662.             },
  663.             bDisplay
  664.         );

  665.         if (bDisplay) System.out.println ("------------------------------------------------------------\n\n");

  666.         return fc3M;
  667.     }

  668.     private static final ForwardCurve MakeComponentPairFC (
  669.         final String strCurrency,
  670.         final MergedDiscountForwardCurve dc,
  671.         final boolean bDisplay)
  672.         throws Exception
  673.     {
  674.         /*
  675.          * Build and run the sampling for the 3M-6M Tenor Basis Swap from its instruments and quotes.
  676.          */

  677.         ForwardCurve fc3M = MakeComponentPairFC (
  678.             dc.epoch(),
  679.             strCurrency,
  680.             dc,
  681.             3,
  682.             new String[] {
  683.                 "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  684.             },
  685.             new double[] {
  686.                 0.00186,    //  1Y
  687.                 0.00127,    //  2Y
  688.                 0.00097,    //  3Y
  689.                 0.00080,    //  4Y
  690.                 0.00067,    //  5Y
  691.                 0.00058,    //  6Y
  692.                 0.00051,    //  7Y
  693.                 0.00046,    //  8Y
  694.                 0.00042,    //  9Y
  695.                 0.00038,    // 10Y
  696.                 0.00035,    // 11Y
  697.                 0.00033,    // 12Y
  698.                 0.00028,    // 15Y
  699.                 0.00022,    // 20Y
  700.                 0.00020,    // 25Y
  701.                 0.00018     // 30Y
  702.             },
  703.             bDisplay
  704.         );

  705.         return fc3M;
  706.     }

  707.     private static final void OTCFloatFloatRun (
  708.         final String strCurrency,
  709.         final JulianDate dtSpot,
  710.         final boolean bDisplay)
  711.         throws Exception
  712.     {
  713.         /*
  714.          * Construct the Discount Curve using its instruments and quotes
  715.          */

  716.         MergedDiscountForwardCurve dc = MakeDC (
  717.             dtSpot,
  718.             strCurrency
  719.         );

  720.         ForwardCurve fc3M = MakeFloatFloatFC (
  721.             strCurrency,
  722.             dc,
  723.             bDisplay
  724.         );

  725.         CurveSurfaceQuoteContainer csqs = MarketParamsBuilder.Create (
  726.             dc,
  727.             fc3M,
  728.             null,
  729.             null,
  730.             null,
  731.             null,
  732.             null,
  733.             null
  734.         );

  735.         FloatFloatComponent ffc = OTCFloatFloat (
  736.             dtSpot,
  737.             strCurrency,
  738.             "3M",
  739.             "10Y",
  740.             0.
  741.         );

  742.         ValuationParams valParams = new ValuationParams (
  743.             dtSpot,
  744.             dtSpot,
  745.             strCurrency
  746.         );

  747.         Map<String, Double> mapFFCMeasures = ffc.value (
  748.             valParams,
  749.             null,
  750.             csqs,
  751.             null
  752.         );

  753.         System.out.println (
  754.             "\t| " + strCurrency + "  [" + ffc.effectiveDate() + " -> " + ffc.maturityDate() + "]  =>  " +
  755.             FormatUtil.FormatDouble (mapFFCMeasures.get ("ReferenceParBasisSpread"), 1, 2, 1.) + "  |  " +
  756.             FormatUtil.FormatDouble (mapFFCMeasures.get ("DerivedParBasisSpread"), 1, 2, 1.) + "  |"
  757.         );
  758.     }

  759.     private static final void OTCComponentPairRun (
  760.         final String strCurrency,
  761.         final JulianDate dtSpot,
  762.         final String strMaturityTenor,
  763.         final boolean bDisplay)
  764.         throws Exception
  765.     {
  766.         /*
  767.          * Construct the Discount Curve using its instruments and quotes
  768.          */

  769.         MergedDiscountForwardCurve dc = MakeDC (
  770.             dtSpot,
  771.             strCurrency
  772.         );

  773.         ForwardCurve fc3M = MakeComponentPairFC (
  774.             strCurrency,
  775.             dc,
  776.             bDisplay
  777.         );

  778.         CurveSurfaceQuoteContainer csqs = MarketParamsBuilder.Create (
  779.             dc,
  780.             fc3M,
  781.             null,
  782.             null,
  783.             null,
  784.             null,
  785.             null,
  786.             null
  787.         );

  788.         ComponentPair cp = OTCComponentPair (
  789.             dtSpot,
  790.             strCurrency,
  791.             "3M",
  792.             strMaturityTenor,
  793.             0.,
  794.             0.,
  795.             0.
  796.         );

  797.         ValuationParams valParams = new ValuationParams (
  798.             dtSpot,
  799.             dtSpot,
  800.             strCurrency
  801.         );

  802.         Map<String, Double> mapComponentPairMeasures = cp.value (
  803.             valParams,
  804.             null,
  805.             csqs,
  806.             null
  807.         );

  808.         System.out.println (
  809.             "\t| " + strCurrency + "  [" + cp.effective() + " -> " + cp.maturity() + "]  =>  " +
  810.             FormatUtil.FormatDouble (mapComponentPairMeasures.get ("DerivedCompDerivedBasis"), 1, 2, 1.) + "  |  " +
  811.             FormatUtil.FormatDouble (mapComponentPairMeasures.get ("DerivedCompReferenceBasis"), 1, 2, 1.) + "  |"
  812.         );
  813.     }

  814.     public static final void main (
  815.         final String[] astrArgs)
  816.         throws Exception
  817.     {

  818.         /*
  819.          * Initialize the Credit Analytics Library
  820.          */

  821.         EnvManager.InitEnv ("");

  822.         JulianDate dtSpot = DateUtil.Today();

  823.         OTCFloatFloatRun ("AUD", dtSpot, true);

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

  825.         System.out.println ("\tL -> R:");

  826.         System.out.println ("\t\tCurrency");

  827.         System.out.println ("\t\tFloat-Float Effective");

  828.         System.out.println ("\t\tFloat-Float Maturity");

  829.         System.out.println ("\t\tReference Stream Par Basis Spread");

  830.         System.out.println ("\t\tDerived Stream Par Basis Spread");

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

  832.         System.out.println ("\t\tFLOAT-FLOAT SINGLE COMPONENT RUNS");

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

  834.         OTCFloatFloatRun ("AUD", dtSpot, false);

  835.         OTCFloatFloatRun ("CAD", dtSpot, false);

  836.         OTCFloatFloatRun ("CHF", dtSpot, false);

  837.         OTCFloatFloatRun ("CNY", dtSpot, false);

  838.         OTCFloatFloatRun ("DKK", dtSpot, false);

  839.         OTCFloatFloatRun ("GBP", dtSpot, false);

  840.         OTCFloatFloatRun ("HKD", dtSpot, false);

  841.         OTCFloatFloatRun ("INR", dtSpot, false);

  842.         OTCFloatFloatRun ("JPY", dtSpot, false);

  843.         OTCFloatFloatRun ("NOK", dtSpot, false);

  844.         OTCFloatFloatRun ("NZD", dtSpot, false);

  845.         OTCFloatFloatRun ("PLN", dtSpot, false);

  846.         OTCFloatFloatRun ("SEK", dtSpot, false);

  847.         OTCFloatFloatRun ("SGD", dtSpot, false);

  848.         OTCFloatFloatRun ("USD", dtSpot, false);

  849.         OTCFloatFloatRun ("ZAR", dtSpot, false);

  850.         OTCComponentPairRun ("EUR", dtSpot, "1Y", true);

  851.         OTCComponentPairRun ("EUR", dtSpot, "2Y", false);

  852.         OTCComponentPairRun ("EUR", dtSpot, "3Y", false);

  853.         OTCComponentPairRun ("EUR", dtSpot, "4Y", false);

  854.         OTCComponentPairRun ("EUR", dtSpot, "5Y", false);

  855.         OTCComponentPairRun ("EUR", dtSpot, "6Y", false);

  856.         OTCComponentPairRun ("EUR", dtSpot, "7Y", false);

  857.         OTCComponentPairRun ("EUR", dtSpot, "8Y", false);

  858.         OTCComponentPairRun ("EUR", dtSpot, "9Y", false);

  859.         OTCComponentPairRun ("EUR", dtSpot, "11Y", false);

  860.         OTCComponentPairRun ("EUR", dtSpot, "12Y", false);

  861.         OTCComponentPairRun ("EUR", dtSpot, "15Y", false);

  862.         OTCComponentPairRun ("EUR", dtSpot, "20Y", false);

  863.         OTCComponentPairRun ("EUR", dtSpot, "25Y", false);

  864.         OTCComponentPairRun ("EUR", dtSpot, "30Y", false);

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

  866.         EnvManager.TerminateEnv();
  867.     }
  868. }