Kamarhati.java

  1. package org.drip.sample.securitysuite;

  2. import org.drip.analytics.cashflow.*;
  3. import org.drip.analytics.date.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.creator.BondBuilder;
  8. import org.drip.product.credit.BondComponent;
  9. import org.drip.product.params.EmbeddedOptionSchedule;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.service.scenario.*;
  12. import org.drip.service.template.LatentMarketStateBuilder;
  13. import org.drip.state.discount.MergedDiscountForwardCurve;
  14. import org.drip.state.identifier.FloaterLabel;

  15. /*
  16.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  17.  */

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

  59. /**
  60.  * Kamarhati demonstrates the Analytics Calculation/Reconciliation for the Bond Kamarhati.
  61.  *
  62.  * @author Lakshmi Krishnamurthy
  63.  */

  64. public class Kamarhati {

  65.     private static final void SetEOS (
  66.         final BondComponent bond,
  67.         final EmbeddedOptionSchedule eosCall,
  68.         final EmbeddedOptionSchedule eosPut)
  69.         throws java.lang.Exception
  70.     {
  71.         if (null != eosPut) bond.setEmbeddedPutSchedule (eosPut);

  72.         if (null != eosCall) bond.setEmbeddedCallSchedule (eosCall);
  73.     }

  74.     public static final void main (
  75.         final String[] astArgs)
  76.         throws Exception
  77.     {
  78.         EnvManager.InitEnv ("");

  79.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  80.             2017,
  81.             DateUtil.OCTOBER,
  82.             10
  83.         );

  84.         String[] astrDepositTenor = new String[] {
  85.             "2D"
  86.         };

  87.         double[] adblDepositQuote = new double[] {
  88.             0.0130411 // 2D
  89.         };

  90.         double[] adblFuturesQuote = new double[] {
  91.             0.01345,    // 98.655
  92.             0.01470,    // 98.530
  93.             0.01575,    // 98.425
  94.             0.01660,    // 98.340
  95.             0.01745,    // 98.255
  96.             0.01845     // 98.155
  97.         };

  98.         String[] astrFixFloatTenor = new String[] {
  99.             "02Y",
  100.             "03Y",
  101.             "04Y",
  102.             "05Y",
  103.             "06Y",
  104.             "07Y",
  105.             "08Y",
  106.             "09Y",
  107.             "10Y",
  108.             "11Y",
  109.             "12Y",
  110.             "15Y",
  111.             "20Y",
  112.             "25Y",
  113.             "30Y",
  114.             "40Y",
  115.             "50Y"
  116.         };

  117.         double[] adblFixFloatQuote = new double[] {
  118.             0.016410, //  2Y
  119.             0.017863, //  3Y
  120.             0.019030, //  4Y
  121.             0.020035, //  5Y
  122.             0.020902, //  6Y
  123.             0.021660, //  7Y
  124.             0.022307, //  8Y
  125.             0.022879, //  9Y
  126.             0.023363, // 10Y
  127.             0.023820, // 11Y
  128.             0.024172, // 12Y
  129.             0.024934, // 15Y
  130.             0.025581, // 20Y
  131.             0.025906, // 25Y
  132.             0.025973, // 30Y
  133.             0.025838, // 40Y
  134.             0.025560  // 50Y
  135.         };

  136.         String[] astrGovvieTenor = new String[] {
  137.             "1Y",
  138.             "2Y",
  139.             "3Y",
  140.             "5Y",
  141.             "7Y",
  142.             "10Y",
  143.             "20Y",
  144.             "30Y"
  145.         };

  146.         double[] adblGovvieYield = new double[] {
  147.             0.01219, //  1Y
  148.             0.01391, //  2Y
  149.             0.01590, //  3Y
  150.             0.01937, //  5Y
  151.             0.02200, //  7Y
  152.             0.02378, // 10Y
  153.             0.02677, // 20Y
  154.             0.02927  // 30Y
  155.         };

  156.         String[] astrCreditTenor = new String[] {
  157.             "06M",
  158.             "01Y",
  159.             "02Y",
  160.             "03Y",
  161.             "04Y",
  162.             "05Y",
  163.             "07Y",
  164.             "10Y"
  165.         };

  166.         double[] adblCreditQuote = new double[] {
  167.              10.,   //  6M
  168.              12.,   //  1Y
  169.              15.,   //  2Y
  170.              19.,   //  3Y
  171.              24.,   //  4Y
  172.              28.,   //  5Y
  173.              38.,   //  7Y
  174.              51.    // 10Y
  175.         };

  176.         double dblFX = 1;
  177.         int iSettleLag = 3;
  178.         double dblSpread = 0.0;
  179.         String strCurrency = "USD";
  180.         double dblCleanPrice = 0.824219;
  181.         double dblIssuePrice = 0.89;
  182.         double dblSpreadBump = 20.;
  183.         String strTreasuryCode = "UST";
  184.         double dblIssueAmount = 7.50e8;
  185.         double dblSpreadDurationMultiplier = 5.;
  186.         double dblResetRate = 0.060603 - dblSpread;

  187.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  188.             2015,
  189.             12,
  190.             10
  191.         );

  192.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  193.             2022,
  194.             12,
  195.             10
  196.         );

  197.         BondComponent bond = BondBuilder.CreateSimpleFloater (
  198.             "Kamarhati",
  199.             "USD",
  200.             "USD-3M",
  201.             "Kamarhati",
  202.             dblSpread,
  203.             4,
  204.             "Act/360",
  205.             dtEffective,
  206.             dtMaturity,
  207.             null,
  208.             null
  209.         );

  210.         SetEOS (
  211.             bond,
  212.             EmbeddedOptionSchedule.FromAmerican (
  213.                 dtSpot.julian(),
  214.                 new int[] {
  215.                     DateUtil.CreateFromYMD (2015, 12, 10).julian(),
  216.                     DateUtil.CreateFromYMD (2016, 12, 10).julian(),
  217.                     DateUtil.CreateFromYMD (2022, 12, 10).julian(),
  218.                 },
  219.                 new double[] {
  220.                     1.01,
  221.                     1.00,
  222.                     1.00,
  223.                 },
  224.                 false,
  225.                 15,
  226.                 15,
  227.                 false,
  228.                 Double.NaN,
  229.                 "",
  230.                 Double.NaN
  231.             ),
  232.             null
  233.         );

  234.         CompositeFloatingPeriod cfp = (CompositeFloatingPeriod) bond.stream().containingPeriod (dtSpot.julian());

  235.         int iResetDate = ((org.drip.analytics.cashflow.ComposableUnitFloatingPeriod) (cfp.periods().get
  236.             (0))).referenceIndexPeriod().fixingDate();

  237.         MergedDiscountForwardCurve mdfc = LatentMarketStateBuilder.SmoothFundingCurve (
  238.             dtSpot,
  239.             strCurrency,
  240.             astrDepositTenor,
  241.             adblDepositQuote,
  242.             "ForwardRate",
  243.             adblFuturesQuote,
  244.             "ForwardRate",
  245.             astrFixFloatTenor,
  246.             adblFixFloatQuote,
  247.             "SwapRate"
  248.         );

  249.         BondReplicator abr = BondReplicator.CorporateSenior (
  250.             dblCleanPrice,
  251.             dblIssuePrice,
  252.             dblIssueAmount,
  253.             dtSpot,
  254.             astrDepositTenor,
  255.             adblDepositQuote,
  256.             adblFuturesQuote,
  257.             astrFixFloatTenor,
  258.             adblFixFloatQuote,
  259.             dblSpreadBump,
  260.             dblSpreadDurationMultiplier,
  261.             strTreasuryCode,
  262.             astrGovvieTenor,
  263.             adblGovvieYield,
  264.             astrCreditTenor,
  265.             adblCreditQuote,
  266.             dblFX,
  267.             dblResetRate,
  268.             iSettleLag,
  269.             bond
  270.         );

  271.         BondReplicationRun abrr = abr.generateRun();

  272.         System.out.println (abrr.display());

  273.         System.out.println ("\t||----------------------------------------------------------------------------------------------------------------------||");

  274.         System.out.println();

  275.         CurveSurfaceQuoteContainer csqc = abr.creditBaseCSQC();

  276.         FloaterLabel fl = bond.floaterSetting().fri();

  277.         csqc.setFixing (iResetDate, fl, dblResetRate);

  278.         ValuationParams valParams = ValuationParams.Spot (dtSpot.julian());

  279.         double dblYield = bond.yieldFromPrice (
  280.             ValuationParams.Spot (dtSpot.julian()),
  281.             csqc,
  282.             null,
  283.             dblCleanPrice
  284.         );

  285.         System.out.println ("Price In  : " + dblCleanPrice);

  286.         System.out.println ("Yield Out : " + dblYield);

  287.         System.out.println ("Price Out : " +
  288.             bond.priceFromYield (
  289.                 ValuationParams.Spot (dtSpot.julian()),
  290.                 csqc,
  291.                 null,
  292.                 dblYield
  293.             )
  294.         );

  295.         System.out.println ("\t||----------------------------------------------------------------------------------------------------------------------||");

  296.         System.out.println ("\t||                                            PERIOD LABELS AND CURVE FACTORS                                           ||");

  297.         System.out.println ("\t||----------------------------------------------------------------------------------------------------------------------||");

  298.         System.out.println ("\t||   L -> R:                                                                                                            ||");

  299.         System.out.println ("\t||           - Period Start Date                                                                                        ||");

  300.         System.out.println ("\t||           - Period End Date                                                                                          ||");

  301.         System.out.println ("\t||           - Period Credit Label                                                                                      ||");

  302.         System.out.println ("\t||           - Period Funding Label                                                                                     ||");

  303.         System.out.println ("\t||           - Period Coupon Rate (%)                                                                                   ||");

  304.         System.out.println ("\t||           - Period Coupon Year Fraction                                                                              ||");

  305.         System.out.println ("\t||           - Period Coupon Amount                                                                                     ||");

  306.         System.out.println ("\t||           - Period Principal Amount                                                                                  ||");

  307.         System.out.println ("\t||           - Period Discount Factor                                                                                   ||");

  308.         System.out.println ("\t||           - Period Survival Probability                                                                              ||");

  309.         System.out.println ("\t||           - Period Recovery                                                                                          ||");

  310.         System.out.println ("\t||----------------------------------------------------------------------------------------------------------------------||");

  311.         for (CompositePeriod p : bond.couponPeriods()) {
  312.             int iEndDate = p.endDate();

  313.             int iPayDate = p.payDate();

  314.             int iStartDate = p.startDate();

  315.             double dblCouponRate = bond.couponMetrics (
  316.                 iPayDate,
  317.                 valParams,
  318.                 csqc
  319.             ).rate();

  320.             double dblCouponDCF = p.couponDCF();

  321.             System.out.println ("\t|| " +
  322.                 DateUtil.YYYYMMDD (iStartDate) + " => " +
  323.                 DateUtil.YYYYMMDD (iEndDate) + " | ? | " +
  324.                 p.fundingLabel().fullyQualifiedName() + " | " +
  325.                 p.floaterLabel().fullyQualifiedName() + " | " +
  326.                 FormatUtil.FormatDouble (dblCouponRate, 1, 2, 100.) + "% | " +
  327.                 FormatUtil.FormatDouble (dblCouponDCF, 1, 4, 1.) + " | " +
  328.                 FormatUtil.FormatDouble (dblCouponRate * dblCouponDCF * p.notional (iEndDate) * p.couponFactor (iEndDate), 1, 4, 1.) + " | " +
  329.                 FormatUtil.FormatDouble (p.notional (iStartDate) - p.notional (iEndDate), 1, 4, 1.) + " | " +
  330.                 FormatUtil.FormatDouble (p.df (csqc), 1, 4, 1.) + " | " +
  331.                 FormatUtil.FormatDouble (p.survival (csqc), 1, 4, 1.) + " | " +
  332.                 FormatUtil.FormatDouble (p.recovery (csqc), 2, 0, 100.) + "% ||"
  333.             );
  334.         }

  335.         System.out.println ("\t|| " +
  336.             DateUtil.YYYYMMDD (dtEffective.julian()) + " => " +
  337.             DateUtil.YYYYMMDD (dtMaturity.julian()) + " | ? | " +
  338.             bond.fundingLabel().fullyQualifiedName() + " | " +
  339.             bond.forwardLabel().get (bond.name()).fullyQualifiedName() + " | " +
  340.             FormatUtil.FormatDouble (0., 1, 2, 100.) + "% | " +
  341.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  342.             FormatUtil.FormatDouble (0., 1, 4, 1.) + " | " +
  343.             FormatUtil.FormatDouble (bond.notional (dtMaturity.julian()), 1, 4, 1.) + " | " +
  344.             FormatUtil.FormatDouble (mdfc.df (dtMaturity), 1, 4, 1.) + " | " +
  345.             FormatUtil.FormatDouble (1., 1, 4, 1.) + " | " +
  346.             FormatUtil.FormatDouble (1., 2, 0, 100.) + "% ||"
  347.         );

  348.         System.out.println ("\t||----------------------------------------------------------------------------------------------------------------------||");

  349.         System.out.println();
  350.     }
  351. }