DateEOMAdjustment.java

  1. package org.drip.analytics.daycount;

  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>DateEOMAdjustment</i> holds the applicable adjustments for a given date pair. It exposes the following
  83.  * functionality:
  84.  *
  85.  *  <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Static Methods for creating 30/360, 30/365, and EOMA Date Adjustments
  89.  *      </li>
  90.  *      <li>
  91.  *          Export Anterior and Posterior EOM Adjustments
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  *  <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  99.  *      <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>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/daycount/README.md">Day Count Year Fraction Utilities</a></li>
  101.  *  </ul>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class DateEOMAdjustment {
  106.     private int _iD1Adj = 0;
  107.     private int _iD2Adj = 0;

  108.     /**
  109.      * Construct a DateEOMAdjustment Instance for the 30/365 Day Count
  110.      *
  111.      * @param iStartDate Start Date
  112.      * @param iEndDate End Date
  113.      * @param bApplyEOMAdj TRUE - Apply EOM Adjustment
  114.      *
  115.      * @return DateEOMAdjustment Instance
  116.      */

  117.     public static final DateEOMAdjustment MakeDEOMA30_365 (
  118.         final int iStartDate,
  119.         final int iEndDate,
  120.         final boolean bApplyEOMAdj)
  121.     {
  122.         DateEOMAdjustment dm = new DateEOMAdjustment();

  123.         if (!bApplyEOMAdj) return dm;

  124.         if (iEndDate > iStartDate) return null;

  125.         try {
  126.             if (org.drip.analytics.date.DateUtil.FEBRUARY == org.drip.analytics.date.DateUtil.Month
  127.                 (iStartDate) && org.drip.analytics.date.DateUtil.IsEOM (iStartDate) &&
  128.                     org.drip.analytics.date.DateUtil.FEBRUARY == org.drip.analytics.date.DateUtil.Month
  129.                         (iEndDate) && org.drip.analytics.date.DateUtil.IsEOM (iEndDate))
  130.                 dm._iD2Adj = (28 == org.drip.analytics.date.DateUtil.DaysInMonth
  131.                     (org.drip.analytics.date.DateUtil.Month (iEndDate),
  132.                         org.drip.analytics.date.DateUtil.Year (iEndDate)) ? 2 : 1);

  133.             if (org.drip.analytics.date.DateUtil.FEBRUARY == org.drip.analytics.date.DateUtil.Month
  134.                 (iStartDate) && org.drip.analytics.date.DateUtil.IsEOM (iStartDate))
  135.                 dm._iD1Adj = (28 == org.drip.analytics.date.DateUtil.DaysInMonth
  136.                     (org.drip.analytics.date.DateUtil.Month (iStartDate),
  137.                         org.drip.analytics.date.DateUtil.Year (iStartDate)) ? 2 : 1);

  138.             if (31 == org.drip.analytics.date.DateUtil.Date (iEndDate) + dm._iD2Adj && (30 ==
  139.                 org.drip.analytics.date.DateUtil.Date (iStartDate) + dm._iD1Adj || 31 ==
  140.                     org.drip.analytics.date.DateUtil.Date (iStartDate) + dm._iD1Adj))
  141.                 dm._iD2Adj -= 1;

  142.             if (31 == org.drip.analytics.date.DateUtil.Date (iStartDate) + dm._iD1Adj) dm._iD1Adj -= 1;

  143.             return dm;
  144.         } catch (java.lang.Exception e) {
  145.             e.printStackTrace();
  146.         }

  147.         return null;
  148.     }

  149.     /**
  150.      * Construct a DateEOMAdjustment Instance for the 30/360 Day Count
  151.      *
  152.      * @param iStartDate Start Date
  153.      * @param iEndDate End Date
  154.      * @param bApplyEOMAdj TRUE - Apply EOM Adjustment
  155.      *
  156.      * @return DateEOMAdjustment Instance
  157.      */

  158.     public static final DateEOMAdjustment MakeDEOMA30_360 (
  159.         final int iStartDate,
  160.         final int iEndDate,
  161.         final boolean bApplyEOMAdj)
  162.     {
  163.         DateEOMAdjustment dm = new DateEOMAdjustment();

  164.         if (!bApplyEOMAdj) return dm;

  165.         try {
  166.             if (31 == org.drip.analytics.date.DateUtil.Date (iStartDate)) dm._iD1Adj -= 1;

  167.             if (!org.drip.analytics.date.DateUtil.IsLeapYear (iStartDate)) {
  168.                 if (org.drip.analytics.date.DateUtil.FEBRUARY == org.drip.analytics.date.DateUtil.Month
  169.                     (iStartDate) && 28 == org.drip.analytics.date.DateUtil.Date (iStartDate))
  170.                     dm._iD1Adj += 2;
  171.             } else {
  172.                 if (org.drip.analytics.date.DateUtil.FEBRUARY == org.drip.analytics.date.DateUtil.Month
  173.                     (iStartDate) && 29 == org.drip.analytics.date.DateUtil.Date (iStartDate))
  174.                     dm._iD1Adj += 1;
  175.             }

  176.             if (31 == org.drip.analytics.date.DateUtil.Date (iEndDate) && (30 ==
  177.                 org.drip.analytics.date.DateUtil.Date (iStartDate) || 31 ==
  178.                     org.drip.analytics.date.DateUtil.Date (iStartDate)))
  179.                 dm._iD2Adj -= 1;

  180.             return dm;
  181.         } catch (java.lang.Exception e) {
  182.             e.printStackTrace();
  183.         }

  184.         return null;
  185.     }

  186.     /**
  187.      * Construct a DateEOMAdjustment Instance for all other Day Counts
  188.      *
  189.      * @param iStartDate Start Date
  190.      * @param iEndDate End Date
  191.      * @param bApplyEOMAdj TRUE - Apply EOM Adjustment
  192.      *
  193.      * @return DateEOMAdjustment Instance
  194.      */

  195.     public static final DateEOMAdjustment MakeDEOMA (
  196.         final int iStartDate,
  197.         final int iEndDate,
  198.         final boolean bApplyEOMAdj)
  199.     {
  200.         DateEOMAdjustment dm = new DateEOMAdjustment();

  201.         if (!bApplyEOMAdj) return dm;

  202.         try {
  203.             if (bApplyEOMAdj) {
  204.                 if (org.drip.analytics.date.DateUtil.IsEOM (iStartDate))
  205.                     dm._iD1Adj = 30 - org.drip.analytics.date.DateUtil.Date (iStartDate);

  206.                 if (org.drip.analytics.date.DateUtil.IsEOM (iEndDate) &&
  207.                     (org.drip.analytics.date.DateUtil.FEBRUARY != org.drip.analytics.date.DateUtil.Month
  208.                         (iEndDate)))
  209.                     dm._iD2Adj = 30 - org.drip.analytics.date.DateUtil.Date (iEndDate);
  210.             }

  211.             return dm;
  212.         } catch (java.lang.Exception e) {
  213.             e.printStackTrace();
  214.         }

  215.         return null;
  216.     }

  217.     /**
  218.      * Construct a DateEOMAdjustment Instance for the 30E/360 Day Count
  219.      *
  220.      * @param iStartDate Start Date
  221.      * @param iEndDate End Date
  222.      * @param bApplyEOMAdj TRUE - Apply EOM Adjustment
  223.      *
  224.      * @return DateEOMAdjustment Instance
  225.      */

  226.     public static final DateEOMAdjustment MakeDEOMA30E_360 (
  227.         final int iStartDate,
  228.         final int iEndDate,
  229.         final boolean bApplyEOMAdj)
  230.     {
  231.         DateEOMAdjustment dm = new DateEOMAdjustment();

  232.         if (!bApplyEOMAdj) return dm;

  233.         try {
  234.             if (bApplyEOMAdj) {
  235.                 if (31 == org.drip.analytics.date.DateUtil.Date (iStartDate)) dm._iD1Adj = -1;

  236.                 if (31 == org.drip.analytics.date.DateUtil.Date (iEndDate)) dm._iD2Adj = -1;
  237.             }

  238.             return dm;
  239.         } catch (java.lang.Exception e) {
  240.             e.printStackTrace();
  241.         }

  242.         return null;
  243.     }

  244.     /**
  245.      * Construct a DateEOMAdjustment Instance for the 30E/360 ISDA Day Count
  246.      *
  247.      * @param iStartDate Start Date
  248.      * @param iEndDate End Date
  249.      * @param bApplyEOMAdj TRUE - Apply EOM Adjustment
  250.      *
  251.      * @return DateEOMAdjustment instance
  252.      */

  253.     public static final DateEOMAdjustment MakeDEOMA30E_360_ISDA (
  254.         final int iStartDate,
  255.         final int iEndDate,
  256.         final boolean bApplyEOMAdj)
  257.     {
  258.         DateEOMAdjustment dm = new DateEOMAdjustment();

  259.         if (!bApplyEOMAdj) return dm;

  260.         try {
  261.             if (bApplyEOMAdj) {
  262.                 if (org.drip.analytics.date.DateUtil.IsEOM (iStartDate))
  263.                     dm._iD1Adj = 30 - org.drip.analytics.date.DateUtil.Date (iStartDate);

  264.                 if (org.drip.analytics.date.DateUtil.IsEOM (iEndDate))
  265.                     dm._iD2Adj = 30 - org.drip.analytics.date.DateUtil.Date (iEndDate);
  266.             }

  267.             return dm;
  268.         } catch (java.lang.Exception e) {
  269.             e.printStackTrace();
  270.         }

  271.         return null;
  272.     }

  273.     /**
  274.      * Construct a DateEOMAdjustment Instance for the 30E+/360 ISDA Day Count
  275.      *
  276.      * @param iStartDate Start Date
  277.      * @param iEndDate End Date
  278.      * @param bApplyEOMAdj TRUE - Apply EOM Adjustment
  279.      *
  280.      * @return DateEOMAdjustment instance
  281.      */

  282.     public static final DateEOMAdjustment MakeDEOMA30EPLUS_360_ISDA (
  283.         final int iStartDate,
  284.         final int iEndDate,
  285.         final boolean bApplyEOMAdj)
  286.     {
  287.         DateEOMAdjustment dm = new DateEOMAdjustment();

  288.         if (!bApplyEOMAdj) return dm;

  289.         try {
  290.             if (bApplyEOMAdj) {
  291.                 if (31 == org.drip.analytics.date.DateUtil.Date (iStartDate)) dm._iD1Adj = -1;

  292.                 if (31 == org.drip.analytics.date.DateUtil.Date (iStartDate)) dm._iD2Adj = +1;
  293.             }

  294.             return dm;
  295.         } catch (java.lang.Exception e) {
  296.             e.printStackTrace();
  297.         }

  298.         return null;
  299.     }

  300.     /**
  301.      * Retrieve the Anterior Date Adjustment
  302.      *
  303.      * @return The Anterior Date Adjustment
  304.      */

  305.     public int anterior()
  306.     {
  307.         return _iD1Adj;
  308.     }

  309.     /**
  310.      * Retrieve the Posterior Date Adjustment
  311.      *
  312.      * @return The Posterior Date Adjustment
  313.      */

  314.     public int posterior()
  315.     {
  316.         return _iD2Adj;
  317.     }
  318. }