IndexFundCurvesReconciliation.java

  1. package org.drip.sample.ois;

  2. import java.util.*;

  3. import org.drip.analytics.date.DateUtil;
  4. import org.drip.analytics.date.JulianDate;
  5. import org.drip.analytics.support.*;
  6. import org.drip.function.r1tor1.QuadraticRationalShapeControl;
  7. import org.drip.numerical.common.*;
  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.*;
  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.ScenarioDiscountCurveBuilder;
  20. import org.drip.state.discount.*;
  21. import org.drip.state.estimator.LatentStateStretchBuilder;
  22. import org.drip.state.identifier.*;
  23. import org.drip.state.inference.*;

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

  27. /*!
  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.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  33.  *
  34.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  35.  *      libraries targeting analysts and developers
  36.  *      https://lakshmidrip.github.io/DRIP/
  37.  *  
  38.  *  DRIP is composed of four main libraries:
  39.  *  
  40.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  41.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  42.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  43.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  44.  *
  45.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  46.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  47.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  48.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  49.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  50.  *
  51.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  52.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  53.  *
  54.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  55.  *
  56.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  57.  *
  58.  *  Licensed under the Apache License, Version 2.0 (the "License");
  59.  *      you may not use this file except in compliance with the License.
  60.  *  
  61.  *  You may obtain a copy of the License at
  62.  *      http://www.apache.org/licenses/LICENSE-2.0
  63.  *  
  64.  *  Unless required by applicable law or agreed to in writing, software
  65.  *      distributed under the License is distributed on an "AS IS" BASIS,
  66.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  67.  *  
  68.  *  See the License for the specific language governing permissions and
  69.  *      limitations under the License.
  70.  */

  71. /**
  72.  * IndexFundCurvesReconciliation demonstrates the Construction, Usage, Coupon Extraction and Measure
  73.  *  Generation for an OIS Product Sample using the Index and the Fund Curves, and their Reconciliation.
  74.  *
  75.  * @author Lakshmi Krishnamurthy
  76.  */

  77. public class IndexFundCurvesReconciliation {

  78.     /*
  79.      * Construct the Array of Deposit Instruments from the given set of parameters
  80.      *
  81.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  82.      */

  83.     private static final SingleStreamComponent[] DepositInstrumentsFromMaturityDays (
  84.         final JulianDate dtEffective,
  85.         final String strCurrency,
  86.         final int[] aiDay)
  87.         throws Exception
  88.     {
  89.         SingleStreamComponent[] aDeposit = new SingleStreamComponent[aiDay.length];

  90.         for (int i = 0; i < aiDay.length; ++i)
  91.             aDeposit[i] = SingleStreamComponentBuilder.Deposit (
  92.                 dtEffective,
  93.                 dtEffective.addBusDays (
  94.                     aiDay[i],
  95.                     strCurrency
  96.                 ),
  97.                 OvernightLabel.Create (
  98.                     strCurrency
  99.                 )
  100.             );

  101.         return aDeposit;
  102.     }

  103.     /*
  104.      * Construct the Array of Overnight Index Instruments from the given set of parameters
  105.      *
  106.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  107.      */

  108.     private static final FixFloatComponent[] OvernightIndexFromMaturityTenor (
  109.         final JulianDate dtEffective,
  110.         final String[] astrMaturityTenor,
  111.         final double[] adblCoupon,
  112.         final String strCurrency)
  113.         throws Exception
  114.     {
  115.         FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];

  116.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  117.             2,
  118.             "Act/360",
  119.             false,
  120.             "Act/360",
  121.             false,
  122.             strCurrency,
  123.             false,
  124.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  125.         );

  126.         CashSettleParams csp = new CashSettleParams (
  127.             0,
  128.             strCurrency,
  129.             0
  130.         );

  131.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  132.             java.lang.String strFixedTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  133.                 astrMaturityTenor[i],
  134.                 "6M"
  135.             ) ? astrMaturityTenor[i] : "6M";

  136.             java.lang.String strFloatingTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  137.                 astrMaturityTenor[i],
  138.                 "3M"
  139.             ) ? astrMaturityTenor[i] : "3M";

  140.             ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  141.                 "ON",
  142.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
  143.                 null,
  144.                 OvernightLabel.Create (
  145.                     strCurrency
  146.                 ),
  147.                 CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  148.                 0.
  149.             );

  150.             ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  151.                 strFixedTenor,
  152.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  153.                 null,
  154.                 adblCoupon[i],
  155.                 0.,
  156.                 strCurrency
  157.             );

  158.             CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  159.                 4,
  160.                 strFloatingTenor,
  161.                 strCurrency,
  162.                 null,
  163.                 -1.,
  164.                 null,
  165.                 null,
  166.                 null,
  167.                 null
  168.             );

  169.             CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  170.                 2,
  171.                 strFixedTenor,
  172.                 strCurrency,
  173.                 null,
  174.                 1.,
  175.                 null,
  176.                 null,
  177.                 null,
  178.                 null
  179.             );

  180.             List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  181.                 dtEffective,
  182.                 strFixedTenor,
  183.                 astrMaturityTenor[i],
  184.                 null
  185.             );

  186.             List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  187.                 dtEffective,
  188.                 strFloatingTenor,
  189.                 astrMaturityTenor[i],
  190.                 null
  191.             );

  192.             Stream floatingStream = new Stream (
  193.                 CompositePeriodBuilder.FloatingCompositeUnit (
  194.                     lsFloatingStreamEdgeDate,
  195.                     cpsFloating,
  196.                     cfusFloating
  197.                 )
  198.             );

  199.             Stream fixedStream = new Stream (
  200.                 CompositePeriodBuilder.FixedCompositeUnit (
  201.                     lsFixedStreamEdgeDate,
  202.                     cpsFixed,
  203.                     ucasFixed,
  204.                     cfusFixed
  205.                 )
  206.             );

  207.             FixFloatComponent ois = new FixFloatComponent (
  208.                 fixedStream,
  209.                 floatingStream,
  210.                 csp
  211.             );

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

  213.             aOIS[i] = ois;
  214.         }

  215.         return aOIS;
  216.     }

  217.     /*
  218.      * Construct the Array of Overnight Fund Instruments from the given set of parameters
  219.      *
  220.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  221.      */

  222.     private static final FixFloatComponent[] OvernightFundFromMaturityTenor (
  223.         final JulianDate dtEffective,
  224.         final String[] astrMaturityTenor,
  225.         final double[] adblCoupon,
  226.         final String strCurrency)
  227.         throws Exception
  228.     {
  229.         FixFloatComponent[] aOIS = new FixFloatComponent[astrMaturityTenor.length];

  230.         UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  231.             2,
  232.             "Act/360",
  233.             false,
  234.             "Act/360",
  235.             false,
  236.             strCurrency,
  237.             false,
  238.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  239.         );

  240.         CashSettleParams csp = new CashSettleParams (
  241.             0,
  242.             strCurrency,
  243.             0
  244.         );

  245.         for (int i = 0; i < astrMaturityTenor.length; ++i) {
  246.             java.lang.String strFixedTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  247.                 astrMaturityTenor[i],
  248.                 "6M"
  249.             ) ? astrMaturityTenor[i] : "6M";

  250.             java.lang.String strFloatingTenor = "ON";

  251.             ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  252.                 "ON",
  253.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
  254.                 null,
  255.                 OvernightLabel.Create (
  256.                     strCurrency
  257.                 ),
  258.                 CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  259.                 0.
  260.             );

  261.             ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  262.                 strFixedTenor,
  263.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  264.                 null,
  265.                 adblCoupon[i],
  266.                 0.,
  267.                 strCurrency
  268.             );

  269.             CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  270.                 360,
  271.                 strFloatingTenor,
  272.                 strCurrency,
  273.                 null,
  274.                 -1.,
  275.                 null,
  276.                 null,
  277.                 null,
  278.                 null
  279.             );

  280.             CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  281.                 2,
  282.                 strFixedTenor,
  283.                 strCurrency,
  284.                 null,
  285.                 1.,
  286.                 null,
  287.                 null,
  288.                 null,
  289.                 null
  290.             );

  291.             List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  292.                 dtEffective,
  293.                 strFixedTenor,
  294.                 astrMaturityTenor[i],
  295.                 null
  296.             );

  297.             List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.OvernightEdgeDates (
  298.                 dtEffective,
  299.                 dtEffective.addTenor (astrMaturityTenor[i]),
  300.                 null
  301.             );

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

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

  317.             FixFloatComponent ois = new FixFloatComponent (
  318.                 fixedStream,
  319.                 floatingStream,
  320.                 csp
  321.             );

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

  323.             aOIS[i] = ois;
  324.         }

  325.         return aOIS;
  326.     }

  327.     /*
  328.      * Construct the Array of Overnight Index Future Instruments from the given set of parameters
  329.      *
  330.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  331.      */

  332.     private static final FixFloatComponent[] OvernightIndexFutureFromMaturityTenor (
  333.         final JulianDate dtSpot,
  334.         final String[] astrStartTenor,
  335.         final String[] astrMaturityTenor,
  336.         final double[] adblCoupon,
  337.         final String strCurrency)
  338.         throws Exception
  339.     {
  340.         FixFloatComponent[] aOIS = new FixFloatComponent[astrStartTenor.length];

  341.         CashSettleParams csp = new CashSettleParams (
  342.             0,
  343.             strCurrency,
  344.             0
  345.         );

  346.         for (int i = 0; i < astrStartTenor.length; ++i) {
  347.             JulianDate dtEffective = dtSpot.addTenor (astrStartTenor[i]);

  348.             java.lang.String strFixedTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  349.                 astrMaturityTenor[i],
  350.                 "6M"
  351.             ) ? astrMaturityTenor[i] : "6M";

  352.             java.lang.String strFloatingTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  353.                 astrMaturityTenor[i],
  354.                 "3M"
  355.             ) ? astrMaturityTenor[i] : "3M";

  356.             ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  357.                 strFixedTenor,
  358.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  359.                 null,
  360.                 adblCoupon[i],
  361.                 0.,
  362.                 strCurrency
  363.             );

  364.             UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  365.                 2,
  366.                 "Act/360",
  367.                 false,
  368.                 "Act/360",
  369.                 false,
  370.                 strCurrency,
  371.                 false,
  372.                 CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  373.             );

  374.             ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  375.                 "ON",
  376.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
  377.                 null,
  378.                 OvernightLabel.Create (
  379.                     strCurrency
  380.                 ),
  381.                 CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  382.                 0.
  383.             );

  384.             CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  385.                 4,
  386.                 strFloatingTenor,
  387.                 strCurrency,
  388.                 null,
  389.                 -1.,
  390.                 null,
  391.                 null,
  392.                 null,
  393.                 null
  394.             );

  395.             CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  396.                 2,
  397.                 strFixedTenor,
  398.                 strCurrency,
  399.                 null,
  400.                 1.,
  401.                 null,
  402.                 null,
  403.                 null,
  404.                 null
  405.             );

  406.             List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  407.                 dtEffective,
  408.                 "6M",
  409.                 astrMaturityTenor[i],
  410.                 null
  411.             );

  412.             List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  413.                 dtEffective,
  414.                 "3M",
  415.                 astrMaturityTenor[i],
  416.                 null
  417.             );

  418.             Stream floatingStream = new Stream (
  419.                 CompositePeriodBuilder.FloatingCompositeUnit (
  420.                     lsFloatingStreamEdgeDate,
  421.                     cpsFloating,
  422.                     cfusFloating
  423.                 )
  424.             );

  425.             Stream fixedStream = new Stream (
  426.                 CompositePeriodBuilder.FixedCompositeUnit (
  427.                     lsFixedStreamEdgeDate,
  428.                     cpsFixed,
  429.                     ucasFixed,
  430.                     cfusFixed
  431.                 )
  432.             );

  433.             FixFloatComponent ois = new FixFloatComponent (
  434.                 fixedStream,
  435.                 floatingStream,
  436.                 csp
  437.             );

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

  439.             aOIS[i] = ois;
  440.         }

  441.         return aOIS;
  442.     }

  443.     /*
  444.      * Construct the Array of Overnight Fund Future Instruments from the given set of parameters
  445.      *
  446.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  447.      */

  448.     private static final FixFloatComponent[] OvernightFundFutureFromMaturityTenor (
  449.         final JulianDate dtSpot,
  450.         final String[] astrStartTenor,
  451.         final String[] astrMaturityTenor,
  452.         final double[] adblCoupon,
  453.         final String strCurrency)
  454.         throws Exception
  455.     {
  456.         FixFloatComponent[] aOIS = new FixFloatComponent[astrStartTenor.length];

  457.         CashSettleParams csp = new CashSettleParams (
  458.             0,
  459.             strCurrency,
  460.             0
  461.         );

  462.         for (int i = 0; i < astrStartTenor.length; ++i) {
  463.             JulianDate dtEffective = dtSpot.addTenor (astrStartTenor[i]);

  464.             java.lang.String strFixedTenor = Helper.LEFT_TENOR_LESSER == Helper.TenorCompare (
  465.                 astrMaturityTenor[i],
  466.                 "6M"
  467.             ) ? astrMaturityTenor[i] : "6M";

  468.             ComposableFixedUnitSetting cfusFixed = new ComposableFixedUnitSetting (
  469.                 strFixedTenor,
  470.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  471.                 null,
  472.                 adblCoupon[i],
  473.                 0.,
  474.                 strCurrency
  475.             );

  476.             UnitCouponAccrualSetting ucasFixed = new UnitCouponAccrualSetting (
  477.                 2,
  478.                 "Act/360",
  479.                 false,
  480.                 "Act/360",
  481.                 false,
  482.                 strCurrency,
  483.                 false,
  484.                 CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  485.             );

  486.             ComposableFloatingUnitSetting cfusFloating = new ComposableFloatingUnitSetting (
  487.                 "ON",
  488.                 CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
  489.                 null,
  490.                 OvernightLabel.Create (
  491.                     strCurrency
  492.                 ),
  493.                 CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  494.                 0.
  495.             );

  496.             CompositePeriodSetting cpsFloating = new CompositePeriodSetting (
  497.                 4,
  498.                 "ON",
  499.                 strCurrency,
  500.                 null,
  501.                 -1.,
  502.                 null,
  503.                 null,
  504.                 null,
  505.                 null
  506.             );

  507.             CompositePeriodSetting cpsFixed = new CompositePeriodSetting (
  508.                 2,
  509.                 strFixedTenor,
  510.                 strCurrency,
  511.                 null,
  512.                 1.,
  513.                 null,
  514.                 null,
  515.                 null,
  516.                 null
  517.             );

  518.             List<Integer> lsFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  519.                 dtEffective,
  520.                 "6M",
  521.                 astrMaturityTenor[i],
  522.                 null
  523.             );

  524.             List<Integer> lsFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  525.                 dtEffective,
  526.                 "3M",
  527.                 astrMaturityTenor[i],
  528.                 null
  529.             );

  530.             Stream floatingStream = new Stream (
  531.                 CompositePeriodBuilder.FloatingCompositeUnit (
  532.                     lsFloatingStreamEdgeDate,
  533.                     cpsFloating,
  534.                     cfusFloating
  535.                 )
  536.             );

  537.             Stream fixedStream = new Stream (
  538.                 CompositePeriodBuilder.FixedCompositeUnit (
  539.                     lsFixedStreamEdgeDate,
  540.                     cpsFixed,
  541.                     ucasFixed,
  542.                     cfusFixed
  543.                 )
  544.             );

  545.             FixFloatComponent ois = new FixFloatComponent (
  546.                 fixedStream,
  547.                 floatingStream,
  548.                 csp
  549.             );

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

  551.             aOIS[i] = ois;
  552.         }

  553.         return aOIS;
  554.     }

  555.     private static final MergedDiscountForwardCurve CustomOISCurveBuilderSample (
  556.         final JulianDate dtToday,
  557.         final String strHeaderComment,
  558.         final String strCurrency,
  559.         final boolean bOvernightIndex)
  560.         throws Exception
  561.     {
  562.         System.out.println ("\n\t----------------------------------------------------------------");

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

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

  565.         /*
  566.          * Construct the Array of Deposit Instruments and their Quotes from the given set of parameters
  567.          */

  568.         SingleStreamComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  569.             dtToday,
  570.             strCurrency,
  571.             new int[] {
  572.                 1, 2, 3
  573.             }
  574.         );

  575.         double[] adblDepositQuote = new double[] {
  576.             0.0004, 0.0004, 0.0004       // Deposit
  577.         };

  578.         /*
  579.          * Construct the Deposit Instrument Set Stretch Builder
  580.          */

  581.         LatentStateStretchSpec depositStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  582.             "   DEPOSIT   ",
  583.             aDepositComp,
  584.             "ForwardRate",
  585.             adblDepositQuote
  586.         );

  587.         /*
  588.          * Construct the Array of Short End OIS Instruments and their Quotes from the given set of parameters
  589.          */

  590.         double[] adblShortEndOISQuote = new double[] {
  591.             0.00070,    //   1W
  592.             0.00069,    //   2W
  593.             0.00078,    //   3W
  594.             0.00074     //   1M
  595.         };

  596.         CalibratableComponent[] aShortEndOISComp = bOvernightIndex ?
  597.             OvernightIndexFromMaturityTenor (
  598.                 dtToday,
  599.                 new java.lang.String[]
  600.                     {"1W", "2W", "3W", "1M"},
  601.                 adblShortEndOISQuote,
  602.                 strCurrency) :
  603.             OvernightFundFromMaturityTenor (
  604.                 dtToday,
  605.                 new java.lang.String[]
  606.                     {"1W", "2W", "3W", "1M"},
  607.                 adblShortEndOISQuote,
  608.                 strCurrency
  609.             );

  610.         /*
  611.          * Construct the Short End OIS Instrument Set Stretch Builder
  612.          */

  613.         LatentStateStretchSpec oisShortEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  614.             "SHORT END OIS",
  615.             aShortEndOISComp,
  616.             "SwapRate",
  617.             adblShortEndOISQuote
  618.         );

  619.         /*
  620.          * Construct the Array of OIS Futures Instruments and their Quotes from the given set of parameters
  621.          */

  622.         double[] adblOISFutureQuote = new double[] {
  623.              0.00046,    //   1M x 1M
  624.              0.00016,    //   2M x 1M
  625.             -0.00007,    //   3M x 1M
  626.             -0.00013,    //   4M x 1M
  627.             -0.00014     //   5M x 1M
  628.         };

  629.         CalibratableComponent[] aOISFutureComp = bOvernightIndex ?
  630.             OvernightIndexFutureFromMaturityTenor (
  631.                 dtToday,
  632.                 new java.lang.String[] {"1M", "2M", "3M", "4M", "5M"},
  633.                 new java.lang.String[] {"1M", "1M", "1M", "1M", "1M"},
  634.                 adblOISFutureQuote,
  635.                 strCurrency
  636.             ) :
  637.             OvernightFundFutureFromMaturityTenor (
  638.                 dtToday,
  639.                 new java.lang.String[] {"1M", "2M", "3M", "4M", "5M"},
  640.                 new java.lang.String[] {"1M", "1M", "1M", "1M", "1M"},
  641.                 adblOISFutureQuote,
  642.                 strCurrency
  643.             );

  644.         /*
  645.          * Construct the OIS Future Instrument Set Stretch Builder
  646.          */

  647.         LatentStateStretchSpec oisFutureStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  648.             " OIS FUTURE  ",
  649.             aOISFutureComp,
  650.             "SwapRate",
  651.             adblOISFutureQuote
  652.         );

  653.         /*
  654.          * Construct the Array of Long End OIS Instruments and their Quotes from the given set of parameters
  655.          */

  656.         double[] adblLongEndOISQuote = new double[] {
  657.             0.00002,    //  15M
  658.             0.00008,    //  18M
  659.             0.00021,    //  21M
  660.             0.00036,    //   2Y
  661.             0.00127,    //   3Y
  662.             0.00274,    //   4Y
  663.             0.00456,    //   5Y
  664.             0.00647,    //   6Y
  665.             0.00827,    //   7Y
  666.             0.00996,    //   8Y
  667.             0.01147,    //   9Y
  668.             0.01280,    //  10Y
  669.             0.01404,    //  11Y
  670.             0.01516,    //  12Y
  671.             0.01764,    //  15Y
  672.             0.01939,    //  20Y
  673.             0.02003,    //  25Y
  674.             0.02038     //  30Y
  675.         };

  676.         CalibratableComponent[] aLongEndOISComp = bOvernightIndex ?
  677.             OvernightIndexFromMaturityTenor (
  678.                 dtToday,
  679.                 new java.lang.String[]
  680.                     {"15M", "18M", "21M", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"},
  681.                 adblLongEndOISQuote,
  682.                 strCurrency
  683.             ) : OvernightFundFromMaturityTenor (
  684.                 dtToday,
  685.                 new java.lang.String[]
  686.                     {"15M", "18M", "21M", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y"},
  687.                 adblLongEndOISQuote,
  688.                 strCurrency
  689.             );


  690.         /*
  691.          * Construct the Long End OIS Instrument Set Stretch Builder
  692.          */

  693.         LatentStateStretchSpec oisLongEndStretch = LatentStateStretchBuilder.ForwardFundingStretchSpec (
  694.             "LONG END OIS ",
  695.             aLongEndOISComp,
  696.             "SwapRate",
  697.             adblLongEndOISQuote
  698.         );

  699.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  700.             depositStretch,
  701.             oisShortEndStretch,
  702.             oisFutureStretch,
  703.             oisLongEndStretch
  704.         };

  705.         /*
  706.          * Set up the Linear Curve Calibrator using the following parameters:
  707.          *  - Cubic Exponential Mixture Basis Spline Set
  708.          *  - Ck = 2, Segment Curvature Penalty = 2
  709.          *  - Quadratic Rational Shape Controller
  710.          *  - Natural Boundary Setting
  711.          */

  712.         LinearLatentStateCalibrator lcc = new LinearLatentStateCalibrator (
  713.             new SegmentCustomBuilderControl (
  714.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  715.                 new PolynomialFunctionSetParams (4),
  716.                 SegmentInelasticDesignControl.Create (
  717.                     2,
  718.                     2
  719.                 ),
  720.                 new ResponseScalingShapeControl (
  721.                     true,
  722.                     new QuadraticRationalShapeControl (0.)
  723.                 ),
  724.                 null
  725.             ),
  726.             BoundarySettings.NaturalStandard(),
  727.             MultiSegmentSequence.CALIBRATE,
  728.             null,
  729.             null
  730.         );

  731.         /*
  732.          * Construct the Shape Preserving Discount Curve by applying the linear curve calibrator to the array
  733.          *  of Deposit and Swap Stretches.
  734.          */

  735.         MergedDiscountForwardCurve dc = ScenarioDiscountCurveBuilder.ShapePreservingDFBuild (
  736.             strCurrency,
  737.             lcc,
  738.             aStretchSpec,
  739.             new ValuationParams (
  740.                 dtToday,
  741.                 dtToday,
  742.                 strCurrency
  743.             ),
  744.             null,
  745.             null,
  746.             null,
  747.             1.
  748.         );

  749.         /*
  750.          * Cross-Comparison of the Deposit Calibration Instrument "Rate" metric across the different curve
  751.          *  construction methodologies.
  752.          */

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

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

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

  756.         for (int i = 0; i < aDepositComp.length; ++i)
  757.             System.out.println ("\t[" + aDepositComp[i].effectiveDate() + " => " + aDepositComp[i].maturityDate() + "] = " +
  758.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  759.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  760.                         null, "Rate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.));

  761.         /*
  762.          * Cross-Comparison of the Short End OIS Calibration Instrument "Rate" metric across the different curve
  763.          *  construction methodologies.
  764.          */

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

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

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

  768.         for (int i = 0; i < aShortEndOISComp.length; ++i)
  769.             System.out.println ("\t[" + aShortEndOISComp[i].effectiveDate() + " => " + aShortEndOISComp[i].maturityDate() + "] = " +
  770.                 FormatUtil.FormatDouble (aShortEndOISComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  771.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  772.                         null, "CalibSwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblShortEndOISQuote[i], 1, 6, 1.) + " | " +
  773.                             FormatUtil.FormatDouble (aShortEndOISComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  774.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  775.                                     null, "FairPremium"), 1, 6, 1.));

  776.         /*
  777.          * Cross-Comparison of the OIS Future Calibration Instrument "Rate" metric across the different curve
  778.          *  construction methodologies.
  779.          */

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

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

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

  783.         for (int i = 0; i < aOISFutureComp.length; ++i)
  784.             System.out.println ("\t[" + aOISFutureComp[i].effectiveDate() + " => " + aOISFutureComp[i].maturityDate() + "] = " +
  785.                 FormatUtil.FormatDouble (aOISFutureComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  786.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  787.                         null, "SwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblOISFutureQuote[i], 1, 6, 1.) + " | " +
  788.                             FormatUtil.FormatDouble (aOISFutureComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  789.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  790.                                     null, "FairPremium"), 1, 6, 1.));

  791.         /*
  792.          * Cross-Comparison of the Long End OIS Calibration Instrument "Rate" metric across the different curve
  793.          *  construction methodologies.
  794.          */

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

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

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

  798.         for (int i = 0; i < aLongEndOISComp.length; ++i)
  799.             System.out.println ("\t[" + aLongEndOISComp[i].effectiveDate() + " => " + aLongEndOISComp[i].maturityDate() + "] = " +
  800.                 FormatUtil.FormatDouble (aLongEndOISComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  801.                     MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  802.                         null, "CalibSwapRate"), 1, 6, 1.) + " | " + FormatUtil.FormatDouble (adblLongEndOISQuote[i], 1, 6, 1.) + " | " +
  803.                             FormatUtil.FormatDouble (aLongEndOISComp[i].measureValue (new ValuationParams (dtToday, dtToday, strCurrency), null,
  804.                                 MarketParamsBuilder.Create (dc, null, null, null, null, null, null),
  805.                                     null, "FairPremium"), 1, 6, 1.));

  806.         return dc;
  807.     }

  808.     public static final void main (
  809.         final String[] astrArgs)
  810.         throws Exception
  811.     {
  812.         /*
  813.          * Initialize the Credit Analytics Library
  814.          */

  815.         EnvManager.InitEnv ("");

  816.         String strCurrency = "EUR";

  817.         JulianDate dtToday = DateUtil.CreateFromYMD (
  818.             2018,
  819.             DateUtil.FEBRUARY,
  820.             18
  821.         );

  822.         MergedDiscountForwardCurve dcIndex = CustomOISCurveBuilderSample (
  823.             dtToday,
  824.             "---- DISCOUNT CURVE WITH OVERNIGHT INDEX ---",
  825.             strCurrency,
  826.             true
  827.         );

  828.         MergedDiscountForwardCurve dcFund = CustomOISCurveBuilderSample (
  829.             dtToday,
  830.             "---- DISCOUNT CURVE WITH OVERNIGHT FUND ---",
  831.             strCurrency,
  832.             false
  833.         );

  834.         JulianDate dtCustomOISStart = dtToday.subtractTenor ("2M");

  835.         UnitCouponAccrualSetting ucasCustomFixed = new UnitCouponAccrualSetting (
  836.             2,
  837.             "Act/360",
  838.             false,
  839.             "Act/360",
  840.             false,
  841.             strCurrency,
  842.             true,
  843.             CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  844.         );

  845.         ComposableFloatingUnitSetting cfusCustomFloating = new ComposableFloatingUnitSetting (
  846.             "ON",
  847.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT,
  848.             null,
  849.             OvernightLabel.Create (
  850.                 strCurrency
  851.             ),
  852.             CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  853.             0.
  854.         );

  855.         ComposableFixedUnitSetting cfusCustomFixed = new ComposableFixedUnitSetting (
  856.             "6M",
  857.             CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  858.             null,
  859.             0.003,
  860.             0.,
  861.             strCurrency
  862.         );

  863.         CompositePeriodSetting cpsCustomFloating = new CompositePeriodSetting (
  864.             4,
  865.             "3M",
  866.             strCurrency,
  867.             null,
  868.             -1.,
  869.             null,
  870.             null,
  871.             null,
  872.             null
  873.         );

  874.         CompositePeriodSetting cpsCustomFixed = new CompositePeriodSetting (
  875.             2,
  876.             "6M",
  877.             strCurrency,
  878.             null,
  879.             1.,
  880.             null,
  881.             null,
  882.             null,
  883.             null
  884.         );

  885.         List<Integer> lsCustomFixedStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  886.             dtCustomOISStart,
  887.             "6M",
  888.             "4M",
  889.             null
  890.         );

  891.         List<Integer> lsCustomFloatingStreamEdgeDate = CompositePeriodBuilder.RegularEdgeDates (
  892.             dtCustomOISStart,
  893.             "3M",
  894.             "4M",
  895.             null
  896.         );

  897.         Stream customFloatingStream = new Stream (
  898.             CompositePeriodBuilder.FloatingCompositeUnit (
  899.                 lsCustomFloatingStreamEdgeDate,
  900.                 cpsCustomFloating,
  901.                 cfusCustomFloating
  902.             )
  903.         );

  904.         Stream customFixedStream = new Stream (
  905.             CompositePeriodBuilder.FixedCompositeUnit (
  906.                 lsCustomFixedStreamEdgeDate,
  907.                 cpsCustomFixed,
  908.                 ucasCustomFixed,
  909.                 cfusCustomFixed
  910.             )
  911.         );

  912.         FixFloatComponent ois = new FixFloatComponent (
  913.             customFixedStream,
  914.             customFloatingStream,
  915.             new CashSettleParams (
  916.                 0,
  917.                 strCurrency,
  918.                 0
  919.             )
  920.         );

  921.         CurveSurfaceQuoteContainer mktParamsIndex = MarketParamsBuilder.Create (
  922.             dcIndex,
  923.             null,
  924.             null,
  925.             null,
  926.             null,
  927.             null,
  928.             null
  929.         );

  930.         CurveSurfaceQuoteContainer mktParamsFund = MarketParamsBuilder.Create (
  931.             dcFund,
  932.             null,
  933.             null,
  934.             null,
  935.             null,
  936.             null,
  937.             null
  938.         );

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

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

  941.         Map<String, Double> mapOISOutputIndex = ois.value (
  942.             new ValuationParams (
  943.                 dtToday,
  944.                 dtToday,
  945.                 strCurrency
  946.             ),
  947.             null,
  948.             mktParamsIndex,
  949.             null
  950.         );

  951.         Map<String, Double> mapOISOutputFund = ois.value (
  952.             new ValuationParams (
  953.                 dtToday,
  954.                 dtToday,
  955.                 strCurrency
  956.             ),
  957.             null,
  958.             mktParamsFund,
  959.             null
  960.         );

  961.         for (Map.Entry<String, Double> me : mapOISOutputIndex.entrySet()) {
  962.             String strKey = me.getKey();

  963.             double dblIndexMeasure = me.getValue();

  964.             double dblFundMeasure = mapOISOutputFund.get (strKey);

  965.             String strReconcile = NumberUtil.WithinTolerance (
  966.                 dblIndexMeasure,
  967.                 dblFundMeasure,
  968.                 1.e-08,
  969.                 1.e-04) ?
  970.             "RECONCILES" :
  971.             "DOES NOT RECONCILE";

  972.             System.out.println ("\t" +
  973.                 FormatUtil.FormatDouble (dblIndexMeasure, 1, 8, 1.) + " | " +
  974.                 FormatUtil.FormatDouble (dblFundMeasure, 1, 8, 1.) + " | " +
  975.                 strReconcile + " <= " + strKey
  976.             );
  977.         }
  978.     }
  979. }