MultiSpanAggregationEstimator.java

  1. package org.drip.sample.stretch;

  2. import java.util.*;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.spline.basis.PolynomialFunctionSetParams;
  5. import org.drip.spline.grid.*;
  6. import org.drip.spline.params.*;
  7. import org.drip.spline.stretch.*;

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

  11. /*!
  12.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  17.  *
  18.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  19.  *      libraries targeting analysts and developers
  20.  *      https://lakshmidrip.github.io/DRIP/
  21.  *  
  22.  *  DRIP is composed of four main libraries:
  23.  *  
  24.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  25.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  26.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  27.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  28.  *
  29.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  30.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  31.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  32.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  33.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  34.  *
  35.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  36.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  37.  *
  38.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  39.  *
  40.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  41.  *
  42.  *  Licensed under the Apache License, Version 2.0 (the "License");
  43.  *      you may not use this file except in compliance with the License.
  44.  *  
  45.  *  You may obtain a copy of the License at
  46.  *      http://www.apache.org/licenses/LICENSE-2.0
  47.  *  
  48.  *  Unless required by applicable law or agreed to in writing, software
  49.  *      distributed under the License is distributed on an "AS IS" BASIS,
  50.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  51.  *  
  52.  *  See the License for the specific language governing permissions and
  53.  *      limitations under the License.
  54.  */

  55. /**
  56.  * MultiSpanAggregationEstimator demonstrates the Construction and Usage of the Multiple Span Aggregation
  57.  *  Functionality.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class MultiSpanAggregationEstimator {

  62.     /*
  63.      * Build Polynomial Segment Control Parameters.
  64.      *
  65.      *  WARNING: Insufficient Error Checking, so use caution
  66.      */

  67.     private static final SegmentCustomBuilderControl PolynomialSegmentControlParams (
  68.         final int iNumBasis,
  69.         final SegmentInelasticDesignControl sdic,
  70.         final ResponseScalingShapeControl rssc)
  71.         throws Exception
  72.     {
  73.         return new SegmentCustomBuilderControl (
  74.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  75.             new PolynomialFunctionSetParams (iNumBasis),
  76.             sdic,
  77.             rssc,
  78.             null
  79.         );
  80.     }

  81.     public static final void main (
  82.         final String[] astrArgs)
  83.         throws Exception
  84.     {
  85.         double[] adblX = new double[] { 1.00,  1.50,  2.00, 3.00, 4.00, 5.00, 6.50, 8.00, 10.00};
  86.         double[] adblY1 = new double[] {25.00, 20.25, 16.00, 9.00, 4.00, 1.00, 0.25, 4.00, 16.00};
  87.         double[] adblY2 = new double[] {27.00, 22.25, 18.00, 11.00, 6.00, 3.00, 2.25, 6.00, 18.00};

  88.         SegmentCustomBuilderControl scbc = PolynomialSegmentControlParams (
  89.             4,
  90.             SegmentInelasticDesignControl.Create (2, 2),
  91.             null
  92.         );

  93.         SegmentCustomBuilderControl[] aSCBC = new SegmentCustomBuilderControl[adblX.length - 1];

  94.         for (int i = 0; i < adblX.length - 1; ++i)
  95.             aSCBC[i] = scbc;

  96.         MultiSegmentSequence mss1 = MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator (
  97.             "SPLINE_STRETCH_1", // Name
  98.             adblX, // predictors
  99.             adblY1, // responses
  100.             aSCBC, // Basis Segment Builder parameters
  101.             null,  // NULL segment Best Fit Response
  102.             BoundarySettings.NaturalStandard(), // Boundary Condition - Natural
  103.             MultiSegmentSequence.CALIBRATE // Calibrate the Stretch predictors to the responses
  104.         );

  105.         Span span1 = new OverlappingStretchSpan (mss1);

  106.         MultiSegmentSequence mss2 = MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator (
  107.             "SPLINE_STRETCH_2", // Name
  108.             adblX, // predictors
  109.             adblY2, // responses
  110.             aSCBC, // Basis Segment Builder parameters
  111.             null,  // NULL segment Best Fit Response
  112.             BoundarySettings.NaturalStandard(), // Boundary Condition - Natural
  113.             MultiSegmentSequence.CALIBRATE // Calibrate the Stretch predictors to the responses
  114.         );

  115.         Span span2 = new OverlappingStretchSpan (mss2);

  116.         List<Double> lsWeight = new ArrayList<Double>();

  117.         lsWeight.add (0.14);

  118.         lsWeight.add (0.71);

  119.         List<Span> lsSpan = new ArrayList<Span>();

  120.         lsSpan.add (span1);

  121.         lsSpan.add (span2);

  122.         AggregatedSpan ass = new AggregatedSpan (
  123.             lsSpan,
  124.             lsWeight
  125.         );

  126.         double dblX = 1.;
  127.         double dblXMax = 10.;

  128.         while (dblX <= dblXMax) {
  129.             double dblStretchResponse = 0.14 * mss1.responseValue (dblX) + 0.71 * mss2.responseValue (dblX);

  130.             System.out.println ("Y[" + dblX + "] " +
  131.                 FormatUtil.FormatDouble (ass.calcResponseValue (dblX), 2, 2, 1.) + " | " +
  132.                 FormatUtil.FormatDouble (dblStretchResponse, 2, 2, 1.)
  133.             );

  134.             dblX += 1.;
  135.         }
  136.     }
  137. }