ZeroCurveRegressor.java

  1. package org.drip.regression.curve;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  81. /**
  82.  * <i>ZeroCurveRegressor</i> implements the regression analysis set for the Zero Curve. The  regression tests
  83.  * do the consists of the following:
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Build a discount curve, followed by the zero curve
  89.  *      </li>
  90.  *      <li>
  91.  *          Regressor #1: Compute zero curve discount factors
  92.  *      </li>
  93.  *      <li>
  94.  *          Regressor #2: Compute zero curve zero rates
  95.  *      </li>
  96.  *  </ul>
  97.  *
  98.  * <br><br>
  99.  *  <ul>
  100.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  101.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  102.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/README.md">Regression Engine Core and the Unit Regressors</a></li>
  103.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/curve/README.md">Curve Construction/Reconciliation Regression Engine</a></li>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class ZeroCurveRegressor implements org.drip.regression.core.RegressorSet {
  110.     private org.drip.state.discount.ZeroCurve _zc = null;
  111.     private java.lang.String _strRegressionScenario = "org.drip.analytics.curve.ZeroCurve";

  112.     private java.util.List<org.drip.regression.core.UnitRegressor> _setRegressors = new
  113.         java.util.ArrayList<org.drip.regression.core.UnitRegressor>();

  114.     /**
  115.      * ZeroCurveRegressor constructor - Creates the base zero curve and initializes the regression objects
  116.      */

  117.     public ZeroCurveRegressor()
  118.     {
  119.     }

  120.     /*
  121.      * Setting up of the zero curve regressor set
  122.      */

  123.     @Override public boolean setupRegressors()
  124.     {
  125.         /*
  126.          * Zero Curve Creation unit regressor - implements the pre-regression, the post-regression, and the
  127.          *  actual regression functionality of the UnitRegressorExecutor class.
  128.          */

  129.         try {
  130.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor
  131.                 ("CreateZeroCurveFromPeriods", _strRegressionScenario)
  132.             {
  133.                 private static final double s_dblZSpread = 0.01;

  134.                 private org.drip.analytics.date.JulianDate _dtStart = null;
  135.                 private org.drip.state.discount.ExplicitBootDiscountCurve _dc = null;

  136.                 private java.util.List<org.drip.analytics.cashflow.CompositePeriod> _lsCouponPeriod = new
  137.                     java.util.ArrayList<org.drip.analytics.cashflow.CompositePeriod>();

  138.                 @Override public boolean preRegression()
  139.                 {
  140.                     if (null == (_dtStart = org.drip.analytics.date.DateUtil.CreateFromYMD (2010,
  141.                         org.drip.analytics.date.DateUtil.MAY, 12)))
  142.                         return false;

  143.                     final int NUM_DC_NODES = 5;
  144.                     final int NUM_PERIOD_NODES  = 40;
  145.                     int aiDate[] = new int[NUM_DC_NODES];
  146.                     double adblRate[] = new double[NUM_DC_NODES];

  147.                     for (int i = 0; i < NUM_DC_NODES; ++i) {
  148.                         aiDate[i] = _dtStart.addYears (2 * i + 1).julian();

  149.                         adblRate[i] = 0.05 + 0.001 * (NUM_DC_NODES - i);
  150.                     }

  151.                     if (null == (_dc = org.drip.state.creator.ScenarioDiscountCurveBuilder.PiecewiseForward
  152.                         (_dtStart, "CHF", aiDate, adblRate)))
  153.                         return false;

  154.                     try {
  155.                         org.drip.param.period.UnitCouponAccrualSetting ucas = new
  156.                             org.drip.param.period.UnitCouponAccrualSetting (2, "30/360", false, "30/360",
  157.                                 false, "ZAR", false,
  158.                                     org.drip.analytics.support.CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC);

  159.                         org.drip.param.period.ComposableFixedUnitSetting cfus = new
  160.                             org.drip.param.period.ComposableFixedUnitSetting ("6M",
  161.                                 org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_REGULAR,
  162.                             null, s_dblZSpread, 0., "ZAR");

  163.                         org.drip.param.period.CompositePeriodSetting cps = new
  164.                             org.drip.param.period.CompositePeriodSetting (2, "6M", "ZAR", null, 1., null,
  165.                                 null, null, null);

  166.                         java.util.List<java.lang.Integer> lsStreamEdgeDate =
  167.                             org.drip.analytics.support.CompositePeriodBuilder.RegularEdgeDates (_dtStart,
  168.                                 "6M", (NUM_PERIOD_NODES * 6) + "M", null);

  169.                         _lsCouponPeriod =
  170.                             org.drip.analytics.support.CompositePeriodBuilder.FixedCompositeUnit (
  171.                                 lsStreamEdgeDate, cps, ucas, cfus);
  172.                     } catch (java.lang.Exception e) {
  173.                         e.printStackTrace();

  174.                         return false;
  175.                     }

  176.                     return true;
  177.                 }

  178.                 @Override public boolean execRegression()
  179.                 {
  180.                     try {
  181.                         if (null == (_zc = org.drip.state.curve.DerivedZeroRate.FromBaseCurve (2, "30/360",
  182.                             _dc.currency(), true, _lsCouponPeriod, _lsCouponPeriod.get
  183.                                 (_lsCouponPeriod.size() - 1).endDate(), _dtStart.julian(), _dtStart.addDays
  184.                                     (2).julian(), _dc, s_dblZSpread, null, new
  185.                                         org.drip.spline.params.SegmentCustomBuilderControl
  186.                                             (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  187.                             new org.drip.spline.basis.PolynomialFunctionSetParams (4),
  188.                                 org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), new
  189.                                     org.drip.spline.params.ResponseScalingShapeControl (true, new
  190.                                         org.drip.function.r1tor1.QuadraticRationalShapeControl (0.)),
  191.                                             null))))
  192.                             return false;
  193.                     } catch (java.lang.Exception e) {
  194.                         e.printStackTrace();

  195.                         return false;
  196.                     }

  197.                     return true;
  198.                 }
  199.             });

  200.             /*
  201.              * Get Zero Discount Factor unit regressor - implements the pre-regression, the post-regression,
  202.              *  and the actual regression functionality of the UnitRegressorExecutor class.
  203.              */

  204.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor ("getZeroDF",
  205.                 _strRegressionScenario)
  206.             {
  207.                 private static final int NUM_DF_NODES = 30;

  208.                 private int _aiDate[] = new int[NUM_DF_NODES];
  209.                 private double _adblDiscFactor[] = new double[NUM_DF_NODES];

  210.                 @Override public boolean preRegression()
  211.                 {
  212.                     org.drip.analytics.date.JulianDate dtStart =
  213.                         org.drip.analytics.date.DateUtil.CreateFromYMD (2008,
  214.                             org.drip.analytics.date.DateUtil.SEPTEMBER, 25);

  215.                     for (int i = 0; i < NUM_DF_NODES; ++i)
  216.                         _aiDate[i] = dtStart.addMonths (6 * i + 6).julian();

  217.                     return true;
  218.                 }

  219.                 @Override public boolean execRegression()
  220.                 {
  221.                     try {
  222.                         for (int i = 0; i < NUM_DF_NODES; ++i)
  223.                             _adblDiscFactor[i] = _zc.df (_aiDate[i]);
  224.                     } catch (java.lang.Exception e) {
  225.                         e.printStackTrace();

  226.                         return false;
  227.                     }

  228.                     return true;
  229.                 }

  230.                 @Override public boolean postRegression (
  231.                     final org.drip.regression.core.RegressionRunDetail rnvd)
  232.                 {
  233.                     try {
  234.                         for (int i = 0; i < NUM_DF_NODES; ++i)
  235.                             rnvd.set ("ZeroDF[" + new org.drip.analytics.date.JulianDate (_aiDate[i]) +
  236.                                 "]", "" + _adblDiscFactor[i]);
  237.                     } catch (java.lang.Exception e) {
  238.                         e.printStackTrace();

  239.                         return false;
  240.                     }

  241.                     return true;
  242.                 }
  243.             });

  244.             /*
  245.              * Get Zero Rate unit regressor - implements the pre-regression, the post-regression, and the
  246.              *  actual regression functionality of the UnitRegressorExecutor class.
  247.              */

  248.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor ("getZeroRate",
  249.                 _strRegressionScenario)
  250.             {
  251.                 private static final int NUM_DF_NODES = 30;

  252.                 private int _aiDate[] = new int[NUM_DF_NODES];
  253.                 private double _adblRate[] = new double[NUM_DF_NODES];

  254.                 @Override public boolean preRegression()
  255.                 {
  256.                     org.drip.analytics.date.JulianDate dtStart =
  257.                         org.drip.analytics.date.DateUtil.CreateFromYMD (2008,
  258.                             org.drip.analytics.date.DateUtil.SEPTEMBER, 25);

  259.                     for (int i = 0; i < NUM_DF_NODES; ++i)
  260.                         _aiDate[i] = dtStart.addMonths (6 * i + 6).julian();

  261.                     return true;
  262.                 }

  263.                 @Override public boolean execRegression()
  264.                 {
  265.                     try {
  266.                         for (int i = 0; i < NUM_DF_NODES; ++i)
  267.                             _adblRate[i] = _zc.zeroRate (_aiDate[i]);
  268.                     } catch (java.lang.Exception e) {
  269.                         e.printStackTrace();

  270.                         return false;
  271.                     }

  272.                     return true;
  273.                 }

  274.                 @Override public boolean postRegression (
  275.                     final org.drip.regression.core.RegressionRunDetail rnvd)
  276.                 {
  277.                     try {
  278.                         for (int i = 0; i < NUM_DF_NODES; ++i)
  279.                             rnvd.set ("ZeroRate[" + new org.drip.analytics.date.JulianDate (_aiDate[i]) +
  280.                                 "]", "" + _adblRate[i]);
  281.                     } catch (java.lang.Exception e) {
  282.                         e.printStackTrace();

  283.                         return false;
  284.                     }

  285.                     return true;
  286.                 }
  287.             });
  288.         } catch (java.lang.Exception e) {
  289.             e.printStackTrace();

  290.             return false;
  291.         }

  292.         return true;
  293.     }

  294.     @Override public java.util.List<org.drip.regression.core.UnitRegressor> getRegressorSet()
  295.     {
  296.         return _setRegressors;
  297.     }

  298.     @Override public java.lang.String getSetName()
  299.     {
  300.         return _strRegressionScenario;
  301.     }
  302. }