PiecewiseDisplacedLebesgue.java

  1. package org.drip.sample.measure;

  2. import org.drip.analytics.date.*;
  3. import org.drip.measure.lebesgue.R1PiecewiseDisplaced;
  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>PiecewiseDisplacedLebesgue</i> demonstrates the Generation, the Reconciliation, and the Usage of a
  72.  * Piece-wise Displaced 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 PiecewiseDisplacedLebesgue {

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

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

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

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

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

  116.         double[] adblDensitySlope = rpdl.piecewiseDensitySlopes();

  117.         for (int i = 0; i < adblDensitySlope.length; ++i)
  118.             strDump += FormatUtil.FormatDouble (adblDensitySlope[i], 1, 9, 1.) + ",";

  119.         System.out.println (strDump + FormatUtil.FormatDouble (rpdl.densityDisplacement(), 1, 9, 1.) + " ||");
  120.     }

  121.     private static final void DateRPDL (
  122.         final String strMessage,
  123.         final double dblXMin,
  124.         final double dblXMax,
  125.         final double[] adblX,
  126.         final double[] adblProb,
  127.         final double dblXMean)
  128.         throws Exception
  129.     {
  130.         R1PiecewiseDisplaced rpdl = R1PiecewiseDisplaced.Standard (
  131.             dblXMin,
  132.             dblXMax,
  133.             adblX,
  134.             adblProb,
  135.             dblXMean
  136.         );

  137.         double[] adblQuintile = new double[] {
  138.             0.25,
  139.             0.50,
  140.             0.75
  141.         };

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

  143.         for (int i = 0; i < adblQuintile.length; ++i)
  144.             strDump +=
  145.                 new JulianDate ((int) rpdl.invCumulative (adblQuintile[i])) + " =>" +
  146.                 FormatUtil.FormatDouble (adblQuintile[i], 1, 2, 1.) + " | ";

  147.         double[] adblDensitySlope = rpdl.piecewiseDensitySlopes();

  148.         for (int i = 0; i < adblDensitySlope.length; ++i)
  149.             strDump += FormatUtil.FormatDouble (adblDensitySlope[i], 1, 9, 1.) + ",";

  150.         System.out.println (strDump + FormatUtil.FormatDouble (rpdl.densityDisplacement(), 1, 9, 1.) + " ||");
  151.     }

  152.     public static final void main (
  153.         final String[] args)
  154.         throws Exception
  155.     {
  156.         EnvManager.InitEnv ("");

  157.         System.out.println();

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

  159.         System.out.println ("\t||             FIELD                |                       CUMULATIVE PROBABILITY                    |                  PROBABILITY DENSITY PARAMETERS                  ||");

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

  161.         RPDL (
  162.             "Age (Months In Balance)         ",
  163.             0.,
  164.             60.,
  165.             new double[] {3., 8., 15.},
  166.             new double[] {0.25, 0.50, 0.75},
  167.             10.1
  168.         );

  169.         DateRPDL (
  170.             "Vintage                         ",
  171.             DateUtil.CreateFromDDMMMYYYY ("15-FEB-2007").julian(),
  172.             DateUtil.CreateFromDDMMMYYYY ("15-FEB-2015").julian(),
  173.             new double[] {
  174.                 DateUtil.CreateFromDDMMMYYYY ("15-APR-2012").julian(),
  175.                 DateUtil.CreateFromDDMMMYYYY ("15-MAR-2013").julian(),
  176.                 DateUtil.CreateFromDDMMMYYYY ("15-FEB-2014").julian()
  177.             },
  178.             new double[] {0.25, 0.50, 0.75},
  179.             DateUtil.CreateFromDDMMMYYYY ("15-FEB-2013").julian()
  180.         );

  181.         RPDL (
  182.             "Original Principal ('000s)      ",
  183.             0.5,
  184.             35.,
  185.             new double[] {8., 12., 20.},
  186.             new double[] {0.25, 0.50, 0.75},
  187.             14.254
  188.         );

  189.         RPDL (
  190.             "Monthly Gross Income ('000s)    ",
  191.             0.25,
  192.             725.549,
  193.             new double[] {3.75, 5.167, 7.333},
  194.             new double[] {0.25, 0.50, 0.75},
  195.             6.066
  196.         );

  197.         RPDL (
  198.             "Coupon (%)                      ",
  199.             5.3,
  200.             29.,
  201.             new double[] {10.6, 13.5, 16.3},
  202.             new double[] {0.25, 0.50, 0.75},
  203.             13.70
  204.         );

  205.         RPDL (
  206.             "FICO At Origination             ",
  207.             612.,
  208.             847.,
  209.             new double[] {677., 692., 717.},
  210.             new double[] {0.25, 0.50, 0.75},
  211.             699.
  212.         );

  213.         RPDL (
  214.             "DTI ex Mortgage (%)             ",
  215.             0.,
  216.             39.,
  217.             new double[] {11., 16., 22.},
  218.             new double[] {0.25, 0.50, 0.75},
  219.             16.6
  220.         );

  221.         RPDL (
  222.             "Total Borrower Accounts         ",
  223.             1,
  224.             162,
  225.             new double[] {16., 23., 31.},
  226.             new double[] {0.25, 0.50, 0.75},
  227.             25
  228.         );

  229.         RPDL (
  230.             "Revolving Utilization Rate (%)  ",
  231.             0.,
  232.             892.,
  233.             new double[] {40., 58., 75.},
  234.             new double[] {0.25, 0.50, 0.75},
  235.             57.
  236.         );

  237.         RPDL (
  238.             "Inquiries in Last 6 Months      ",
  239.             0.,
  240.             33.,
  241.             new double[] {1.},
  242.             new double[] {0.75},
  243.             0.9
  244.         );

  245.         EnvManager.TerminateEnv();
  246.     }
  247. }