Kurnool.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.  * Kurnool demonstrates EOS Fixed/Float Coupon Multi-flavor Pricing and Relative Value Measure Generation for
  60.  *  Kurnool.
  61.  *
  62.  * @author Lakshmi Krishnamurthy
  63.  */

  64. public class Kurnool {

  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 = (dblCleanPrice - bond.priceFromOAS (
  268.                     valParams,
  269.                     csqc,
  270.                     null,
  271.                     wi.date(),
  272.                     wi.factor(),
  273.                     dblOASTW + 0.0001
  274.                 )
  275.             ) / dblCleanPrice;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  345.         JulianDate dtEffective = DateUtil.CreateFromYMD (2014,  4, 11);
  346.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2029,  4, 11);
  347.         double dblCoupon = 0.0400;
  348.         double dblCleanPrice = 1.00085;
  349.         int iFreq = 2;
  350.         String strCUSIP = "Kurnool";
  351.         String strDayCount = "30/360";
  352.         int[] aiExerciseDate = new int[] {
  353.             DateUtil.CreateFromYMD (2017,  4, 11).julian(),
  354.         };
  355.         double[] adblExercisePrice = new double[] {
  356.             1.,
  357.         };

  358.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  359.             strCUSIP,
  360.             strCurrency,
  361.             "",
  362.             dblCoupon,
  363.             iFreq,
  364.             strDayCount,
  365.             dtEffective,
  366.             dtMaturity,
  367.             null,
  368.             null
  369.         );

  370.         EmbeddedOptionSchedule eos = new EmbeddedOptionSchedule (
  371.             aiExerciseDate,
  372.             adblExercisePrice,
  373.             false,
  374.             30,
  375.             false,
  376.             Double.NaN,
  377.             "",
  378.             Double.NaN
  379.         );

  380.         bond.setEmbeddedCallSchedule (eos);

  381.         RVMeasures (
  382.             bond,
  383.             dtSpot,
  384.             MarketParamsBuilder.Create (
  385.                 FundingCurve (
  386.                     dtSpot,
  387.                     strCurrency,
  388.                     0.
  389.                 ),
  390.                 GovvieCurve (
  391.                     dtSpot,
  392.                     strTreasuryCode,
  393.                     adblTreasuryCoupon,
  394.                     adblTreasuryYield
  395.                 ),
  396.                 null,
  397.                 null,
  398.                 null,
  399.                 null,
  400.                 null
  401.             ),
  402.             dblCleanPrice
  403.         );

  404.         System.out.println();
  405.     }
  406. }