Changde.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.definition.*;
  10. import org.drip.product.params.EmbeddedOptionSchedule;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.service.template.*;
  13. import org.drip.state.discount.MergedDiscountForwardCurve;
  14. import org.drip.state.govvie.GovvieCurve;

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

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

  89. /**
  90.  * <i>Changde</i> demonstrates EOS Fixed Coupon Multi-flavor Pricing and Relative Value Measure Generation
  91.  * for Changde.
  92.  *  
  93.  * <br><br>
  94.  *  <ul>
  95.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  96.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  97.  *      <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>
  98.  *      <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>
  99.  *  </ul>
  100.  * <br><br>
  101.  *
  102.  * @author Lakshmi Krishnamurthy
  103.  */

  104. public class Changde {

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

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

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

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

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

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

  175.         Component[] aDepositComp = OTCInstrumentBuilder.FundingDeposit (
  176.             dtSpot,
  177.             strCurrency,
  178.             astrDepositMaturityTenor
  179.         );

  180.         Component[] aFuturesComp = ExchangeInstrumentBuilder.ForwardRateFuturesPack (
  181.             dtSpot,
  182.             adblFuturesQuote.length,
  183.             strCurrency
  184.         );

  185.         Component[] aFixFloatComp = OTCInstrumentBuilder.FixFloatStandard (
  186.             dtSpot,
  187.             strCurrency,
  188.             "ALL",
  189.             astrFixFloatMaturityTenor,
  190.             "MAIN",
  191.             0.
  192.         );

  193.         ValuationParams valParams = new ValuationParams (
  194.             dtSpot,
  195.             dtSpot,
  196.             strCurrency
  197.         );

  198.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Create (
  199.             dcFunding,
  200.             null,
  201.             null,
  202.             null,
  203.             null,
  204.             null,
  205.             null
  206.         );

  207.         System.out.println();

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

  209.         System.out.println ("\t|        DEPOSIT INPUT vs. CALC       ||");

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

  211.         for (int i = 0; i < aDepositComp.length; ++i)
  212.             System.out.println ("\t| [" + aDepositComp[i].maturityDate() + "] =" +
  213.                 FormatUtil.FormatDouble (aDepositComp[i].measureValue (
  214.                     valParams,
  215.                     null,
  216.                     csqc,
  217.                     null,
  218.                     "ForwardRate"
  219.                 ), 1, 6, 1.) + " |" +
  220.                 FormatUtil.FormatDouble (adblDepositQuote[i], 1, 6, 1.) + " ||"
  221.             );

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

  223.         System.out.println();

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

  225.         System.out.println ("\t|        FUTURES INPUT vs. CALC       ||");

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

  227.         for (int i = 0; i < aFuturesComp.length; ++i)
  228.             System.out.println ("\t| [" + aFuturesComp[i].maturityDate() + "] =" +
  229.                 FormatUtil.FormatDouble (aFuturesComp[i].measureValue (
  230.                     valParams,
  231.                     null,
  232.                     csqc,
  233.                     null,
  234.                     "ForwardRate"
  235.                 ), 1, 6, 1.) + " |" +
  236.                 FormatUtil.FormatDouble (adblFuturesQuote[i], 1, 6, 1.) + " ||"
  237.             );

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

  239.         System.out.println();

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

  241.         System.out.println ("\t|          FIX-FLOAT INPUTS vs CALIB             ||");

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

  243.         for (int i = 0; i < aFixFloatComp.length; ++i)
  244.             System.out.println ("\t| [" + aFixFloatComp[i].maturityDate() + "] =" +
  245.                 FormatUtil.FormatDouble (aFixFloatComp[i].measureValue (
  246.                     valParams,
  247.                     null,
  248.                     csqc,
  249.                     null,
  250.                     "CalibSwapRate"
  251.                 ), 1, 6, 1.) + " |" +
  252.                 FormatUtil.FormatDouble (adblFixFloatQuote[i], 1, 6, 1.) + " |" +
  253.                 FormatUtil.FormatDouble (aFixFloatComp[i].measureValue (
  254.                     valParams,
  255.                     null,
  256.                     csqc,
  257.                     null,
  258.                     "FairPremium"
  259.                 ), 1, 6, 1.) + " ||"
  260.             );

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

  262.         System.out.println();

  263.         return dcFunding;
  264.     }

  265.     private static final GovvieCurve GovvieCurve (
  266.         final JulianDate dtSpot,
  267.         final String strCode,
  268.         final double[] adblCoupon,
  269.         final double[] adblYield)
  270.         throws Exception
  271.     {
  272.         JulianDate[] adtEffective = new JulianDate[] {
  273.             dtSpot,
  274.             dtSpot,
  275.             dtSpot,
  276.             dtSpot,
  277.             dtSpot,
  278.             dtSpot,
  279.             dtSpot,
  280.             dtSpot
  281.         };

  282.         JulianDate[] adtMaturity = new JulianDate[] {
  283.             dtSpot.addTenor ("1Y"),
  284.             dtSpot.addTenor ("2Y"),
  285.             dtSpot.addTenor ("3Y"),
  286.             dtSpot.addTenor ("5Y"),
  287.             dtSpot.addTenor ("7Y"),
  288.             dtSpot.addTenor ("10Y"),
  289.             dtSpot.addTenor ("20Y"),
  290.             dtSpot.addTenor ("30Y")
  291.         };

  292.         GovvieCurve gc = LatentMarketStateBuilder.GovvieCurve (
  293.             strCode,
  294.             dtSpot,
  295.             adtEffective,
  296.             adtMaturity,
  297.             adblCoupon,
  298.             adblYield,
  299.             "Yield",
  300.             LatentMarketStateBuilder.SHAPE_PRESERVING
  301.         );

  302.         BondComponent[] aComp = TreasuryBuilder.FromCode (
  303.             strCode,
  304.             adtEffective,
  305.             adtMaturity,
  306.             adblCoupon
  307.         );

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

  309.         CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  310.         csqc.setGovvieState (gc);

  311.         System.out.println();

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

  313.         System.out.println ("\t|       TREASURY INPUT vs CALIB YIELD       ||");

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

  315.         for (int i = 0; i < aComp.length; ++i)
  316.             System.out.println ("\t| " + aComp[i].name() + " | " +
  317.                 FormatUtil.FormatDouble (adblYield[i], 1, 3, 100.) + "% | " +
  318.                 FormatUtil.FormatDouble (aComp[i].yieldFromPrice (
  319.                     valParams,
  320.                     null,
  321.                     null,
  322.                     aComp[i].maturityDate().julian(),
  323.                     1.,
  324.                     aComp[i].priceFromYield (
  325.                         valParams,
  326.                         null,
  327.                         null,
  328.                         gc.yield (aComp[i].maturityDate().julian())
  329.                     )
  330.                 ), 1, 3, 100.) + "% ||"
  331.             );

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

  333.         return gc;
  334.     }

  335.     private static final void RVMeasures (
  336.         final BondComponent bond,
  337.         final JulianDate dtValue,
  338.         final CurveSurfaceQuoteContainer csqc,
  339.         final double dblCleanPrice)
  340.         throws Exception
  341.     {
  342.         JulianDate dtSettle = dtValue.addBusDays (
  343.             3,
  344.             bond.currency()
  345.         );

  346.         ValuationParams valParams = new ValuationParams (
  347.             dtValue,
  348.             dtSettle,
  349.             bond.currency()
  350.         );

  351.         System.out.println();

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

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

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

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

  356.         System.out.println();

  357.         double dblYTM = Double.NaN;
  358.         double dblYTW = Double.NaN;
  359.         double dblOASTW = Double.NaN;
  360.         double dblWALTM = Double.NaN;
  361.         double dblWALTW = Double.NaN;
  362.         double dblZSpreadTW = Double.NaN;
  363.         double dblModifiedDurationTW = Double.NaN;

  364.         WorkoutInfo wi = bond.exerciseYieldFromPrice (
  365.             valParams,
  366.             csqc,
  367.             null,
  368.             dblCleanPrice
  369.         );

  370.         try {
  371.             dblYTW = wi.yield();

  372.             dblYTM = bond.yieldFromPrice (
  373.                 valParams,
  374.                 csqc,
  375.                 null,
  376.                 bond.maturityDate().julian(),
  377.                 1.,
  378.                 dblCleanPrice
  379.             );

  380.             dblWALTW = bond.weightedAverageLife (
  381.                 valParams,
  382.                 csqc,
  383.                 wi.date(),
  384.                 wi.factor()
  385.             );

  386.             dblWALTM = bond.weightedAverageLife (
  387.                 valParams,
  388.                 csqc,
  389.                 bond.maturityDate().julian(),
  390.                 1.
  391.             );

  392.             dblZSpreadTW = bond.zSpreadFromYield (
  393.                 valParams,
  394.                 csqc,
  395.                 null,
  396.                 wi.date(),
  397.                 wi.factor(),
  398.                 wi.yield()
  399.             );

  400.             dblOASTW = bond.oasFromYield (
  401.                 valParams,
  402.                 csqc,
  403.                 null,
  404.                 wi.date(),
  405.                 wi.factor(),
  406.                 wi.yield()
  407.             );

  408.             dblModifiedDurationTW = bond.modifiedDurationFromPrice (
  409.                 valParams,
  410.                 csqc,
  411.                 null,
  412.                 wi.date(),
  413.                 wi.factor(),
  414.                 dblCleanPrice
  415.             );
  416.         } catch (Exception e) {
  417.             // e.printStackTrace();
  418.         }

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

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

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

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

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

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

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

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

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

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

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

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

  431.         System.out.println ("\t Bond OAS TW               => " + FormatUtil.FormatDouble (dblOASTW, 1, 1, 10000.));
  432.     }

  433.     public static final void main (
  434.         final String[] astrArgs)
  435.         throws Exception
  436.     {
  437.         EnvManager.InitEnv (
  438.             "",
  439.             true
  440.         );

  441.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  442.             2017,
  443.             DateUtil.MARCH,
  444.             10
  445.         );

  446.         String strCurrency = "USD";
  447.         String strTreasuryCode = "UST";

  448.         double[] adblTreasuryCoupon = new double[] {
  449.             0.0100,
  450.             0.0100,
  451.             0.0125,
  452.             0.0150,
  453.             0.0200,
  454.             0.0225,
  455.             0.0250,
  456.             0.0300
  457.         };

  458.         double[] adblTreasuryYield = new double[] {
  459.             0.0083, //  1Y
  460.             0.0122, //  2Y
  461.             0.0149, //  3Y
  462.             0.0193, //  5Y
  463.             0.0227, //  7Y
  464.             0.0248, // 10Y
  465.             0.0280, // 20Y
  466.             0.0308  // 30Y
  467.         };

  468.         JulianDate dtEffective = DateUtil.CreateFromYMD (2014,  9, 12);
  469.         JulianDate dtMaturity  = DateUtil.CreateFromYMD (2024, 10, 15);
  470.         double dblCoupon = 0.04350;
  471.         double dblCleanPrice = 1.0138900;
  472.         int iFreq = 2;
  473.         String strCUSIP = "Changde";
  474.         String strDayCount = "30/360";
  475.         int[] aiExerciseDate = new int[] {
  476.             DateUtil.CreateFromYMD (2024,  7, 15).julian(),
  477.         };
  478.         double[] adblExercisePrice = new double[] {
  479.             1.,
  480.         };

  481.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  482.             strCUSIP,
  483.             strCurrency,
  484.             "",
  485.             dblCoupon,
  486.             iFreq,
  487.             strDayCount,
  488.             dtEffective,
  489.             dtMaturity,
  490.             null,
  491.             null
  492.         );

  493.         EmbeddedOptionSchedule eos = new EmbeddedOptionSchedule (
  494.             aiExerciseDate,
  495.             adblExercisePrice,
  496.             false,
  497.             30,
  498.             false,
  499.             Double.NaN,
  500.             "",
  501.             Double.NaN
  502.         );

  503.         bond.setEmbeddedCallSchedule (eos);

  504.         RVMeasures (
  505.             bond,
  506.             dtSpot,
  507.             MarketParamsBuilder.Create (
  508.                 FundingCurve (
  509.                     dtSpot,
  510.                     strCurrency,
  511.                     0.
  512.                 ),
  513.                 GovvieCurve (
  514.                     dtSpot,
  515.                     strTreasuryCode,
  516.                     adblTreasuryCoupon,
  517.                     adblTreasuryYield
  518.                 ),
  519.                 null,
  520.                 null,
  521.                 null,
  522.                 null,
  523.                 null
  524.             ),
  525.             dblCleanPrice
  526.         );

  527.         EnvManager.TerminateEnv();
  528.     }
  529. }