Raipur.java

  1. package org.drip.sample.bondeos;

  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) 2020 Lakshmi Krishnamurthy
  19.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  20.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  22.  *
  23.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  24.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  25.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  26.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  27.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  28.  *      and computational support.
  29.  *  
  30.  *      https://lakshmidrip.github.io/DROP/
  31.  *  
  32.  *  DROP is composed of three modules:
  33.  *  
  34.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  35.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  36.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  37.  *
  38.  *  DROP Product Core implements libraries for the following:
  39.  *  - Fixed Income Analytics
  40.  *  - Loan Analytics
  41.  *  - Transaction Cost Analytics
  42.  *
  43.  *  DROP Portfolio Core implements libraries for the following:
  44.  *  - Asset Allocation Analytics
  45.  *  - Asset Liability Management Analytics
  46.  *  - Capital Estimation Analytics
  47.  *  - Exposure Analytics
  48.  *  - Margin Analytics
  49.  *  - XVA Analytics
  50.  *
  51.  *  DROP Computational Core implements libraries for the following:
  52.  *  - Algorithm Support
  53.  *  - Computation Support
  54.  *  - Function Analysis
  55.  *  - Model Validation
  56.  *  - Numerical Analysis
  57.  *  - Numerical Optimizer
  58.  *  - Spline Builder
  59.  *  - Statistical Learning
  60.  *
  61.  *  Documentation for DROP is Spread Over:
  62.  *
  63.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  64.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  65.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  66.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  67.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  68.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  69.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  70.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  71.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  72.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  73.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  74.  *
  75.  *  Licensed under the Apache License, Version 2.0 (the "License");
  76.  *      you may not use this file except in compliance with the License.
  77.  *  
  78.  *  You may obtain a copy of the License at
  79.  *      http://www.apache.org/licenses/LICENSE-2.0
  80.  *  
  81.  *  Unless required by applicable law or agreed to in writing, software
  82.  *      distributed under the License is distributed on an "AS IS" BASIS,
  83.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84.  *  
  85.  *  See the License for the specific language governing permissions and
  86.  *      limitations under the License.
  87.  */

  88. /**
  89.  * <i>Raipur</i> demonstrates EOS Fixed Coupon Multi-flavor Pricing and Relative Value Measure Generation for
  90.  * Raipur.
  91.  *  
  92.  * <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  96.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  97.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/bondeos/README.md">EOS Bond Bullet/Exercise Measures</a></li>
  98.  *  </ul>
  99.  * <br><br>
  100.  *
  101.  * @author Lakshmi Krishnamurthy
  102.  */

  103. public class Raipur {

  104.     private static final MergedDiscountForwardCurve FundingCurve (
  105.         final JulianDate dtSpot,
  106.         final String strCurrency,
  107.         final double dblBump)
  108.         throws Exception
  109.     {
  110.         String[] astrDepositMaturityTenor = new String[] {
  111.             "2D"
  112.         };

  113.         double[] adblDepositQuote = new double[] {
  114.             0.0111956 + dblBump // 2D
  115.         };

  116.         double[] adblFuturesQuote = new double[] {
  117.             0.011375 + dblBump, // 98.8625
  118.             0.013350 + dblBump, // 98.6650
  119.             0.014800 + dblBump, // 98.5200
  120.             0.016450 + dblBump, // 98.3550
  121.             0.017850 + dblBump, // 98.2150
  122.             0.019300 + dblBump  // 98.0700
  123.         };

  124.         String[] astrFixFloatMaturityTenor = new String[] {
  125.             "02Y",
  126.             "03Y",
  127.             "04Y",
  128.             "05Y",
  129.             "06Y",
  130.             "07Y",
  131.             "08Y",
  132.             "09Y",
  133.             "10Y",
  134.             "11Y",
  135.             "12Y",
  136.             "15Y",
  137.             "20Y",
  138.             "25Y",
  139.             "30Y",
  140.             "40Y",
  141.             "50Y"
  142.         };

  143.         double[] adblFixFloatQuote = new double[] {
  144.             0.017029 + dblBump, //  2Y
  145.             0.019354 + dblBump, //  3Y
  146.             0.021044 + dblBump, //  4Y
  147.             0.022291 + dblBump, //  5Y
  148.             0.023240 + dblBump, //  6Y
  149.             0.024025 + dblBump, //  7Y
  150.             0.024683 + dblBump, //  8Y
  151.             0.025243 + dblBump, //  9Y
  152.             0.025720 + dblBump, // 10Y
  153.             0.026130 + dblBump, // 11Y
  154.             0.026495 + dblBump, // 12Y
  155.             0.027230 + dblBump, // 15Y
  156.             0.027855 + dblBump, // 20Y
  157.             0.028025 + dblBump, // 25Y
  158.             0.028028 + dblBump, // 30Y
  159.             0.027902 + dblBump, // 40Y
  160.             0.027655 + dblBump  // 50Y
  161.         };

  162.         return LatentMarketStateBuilder.SmoothFundingCurve (
  163.             dtSpot,
  164.             strCurrency,
  165.             astrDepositMaturityTenor,
  166.             adblDepositQuote,
  167.             "ForwardRate",
  168.             adblFuturesQuote,
  169.             "ForwardRate",
  170.             astrFixFloatMaturityTenor,
  171.             adblFixFloatQuote,
  172.             "SwapRate"
  173.         );
  174.     }

  175.     private static final GovvieCurve GovvieCurve (
  176.         final JulianDate dtSpot,
  177.         final String strCode,
  178.         final double[] adblCoupon,
  179.         final double[] adblYield)
  180.         throws Exception
  181.     {
  182.         JulianDate[] adtEffective = new JulianDate[] {
  183.             dtSpot,
  184.             dtSpot,
  185.             dtSpot,
  186.             dtSpot,
  187.             dtSpot,
  188.             dtSpot,
  189.             dtSpot,
  190.             dtSpot
  191.         };

  192.         JulianDate[] adtMaturity = new JulianDate[] {
  193.             dtSpot.addTenor ("1Y"),
  194.             dtSpot.addTenor ("2Y"),
  195.             dtSpot.addTenor ("3Y"),
  196.             dtSpot.addTenor ("5Y"),
  197.             dtSpot.addTenor ("7Y"),
  198.             dtSpot.addTenor ("10Y"),
  199.             dtSpot.addTenor ("20Y"),
  200.             dtSpot.addTenor ("30Y")
  201.         };

  202.         return LatentMarketStateBuilder.GovvieCurve (
  203.             strCode,
  204.             dtSpot,
  205.             adtEffective,
  206.             adtMaturity,
  207.             adblCoupon,
  208.             adblYield,
  209.             "Yield",
  210.             LatentMarketStateBuilder.SHAPE_PRESERVING
  211.         );
  212.     }

  213.     private static final void RVMeasures (
  214.         final BondComponent bond,
  215.         final JulianDate dtValue,
  216.         final CurveSurfaceQuoteContainer csqc,
  217.         final double dblCleanPrice)
  218.         throws Exception
  219.     {
  220.         JulianDate dtSettle = dtValue.addBusDays (
  221.             0,
  222.             bond.currency()
  223.         );

  224.         ValuationParams valParams = new ValuationParams (
  225.             dtValue,
  226.             dtSettle,
  227.             bond.currency()
  228.         );

  229.         System.out.println();

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

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

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

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

  234.         System.out.println();

  235.         double dblYTM = Double.NaN;
  236.         double dblYTW = Double.NaN;
  237.         double dblOASTM = Double.NaN;
  238.         double dblOASTW = Double.NaN;
  239.         double dblWALTM = Double.NaN;
  240.         double dblWALTW = Double.NaN;
  241.         double dblZSpreadTM = Double.NaN;
  242.         double dblZSpreadTW = Double.NaN;
  243.         double dblOASDurationTW = Double.NaN;
  244.         double dblModifiedDurationTM = Double.NaN;
  245.         double dblModifiedDurationTW = Double.NaN;

  246.         WorkoutInfo wi = bond.exerciseYieldFromPrice (
  247.             valParams,
  248.             csqc,
  249.             null,
  250.             dblCleanPrice
  251.         );

  252.         try {
  253.             dblYTW = wi.yield();

  254.             dblYTM = bond.yieldFromPrice (
  255.                 valParams,
  256.                 csqc,
  257.                 null,
  258.                 bond.maturityDate().julian(),
  259.                 1.,
  260.                 dblCleanPrice
  261.             );

  262.             dblWALTW = bond.weightedAverageLife (
  263.                 valParams,
  264.                 csqc,
  265.                 wi.date(),
  266.                 wi.factor()
  267.             );

  268.             dblWALTM = bond.weightedAverageLife (
  269.                 valParams,
  270.                 csqc,
  271.                 bond.maturityDate().julian(),
  272.                 1.
  273.             );

  274.             dblZSpreadTM = bond.zSpreadFromYield (
  275.                 valParams,
  276.                 csqc,
  277.                 null,
  278.                 bond.maturityDate().julian(),
  279.                 1.,
  280.                 dblYTM
  281.             );

  282.             dblZSpreadTW = bond.zSpreadFromYield (
  283.                 valParams,
  284.                 csqc,
  285.                 null,
  286.                 wi.date(),
  287.                 wi.factor(),
  288.                 wi.yield()
  289.             );

  290.             dblOASTM = bond.oasFromYield (
  291.                 valParams,
  292.                 csqc,
  293.                 null,
  294.                 wi.date(),
  295.                 wi.factor(),
  296.                 dblYTM
  297.             );

  298.             dblOASTW = bond.oasFromYield (
  299.                 valParams,
  300.                 csqc,
  301.                 null,
  302.                 wi.date(),
  303.                 wi.factor(),
  304.                 wi.yield()
  305.             );

  306.             dblOASDurationTW = (
  307.                 dblCleanPrice - bond.priceFromOAS (
  308.                     valParams,
  309.                     csqc,
  310.                     null,
  311.                     wi.date(),
  312.                     wi.factor(),
  313.                     dblOASTW + 0.0001
  314.                 )
  315.             ) / dblCleanPrice;

  316.             dblModifiedDurationTM = bond.modifiedDurationFromPrice (
  317.                 valParams,
  318.                 csqc,
  319.                 null,
  320.                 bond.maturityDate().julian(),
  321.                 1.,
  322.                 dblCleanPrice
  323.             );

  324.             dblModifiedDurationTW = bond.modifiedDurationFromPrice (
  325.                 valParams,
  326.                 csqc,
  327.                 null,
  328.                 wi.date(),
  329.                 wi.factor(),
  330.                 dblCleanPrice
  331.             );
  332.         } catch (Exception e) {
  333.             // e.printStackTrace();
  334.         }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  353.     public static final void main (
  354.         final String[] astrArgs)
  355.         throws Exception
  356.     {
  357.         EnvManager.InitEnv (
  358.             "",
  359.             true
  360.         );

  361.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  362.             2017,
  363.             DateUtil.MARCH,
  364.             24
  365.         );

  366.         String strCurrency = "USD";
  367.         String strTreasuryCode = "UST";

  368.         double[] adblTreasuryCoupon = new double[] {
  369.             0.0100,
  370.             0.0100,
  371.             0.0125,
  372.             0.0150,
  373.             0.0200,
  374.             0.0225,
  375.             0.0250,
  376.             0.0300
  377.         };

  378.         double[] adblTreasuryYield = new double[] {
  379.             0.0083, //  1Y
  380.             0.0122, //  2Y
  381.             0.0149, //  3Y
  382.             0.0193, //  5Y
  383.             0.0227, //  7Y
  384.             0.0248, // 10Y
  385.             0.0280, // 20Y
  386.             0.0308  // 30Y
  387.         };

  388.         JulianDate dtEffective = DateUtil.CreateFromYMD (2014,  7, 24);
  389.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2034,  7, 24);
  390.         double dblCoupon = 0.0399;
  391.         double dblCleanPrice = 1.00719;
  392.         int iFreq = 2;
  393.         String strCUSIP = "Raipur";
  394.         String strDayCount = "30/360";
  395.         int[] aiExerciseDate = new int[] {
  396.             DateUtil.CreateFromYMD (2017,  7, 24).julian(),
  397.         };
  398.         double[] adblExercisePrice = new double[] {
  399.             1.,
  400.         };

  401.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  402.             strCUSIP,
  403.             strCurrency,
  404.             "",
  405.             dblCoupon,
  406.             iFreq,
  407.             strDayCount,
  408.             dtEffective,
  409.             dtMaturity,
  410.             null,
  411.             null
  412.         );

  413.         EmbeddedOptionSchedule eos = new EmbeddedOptionSchedule (
  414.             aiExerciseDate,
  415.             adblExercisePrice,
  416.             false,
  417.             30,
  418.             false,
  419.             Double.NaN,
  420.             "",
  421.             Double.NaN
  422.         );

  423.         bond.setEmbeddedCallSchedule (eos);

  424.         RVMeasures (
  425.             bond,
  426.             dtSpot,
  427.             MarketParamsBuilder.Create (
  428.                 FundingCurve (
  429.                     dtSpot,
  430.                     strCurrency,
  431.                     0.
  432.                 ),
  433.                 GovvieCurve (
  434.                     dtSpot,
  435.                     strTreasuryCode,
  436.                     adblTreasuryCoupon,
  437.                     adblTreasuryYield
  438.                 ),
  439.                 null,
  440.                 null,
  441.                 null,
  442.                 null,
  443.                 null
  444.             ),
  445.             dblCleanPrice
  446.         );

  447.         EnvManager.TerminateEnv();
  448.     }
  449. }