Kozhikode.java

  1. package org.drip.sample.municipal;

  2. import org.drip.analytics.date.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.param.creator.MarketParamsBuilder;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.*;
  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.template.*;
  12. import org.drip.state.discount.MergedDiscountForwardCurve;
  13. import org.drip.state.govvie.GovvieCurve;

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

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

  58. /**
  59.  * Kozhikode demonstrates EOS Fixed/Float Coupon Multi-flavor Pricing and Relative Value Measure
  60.  *  Generation for Kozhikode.
  61.  *
  62.  * @author Lakshmi Krishnamurthy
  63.  */

  64. public class Kozhikode {

  65.     private static final MergedDiscountForwardCurve FundingCurve (
  66.         final JulianDate dtSpot,
  67.         final String strCurrency,
  68.         final double dblBump)
  69.         throws Exception
  70.     {
  71.         String[] astrDepositMaturityTenor = new String[] {
  72.             "2D"
  73.         };

  74.         double[] adblDepositQuote = new double[] {
  75.             0.0111956 + dblBump // 2D
  76.         };

  77.         double[] adblFuturesQuote = new double[] {
  78.             0.011375 + dblBump, // 98.8625
  79.             0.013350 + dblBump, // 98.6650
  80.             0.014800 + dblBump, // 98.5200
  81.             0.016450 + dblBump, // 98.3550
  82.             0.017850 + dblBump, // 98.2150
  83.             0.019300 + dblBump  // 98.0700
  84.         };

  85.         String[] astrFixFloatMaturityTenor = new String[] {
  86.             "02Y",
  87.             "03Y",
  88.             "04Y",
  89.             "05Y",
  90.             "06Y",
  91.             "07Y",
  92.             "08Y",
  93.             "09Y",
  94.             "10Y",
  95.             "11Y",
  96.             "12Y",
  97.             "15Y",
  98.             "20Y",
  99.             "25Y",
  100.             "30Y",
  101.             "40Y",
  102.             "50Y"
  103.         };

  104.         double[] adblFixFloatQuote = new double[] {
  105.             0.017029 + dblBump, //  2Y
  106.             0.019354 + dblBump, //  3Y
  107.             0.021044 + dblBump, //  4Y
  108.             0.022291 + dblBump, //  5Y
  109.             0.023240 + dblBump, //  6Y
  110.             0.024025 + dblBump, //  7Y
  111.             0.024683 + dblBump, //  8Y
  112.             0.025243 + dblBump, //  9Y
  113.             0.025720 + dblBump, // 10Y
  114.             0.026130 + dblBump, // 11Y
  115.             0.026495 + dblBump, // 12Y
  116.             0.027230 + dblBump, // 15Y
  117.             0.027855 + dblBump, // 20Y
  118.             0.028025 + dblBump, // 25Y
  119.             0.028028 + dblBump, // 30Y
  120.             0.027902 + dblBump, // 40Y
  121.             0.027655 + dblBump  // 50Y
  122.         };

  123.         return LatentMarketStateBuilder.SmoothFundingCurve (
  124.             dtSpot,
  125.             strCurrency,
  126.             astrDepositMaturityTenor,
  127.             adblDepositQuote,
  128.             "ForwardRate",
  129.             adblFuturesQuote,
  130.             "ForwardRate",
  131.             astrFixFloatMaturityTenor,
  132.             adblFixFloatQuote,
  133.             "SwapRate"
  134.         );
  135.     }

  136.     private static final GovvieCurve GovvieCurve (
  137.         final JulianDate dtSpot,
  138.         final String strCode,
  139.         final double[] adblCoupon,
  140.         final double[] adblYield)
  141.         throws Exception
  142.     {
  143.         JulianDate[] adtEffective = new JulianDate[] {
  144.             dtSpot,
  145.             dtSpot,
  146.             dtSpot,
  147.             dtSpot,
  148.             dtSpot,
  149.             dtSpot,
  150.             dtSpot,
  151.             dtSpot
  152.         };

  153.         JulianDate[] adtMaturity = new JulianDate[] {
  154.             dtSpot.addTenor ("1Y"),
  155.             dtSpot.addTenor ("2Y"),
  156.             dtSpot.addTenor ("3Y"),
  157.             dtSpot.addTenor ("5Y"),
  158.             dtSpot.addTenor ("7Y"),
  159.             dtSpot.addTenor ("10Y"),
  160.             dtSpot.addTenor ("20Y"),
  161.             dtSpot.addTenor ("30Y")
  162.         };

  163.         return LatentMarketStateBuilder.GovvieCurve (
  164.             strCode,
  165.             dtSpot,
  166.             adtEffective,
  167.             adtMaturity,
  168.             adblCoupon,
  169.             adblYield,
  170.             "Yield",
  171.             LatentMarketStateBuilder.SHAPE_PRESERVING
  172.         );
  173.     }

  174.     private static final void RVMeasures (
  175.         final BondComponent bond,
  176.         final JulianDate dtValue,
  177.         final CurveSurfaceQuoteContainer csqc,
  178.         final double dblCleanPrice)
  179.         throws Exception
  180.     {
  181.         JulianDate dtSettle = dtValue.addBusDays (
  182.             0,
  183.             bond.currency()
  184.         );

  185.         ValuationParams valParams = new ValuationParams (
  186.             dtValue,
  187.             dtSettle,
  188.             bond.currency()
  189.         );

  190.         System.out.println();

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

  192.         System.out.println ("\t| Trade Date       : " + dtValue + " ||");

  193.         System.out.println ("\t| Cash Settle Date : " + dtSettle + " ||");

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

  195.         System.out.println();

  196.         double dblYTM = Double.NaN;
  197.         double dblYTW = Double.NaN;
  198.         double dblOASTM = Double.NaN;
  199.         double dblOASTW = Double.NaN;
  200.         double dblWALTM = Double.NaN;
  201.         double dblWALTW = Double.NaN;
  202.         double dblZSpreadTM = Double.NaN;
  203.         double dblZSpreadTW = Double.NaN;
  204.         double dblOASDurationTW = Double.NaN;
  205.         double dblModifiedDurationTM = Double.NaN;
  206.         double dblModifiedDurationTW = Double.NaN;

  207.         WorkoutInfo wi = bond.exerciseYieldFromPrice (
  208.             valParams,
  209.             csqc,
  210.             null,
  211.             dblCleanPrice
  212.         );

  213.         try {
  214.             dblYTW = wi.yield();

  215.             dblYTM = bond.yieldFromPrice (
  216.                 valParams,
  217.                 csqc,
  218.                 null,
  219.                 bond.maturityDate().julian(),
  220.                 1.,
  221.                 dblCleanPrice
  222.             );

  223.             dblWALTW = bond.weightedAverageLife (
  224.                 valParams,
  225.                 csqc,
  226.                 wi.date(),
  227.                 wi.factor()
  228.             );

  229.             dblWALTM = bond.weightedAverageLife (
  230.                 valParams,
  231.                 csqc,
  232.                 bond.maturityDate().julian(),
  233.                 1.
  234.             );

  235.             dblZSpreadTM = bond.zSpreadFromYield (
  236.                 valParams,
  237.                 csqc,
  238.                 null,
  239.                 bond.maturityDate().julian(),
  240.                 1.,
  241.                 dblYTM
  242.             );

  243.             dblZSpreadTW = bond.zSpreadFromYield (
  244.                 valParams,
  245.                 csqc,
  246.                 null,
  247.                 wi.date(),
  248.                 wi.factor(),
  249.                 wi.yield()
  250.             );

  251.             dblOASTM = bond.oasFromYield (
  252.                 valParams,
  253.                 csqc,
  254.                 null,
  255.                 wi.date(),
  256.                 wi.factor(),
  257.                 dblYTM
  258.             );

  259.             dblOASTW = bond.oasFromYield (
  260.                 valParams,
  261.                 csqc,
  262.                 null,
  263.                 wi.date(),
  264.                 wi.factor(),
  265.                 wi.yield()
  266.             );

  267.             dblOASDurationTW = (
  268.                 dblCleanPrice - bond.priceFromOAS (
  269.                     valParams,
  270.                     csqc,
  271.                     null,
  272.                     wi.date(),
  273.                     wi.factor(),
  274.                     dblOASTW + 0.0001
  275.                 )
  276.             ) / dblCleanPrice;

  277.             dblModifiedDurationTM = bond.modifiedDurationFromPrice (
  278.                 valParams,
  279.                 csqc,
  280.                 null,
  281.                 bond.maturityDate().julian(),
  282.                 1.,
  283.                 dblCleanPrice
  284.             );

  285.             dblModifiedDurationTW = bond.modifiedDurationFromPrice (
  286.                 valParams,
  287.                 csqc,
  288.                 null,
  289.                 wi.date(),
  290.                 wi.factor(),
  291.                 dblCleanPrice
  292.             );
  293.         } catch (Exception e) {
  294.             // e.printStackTrace();
  295.         }

  296.         System.out.println ("\t Bond Name                 => " + bond.name());

  297.         System.out.println ("\t Effective Date            => " + bond.effectiveDate());

  298.         System.out.println ("\t Maturity Date             => " + bond.maturityDate());

  299.         System.out.println ("\t Exercise Date             => " + new JulianDate (wi.date()));

  300.         System.out.println ("\t Price                     => " + FormatUtil.FormatDouble (dblCleanPrice, 1, 5, 100.));

  301.         System.out.println ("\t Bond Accrued              => " + FormatUtil.FormatDouble (bond.accrued (dtValue.julian(), csqc), 1, 4, 100.));

  302.         System.out.println ("\t Bond YTM                  => " + FormatUtil.FormatDouble (dblYTM, 1, 2, 100.) + "%");

  303.         System.out.println ("\t Bond YTW                  => " + FormatUtil.FormatDouble (dblYTW, 1, 2, 100.) + "%");

  304.         System.out.println ("\t Bond WAL TM               => " + FormatUtil.FormatDouble (dblWALTM, 2, 1, 1.));

  305.         System.out.println ("\t Bond WAL TW               => " + FormatUtil.FormatDouble (dblWALTW, 2, 1, 1.));

  306.         System.out.println ("\t Bond Modified Duration TM => " + FormatUtil.FormatDouble (dblModifiedDurationTM, 2, 4, 10000.));

  307.         System.out.println ("\t Bond Modified Duration TW => " + FormatUtil.FormatDouble (dblModifiedDurationTW, 2, 4, 10000.));

  308.         System.out.println ("\t Bond OAS Duration         => " + FormatUtil.FormatDouble (dblOASDurationTW, 2, 4, 10000.));

  309.         System.out.println ("\t Bond Z Spread TM          => " + FormatUtil.FormatDouble (dblZSpreadTM, 3, 0, 10000.));

  310.         System.out.println ("\t Bond Z Spread TW          => " + FormatUtil.FormatDouble (dblZSpreadTW, 3, 0, 10000.));

  311.         System.out.println ("\t Bond OAS TM               => " + FormatUtil.FormatDouble (dblOASTM, 3, 0, 10000.));

  312.         System.out.println ("\t Bond OAS TW               => " + FormatUtil.FormatDouble (dblOASTW, 3, 0, 10000.));
  313.     }

  314.     public static final void main (
  315.         final String[] astrArgs)
  316.         throws Exception
  317.     {
  318.         EnvManager.InitEnv ("");

  319.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  320.             2017,
  321.             DateUtil.MARCH,
  322.             24
  323.         );

  324.         String strCurrency = "USD";
  325.         String strTreasuryCode = "UST";

  326.         double[] adblTreasuryCoupon = new double[] {
  327.             0.0100,
  328.             0.0100,
  329.             0.0125,
  330.             0.0150,
  331.             0.0200,
  332.             0.0225,
  333.             0.0250,
  334.             0.0300
  335.         };

  336.         double[] adblTreasuryYield = new double[] {
  337.             0.0083, //  1Y
  338.             0.0122, //  2Y
  339.             0.0149, //  3Y
  340.             0.0193, //  5Y
  341.             0.0227, //  7Y
  342.             0.0248, // 10Y
  343.             0.0280, // 20Y
  344.             0.0308  // 30Y
  345.         };

  346.         JulianDate dtEffective = DateUtil.CreateFromYMD (2016,  5, 23);
  347.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2036,  5, 23);
  348.         double dblCoupon = 0.0303;
  349.         double dblCleanPrice = 0.93898;
  350.         int iFreq = 2;
  351.         String strCUSIP = "Kozhikode";
  352.         String strDayCount = "30/360";
  353.         int[] aiExerciseDate = new int[] {
  354.             DateUtil.CreateFromYMD (2016, 11, 23).julian(),
  355.             DateUtil.CreateFromYMD (2017,  5, 23).julian(),
  356.             DateUtil.CreateFromYMD (2017, 11, 23).julian(),
  357.             DateUtil.CreateFromYMD (2018,  5, 23).julian(),
  358.             DateUtil.CreateFromYMD (2018, 11, 23).julian(),
  359.             DateUtil.CreateFromYMD (2019,  5, 23).julian(),
  360.             DateUtil.CreateFromYMD (2019, 11, 23).julian(),
  361.             DateUtil.CreateFromYMD (2020,  5, 23).julian(),
  362.             DateUtil.CreateFromYMD (2020, 11, 23).julian(),
  363.             DateUtil.CreateFromYMD (2021,  5, 23).julian(),
  364.             DateUtil.CreateFromYMD (2021, 11, 23).julian(),
  365.             DateUtil.CreateFromYMD (2022,  5, 23).julian(),
  366.             DateUtil.CreateFromYMD (2022, 11, 23).julian(),
  367.             DateUtil.CreateFromYMD (2023,  5, 23).julian(),
  368.             DateUtil.CreateFromYMD (2023, 11, 23).julian(),
  369.             DateUtil.CreateFromYMD (2024,  5, 23).julian(),
  370.             DateUtil.CreateFromYMD (2024, 11, 23).julian(),
  371.             DateUtil.CreateFromYMD (2025,  5, 23).julian(),
  372.             DateUtil.CreateFromYMD (2025, 11, 23).julian(),
  373.             DateUtil.CreateFromYMD (2026,  5, 23).julian(),
  374.             DateUtil.CreateFromYMD (2026, 11, 23).julian(),
  375.             DateUtil.CreateFromYMD (2027,  5, 23).julian(),
  376.             DateUtil.CreateFromYMD (2027, 11, 23).julian(),
  377.             DateUtil.CreateFromYMD (2028,  5, 23).julian(),
  378.             DateUtil.CreateFromYMD (2028, 11, 23).julian(),
  379.             DateUtil.CreateFromYMD (2029,  5, 23).julian(),
  380.             DateUtil.CreateFromYMD (2029, 11, 23).julian(),
  381.             DateUtil.CreateFromYMD (2030,  5, 23).julian(),
  382.             DateUtil.CreateFromYMD (2030, 11, 23).julian(),
  383.             DateUtil.CreateFromYMD (2031,  5, 23).julian(),
  384.             DateUtil.CreateFromYMD (2031, 11, 23).julian(),
  385.             DateUtil.CreateFromYMD (2032,  5, 23).julian(),
  386.             DateUtil.CreateFromYMD (2032, 11, 23).julian(),
  387.             DateUtil.CreateFromYMD (2033,  5, 23).julian(),
  388.             DateUtil.CreateFromYMD (2033, 11, 23).julian(),
  389.             DateUtil.CreateFromYMD (2034,  5, 23).julian(),
  390.             DateUtil.CreateFromYMD (2034, 11, 23).julian(),
  391.             DateUtil.CreateFromYMD (2035,  5, 23).julian(),
  392.             DateUtil.CreateFromYMD (2035, 11, 23).julian(),
  393.             DateUtil.CreateFromYMD (2036,  5, 23).julian(),
  394.         };
  395.         double[] adblExercisePrice = new double[] {
  396.             1.,
  397.             1.,
  398.             1.,
  399.             1.,
  400.             1.,
  401.             1.,
  402.             1.,
  403.             1.,
  404.             1.,
  405.             1.,
  406.             1.,
  407.             1.,
  408.             1.,
  409.             1.,
  410.             1.,
  411.             1.,
  412.             1.,
  413.             1.,
  414.             1.,
  415.             1.,
  416.             1.,
  417.             1.,
  418.             1.,
  419.             1.,
  420.             1.,
  421.             1.,
  422.             1.,
  423.             1.,
  424.             1.,
  425.             1.,
  426.             1.,
  427.             1.,
  428.             1.,
  429.             1.,
  430.             1.,
  431.             1.,
  432.             1.,
  433.             1.,
  434.             1.,
  435.             1.,
  436.         };

  437.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  438.             strCUSIP,
  439.             strCurrency,
  440.             "",
  441.             dblCoupon,
  442.             iFreq,
  443.             strDayCount,
  444.             dtEffective,
  445.             dtMaturity,
  446.             null,
  447.             null
  448.         );

  449.         EmbeddedOptionSchedule eos = new EmbeddedOptionSchedule (
  450.             aiExerciseDate,
  451.             adblExercisePrice,
  452.             false,
  453.             5,
  454.             false,
  455.             Double.NaN,
  456.             "",
  457.             Double.NaN
  458.         );

  459.         bond.setEmbeddedCallSchedule (eos);

  460.         RVMeasures (
  461.             bond,
  462.             dtSpot,
  463.             MarketParamsBuilder.Create (
  464.                 FundingCurve (
  465.                     dtSpot,
  466.                     strCurrency,
  467.                     0.
  468.                 ),
  469.                 GovvieCurve (
  470.                     dtSpot,
  471.                     strTreasuryCode,
  472.                     adblTreasuryCoupon,
  473.                     adblTreasuryYield
  474.                 ),
  475.                 null,
  476.                 null,
  477.                 null,
  478.                 null,
  479.                 null
  480.             ),
  481.             dblCleanPrice
  482.         );

  483.         System.out.println();
  484.     }
  485. }