UnconstrainedCovarianceEllipsoid.java

  1. package org.drip.sample.rdtor1;

  2. import org.drip.function.rdtor1.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;

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

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

  50. /**
  51.  * UnconstrainedCovarianceEllipsoid demonstrates the Construction and Usage of a Co-variance Ellipsoid.
  52.  *
  53.  * @author Lakshmi Krishnamurthy
  54.  */

  55. public class UnconstrainedCovarianceEllipsoid {

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

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

  65.         CovarianceEllipsoidMultivariate ce = new CovarianceEllipsoidMultivariate (aadblCovarianceMatrix);

  66.         double[][] aadblVariate = {
  67.             {0.0, 1.0},
  68.             {0.1, 0.9},
  69.             {0.2, 0.8},
  70.             {0.3, 0.7},
  71.             {0.4, 0.6},
  72.             {0.5, 0.5},
  73.             {0.6, 0.4},
  74.             {0.7, 0.3},
  75.             {0.8, 0.2},
  76.             {0.9, 0.1},
  77.             {1.0, 0.0},
  78.         };

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

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

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

  82.         for (double[] adblVariate : aadblVariate)
  83.             System.out.println (
  84.                 "\t|  [" + adblVariate[0] +
  85.                 " | " + adblVariate[1] +
  86.                 "] = " + FormatUtil.FormatDouble (ce.evaluate (adblVariate), 1, 4, 1.) + " ||"
  87.             );

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

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

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

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

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

  94.             double[] adblJacobian = ce.jacobian (adblVariate);

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

  97.             System.out.println (
  98.                 "\t|  [" + adblVariate[0] +
  99.                 " | " + adblVariate[1] +
  100.                 "] = {" + strJacobian + "} ||"
  101.             );
  102.         }

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

  104.         double[][] aadblHessian = ce.hessian (
  105.             new double[] {
  106.                 0.20,
  107.                 0.80
  108.             }
  109.         );

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

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

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

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

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

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

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