ConstrainedCovarianceEllipsoid.java

  1. package org.drip.sample.rdtor1;

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

  51. /**
  52.  * ConstrainedCovarianceEllipsoid demonstrates the Construction and Usage of a Co-variance Ellipsoid with
  53.  *  Linear Constraints.
  54.  *
  55.  * @author Lakshmi Krishnamurthy
  56.  */

  57. public class ConstrainedCovarianceEllipsoid {

  58.     public static final void main (
  59.         final String[] astrArgs)
  60.         throws Exception
  61.     {
  62.         EnvManager.InitEnv ("");

  63.         double[][] aadblCovarianceMatrix = new double[][] {
  64.             {0.09, 0.12},
  65.             {0.12, 0.04}
  66.         };

  67.         double[] adblEqualityConstraint = new double[] {
  68.             1.,
  69.             1.
  70.         };

  71.         double dblEqualityConstraintConstant = -1.;

  72.         AffineMultivariate lmConstraintRdToR1 = new AffineMultivariate (
  73.             adblEqualityConstraint,
  74.             dblEqualityConstraintConstant
  75.         );

  76.         CovarianceEllipsoidMultivariate ceObjectiveRdToR1 = new CovarianceEllipsoidMultivariate (aadblCovarianceMatrix);

  77.         LagrangianMultivariate ceec = new LagrangianMultivariate (
  78.             ceObjectiveRdToR1,
  79.             new RdToR1[] {
  80.                 lmConstraintRdToR1
  81.             }
  82.         );

  83.         double[][] aadblVariate = {
  84.             {0.0, 1.0, 1.0},
  85.             {0.1, 0.9, 1.0},
  86.             {0.2, 0.8, 1.0},
  87.             {0.3, 0.7, 1.0},
  88.             {0.4, 0.6, 1.0},
  89.             {0.5, 0.5, 1.0},
  90.             {0.6, 0.4, 1.0},
  91.             {0.7, 0.3, 1.0},
  92.             {0.8, 0.2, 1.0},
  93.             {0.9, 0.1, 1.0},
  94.             {1.0, 0.0, 1.0},
  95.         };

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

  97.         System.out.println ("\t|       POINT VALUE      ||");

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

  99.         for (double[] adblVariate : aadblVariate)
  100.             System.out.println (
  101.                 "\t|  [" + adblVariate[0] +
  102.                 " | " + adblVariate[1] +
  103.                 "] = " + FormatUtil.FormatDouble (ceec.evaluate (adblVariate), 1, 4, 1.) + " ||"
  104.             );

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

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

  107.         System.out.println ("\t|                 JACOBIAN                  ||");

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

  109.         for (double[] adblVariate : aadblVariate) {
  110.             String strJacobian = "";

  111.             double[] adblJacobian = ceec.jacobian (adblVariate);

  112.             for (double dblJacobian : adblJacobian)
  113.                 strJacobian += FormatUtil.FormatDouble (dblJacobian, 1, 4, 1.) + ",";

  114.             System.out.println (
  115.                 "\t|  [" + adblVariate[0] +
  116.                 " | " + adblVariate[1] +
  117.                 "] = {" + strJacobian + "} ||"
  118.             );
  119.         }

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

  121.         double[][] aadblHessian = ceec.hessian (
  122.             new double[] {
  123.                 0.20,
  124.                 0.80,
  125.                 1.
  126.             }
  127.         );

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

  129.         System.out.println ("\t|          HESSIAN           ||");

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

  131.         for (double[] adblHessian : aadblHessian) {
  132.             String strHessian = "";

  133.             for (double dblHessian : adblHessian)
  134.                 strHessian += FormatUtil.FormatDouble (dblHessian, 1, 4, 1.) + ",";

  135.             System.out.println ("\t| [" + strHessian + "] ||");
  136.         }

  137.         System.out.println ("\t|----------------------------||");
  138.     }
  139. }