LinearLatentStateCalibrator.java

  1. package org.drip.state.inference;

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

  79. /**
  80.  * <i>LinearLatentStateCalibrator</i> calibrates/constructs the Latent State Stretch/Span from the
  81.  * calibration instrument details. The span construction may be customized using specific settings provided
  82.  * in GlobalControlCurveParams.
  83.  *
  84.  *  <br><br>
  85.  *  <ul>
  86.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  87.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  88.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  89.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/inference/README.md">Latent State Stretch Sequence Inference</a></li>
  90.  *  </ul>
  91.  * <br><br>
  92.  *
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public class LinearLatentStateCalibrator extends org.drip.state.estimator.GlobalControlCurveParams {

  96.     /**
  97.      * LinearLatentStateCalibrator constructor
  98.      *
  99.      * @param scbc Segment Builder Control Parameters
  100.      * @param bs The Calibration Boundary Condition
  101.      * @param iCalibrationDetail The Calibration Detail
  102.      * @param sbfr Curve Fitness Weighted Response
  103.      * @param sbfrSensitivity Curve Fitness Weighted Response Sensitivity
  104.      *
  105.      * @throws java.lang.Exception Thrown if the inputs are invalid
  106.      */

  107.     public LinearLatentStateCalibrator (
  108.         final org.drip.spline.params.SegmentCustomBuilderControl scbc,
  109.         final org.drip.spline.stretch.BoundarySettings bs,
  110.         final int iCalibrationDetail,
  111.         final org.drip.spline.params.StretchBestFitResponse sbfr,
  112.         final org.drip.spline.params.StretchBestFitResponse sbfrSensitivity)
  113.         throws java.lang.Exception
  114.     {
  115.         super ("", scbc, bs, iCalibrationDetail, sbfr, sbfrSensitivity);
  116.     }

  117.     /**
  118.      * Calibrate the Span from the Instruments in the Stretches and their Details.
  119.      *
  120.      * @param aStretchSpec The Stretch Sequence constituting the Span
  121.      * @param dblEpochResponse Segment Sequence Left-most Response Value
  122.      * @param valParams Valuation Parameter
  123.      * @param pricerParams Pricer Parameter
  124.      * @param vcp The Valuation Customization Parameters
  125.      * @param csqs The Market Parameters Surface and Quote
  126.      *
  127.      * @return Instance of the Latent State Span
  128.      */

  129.     public org.drip.spline.grid.OverlappingStretchSpan calibrateSpan (
  130.         final org.drip.state.inference.LatentStateStretchSpec[] aStretchSpec,
  131.         final double dblEpochResponse,
  132.         final org.drip.param.valuation.ValuationParams valParams,
  133.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  134.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  135.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs)
  136.     {
  137.         if (null == aStretchSpec || null == valParams) return null;

  138.         int iNumStretch = aStretchSpec.length;
  139.         org.drip.spline.grid.OverlappingStretchSpan oss = null;

  140.         if (0 == iNumStretch) return null;

  141.         for (org.drip.state.inference.LatentStateStretchSpec stretchSpec : aStretchSpec) {
  142.             if (null == stretchSpec) continue;

  143.             org.drip.state.inference.LatentStateSegmentSpec[] aSegmentSpec = stretchSpec.segmentSpec();

  144.             int iNumCalibComp = aSegmentSpec.length;
  145.             org.drip.state.estimator.CurveStretch cs = null;
  146.             double[] adblPredictorOrdinate = new double[iNumCalibComp + 1];
  147.             org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  148.                 org.drip.spline.params.SegmentCustomBuilderControl[iNumCalibComp];

  149.             for (int i = 0; i <= iNumCalibComp; ++i) {
  150.                 adblPredictorOrdinate[i] = 0 == i ? valParams.valueDate() :
  151.                     aSegmentSpec[i - 1].component().maturityDate().julian();

  152.                 if (i != iNumCalibComp) aSCBC[i] = segmentBuilderControl (stretchSpec.name());
  153.             }

  154.             try {
  155.                 cs = new org.drip.state.estimator.CurveStretch (stretchSpec.name(),
  156.                     org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateSegmentSet
  157.                         (adblPredictorOrdinate, aSCBC), aSCBC);

  158.                 if (!cs.setup (new org.drip.state.inference.LatentStateSequenceBuilder (dblEpochResponse,
  159.                     stretchSpec, valParams, pricerParams, csqs, vcp, oss, bestFitWeightedResponse(), new
  160.                         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.spline.params.PreceedingManifestSensitivityControl>(),
  161.                     bestFitWeightedResponseSensitivity(), calibrationBoundaryCondition()),
  162.                         calibrationDetail())) {
  163.                     System.out.println ("\tMSS Setup Failed!");

  164.                     return null;
  165.                 }
  166.             } catch (java.lang.Exception e) {
  167.                 e.printStackTrace();

  168.                 return null;
  169.             }

  170.             if (null == oss) {
  171.                 try {
  172.                     oss = new org.drip.spline.grid.OverlappingStretchSpan (cs);
  173.                 } catch (java.lang.Exception e) {
  174.                     e.printStackTrace();

  175.                     return null;
  176.                 }
  177.             } else {
  178.                 if (!oss.addStretch (cs)) return null;
  179.             }
  180.         }

  181.         return oss;
  182.     }
  183. }