VertexDateBuilder.java

  1. package org.drip.analytics.support;

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

  76. /**
  77.  * <i>VertexDateBuilder</i> exports Static Functions that create Vertex Dates using different Schemes. The
  78.  * References are:
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li>
  83.  *          Burgard, C., and M. Kjaer (2013): Funding Strategies, Funding Costs <i>Risk</i> <b>24 (12)</b>
  84.  *              82-87
  85.  *      </li>
  86.  *      <li>
  87.  *          Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party
  88.  *              Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  89.  *      </li>
  90.  *      <li>
  91.  *          Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  92.  *      </li>
  93.  *      <li>
  94.  *          Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  95.  *              86-90
  96.  *      </li>
  97.  *      <li>
  98.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  99.  *          <i>Risk</i> <b>21 (2)</b> 97-102
  100.  *      </li>
  101.  *  </ul>
  102.  *
  103.  *  <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  107.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  108.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/support/README.md">Assorted Support and Helper Utilities</a></li>
  109.  *  </ul>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class VertexDateBuilder
  114. {

  115.     /**
  116.      * Construct an Array of Dates from the Spot Date and the Vertex Tenor Array
  117.      *
  118.      * @param spotDate The Spot Date
  119.      * @param vertexTenorArray The Vertex Tenor Array
  120.      *
  121.      * @return The Array of Dates
  122.      */

  123.     public static final int[] SpotDateVertexTenor (
  124.         final int spotDate,
  125.         final java.lang.String[] vertexTenorArray)
  126.     {
  127.         if (0 >= spotDate || null == vertexTenorArray) return null;

  128.         int vertexCount = vertexTenorArray.length;
  129.         int[] vertexDateArray = 0 == vertexCount ? null : new int[vertexCount];

  130.         if (0 == vertexCount) return null;

  131.         org.drip.analytics.date.JulianDate spotDateJulian = new org.drip.analytics.date.JulianDate
  132.             (spotDate);

  133.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  134.         {
  135.             org.drip.analytics.date.JulianDate vertexDateJulian = spotDateJulian.addTenor
  136.                 (vertexTenorArray[vertexIndex]);

  137.             if (null == vertexDateJulian)
  138.             {
  139.                 return null;
  140.             }

  141.             vertexDateArray[vertexIndex] = vertexDateJulian.julian();
  142.         }

  143.         return vertexDateArray;
  144.     }

  145.     /**
  146.      * Construct an Array of Vertex Dates from the Spot Date, Tenor Spacing Width, and the Vertex Count
  147.      *
  148.      * @param spotDate The Spot Date
  149.      * @param periodTenor The Tenor Spacing Width
  150.      * @param vertexCount The Number of Vertexes
  151.      *
  152.      * @return The Array of Vertex Dates
  153.      */

  154.     public static final int[] SpotDatePeriodTenor (
  155.         final int spotDate,
  156.         final java.lang.String periodTenor,
  157.         final int vertexCount)
  158.     {
  159.         if (0 >= spotDate || 0 >= vertexCount)
  160.         {
  161.             return null;
  162.         }

  163.         int[] vertexDateArray = new int[vertexCount];

  164.         org.drip.analytics.date.JulianDate previousDateJulian = new org.drip.analytics.date.JulianDate
  165.             (spotDate);

  166.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  167.         {
  168.             org.drip.analytics.date.JulianDate vertexDateJulian = previousDateJulian.addTenor (periodTenor);

  169.             if (null == vertexDateJulian)
  170.             {
  171.                 return null;
  172.             }

  173.             vertexDateArray[vertexIndex] = vertexDateJulian.julian();

  174.             previousDateJulian = vertexDateJulian;
  175.         }

  176.         return vertexDateArray;
  177.     }

  178.     /**
  179.      * Generate Equal Width Vertex Dates from the specified Spot Date and the Terminal Date
  180.      *
  181.      * @param spotDate The Spot Date
  182.      * @param terminalDate The Terminal Date
  183.      * @param vertexCount The Number of Vertexes
  184.      *
  185.      * @return Array of Equal Width Vertex Dates
  186.      */

  187.     public static final int[] EqualWidth (
  188.         final int spotDate,
  189.         final int terminalDate,
  190.         final int vertexCount)
  191.     {
  192.         if (0 >= spotDate || terminalDate <= spotDate || 0 >= vertexCount)
  193.         {
  194.             return null;
  195.         }

  196.         int[] vertexDateArray = new int[vertexCount];
  197.         vertexDateArray[vertexCount - 1] = terminalDate;
  198.         double periodWidth = ((double) (terminalDate - spotDate)) / vertexCount;

  199.         for (int vertexIndex = 0; vertexIndex < vertexCount - 1; ++vertexIndex)
  200.         {
  201.             vertexDateArray[vertexIndex] = spotDate + (int) ((vertexIndex + 1) * periodWidth);
  202.         }

  203.         return vertexDateArray;
  204.     }
  205. }