SplineGovvieCurve.java

  1. package org.drip.sample.govvie;

  2. import org.drip.analytics.date.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.product.credit.BondComponent;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.service.template.TreasuryBuilder;
  7. import org.drip.state.creator.ScenarioGovvieCurveBuilder;
  8. import org.drip.state.govvie.GovvieCurve;

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

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

  73. /**
  74.  * <i>SplineGovvieCurve</i> demonstrates the Construction and Usage of the Spline-based Govvie Curve.
  75.  *  
  76.  * <br><br>
  77.  *  <ul>
  78.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  79.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  80.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  81.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/govvie/README.md">Govvie Curve Builder</a></li>
  82.  *  </ul>
  83.  * <br><br>
  84.  *
  85.  * @author Lakshmi Krishnamurthy
  86.  */

  87. public class SplineGovvieCurve {

  88.     public static void main (
  89.         final String[] astrArgs)
  90.         throws Exception
  91.     {
  92.         EnvManager.InitEnv ("");

  93.         JulianDate dtSpot = DateUtil.Today();

  94.         String strTreasuryCode = "UST";
  95.         String strCurrency = "USD";

  96.         int[] aiMaturityDate = {
  97.             dtSpot.addTenor ("01Y").julian(),
  98.             dtSpot.addTenor ("02Y").julian(),
  99.             dtSpot.addTenor ("03Y").julian(),
  100.             dtSpot.addTenor ("05Y").julian(),
  101.             dtSpot.addTenor ("07Y").julian(),
  102.             dtSpot.addTenor ("10Y").julian(),
  103.             dtSpot.addTenor ("30Y").julian()
  104.         };

  105.         double[] adblYield = {
  106.             0.0113, // "01Y",
  107.             0.0121, // "02Y",
  108.             0.0127, // "03Y",
  109.             0.0137, // "05Y",
  110.             0.0145, // "07Y",
  111.             0.0154, // "10Y"
  112.             0.0198  // "30Y"
  113.         };

  114.         GovvieCurve gc = ScenarioGovvieCurveBuilder.CubicPolynomialCurve (
  115.             strTreasuryCode,
  116.             dtSpot,
  117.             strTreasuryCode,
  118.             strCurrency,
  119.             aiMaturityDate,
  120.             adblYield
  121.         );

  122.         JulianDate[] adtEffective = new JulianDate[] {
  123.             dtSpot,
  124.             dtSpot,
  125.             dtSpot,
  126.             dtSpot,
  127.             dtSpot,
  128.             dtSpot,
  129.             dtSpot
  130.         };

  131.         JulianDate[] adtMaturity = new JulianDate[] {
  132.             dtSpot.addTenor ("1Y"),
  133.             dtSpot.addTenor ("2Y"),
  134.             dtSpot.addTenor ("3Y"),
  135.             dtSpot.addTenor ("5Y"),
  136.             dtSpot.addTenor ("7Y"),
  137.             dtSpot.addTenor ("10Y"),
  138.             dtSpot.addTenor ("30Y")
  139.         };

  140.         BondComponent[] aTreasury = TreasuryBuilder.FromCode (
  141.             strTreasuryCode,
  142.             adtEffective,
  143.             adtMaturity,
  144.             adblYield
  145.         );

  146.         GovvieCurve gcCalib = ScenarioGovvieCurveBuilder.CubicPolyShapePreserver (
  147.             strTreasuryCode,
  148.             strTreasuryCode,
  149.             strCurrency,
  150.             dtSpot.julian(),
  151.             aTreasury,
  152.             adblYield,
  153.             "Yield"
  154.         );

  155.         System.out.println();

  156.         for (int i = 0; i < adblYield.length; ++i)
  157.             System.out.println (
  158.                 "\t[" + new JulianDate (aiMaturityDate[i]) + "] => " +
  159.                 FormatUtil.FormatDouble (adblYield[i], 1, 2, 100.) + "% | " +
  160.                 FormatUtil.FormatDouble (gc.yield (aiMaturityDate[i]), 1, 2, 100.) + "% | " +
  161.                 FormatUtil.FormatDouble (gcCalib.yield (aiMaturityDate[i]), 1, 2, 100.) + "% || "
  162.             );

  163.         EnvManager.TerminateEnv();
  164.     }
  165. }