PiecewiseLinearLebesgue.java

  1. package org.drip.sample.measure;

  2. import org.drip.analytics.date.*;
  3. import org.drip.measure.lebesgue.R1PiecewiseLinear;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;

  6. /*
  7.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  8.  */

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

  70. /**
  71.  * <i>PiecewiseLinearLebesgue</i> demonstrates the Generation, the Reconciliation, and the Usage of a
  72.  * Piece-wise Linear Lebesgue Measure.
  73.  *  
  74.  * <br><br>
  75.  *  <ul>
  76.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  77.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalSupportLibrary.md">Numerical Support Library</a></li>
  78.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  79.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/measure/README.md">Probability Measure Generators</a></li>
  80.  *  </ul>
  81.  * <br><br>
  82.  *
  83.  * @author Lakshmi Krishnamurthy
  84.  */

  85. public class PiecewiseLinearLebesgue {

  86.     private static final void RPLL (
  87.         final String strMessage,
  88.         final double dblXMin,
  89.         final double dblXMax,
  90.         final double[] adblX,
  91.         final double[] adblProb)
  92.         throws Exception
  93.     {
  94.         R1PiecewiseLinear rpll = R1PiecewiseLinear.Standard (
  95.             dblXMin,
  96.             dblXMax,
  97.             adblX,
  98.             adblProb
  99.         );

  100.         double[] adblQuintile = new double[] {
  101.             0.25,
  102.             0.50,
  103.             0.75
  104.         };

  105.         String strDump = "\t|| " + strMessage + " | ";

  106.         /* for (int i = 0; i < adblX.length; ++i)
  107.             strDump +=
  108.                 FormatUtil.FormatDouble (adblX[i], 3, 3, 1.) + " =>" +
  109.                 FormatUtil.FormatDouble (rpll.cumulative (adblX[i]), 1, 2, 1.) + " | "; */

  110.         for (int i = 0; i < adblQuintile.length; ++i)
  111.             strDump += " " +
  112.                 FormatUtil.FormatDouble (rpll.invCumulative (adblQuintile[i]), 3, 3, 1.) + " =>" +
  113.                 FormatUtil.FormatDouble (adblQuintile[i], 1, 2, 1.) + "   | ";

  114.         double[] adblDensity = rpll.piecewiseDensities();

  115.         for (int i = 0; i < adblDensity.length; ++i)
  116.             strDump += FormatUtil.FormatDouble (adblDensity[i], 1, 9, 1.) + ",";

  117.         System.out.println (strDump + " ||");
  118.     }

  119.     private static final void DateRPLL (
  120.         final String strMessage,
  121.         final double dblXMin,
  122.         final double dblXMax,
  123.         final double[] adblX,
  124.         final double[] adblProb)
  125.         throws Exception
  126.     {
  127.         R1PiecewiseLinear rpll = R1PiecewiseLinear.Standard (
  128.             dblXMin,
  129.             dblXMax,
  130.             adblX,
  131.             adblProb
  132.         );

  133.         double[] adblQuintile = new double[] {
  134.             0.25,
  135.             0.50,
  136.             0.75
  137.         };

  138.         String strDump = "\t|| " + strMessage + " | ";

  139.         for (int i = 0; i < adblQuintile.length; ++i)
  140.             strDump +=
  141.                 new JulianDate ((int) rpll.invCumulative (adblQuintile[i])) + " =>" +
  142.                 FormatUtil.FormatDouble (adblQuintile[i], 1, 2, 1.) + " | ";

  143.         double[] adblDensity = rpll.piecewiseDensities();

  144.         for (int i = 0; i < adblDensity.length; ++i)
  145.             strDump += FormatUtil.FormatDouble (adblDensity[i], 1, 9, 1.) + ",";

  146.         System.out.println (strDump + " ||");
  147.     }

  148.     public static final void main (
  149.         final String[] args)
  150.         throws Exception
  151.     {
  152.         EnvManager.InitEnv ("");

  153.         System.out.println();

  154.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------||");

  155.         System.out.println ("\t||             FIELD                |                       CUMULATIVE PROBABILITY                    |               PROBABILITY DENSITY NODES              ||");

  156.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------||");

  157.         RPLL (
  158.             "Age (Months In Balance)         ",
  159.             0.,
  160.             60.,
  161.             new double[] {3., 8., 15.},
  162.             new double[] {0.25, 0.50, 0.75}
  163.         );

  164.         DateRPLL (
  165.             "Vintage                         ",
  166.             DateUtil.CreateFromDDMMMYYYY ("01-FEB-2007").julian(),
  167.             DateUtil.CreateFromDDMMMYYYY ("01-FEB-2015").julian(),
  168.             new double[] {
  169.                 DateUtil.CreateFromDDMMMYYYY ("01-APR-2012").julian(),
  170.                 DateUtil.CreateFromDDMMMYYYY ("01-MAR-2013").julian(),
  171.                 DateUtil.CreateFromDDMMMYYYY ("01-FEB-2014").julian()
  172.             },
  173.             new double[] {0.25, 0.50, 0.75}
  174.         );

  175.         RPLL (
  176.             "Original Principal ('000s)      ",
  177.             0.5,
  178.             35.,
  179.             new double[] {8., 12., 20.},
  180.             new double[] {0.25, 0.50, 0.75}
  181.         );

  182.         RPLL (
  183.             "Monthly Gross Income ('000s)    ",
  184.             0.25,
  185.             725.549,
  186.             new double[] {3.75, 5.167, 7.333},
  187.             new double[] {0.25, 0.50, 0.75}
  188.         );

  189.         RPLL (
  190.             "Coupon (%)                      ",
  191.             5.3,
  192.             29.,
  193.             new double[] {10.6, 13.5, 16.3},
  194.             new double[] {0.25, 0.50, 0.75}
  195.         );

  196.         RPLL (
  197.             "FICO At Origination             ",
  198.             612.,
  199.             847.,
  200.             new double[] {677., 692., 717.},
  201.             new double[] {0.25, 0.50, 0.75}
  202.         );

  203.         RPLL (
  204.             "DTI ex Mortgage (%)             ",
  205.             0.,
  206.             30.,
  207.             new double[] {11., 16., 22.},
  208.             new double[] {0.25, 0.50, 0.75}
  209.         );

  210.         RPLL (
  211.             "Total Borrower Accounts         ",
  212.             1.,
  213.             162.,
  214.             new double[] {16., 23., 31.},
  215.             new double[] {0.25, 0.50, 0.75}
  216.         );

  217.         RPLL (
  218.             "Revolving Utilization Rate (%)  ",
  219.             0.,
  220.             892.,
  221.             new double[] {40., 58., 75.},
  222.             new double[] {0.25, 0.50, 0.75}
  223.         );

  224.         RPLL (
  225.             "Inquiries in Last 6 Months      ",
  226.             0.,
  227.             33.,
  228.             new double[] {1.},
  229.             new double[] {0.75}
  230.         );

  231.         RPLL (
  232.             "Months Since Last Delinquency   ",
  233.             0.,
  234.             188.,
  235.             new double[] {16., 31., 50.},
  236.             new double[] {0.25, 0.50, 0.75}
  237.         );

  238.         RPLL (
  239.             "Months Since Last Public Record ",
  240.             0.,
  241.             129.,
  242.             new double[] {55., 79., 102.},
  243.             new double[] {0.25, 0.50, 0.75}
  244.         );

  245.         RPLL (
  246.             "Total Open Credit Lines         ",
  247.             0.,
  248.             90.,
  249.             new double[] {8., 10., 14.},
  250.             new double[] {0.25, 0.50, 0.75}
  251.         );

  252.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------||");

  253.         EnvManager.TerminateEnv();
  254.     }
  255. }