CustomRiskUtilitySettings.java

  1. package org.drip.portfolioconstruction.allocator;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  77. /**
  78.  * <i>CustomRiskUtilitySettings</i> contains the settings used to generate the Risk Objective Utility
  79.  * Function. It accommodates both the Risk Tolerance and Risk Aversion Variants.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/README.md">Portfolio Construction under Allocation Constraints</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/allocator/README.md">MVO Based Portfolio Allocation Construction</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class CustomRiskUtilitySettings
  92. {
  93.     private double _riskAversion = java.lang.Double.NaN;
  94.     private double _riskTolerance = java.lang.Double.NaN;

  95.     /**
  96.      * The Variance Minimizer CustomRiskUtilitySettings Instance
  97.      *
  98.      * @return The Variance Minimizer CustomRiskUtilitySettings Instance
  99.      */

  100.     public static final CustomRiskUtilitySettings VarianceMinimizer()
  101.     {
  102.         try
  103.         {
  104.             return new CustomRiskUtilitySettings (
  105.                 1.,
  106.                 0.
  107.             );
  108.         }
  109.         catch (java.lang.Exception e)
  110.         {
  111.             e.printStackTrace();
  112.         }

  113.         return null;
  114.     }

  115.     /**
  116.      * The Risk Tolerant Variance Minimizer CustomRiskUtilitySettings Instance
  117.      *
  118.      * @param riskTolerance The Risk Tolerance Parameter
  119.      *
  120.      * @return The Risk Tolerant Variance Minimizer CustomRiskUtilitySettings Instance
  121.      */

  122.     public static final CustomRiskUtilitySettings RiskTolerant (
  123.         final double riskTolerance)
  124.     {
  125.         try
  126.         {
  127.             return new CustomRiskUtilitySettings (
  128.                 1.,
  129.                 riskTolerance
  130.             );
  131.         }
  132.         catch (java.lang.Exception e)
  133.         {
  134.             e.printStackTrace();
  135.         }

  136.         return null;
  137.     }

  138.     /**
  139.      * The Risk Aversion Variance Minimizer CustomRiskUtilitySettings Instance
  140.      *
  141.      * @param riskAversion The Risk Aversion Parameter
  142.      *
  143.      * @return The Risk Aversion Variance Minimizer CustomRiskUtilitySettings Instance
  144.      */

  145.     public static final CustomRiskUtilitySettings RiskAversion (
  146.         final double riskAversion)
  147.     {
  148.         try
  149.         {
  150.             return new CustomRiskUtilitySettings (
  151.                 riskAversion,
  152.                 1.
  153.             );
  154.         }
  155.         catch (java.lang.Exception e)
  156.         {
  157.             e.printStackTrace();
  158.         }

  159.         return null;
  160.     }

  161.     /**
  162.      * CustomRiskUtilitySettings Constructor
  163.      *
  164.      * @param riskAversion The Risk Aversion Parameter
  165.      * @param riskTolerance The Risk Tolerance Parameter
  166.      *
  167.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  168.      */

  169.     public CustomRiskUtilitySettings (
  170.         final double riskAversion,
  171.         final double riskTolerance)
  172.         throws java.lang.Exception
  173.     {
  174.         if (!org.drip.numerical.common.NumberUtil.IsValid (_riskAversion = riskAversion) ||
  175.             !org.drip.numerical.common.NumberUtil.IsValid (_riskTolerance = riskTolerance) ||
  176.                 0. > _riskTolerance)
  177.         {
  178.             throw new java.lang.Exception ("CustomRiskUtilitySettings Constructor => Invalid Inputs");
  179.         }
  180.     }

  181.     /**
  182.      * Retrieve the Risk Aversion Factor
  183.      *
  184.      * @return The Risk Aversion Factor
  185.      */

  186.     public double riskAversion()
  187.     {
  188.         return _riskAversion;
  189.     }

  190.     /**
  191.      * Retrieve the Risk Tolerance Factor
  192.      *
  193.      * @return The Risk Tolerance Factor
  194.      */

  195.     public double riskTolerance()
  196.     {
  197.         return _riskTolerance;
  198.     }

  199.     /**
  200.      * Retrieve the Custom Risk Objective Utility Multivariate
  201.      *
  202.      * @param assetIDArray Array of the Asset IDs
  203.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  204.      *
  205.      * @return The Custom Risk Objective Utility Multivariate
  206.      */

  207.     public org.drip.function.definition.RdToR1 riskObjectiveUtility (
  208.         final java.lang.String[] assetIDArray,
  209.         final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  210.             assetUniverseStatisticalProperties)
  211.     {
  212.         if (null == assetIDArray || null == assetUniverseStatisticalProperties)
  213.         {
  214.             return null;
  215.         }

  216.         int assetCount = assetIDArray.length;
  217.         double[] expectedReturnsArray = new double[assetCount];
  218.         double[][] covarianceMatrix = new double[assetCount][assetCount];
  219.         org.drip.portfolioconstruction.params.AssetStatisticalProperties[] assetStatisticalPropertiesArray =
  220.             new org.drip.portfolioconstruction.params.AssetStatisticalProperties[assetCount];

  221.         if (0 == assetCount)
  222.         {
  223.             return null;
  224.         }

  225.         for (int assetIndex = 0; assetIndex < assetCount; ++assetIndex)
  226.         {
  227.             if (null == (assetStatisticalPropertiesArray[assetIndex] =
  228.                 assetUniverseStatisticalProperties.assetStatisticalProperties (assetIDArray[assetIndex])))
  229.             {
  230.                 return null;
  231.             }

  232.             expectedReturnsArray[assetIndex] = assetStatisticalPropertiesArray[assetIndex].expectedReturn();
  233.         }

  234.         for (int assetIndexI = 0; assetIndexI < assetCount; ++assetIndexI)
  235.         {
  236.             double dblVarianceI = assetStatisticalPropertiesArray[assetIndexI].variance();

  237.             for (int assetIndexJ = 0; assetIndexJ < assetCount; ++assetIndexJ)
  238.             {
  239.                 try
  240.                 {
  241.                     covarianceMatrix[assetIndexI][assetIndexJ] = java.lang.Math.sqrt (
  242.                         dblVarianceI * assetStatisticalPropertiesArray[assetIndexJ].variance()
  243.                     ) * (
  244.                         assetIndexI == assetIndexJ ? 1. : assetUniverseStatisticalProperties.correlation (
  245.                             assetIDArray[assetIndexI], assetIDArray[assetIndexJ]
  246.                         )
  247.                     );
  248.                 }
  249.                 catch (java.lang.Exception e)
  250.                 {
  251.                     e.printStackTrace();

  252.                     return null;
  253.                 }
  254.             }
  255.         }

  256.         try
  257.         {
  258.             return new org.drip.function.rdtor1.RiskObjectiveUtilityMultivariate (
  259.                 covarianceMatrix,
  260.                 expectedReturnsArray,
  261.                 _riskAversion,
  262.                 _riskTolerance,
  263.                 assetUniverseStatisticalProperties.riskFreeRate()
  264.             );
  265.         }
  266.         catch (java.lang.Exception e)
  267.         {
  268.             e.printStackTrace();
  269.         }

  270.         return null;
  271.     }
  272. }