WN1KeyRateDuration.java

  1. package org.drip.sample.treasuryfuturesrisk;

  2. import java.util.*;

  3. import org.drip.analytics.date.JulianDate;
  4. import org.drip.feed.loader.*;
  5. import org.drip.historical.sensitivity.TenorDurationNodeMetrics;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.service.product.TreasuryFuturesAPI;

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

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

  54. /**
  55.  * WN1KeyRateDuration demonstrates the Computation of the Key Rate Duration for the WN1 Treasury Futures.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class WN1KeyRateDuration {

  60.     public static final void main (
  61.         final String[] astrArgs)
  62.         throws Exception
  63.     {
  64.         EnvManager.InitEnv ("");

  65.         String strTreasuryCode = "WN1";
  66.         String strTreasuryMarkLocation = "C:\\DRIP\\CreditAnalytics\\Daemons\\Transforms\\TreasuryYieldMarks\\USTBenchmarksReconstituted.csv";
  67.         String strPrintLocation = "C:\\DRIP\\CreditAnalytics\\Daemons\\Transforms\\TreasuryFuturesCloses\\" +
  68.             strTreasuryCode + "ClosesReconstitutor.csv";

  69.         CSVGrid csvGridTreasuryMark = CSVParser.StringGrid (
  70.             strTreasuryMarkLocation,
  71.             true
  72.         );

  73.         JulianDate[] adtTreasuryMark = csvGridTreasuryMark.dateArrayAtColumn (0);

  74.         double[] adblYieldUST02Y = csvGridTreasuryMark.doubleArrayAtColumn (1);

  75.         double[] adblYieldUST03Y = csvGridTreasuryMark.doubleArrayAtColumn (2);

  76.         double[] adblYieldUST04Y = csvGridTreasuryMark.doubleArrayAtColumn (3);

  77.         double[] adblYieldUST05Y = csvGridTreasuryMark.doubleArrayAtColumn (4);

  78.         double[] adblYieldUST07Y = csvGridTreasuryMark.doubleArrayAtColumn (5);

  79.         double[] adblYieldUST10Y = csvGridTreasuryMark.doubleArrayAtColumn (6);

  80.         double[] adblYieldUST20Y = csvGridTreasuryMark.doubleArrayAtColumn (7);

  81.         double[] adblYieldUST30Y = csvGridTreasuryMark.doubleArrayAtColumn (8);

  82.         Map<JulianDate, Double> mapTreasuryMark02Y = new TreeMap<JulianDate, Double>();

  83.         Map<JulianDate, Double> mapTreasuryMark03Y = new TreeMap<JulianDate, Double>();

  84.         Map<JulianDate, Double> mapTreasuryMark04Y = new TreeMap<JulianDate, Double>();

  85.         Map<JulianDate, Double> mapTreasuryMark05Y = new TreeMap<JulianDate, Double>();

  86.         Map<JulianDate, Double> mapTreasuryMark07Y = new TreeMap<JulianDate, Double>();

  87.         Map<JulianDate, Double> mapTreasuryMark10Y = new TreeMap<JulianDate, Double>();

  88.         Map<JulianDate, Double> mapTreasuryMark20Y = new TreeMap<JulianDate, Double>();

  89.         Map<JulianDate, Double> mapTreasuryMark30Y = new TreeMap<JulianDate, Double>();

  90.         for (int i = 0; i < adtTreasuryMark.length; ++i) {
  91.             mapTreasuryMark02Y.put (adtTreasuryMark[i], adblYieldUST02Y[i]);

  92.             mapTreasuryMark03Y.put (adtTreasuryMark[i], adblYieldUST03Y[i]);

  93.             mapTreasuryMark04Y.put (adtTreasuryMark[i], adblYieldUST04Y[i]);

  94.             mapTreasuryMark05Y.put (adtTreasuryMark[i], adblYieldUST05Y[i]);

  95.             mapTreasuryMark07Y.put (adtTreasuryMark[i], adblYieldUST07Y[i]);

  96.             mapTreasuryMark10Y.put (adtTreasuryMark[i], adblYieldUST10Y[i]);

  97.             mapTreasuryMark20Y.put (adtTreasuryMark[i], adblYieldUST20Y[i]);

  98.             mapTreasuryMark30Y.put (adtTreasuryMark[i], adblYieldUST30Y[i]);
  99.         }

  100.         CSVGrid csvGrid = CSVParser.StringGrid (
  101.             strPrintLocation,
  102.             true
  103.         );

  104.         JulianDate[] adtSpot = csvGrid.dateArrayAtColumn (0);

  105.         double[] adblCleanPrice = csvGrid.doubleArrayAtColumn (2);

  106.         double[] adblCoupon = csvGrid.doubleArrayAtColumn (3);

  107.         JulianDate[] adtEffective = csvGrid.dateArrayAtColumn (4);

  108.         JulianDate[] adtMaturity = csvGrid.dateArrayAtColumn (5);

  109.         JulianDate[] adtExpiry = csvGrid.dateArrayAtColumn (6);

  110.         int iNumCompute = adtSpot.length;
  111.         JulianDate[] adtEffectiveCompute = new JulianDate[iNumCompute];
  112.         JulianDate[] adtMaturityCompute = new JulianDate[iNumCompute];
  113.         double[] adblCouponCompute = new double[iNumCompute];
  114.         JulianDate[] adtExpiryCompute = new JulianDate[iNumCompute];
  115.         JulianDate[] adtSpotCompute = new JulianDate[iNumCompute];
  116.         double[] adblCleanPriceCompute = new double[iNumCompute];
  117.         double[][] aadblUSTComputeYield = new double[iNumCompute][8];
  118.         String[] astrBenchmarkTenor = new String[] {
  119.             "2Y",
  120.             "3Y",
  121.             "4Y",
  122.             "5Y",
  123.             "7Y",
  124.             "10Y",
  125.             "20Y",
  126.             "30Y"
  127.         };

  128.         for (int i = 0; i < iNumCompute; ++i) {
  129.             adtEffectiveCompute[i] = adtEffective[i];
  130.             adtMaturityCompute[i] = adtMaturity[i];
  131.             adblCouponCompute[i] = adblCoupon[i];
  132.             adtExpiryCompute[i] = adtExpiry[i];
  133.             adtSpotCompute[i] = adtSpot[i];
  134.             adblCleanPriceCompute[i] = adblCleanPrice[i];
  135.             aadblUSTComputeYield[i][0] = adblYieldUST02Y[i];
  136.             aadblUSTComputeYield[i][1] = adblYieldUST03Y[i];
  137.             aadblUSTComputeYield[i][2] = adblYieldUST04Y[i];
  138.             aadblUSTComputeYield[i][3] = adblYieldUST05Y[i];
  139.             aadblUSTComputeYield[i][4] = adblYieldUST07Y[i];
  140.             aadblUSTComputeYield[i][5] = adblYieldUST10Y[i];
  141.             aadblUSTComputeYield[i][6] = adblYieldUST20Y[i];
  142.             aadblUSTComputeYield[i][7] = adblYieldUST30Y[i];
  143.         }

  144.         List<TenorDurationNodeMetrics> lsTDNM = TreasuryFuturesAPI.HorizonKeyRateDuration (
  145.             "UST",
  146.             adtEffectiveCompute,
  147.             adtMaturityCompute,
  148.             adblCouponCompute,
  149.             adtExpiryCompute,
  150.             adtSpotCompute,
  151.             adblCleanPriceCompute,
  152.             astrBenchmarkTenor,
  153.             aadblUSTComputeYield
  154.         );

  155.         System.out.println ("SpotDate,ExpiryDate,CTDName,SpotCTDCleanPrice,ExpiryCTDCleanPrice,SpotGSpread,ExpiryGSpread,SpotYield,ExpiryYield,Parallel,2Y,3Y,4Y,5Y,7Y,10Y,20Y,30Y");

  156.         for (TenorDurationNodeMetrics tdnm : lsTDNM) {
  157.             String strTDNM =
  158.                 tdnm.dateSnap() + "," +
  159.                 tdnm.date ("ExpiryDate") + "," +
  160.                 tdnm.c1 ("CTDName") + "," +
  161.                 FormatUtil.FormatDouble (tdnm.r1 ("SpotCTDCleanPrice"), 1, 5, 100.) + "," +
  162.                 FormatUtil.FormatDouble (tdnm.r1 ("ExpiryCTDCleanPrice"), 1, 5, 100.) + "," +
  163.                 FormatUtil.FormatDouble (tdnm.r1 ("SpotGSpread"), 1, 1, 10000.) + "," +
  164.                 FormatUtil.FormatDouble (tdnm.r1 ("ExpiryGSpread"), 1, 1, 10000.) + "," +
  165.                 FormatUtil.FormatDouble (tdnm.r1 ("SpotYield"), 1, 4, 100.) + "," +
  166.                 FormatUtil.FormatDouble (tdnm.r1 ("ExpiryYield"), 1, 4, 100.) + "," +
  167.                 FormatUtil.FormatDouble (tdnm.r1 ("ParallelKRD"), 1, 4, 1.);

  168.             for (Map.Entry<String, Double> meTDNM : tdnm.krdMap().entrySet())
  169.                 strTDNM += "," + FormatUtil.FormatDouble (meTDNM.getValue(), 1, 4, 1.);

  170.             System.out.println (strTDNM);
  171.         }
  172.     }
  173. }