CompositeFedFundLIBORSwap.java

  1. package org.drip.sample.fedfund;

  2. import java.util.*;

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

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

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

  91. /**
  92.  * <i>CompositeFedFundLIBORSwap</i> demonstrates the Construction, the Valuation, and Bloomberg Metrics
  93.  * Analysis for the Composite Fed Fund vs. LIBOR Basis Swaps.
  94.  *  
  95.  * <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  99.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/fedfund/README.md">Fed Fund Analytics</a></li>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class CompositeFedFundLIBORSwap {

  107.     private static final FloatFloatComponent OTCFloatFloat (
  108.         final JulianDate dtSpot,
  109.         final String strCurrency,
  110.         final String strDerivedTenor,
  111.         final String strMaturityTenor,
  112.         final double dblBasis)
  113.     {
  114.         FloatFloatSwapConvention ffConv = IBORFloatFloatContainer.ConventionFromJurisdiction (strCurrency);

  115.         return ffConv.createFloatFloatComponent (
  116.             dtSpot,
  117.             strDerivedTenor,
  118.             strMaturityTenor,
  119.             dblBasis,
  120.             1.
  121.         );
  122.     }

  123.     private static final FixFloatComponent OTCOISFixFloat (
  124.         final JulianDate dtSpot,
  125.         final String strCurrency,
  126.         final String strMaturityTenor,
  127.         final double dblCoupon)
  128.     {
  129.         FixedFloatSwapConvention ffConv = OvernightFixedFloatContainer.FundConventionFromJurisdiction (
  130.             strCurrency
  131.         );

  132.         return ffConv.createFixFloatComponent (
  133.             dtSpot,
  134.             strMaturityTenor,
  135.             dblCoupon,
  136.             0.,
  137.             1.
  138.         );
  139.     }

  140.     /*
  141.      * Construct the Array of Deposit Instruments from the given set of parameters
  142.      *
  143.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  144.      */

  145.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  146.         final JulianDate dtEffective,
  147.         final String strCurrency,
  148.         final int[] aiDay)
  149.         throws Exception
  150.     {
  151.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  152.         for (int i = 0; i < aiDay.length; ++i)
  153.             aDeposit[i] = SingleStreamComponentBuilder.Deposit (
  154.                 dtEffective,
  155.                 dtEffective.addBusDays (
  156.                     aiDay[i],
  157.                     strCurrency
  158.                 ),
  159.                 OvernightLabel.Create (
  160.                     strCurrency
  161.                 )
  162.             );

  163.         return aDeposit;
  164.     }

  165.     /*
  166.      * Construct the Array of Overnight Index Instruments from the given set of parameters
  167.      *
  168.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  169.      */

  170.     private static final FixFloatComponent[] OISFromMaturityTenor (
  171.         final JulianDate dtSpot,
  172.         final String strCurrency,
  173.         final String[] astrMaturityTenor,
  174.         final double[] adblCoupon)
  175.         throws Exception
  176.     {
  177.         FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];

  178.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  179.             aOIS[i] = OTCOISFixFloat (
  180.                 dtSpot,
  181.                 strCurrency,
  182.                 astrMaturityTenor[i],
  183.                 adblCoupon[i]
  184.             );

  185.         return aOIS;
  186.     }

  187.     /*
  188.      * Construct the Array of Overnight Index Future Instruments from the given set of parameters
  189.      *
  190.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  191.      */

  192.     private static final FixFloatComponent[] OISFuturesFromMaturityTenor (
  193.         final JulianDate dtSpot,
  194.         final String strCurrency,
  195.         final String[] astrStartTenor,
  196.         final String[] astrMaturityTenor,
  197.         final double[] adblCoupon)
  198.         throws Exception
  199.     {
  200.         FixFloatComponent[] aOISFutures = new FixFloatComponent[astrMaturityTenor.length];

  201.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  202.             aOISFutures[i] = OTCOISFixFloat (
  203.                 dtSpot.addTenor (astrStartTenor[i]),
  204.                 strCurrency,
  205.                 astrMaturityTenor[i],
  206.                 adblCoupon[i]
  207.             );

  208.         return aOISFutures;
  209.     }

  210.     private static final MergedDiscountForwardCurve OISDiscountCurve (
  211.         final JulianDate dtSpot,
  212.         final String strCurrency,
  213.         final String strHeaderComment)
  214.         throws Exception
  215.     {
  216.         System.out.println ("\n\t----------------------------------------------------------------");

  217.         System.out.println ("\t     " + strHeaderComment);

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

  219.         /*
  220.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  221.          */

  222.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  223.             dtSpot,
  224.             strCurrency,
  225.             new int[] {
  226.                 1, 2, 3
  227.             }
  228.         );

  229.         double[] adblDepositQuote = new double[] {
  230.             0.0004, 0.0004, 0.0004       // Deposit
  231.         };

  232.         /*
  233.          * Construct the Deposit Instrument Set Stretch Builder
  234.          */

  235.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  236.             "   DEPOSIT   ",
  237.             aDepositComp,
  238.             "ForwardRate",
  239.             adblDepositQuote
  240.         );

  241.         /*
  242.          * Construct the Array of Short End OIS Instruments and their Quotes from the given set of parameters
  243.          */

  244.         double[] adblShortEndOISQuote = new double[] {
  245.             0.00070,    //   1W
  246.             0.00069,    //   2W
  247.             0.00078,    //   3W
  248.             0.00074     //   1M
  249.         };

  250.         CalibratableComponent[] aShortEndOISComp = OISFromMaturityTenor (
  251.             dtSpot,
  252.             strCurrency,
  253.             new java.lang.String[] {
  254.                 "1W", "2W", "3W", "1M"
  255.             },
  256.             adblShortEndOISQuote
  257.         );

  258.         /*
  259.          * Construct the Short End OIS Instrument Set Stretch Builder
  260.          */

  261.         LatentStateStretchSpec oisShortEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  262.             "SHORT END OIS",
  263.             aShortEndOISComp,
  264.             "SwapRate",
  265.             adblShortEndOISQuote
  266.         );

  267.         /*
  268.          * Construct the Array of OIS Futures Instruments and their Quotes from the given set of parameters
  269.          */

  270.         double[] adblOISFutureQuote = new double[] {
  271.              0.00046,    //   1M x 1M
  272.              0.00016,    //   2M x 1M
  273.             -0.00007,    //   3M x 1M
  274.             -0.00013,    //   4M x 1M
  275.             -0.00014     //   5M x 1M
  276.         };

  277.         CalibratableComponent[] aOISFutureComp = OISFuturesFromMaturityTenor (
  278.             dtSpot,
  279.             strCurrency,
  280.             new java.lang.String[] {
  281.                 "1M", "2M", "3M", "4M", "5M"
  282.             },
  283.             new java.lang.String[] {
  284.                 "1M", "1M", "1M", "1M", "1M"
  285.             },
  286.             adblOISFutureQuote
  287.         );

  288.         /*
  289.          * Construct the OIS Future Instrument Set Stretch Builder
  290.          */

  291.         LatentStateStretchSpec oisFutureStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  292.             " OIS FUTURE  ",
  293.             aOISFutureComp,
  294.             "SwapRate",
  295.             adblOISFutureQuote
  296.         );

  297.         /*
  298.          * Construct the Array of Long End OIS Instruments and their Quotes from the given set of parameters
  299.          */

  300.         double[] adblLongEndOISQuote = new double[] {
  301.             0.00002,    //  15M
  302.             0.00008,    //  18M
  303.             0.00021,    //  21M
  304.             0.00036,    //   2Y
  305.             0.00127,    //   3Y
  306.             0.00274,    //   4Y
  307.             0.00456,    //   5Y
  308.             0.00647,    //   6Y
  309.             0.00827,    //   7Y
  310.             0.00996,    //   8Y
  311.             0.01147,    //   9Y
  312.             0.01280,    //  10Y
  313.             0.01404,    //  11Y
  314.             0.01516,    //  12Y
  315.             0.01764,    //  15Y
  316.             0.01939,    //  20Y
  317.             0.02003,    //  25Y
  318.             0.02038     //  30Y
  319.         };

  320.         CalibratableComponent[] aLongEndOISComp = OISFromMaturityTenor (
  321.             dtSpot,
  322.             strCurrency,
  323.             new java.lang.String[] {
  324.                 "15M", "18M", "21M", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  325.             },
  326.             adblLongEndOISQuote
  327.         );

  328.         /*
  329.          * Construct the Long End OIS Instrument Set Stretch Builder
  330.          */

  331.         LatentStateStretchSpec oisLongEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  332.             "LONG END OIS ",
  333.             aLongEndOISComp,
  334.             "SwapRate",
  335.             adblLongEndOISQuote
  336.         );

  337.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  338.             depositStretch,
  339.             oisShortEndStretch,
  340.             oisFutureStretch,
  341.             oisLongEndStretch
  342.         };

  343.         /*
  344.          * Set up the Linear Curve Calibrator using the following parameters:
  345.          *  - Cubic Exponential Mixture Basis Spline Set
  346.          *  - Ck = 2, Segment Curvature Penalty = 2
  347.          *  - Quadratic Rational Shape Controller
  348.          *  - Natural Boundary Setting
  349.          */

  350.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  351.             new SegmentCustomBuilderControl (
  352.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  353.                 new PolynomialFunctionSetParams (4),
  354.                 SegmentInelasticDesignControl.Create (
  355.                     2,
  356.                     2
  357.                 ),
  358.                 new ResponseScalingShapeControl (
  359.                     true,
  360.                     new QuadraticRationalShapeControl (0.)
  361.                 ),
  362.                 null
  363.             ),
  364.             BoundarySettings.NaturalStandard(),
  365.             MultiSegmentSequence.CALIBRATE,
  366.             null,
  367.             null
  368.         );

  369.         /*
  370.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  371.          *  of Deposit and Swap Stretches.
  372.          */

  373.         ValuationParams valParams = new ValuationParams (
  374.             dtSpot,
  375.             dtSpot,
  376.             strCurrency
  377.         );

  378.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  379.             strCurrency,
  380.             lcc,
  381.             aStretchSpec,
  382.             valParams,
  383.             null,
  384.             null,
  385.             null,
  386.             1.
  387.         );

  388.         CurveSurfaceQuoteContainer csqs = MarketParamsBuilder.Create (
  389.             dc,
  390.             null,
  391.             null,
  392.             null,
  393.             null,
  394.             null,
  395.             null
  396.         );

  397.         /*
  398.          * Cross-Comparison of the Deposit Calibration Instrument "Rate" metric across the different curve
  399.          *  construction methodologies.
  400.          */

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

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

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

  404.         for (int i = 0; i < aDepositComp.length; ++i)
  405.             System.out.println ("\t[" + aDepositComp[i].effectiveDate() + " => " + aDepositComp[i].maturityDate() + "] = " +
  406.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (valParams, null, csqs, null, "Rate"), 1, 6, 1.) +
  407.                 " | " + FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  408.         /*
  409.          * Cross-Comparison of the Short End OIS Calibration Instrument "Rate" metric across the different curve
  410.          *  construction methodologies.
  411.          */

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

  413.         System.out.println ("\t     OIS SHORT END INSTRUMENTS CALIBRATION RECOVERY");

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

  415.         for (int i = 0; i < aShortEndOISComp.length; ++i) {
  416.             Map<String, Double> mapCalc = aShortEndOISComp[i].value (
  417.                 valParams,
  418.                 null,
  419.                 csqs,
  420.                 null
  421.             );

  422.             double dblCalibSwapRate = mapCalc.get ("CalibSwapRate");

  423.             double dblFairPremium = mapCalc.get ("FairPremium");

  424.             System.out.println ("\t[" + aShortEndOISComp[i].effectiveDate() + " => " + aShortEndOISComp[i].maturityDate() + "] = " +
  425.                 FormatUtil.FormatDouble (dblCalibSwapRate, 1, 6, 1.) + " | " +
  426.                 FormatUtil.FormatDouble (adblShortEndOISQuote[i], 1, 6, 1.) + " | " +
  427.                 FormatUtil.FormatDouble (dblFairPremium, 1, 6, 1.) + " | " +
  428.                 FormatUtil.FormatDouble (dblFairPremium - dblCalibSwapRate, 1, 2, 10000.)
  429.             );
  430.         }

  431.         /*
  432.          * Cross-Comparison of the OIS Future Calibration Instrument "Rate" metric across the different curve
  433.          *  construction methodologies.
  434.          */

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

  436.         System.out.println ("\t     OIS FUTURE INSTRUMENTS CALIBRATION RECOVERY");

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

  438.         for (int i = 0; i < aOISFutureComp.length; ++i) {
  439.             Map<String, Double> mapCalc = aOISFutureComp[i].value (
  440.                 valParams,
  441.                 null,
  442.                 csqs,
  443.                 null
  444.             );

  445.             double dblSwapRate = mapCalc.get ("SwapRate");

  446.             double dblFairPremium = mapCalc.get ("FairPremium");

  447.             System.out.println ("\t[" + aOISFutureComp[i].effectiveDate() + " => " + aOISFutureComp[i].maturityDate() + "] = " +
  448.                 FormatUtil.FormatDouble (dblSwapRate, 1, 6, 1.) + " | " +
  449.                 FormatUtil.FormatDouble (adblOISFutureQuote[i], 1, 6, 1.) + " | " +
  450.                 FormatUtil.FormatDouble (dblFairPremium, 1, 6, 1.) + " | " +
  451.                 FormatUtil.FormatDouble (dblFairPremium - dblSwapRate, 1, 2, 10000.)
  452.             );
  453.         }

  454.         /*
  455.          * Cross-Comparison of the Long End OIS Calibration Instrument "Rate" metric across the different curve
  456.          *  construction methodologies.
  457.          */

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

  459.         System.out.println ("\t     OIS LONG END INSTRUMENTS CALIBRATION RECOVERY");

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

  461.         for (int i = 0; i < aLongEndOISComp.length; ++i) {
  462.             Map<String, Double> mapCalc = aLongEndOISComp[i].value (
  463.                 valParams,
  464.                 null,
  465.                 csqs,
  466.                 null
  467.             );

  468.             double dblCalibSwapRate = mapCalc.get ("CalibSwapRate");

  469.             double dblFairPremium = mapCalc.get ("FairPremium");

  470.             System.out.println ("\t[" + aLongEndOISComp[i].effectiveDate() + " => " + aLongEndOISComp[i].maturityDate() + "] = " +
  471.                 FormatUtil.FormatDouble (dblCalibSwapRate, 1, 6, 1.) + " | " +
  472.                 FormatUtil.FormatDouble (adblLongEndOISQuote[i], 1, 6, 1.) + " | " +
  473.                 FormatUtil.FormatDouble (dblFairPremium, 1, 6, 1.) + " | " +
  474.                 FormatUtil.FormatDouble (dblFairPremium - dblCalibSwapRate, 1, 2, 10000.)
  475.             );
  476.         }

  477.         return dc;
  478.     }

  479.     /*
  480.      * Construct an array of float-float swaps from the corresponding reference (6M) and the derived legs.
  481.      *
  482.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  483.      */

  484.     private static final FloatFloatComponent[] MakexM6MBasisSwap (
  485.         final JulianDate dtSpot,
  486.         final String strCurrency,
  487.         final String[] astrMaturityTenor,
  488.         final int iTenorInMonths)
  489.         throws Exception
  490.     {
  491.         FloatFloatComponent[] aFFC = new FloatFloatComponent[astrMaturityTenor.length];

  492.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  493.             aFFC[i] = OTCFloatFloat (
  494.                 dtSpot,
  495.                 strCurrency,
  496.                 iTenorInMonths + "M",
  497.                 astrMaturityTenor[i],
  498.                 0.
  499.             );

  500.         return aFFC;
  501.     }

  502.     private static final ForwardCurve MakexMForwardCurve (
  503.         final JulianDate dtSpot,
  504.         final String strCurrency,
  505.         final MergedDiscountForwardCurve dc,
  506.         final int iTenorInMonths,
  507.         final String[] astrxM6MFwdTenor,
  508.         final String strManifestMeasure,
  509.         final double[] adblxM6MBasisSwapQuote)
  510.         throws Exception
  511.     {
  512.         /*
  513.          * Construct the 6M-xM float-float basis swap.
  514.          */

  515.         FloatFloatComponent[] aFFC = MakexM6MBasisSwap (
  516.             dtSpot,
  517.             strCurrency,
  518.             astrxM6MFwdTenor,
  519.             iTenorInMonths
  520.         );

  521.         String strBasisTenor = iTenorInMonths + "M";

  522.         ValuationParams valParams = new ValuationParams (
  523.             dtSpot,
  524.             dtSpot,
  525.             strCurrency
  526.         );

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

  530.         double dblStartingFwd = dc.forward (
  531.             dtSpot.julian(),
  532.             dtSpot.addTenor (strBasisTenor).julian()
  533.         );

  534.         /*
  535.          * Set the discount curve based component market parameters.
  536.          */

  537.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  538.             dc,
  539.             null,
  540.             null,
  541.             null,
  542.             null,
  543.             null,
  544.             null
  545.         );

  546.         /*
  547.          * Construct the shape preserving forward curve off of Cubic Polynomial Basis Spline.
  548.          */

  549.         return ScenarioForwardCurveBuilder.ShapePreservingForwardCurve (
  550.             "CUBIC_FWD" + strBasisTenor,
  551.             ForwardLabel.Create (
  552.                 strCurrency,
  553.                 strBasisTenor
  554.             ),
  555.             valParams,
  556.             null,
  557.             mktParams,
  558.             null,
  559.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  560.             new PolynomialFunctionSetParams (4),
  561.             aFFC,
  562.             strManifestMeasure,
  563.             adblxM6MBasisSwapQuote,
  564.             dblStartingFwd
  565.         );
  566.     }

  567.     private static final FloatFloatComponent[] FedFundLIBORBasisSwap (
  568.         final JulianDate dtEffective,
  569.         final String strCurrency,
  570.         final String[] astrMaturityTenor)
  571.         throws Exception
  572.     {
  573.         FloatFloatComponent[] aFFC = new FloatFloatComponent[astrMaturityTenor.length];

  574.         ComposableFloatingUnitSetting cfusLIBOR = new ComposableFloatingUnitSetting (
  575.             "3M",
  576.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  577.             null,
  578.             ForwardLabel.Standard (strCurrency + "-3M"),
  579.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  580.             0.
  581.         );

  582.         ComposableFloatingUnitSetting cfusFedFund = new ComposableFloatingUnitSetting (
  583.             "ON",
  584.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
  585.             null,
  586.             OvernightLabel.Create (
  587.                 strCurrency
  588.             ),
  589.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  590.             0.
  591.         );

  592.         CompositePeriodSetting cpsLIBOR = new CompositePeriodSetting (
  593.             4,
  594.             "3M",
  595.             strCurrency,
  596.             null,
  597.             -1.,
  598.             null,
  599.             null,
  600.             null,
  601.             null
  602.         );

  603.         CompositePeriodSetting cpsFedFund = new CompositePeriodSetting (
  604.             4,
  605.             "3M",
  606.             strCurrency,
  607.             null,
  608.             1.,
  609.             null,
  610.             null,
  611.             null,
  612.             null
  613.         );

  614.         CashSettleParams csp = new CashSettleParams (
  615.             0,
  616.             strCurrency,
  617.             0
  618.         );

  619.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  620.             List<Integer> lsReferenceStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  621.                 dtEffective,
  622.                 "3M",
  623.                 astrMaturityTenor[i],
  624.                 null
  625.             );

  626.             List<Integer> lsDerivedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  627.                 dtEffective,
  628.                 "3M",
  629.                 astrMaturityTenor[i],
  630.                 null
  631.             );

  632.             Stream streamLIBOR = new Stream (
  633.                 CompositePeriodBuilder.FloatingCompositeUnit (
  634.                     lsReferenceStreamEdgeDate,
  635.                     cpsLIBOR,
  636.                     cfusLIBOR
  637.                 )
  638.             );

  639.             Stream streamFedFund = new Stream (
  640.                 CompositePeriodBuilder.FloatingCompositeUnit (
  641.                     lsDerivedStreamEdgeDate,
  642.                     cpsFedFund,
  643.                     cfusFedFund
  644.                 )
  645.             );

  646.             aFFC[i] = new FloatFloatComponent (
  647.                 streamLIBOR,
  648.                 streamFedFund,
  649.                 csp
  650.             );
  651.         }

  652.         return aFFC;
  653.     }

  654.     /*
  655.      * Construct the Array of Overnight Index Instruments from the given set of parameters
  656.      *
  657.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  658.      */

  659.     private static final FixFloatComponent[] SwapInstrumentsFromMaturityTenor (
  660.         final JulianDate dtEffective,
  661.         final String[] astrMaturityTenor,
  662.         final double[] adblCoupon,
  663.         final String strCurrency)
  664.         throws Exception
  665.     {
  666.         FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];

  667.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  668.             2,
  669.             "Act/360",
  670.             false,
  671.             "Act/360",
  672.             false,
  673.             strCurrency,
  674.             false,
  675.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  676.         );

  677.         CashSettleParams csp = new CashSettleParams (
  678.             0,
  679.             strCurrency,
  680.             0
  681.         );

  682.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  683.             java.lang.String strFixedTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  684.                 astrMaturityTenor[i],
  685.                 "6M"
  686.             ) ? astrMaturityTenor[i] : "6M";

  687.             java.lang.String strFloatingTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  688.                 astrMaturityTenor[i],
  689.                 "3M"
  690.             ) ? astrMaturityTenor[i] : "3M";

  691.             ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  692.                 "3M",
  693.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  694.                 null,
  695.                 ForwardLabel.Create (
  696.                     strCurrency,
  697.                     "3M"
  698.                 ),
  699.                 CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  700.                 0.
  701.             );

  702.             ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  703.                 strFixedTenor,
  704.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  705.                 null,
  706.                 adblCoupon[i],
  707.                 0.,
  708.                 strCurrency
  709.             );

  710.             CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  711.                 4,
  712.                 strFloatingTenor,
  713.                 strCurrency,
  714.                 null,
  715.                 -1.,
  716.                 null,
  717.                 null,
  718.                 null,
  719.                 null
  720.             );

  721.             CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  722.                 2,
  723.                 strFixedTenor,
  724.                 strCurrency,
  725.                 null,
  726.                 1.,
  727.                 null,
  728.                 null,
  729.                 null,
  730.                 null
  731.             );

  732.             List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  733.                 dtEffective,
  734.                 strFixedTenor,
  735.                 astrMaturityTenor[i],
  736.                 null
  737.             );

  738.             List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  739.                 dtEffective,
  740.                 strFloatingTenor,
  741.                 astrMaturityTenor[i],
  742.                 null
  743.             );

  744.             Stream floatingStream = new Stream (
  745.                 CompositePeriodBuilder.FloatingCompositeUnit (
  746.                     lsFloatingStreamEdgeDate,
  747.                     cpsFloating,
  748.                     cfusFloating
  749.                 )
  750.             );

  751.             Stream fixedStream = new Stream (
  752.                 CompositePeriodBuilder.FixedCompositeUnit (
  753.                     lsFixedStreamEdgeDate,
  754.                     cpsFixed,
  755.                     ucasFixed,
  756.                     cfusFixed
  757.                 )
  758.             );

  759.             FixFloatComponent ois = new FixFloatComponent (
  760.                 fixedStream,
  761.                 floatingStream,
  762.                 csp
  763.             );

  764.             ois.setPrimaryCode ("OIS." + astrMaturityTenor[i] + "." + strCurrency);

  765.             aOIS[i] = ois;
  766.         }

  767.         return aOIS;
  768.     }

  769.     public static final void main (
  770.         final String[] astrArgs)
  771.         throws Exception
  772.     {
  773.         /*
  774.          * Initialize the Credit Analytics Library
  775.          */

  776.         EnvManager.InitEnv ("");

  777.         String strCurrency = "USD";

  778.         JulianDate dtToday = DateUtil.CreateFromYMD (
  779.             2012,
  780.             DateUtil.DECEMBER,
  781.             11
  782.         );

  783.         MergedDiscountForwardCurve dcOIS = OISDiscountCurve (
  784.             dtToday,
  785.             strCurrency,
  786.             "OVERNIGHT INDEX RUN RECONCILIATION"
  787.         );

  788.         ForwardCurve fc = MakexMForwardCurve (
  789.             dtToday,
  790.             strCurrency,
  791.             dcOIS,
  792.             3,
  793.             new String[] {
  794.                 "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  795.             },
  796.             "ReferenceParBasisSpread",
  797.             new double[] {
  798.                 0.00186,    //  1Y
  799.                 0.00127,    //  2Y
  800.                 0.00097,    //  3Y
  801.                 0.00080,    //  4Y
  802.                 0.00067,    //  5Y
  803.                 0.00058,    //  6Y
  804.                 0.00051,    //  7Y
  805.                 0.00046,    //  8Y
  806.                 0.00042,    //  9Y
  807.                 0.00038,    // 10Y
  808.                 0.00035,    // 11Y
  809.                 0.00033,    // 12Y
  810.                 0.00028,    // 15Y
  811.                 0.00022,    // 20Y
  812.                 0.00020,    // 25Y
  813.                 0.00018     // 30Y
  814.             }
  815.         );

  816.         CurveSurfaceQuoteContainer mktParams = MarketParamsBuilder.Create (
  817.             dcOIS,
  818.             null,
  819.             null,
  820.             null,
  821.             null,
  822.             null,
  823.             null
  824.         );

  825.         mktParams.setForwardState (fc);

  826.         String[] astrMaturityTenor = new String[] {
  827.             "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"
  828.         };

  829.         FloatFloatComponent[] aFedFundLIBORSwap = FedFundLIBORBasisSwap (
  830.             dtToday,
  831.             strCurrency,
  832.             astrMaturityTenor
  833.         );

  834.         FixFloatComponent[] aOIS = OISFromMaturityTenor (
  835.             dtToday,
  836.             strCurrency,
  837.             astrMaturityTenor,
  838.             new double[] {
  839.                 0.00002,
  840.                 0.00036,
  841.                 0.00127,
  842.                 0.00274,
  843.                 0.00456,
  844.                 0.00647,
  845.                 0.00827,
  846.                 0.00996,
  847.                 0.01147,
  848.                 0.01280,
  849.                 0.01404,
  850.                 0.01516,
  851.                 0.01764,
  852.                 0.01939,
  853.                 0.02003,
  854.                 0.02038
  855.             }
  856.         );

  857.         FixFloatComponent[] aIRS = SwapInstrumentsFromMaturityTenor (
  858.             dtToday,
  859.             astrMaturityTenor,
  860.             new double[] {
  861.                 0.00002,
  862.                 0.00036,
  863.                 0.00127,
  864.                 0.00274,
  865.                 0.00456,
  866.                 0.00647,
  867.                 0.00827,
  868.                 0.00996,
  869.                 0.01147,
  870.                 0.01280,
  871.                 0.01404,
  872.                 0.01516,
  873.                 0.01764,
  874.                 0.01939,
  875.                 0.02003,
  876.                 0.02038
  877.             },
  878.             strCurrency
  879.         );

  880.         ValuationParams valParams = new ValuationParams (
  881.             dtToday,
  882.             dtToday,
  883.             strCurrency
  884.         );

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

  886.         System.out.println ("\t                    FED FUND OIS BASIS COMPARISON");

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

  888.         System.out.println ("\t\tOutput Order[Effective Date - Maturity Date]");

  889.         System.out.println ("\t\t\t IRS Rate (%)");

  890.         System.out.println ("\t\t\t Fed Fund LIBOR Basis (bp)");

  891.         System.out.println ("\t\t\t OIS Rate Uncompounded (%) (Bloomberg 2010 Methodology)");

  892.         System.out.println ("\t\t\t OIS Rate Daily Compounded (%) (Bloomberg 2010 Methodology)");

  893.         System.out.println ("\t\t\t OIS Rate (%) From Full Calibration\n");

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

  895.         for (int i = 0; i < aFedFundLIBORSwap.length; ++i) {
  896.             Map<String, Double> mapOIS = aOIS[i].value (
  897.                 valParams,
  898.                 null,
  899.                 mktParams,
  900.                 null
  901.             );

  902.             Map<String, Double> mapIRS = aIRS[i].value (
  903.                 valParams,
  904.                 null,
  905.                 mktParams,
  906.                 null
  907.             );

  908.             double dblOISRate = mapOIS.get ("SwapRate");

  909.             double dblIRSRate = mapIRS.get ("SwapRate");

  910.             double dblLIBORFedFundBasis = dblIRSRate - dblOISRate;

  911.             System.out.println ("\t[" +
  912.                 aFedFundLIBORSwap[i].effectiveDate() + " - " +
  913.                 aFedFundLIBORSwap[i].maturityDate() + "] => " +
  914.                 FormatUtil.FormatDouble (dblIRSRate, 1, 4, 100.) + "% | " +
  915.                 FormatUtil.FormatDouble (dblLIBORFedFundBasis, 1, 1, 10000.) + " | " +
  916.                 FormatUtil.FormatDouble (Helper.OISFromLIBORSwapFedFundBasis (dblIRSRate, -dblLIBORFedFundBasis), 1, 4, 100.) + "% | " +
  917.                 FormatUtil.FormatDouble (Helper.OISFromLIBORSwapFedFundBasis2 (dblIRSRate, -dblLIBORFedFundBasis), 1, 4, 100.) + "% | " +
  918.                 FormatUtil.FormatDouble (dblOISRate, 1, 4, 100.) + "%"
  919.             );
  920.         }

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

  922.         EnvManager.TerminateEnv();
  923.     }
  924. }