VanillaVarianceMinimizer.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>VanillaVarianceMinimizer</i> demonstrates the Construction of an Optimal Portfolio using the Variance
  86.  * Minimizing Allocator with only the Fully Invested Constraint.
  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 VanillaVarianceMinimizer
  100. {

  101.     private 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.VarianceMinimizer(),
  111.                 new EqualityConstraintSettings (
  112.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  113.                     Double.NaN
  114.                 )
  115.             ),
  116.             assetUniverseStatisticalProperties
  117.         );

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

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

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

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

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

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

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

  144.         System.out.println ("\t|---------------------------------------||\n");
  145.     }

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

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

  155.         CSVGrid csvGrid = CSVParser.NamedStringGrid (
  156.             seriesPath
  157.         );

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

  159.         String[] assetIDArray = new String[variateHeaderArray.length - 1];
  160.         double[][] variateSampleGrid = new double[variateHeaderArray.length - 1][];

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

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

  170.         RiskTolerancePortfolio (
  171.             assetIDArray,
  172.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  173.                 MultivariateMoments.Standard (
  174.                     assetIDArray,
  175.                     variateSampleGrid
  176.                 )
  177.             ),
  178.             0.
  179.         );

  180.         EnvManager.TerminateEnv();
  181.     }
  182. }