RiskTolerantVarianceMinimizer.java

  1. package org.drip.sample.assetallocation;

  2. import org.drip.feed.loader.*;
  3. import org.drip.measure.statistics.MultivariateMoments;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.portfolioconstruction.allocator.*;
  6. import org.drip.portfolioconstruction.asset.*;
  7. import org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties;
  8. import org.drip.service.env.EnvManager;

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

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

  84. /**
  85.  * <i>RiskTolerantVarianceMinimizer</i> demonstrates the Construction of an Optimal Portfolio using the
  86.  * Variance Minimization with a Fully Invested Constraint on a Risk Tolerance Objective Function.
  87.  *  
  88.  * <br><br>
  89.  *  <ul>
  90.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  91.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  92.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  93.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/assetallocation/README.md">MVO Based Constrained Optimal Allocator</a></li>
  94.  *  </ul>
  95.  * <br><br>
  96.  *
  97.  * @author Lakshmi Krishnamurthy
  98.  */

  99. public class RiskTolerantVarianceMinimizer
  100. {

  101.     static final void RiskTolerancePortfolio (
  102.         final String[] assetIDArray,
  103.         final AssetUniverseStatisticalProperties assetUniverseStatisticalProperties,
  104.         final double riskTolerance)
  105.         throws Exception
  106.     {
  107.         HoldingsAllocation optimizationOutput = new QuadraticMeanVarianceOptimizer().allocate (
  108.             new HoldingsAllocationControl (
  109.                 assetIDArray,
  110.                 CustomRiskUtilitySettings.RiskTolerant (
  111.                     riskTolerance
  112.                 ),
  113.                 new EqualityConstraintSettings (
  114.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  115.                     Double.NaN
  116.                 )
  117.             ),
  118.             assetUniverseStatisticalProperties
  119.         );

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

  121.         for (AssetComponent assetComponent : optimizationOutput.optimalPortfolio().assetComponentArray())
  122.         {
  123.             System.out.println (
  124.                 "\t| " + assetComponent.id() + " | " + FormatUtil.FormatDouble (
  125.                     assetComponent.amount(), 3, 2, 100.
  126.                 ) + "% ||"
  127.             );
  128.         }

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

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

  131.         System.out.println (
  132.             "\t| Portfolio Notional           : " + FormatUtil.FormatDouble (
  133.                 optimizationOutput.optimalPortfolio().notional(), 1, 3, 1.
  134.             ) + " ||"
  135.         );

  136.         System.out.println (
  137.             "\t| Portfolio Expected Return    : " + FormatUtil.FormatDouble (
  138.                 optimizationOutput.optimalMetrics().excessReturnsMean(), 1, 2, 100.
  139.             ) + "% ||"
  140.         );

  141.         System.out.println (
  142.             "\t| Portfolio Standard Deviation : " + FormatUtil.FormatDouble (
  143.                 optimizationOutput.optimalMetrics().excessReturnsStandardDeviation(), 1, 2, 100.
  144.             ) + "% ||"
  145.         );

  146.         System.out.println ("\t|---------------------------------------||\n");
  147.     }

  148.     public static final void main (
  149.         final String[] argumentArray)
  150.         throws Exception
  151.     {
  152.         EnvManager.InitEnv (
  153.             "",
  154.             true
  155.         );

  156.         String seriesPath = "C:\\DROP\\Daemons\\Feeds\\MeanVarianceOptimizer\\FormattedSeries1.csv";

  157.         CSVGrid csvGrid = CSVParser.NamedStringGrid (
  158.             seriesPath
  159.         );

  160.         String[] variateHeaderArray = csvGrid.headers();

  161.         String[] assetIDArray = new String[variateHeaderArray.length - 1];
  162.         double[][] variateSampleGrid = new double[variateHeaderArray.length - 1][];

  163.         for (int assetIndex = 0;
  164.             assetIndex < assetIDArray.length;
  165.             ++assetIndex)
  166.         {
  167.             assetIDArray[assetIndex] = variateHeaderArray[assetIndex + 1];

  168.             variateSampleGrid[assetIndex] = csvGrid.doubleArrayAtColumn (
  169.                 assetIndex + 1
  170.             );
  171.         }

  172.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  173.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  174.                 MultivariateMoments.Standard (
  175.                     assetIDArray,
  176.                     variateSampleGrid
  177.                 )
  178.             );

  179.         double[] riskToleranceArray = new double[]
  180.         {
  181.             0.1,
  182.             0.2,
  183.             0.3,
  184.             0.5,
  185.             1.0
  186.         };

  187.         for (double riskTolerance : riskToleranceArray)
  188.         {
  189.             System.out.println ("\n\t|---------------------------------------------||");

  190.             System.out.println ("\t| Running Optimization For Risk Tolerance " + riskTolerance + " ||");

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

  192.             RiskTolerancePortfolio (
  193.                 assetIDArray,
  194.                 assetUniverseStatisticalProperties,
  195.                 riskTolerance
  196.             );
  197.         }

  198.         EnvManager.TerminateEnv();
  199.     }
  200. }