FixedCouponKeyRateDuration.java

  1. package org.drip.sample.bondapi;

  2. import java.util.*;

  3. import org.drip.analytics.date.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.service.product.FixedBondAPI;

  7. /*
  8.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  9.  */

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

  82. /**
  83.  * <i>FixedCouponKeyRateDuration</i> demonstrates the Invocation and Examination of the Key Rate Duration
  84.  * Computation for the Specified Treasury Futures.
  85.  *  
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  90.  *      <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>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/bondapi/README.md">Fixed Coupon KRD + RV Measures</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class FixedCouponKeyRateDuration {

  98.     private static void ComputeKeyRateDuration (
  99.         final java.lang.String strIssuerName,
  100.         final JulianDate dtBondEffective,
  101.         final JulianDate dtBondMaturity,
  102.         final double dblBondCoupon,
  103.         final int iBondCouponFrequency,
  104.         final java.lang.String strBondCouponDayCount,
  105.         final java.lang.String strBondCouponCurrency,
  106.         final int iSpotDate,
  107.         final java.lang.String strGovvieCode,
  108.         final int[] aiGovvieCurveTreasuryEffectiveDate,
  109.         final int[] aiGovvieCurveTreasuryMaturityDate,
  110.         final double[] adblGovvieCurveTreasuryCoupon,
  111.         final double[] adblGovvieCurveTreasuryYield,
  112.         final java.lang.String strGovvieCurveTreasuryMeasure,
  113.         final double dblBondMarketCleanPrice,
  114.         final boolean bHeader,
  115.         final boolean bTrailer)
  116.         throws Exception
  117.     {
  118.         Map<String, Double> mapKeyRateDuration = FixedBondAPI.KeyRateDuration (
  119.             strIssuerName,
  120.             dtBondEffective.julian(),
  121.             dtBondMaturity.julian(),
  122.             dblBondCoupon,
  123.             iBondCouponFrequency,
  124.             strBondCouponDayCount,
  125.             strBondCouponCurrency,
  126.             iSpotDate,
  127.             strGovvieCode,
  128.             aiGovvieCurveTreasuryEffectiveDate,
  129.             aiGovvieCurveTreasuryMaturityDate,
  130.             adblGovvieCurveTreasuryCoupon,
  131.             adblGovvieCurveTreasuryYield,
  132.             strGovvieCurveTreasuryMeasure,
  133.             dblBondMarketCleanPrice
  134.         );

  135.         if (bHeader) {
  136.             System.out.println ("\n\t|------------------------------------------------------------------------------------------------------------------------------------------------------||");

  137.             System.out.print ("\t|          ISSUE         |");

  138.             Set<String> setstrKey = mapKeyRateDuration.keySet();

  139.             for (String strKey : setstrKey)
  140.                 System.out.print (" " + strKey + " |");

  141.             System.out.println ("|");

  142.             System.out.println ("\t|------------------------------------------------------------------------------------------------------------------------------------------------------||");
  143.         }

  144.         System.out.print ("\t|  " + strIssuerName + FormatUtil.FormatDouble (dblBondCoupon, 1, 4, 100.) + " " + dtBondMaturity + " |");

  145.         for (Map.Entry<String, Double> me : mapKeyRateDuration.entrySet())
  146.             System.out.print ("     " + FormatUtil.FormatDouble (-1. * me.getValue(), 1, 5, 1.) + "    |");

  147.         System.out.println ("|");

  148.         if (bTrailer)
  149.             System.out.println ("\t|------------------------------------------------------------------------------------------------------------------------------------------------------||");
  150.     }

  151.     public static final void main (
  152.         final String[] args)
  153.         throws Exception
  154.     {
  155.         EnvManager.InitEnv (
  156.             "",
  157.             true
  158.         );

  159.         int iSpotDate = DateUtil.CreateFromYMD (
  160.             2015,
  161.             DateUtil.NOVEMBER,
  162.             18
  163.         ).julian();

  164.         int[] aiGovvieCurveTreasuryEffectiveDate = new int[] {
  165.             iSpotDate,
  166.             iSpotDate,
  167.             iSpotDate,
  168.             iSpotDate,
  169.             iSpotDate,
  170.             iSpotDate,
  171.             iSpotDate
  172.         };

  173.         int[] aiGovvieCurveTreasuryMaturityDate = new int[] {
  174.             new JulianDate (iSpotDate).addTenor ("1Y").julian(),
  175.             new JulianDate (iSpotDate).addTenor ("2Y").julian(),
  176.             new JulianDate (iSpotDate).addTenor ("3Y").julian(),
  177.             new JulianDate (iSpotDate).addTenor ("5Y").julian(),
  178.             new JulianDate (iSpotDate).addTenor ("7Y").julian(),
  179.             new JulianDate (iSpotDate).addTenor ("10Y").julian(),
  180.             new JulianDate (iSpotDate).addTenor ("30Y").julian()
  181.         };

  182.         double[] adblGovvieCurveTreasuryCoupon = new double[] {
  183.             0.0100,
  184.             0.0100,
  185.             0.0125,
  186.             0.0150,
  187.             0.0200,
  188.             0.0225,
  189.             0.0300
  190.         };

  191.         double[] adblGovvieCurveTreasuryYield = new double[] {
  192.             0.00692,
  193.             0.00945,
  194.             0.01257,
  195.             0.01678,
  196.             0.02025,
  197.             0.02235,
  198.             0.02972
  199.         };

  200.         String strGovvieCurveTreasuryMeasure = "Yield";

  201.         String strIssuerName = "AEG";
  202.         int iBondCouponFrequency = 2;
  203.         String strBondCouponDayCount = "30/360";
  204.         String strBondCouponCurrency = "USD";
  205.         String strGovvieCode = "UST";

  206.         ComputeKeyRateDuration (
  207.             strIssuerName,
  208.             DateUtil.CreateFromYMD (2007, 12, 20),
  209.             DateUtil.CreateFromYMD (2018,  1, 15),
  210.             0.06000,
  211.             iBondCouponFrequency,
  212.             strBondCouponDayCount,
  213.             strBondCouponCurrency,
  214.             iSpotDate,
  215.             strGovvieCode,
  216.             aiGovvieCurveTreasuryEffectiveDate,
  217.             aiGovvieCurveTreasuryMaturityDate,
  218.             adblGovvieCurveTreasuryCoupon,
  219.             adblGovvieCurveTreasuryYield,
  220.             strGovvieCurveTreasuryMeasure,
  221.             1.08529,
  222.             true,
  223.             false
  224.         );

  225.         ComputeKeyRateDuration (
  226.             strIssuerName,
  227.             DateUtil.CreateFromYMD (1996,  7, 25),
  228.             DateUtil.CreateFromYMD (2025,  7, 15),
  229.             0.07750,
  230.             iBondCouponFrequency,
  231.             strBondCouponDayCount,
  232.             strBondCouponCurrency,
  233.             iSpotDate,
  234.             strGovvieCode,
  235.             aiGovvieCurveTreasuryEffectiveDate,
  236.             aiGovvieCurveTreasuryMaturityDate,
  237.             adblGovvieCurveTreasuryCoupon,
  238.             adblGovvieCurveTreasuryYield,
  239.             strGovvieCurveTreasuryMeasure,
  240.             1.27021,
  241.             false,
  242.             false
  243.         );

  244.         ComputeKeyRateDuration (
  245.             strIssuerName,
  246.             DateUtil.CreateFromYMD (1996, 10, 29),
  247.             DateUtil.CreateFromYMD (2026, 10, 15),
  248.             0.07625,
  249.             iBondCouponFrequency,
  250.             strBondCouponDayCount,
  251.             strBondCouponCurrency,
  252.             iSpotDate,
  253.             strGovvieCode,
  254.             aiGovvieCurveTreasuryEffectiveDate,
  255.             aiGovvieCurveTreasuryMaturityDate,
  256.             adblGovvieCurveTreasuryCoupon,
  257.             adblGovvieCurveTreasuryYield,
  258.             strGovvieCurveTreasuryMeasure,
  259.             1.27274,
  260.             false,
  261.             false
  262.         );

  263.         ComputeKeyRateDuration (
  264.             strIssuerName,
  265.             DateUtil.CreateFromYMD (2014, 12,  9),
  266.             DateUtil.CreateFromYMD (2026, 12, 15),
  267.             0.04125,
  268.             iBondCouponFrequency,
  269.             strBondCouponDayCount,
  270.             strBondCouponCurrency,
  271.             iSpotDate,
  272.             strGovvieCode,
  273.             aiGovvieCurveTreasuryEffectiveDate,
  274.             aiGovvieCurveTreasuryMaturityDate,
  275.             adblGovvieCurveTreasuryCoupon,
  276.             adblGovvieCurveTreasuryYield,
  277.             strGovvieCurveTreasuryMeasure,
  278.             1.01235,
  279.             false,
  280.             false
  281.         );

  282.         ComputeKeyRateDuration (
  283.             strIssuerName,
  284.             DateUtil.CreateFromYMD (1997,  4, 29),
  285.             DateUtil.CreateFromYMD (2027,  4, 29),
  286.             0.08000,
  287.             iBondCouponFrequency,
  288.             strBondCouponDayCount,
  289.             strBondCouponCurrency,
  290.             iSpotDate,
  291.             strGovvieCode,
  292.             aiGovvieCurveTreasuryEffectiveDate,
  293.             aiGovvieCurveTreasuryMaturityDate,
  294.             adblGovvieCurveTreasuryCoupon,
  295.             adblGovvieCurveTreasuryYield,
  296.             strGovvieCurveTreasuryMeasure,
  297.             1.31527,
  298.             false,
  299.             false
  300.         );

  301.         ComputeKeyRateDuration (
  302.             strIssuerName,
  303.             DateUtil.CreateFromYMD (2014,  9, 25),
  304.             DateUtil.CreateFromYMD (2027, 10,  1),
  305.             0.04250,
  306.             iBondCouponFrequency,
  307.             strBondCouponDayCount,
  308.             strBondCouponCurrency,
  309.             iSpotDate,
  310.             strGovvieCode,
  311.             aiGovvieCurveTreasuryEffectiveDate,
  312.             aiGovvieCurveTreasuryMaturityDate,
  313.             adblGovvieCurveTreasuryCoupon,
  314.             adblGovvieCurveTreasuryYield,
  315.             strGovvieCurveTreasuryMeasure,
  316.             1.02263,
  317.             false,
  318.             false
  319.         );

  320.         ComputeKeyRateDuration (
  321.             strIssuerName,
  322.             DateUtil.CreateFromYMD (2008,  5, 22),
  323.             DateUtil.CreateFromYMD (2038,  5, 15),
  324.             0.06400,
  325.             iBondCouponFrequency,
  326.             strBondCouponDayCount,
  327.             strBondCouponCurrency,
  328.             iSpotDate,
  329.             strGovvieCode,
  330.             aiGovvieCurveTreasuryEffectiveDate,
  331.             aiGovvieCurveTreasuryMaturityDate,
  332.             adblGovvieCurveTreasuryCoupon,
  333.             adblGovvieCurveTreasuryYield,
  334.             strGovvieCurveTreasuryMeasure,
  335.             1.27570,
  336.             false,
  337.             false
  338.         );

  339.         ComputeKeyRateDuration (
  340.             strIssuerName,
  341.             DateUtil.CreateFromYMD (2011,  7, 21),
  342.             DateUtil.CreateFromYMD (2041,  7, 15),
  343.             0.05600,
  344.             iBondCouponFrequency,
  345.             strBondCouponDayCount,
  346.             strBondCouponCurrency,
  347.             iSpotDate,
  348.             strGovvieCode,
  349.             aiGovvieCurveTreasuryEffectiveDate,
  350.             aiGovvieCurveTreasuryMaturityDate,
  351.             adblGovvieCurveTreasuryCoupon,
  352.             adblGovvieCurveTreasuryYield,
  353.             strGovvieCurveTreasuryMeasure,
  354.             1.17460,
  355.             false,
  356.             true
  357.         );

  358.         EnvManager.TerminateEnv();
  359.     }
  360. }