AssetUniverseStatisticalProperties.java

  1. package org.drip.portfolioconstruction.params;

  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>AssetUniverseStatisticalProperties</i> holds the Statistical Properties of a Pool of Assets.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  84.  *      <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>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/params/README.md">Asset Universe Statistical Properties Container</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class AssetUniverseStatisticalProperties
  92. {
  93.     private double _riskFreeRate = java.lang.Double.NaN;

  94.     private
  95.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.portfolioconstruction.params.AssetStatisticalProperties>
  96.             _assetUniverseStatisticalPropertiesMap = new
  97.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.portfolioconstruction.params.AssetStatisticalProperties>();

  98.     private org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double> _correlationMap =
  99.         new org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  100.     /**
  101.      * Construct an Instance of AssetUniverseStatisticalProperties from the corresponding MultivariateMetrics
  102.      *  Instance
  103.      *
  104.      * @param multivariateMoments The MultivariateMetrics Instance
  105.      *
  106.      * @return The AssetUniverseStatisticalProperties Instance
  107.      */

  108.     public static final AssetUniverseStatisticalProperties FromMultivariateMetrics (
  109.         final org.drip.measure.statistics.MultivariateMoments multivariateMoments)
  110.     {
  111.         if (null == multivariateMoments)
  112.         {
  113.             return null;
  114.         }

  115.         java.util.Set<java.lang.String> assetSet = multivariateMoments.variateList();

  116.         if (null == assetSet || 0 == assetSet.size())
  117.         {
  118.             return null;
  119.         }

  120.         try
  121.         {
  122.             AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  123.                 new AssetUniverseStatisticalProperties (0.);

  124.             for (java.lang.String asset : assetSet)
  125.             {
  126.                 if (!assetUniverseStatisticalProperties.setAssetStatisticalProperties (
  127.                     new org.drip.portfolioconstruction.params.AssetStatisticalProperties (
  128.                         asset,
  129.                         asset,
  130.                         multivariateMoments.mean (asset),
  131.                         multivariateMoments.variance (asset)
  132.                     )
  133.                 ))
  134.                 {
  135.                     return null;
  136.                 }
  137.             }

  138.             for (java.lang.String asset1 : assetSet)
  139.             {
  140.                 for (java.lang.String asset2 : assetSet)
  141.                 {
  142.                     if (!assetUniverseStatisticalProperties.setCorrelation (
  143.                         asset1,
  144.                         asset2,
  145.                         multivariateMoments.correlation (
  146.                             asset1,
  147.                             asset2
  148.                         )
  149.                     ))
  150.                     {
  151.                         return null;
  152.                     }
  153.                 }
  154.             }

  155.             return assetUniverseStatisticalProperties;
  156.         }
  157.         catch (java.lang.Exception e)
  158.         {
  159.             e.printStackTrace();
  160.         }

  161.         return null;
  162.     }

  163.     /**
  164.      * AssetUniverseStatisticalProperties Constructor
  165.      *
  166.      * @param riskFreeRate The Risk Free Rate
  167.      *
  168.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  169.      */

  170.     public AssetUniverseStatisticalProperties (
  171.         final double riskFreeRate)
  172.         throws java.lang.Exception
  173.     {
  174.         if (!org.drip.numerical.common.NumberUtil.IsValid (
  175.             _riskFreeRate = riskFreeRate
  176.         ))
  177.         {
  178.             throw new java.lang.Exception (
  179.                 "AssetUniverseStatisticalProperties Constructor => Invalid Inputs"
  180.             );
  181.         }
  182.     }

  183.     /**
  184.      * Set the AssetStatisticalProperties Instance
  185.      *
  186.      * @param assetStatisticalProperties AssetStatisticalProperties Instance
  187.      *
  188.      * @return TRUE - AssetStatisticalProperties Instance Successfully added
  189.      */

  190.     public boolean setAssetStatisticalProperties (
  191.         final org.drip.portfolioconstruction.params.AssetStatisticalProperties assetStatisticalProperties)
  192.     {
  193.         if (null == assetStatisticalProperties)
  194.         {
  195.             return false;
  196.         }

  197.         _assetUniverseStatisticalPropertiesMap.put (
  198.             assetStatisticalProperties.id(),
  199.             assetStatisticalProperties
  200.         );

  201.         return true;
  202.     }

  203.     /**
  204.      * Set the Correlation Between the Specified Pair of Assets
  205.      *
  206.      * @param id1 Asset #1
  207.      * @param id2 Asset #2
  208.      * @param correlation Cross-asset Correlation
  209.      *
  210.      * @return Correlation Between the Specified Pair of Assets
  211.      */

  212.     public boolean setCorrelation (
  213.         final java.lang.String id1,
  214.         final java.lang.String id2,
  215.         final double correlation)
  216.     {
  217.         if (null == id1 || id1.isEmpty() ||
  218.             null == id2 || id2.isEmpty() ||
  219.             !org.drip.numerical.common.NumberUtil.IsValid (
  220.                 correlation
  221.             ) || 1. < correlation || -1. > correlation
  222.         )
  223.         {
  224.             return false;
  225.         }

  226.         _correlationMap.put (
  227.             id1 + "@#" + id2,
  228.             correlation
  229.         );

  230.         _correlationMap.put (
  231.             id2 + "@#" + id1,
  232.             correlation
  233.         );

  234.         return true;
  235.     }

  236.     /**
  237.      * Retrieve the Risk Free Rate
  238.      *
  239.      * @return The Risk Free Rate
  240.      */

  241.     public double riskFreeRate()
  242.     {
  243.         return _riskFreeRate;
  244.     }

  245.     /**
  246.      * Retrieve the AssetStatisticalProperties Instance corresponding to the specified ID
  247.      *
  248.      * @param id The AssetStatisticalProperties ID
  249.      *
  250.      * @return The AssetStatisticalProperties Instance
  251.      */

  252.     public org.drip.portfolioconstruction.params.AssetStatisticalProperties assetStatisticalProperties (
  253.         final java.lang.String id)
  254.     {
  255.         return null == id || id.isEmpty() || !_assetUniverseStatisticalPropertiesMap.containsKey (
  256.             id
  257.         ) ? null : _assetUniverseStatisticalPropertiesMap.get (
  258.             id
  259.         );
  260.     }

  261.     /**
  262.      * Retrieve the Correlation between the Specified Assets
  263.      *
  264.      * @param id1 Asset #1
  265.      * @param id2 Asset #2
  266.      *
  267.      * @return Correlation between the Specified Assets
  268.      *
  269.      * @throws java.lang.Exception Thtrown if the Inputs are Invalid
  270.      */

  271.     public double correlation (
  272.         final java.lang.String id1,
  273.         final java.lang.String id2)
  274.         throws java.lang.Exception
  275.     {
  276.         if (null == id1 || id1.isEmpty() ||
  277.             null == id2 || id2.isEmpty())
  278.         {
  279.             throw new java.lang.Exception (
  280.                 "AssetUniverseStatisticalProperties::correlation => Invalid Inputs"
  281.             );
  282.         }

  283.         if (id1.equalsIgnoreCase (
  284.             id2
  285.         ))
  286.         {
  287.             return 1.;
  288.         }

  289.         java.lang.String strCorrelationSlot = id1 + "@#" + id2;

  290.         if (!_correlationMap.containsKey (
  291.             strCorrelationSlot
  292.         ))
  293.         {
  294.             throw new java.lang.Exception (
  295.                 "AssetUniverseStatisticalProperties::correlation => Invalid Inputs"
  296.             );
  297.         }

  298.         return _correlationMap.get (
  299.             strCorrelationSlot
  300.         );
  301.     }

  302.     /**
  303.      * Retrieve the Asset Expected Returns Array
  304.      *
  305.      * @param idArray Array of Asset IDs
  306.      *
  307.      * @return The Asset Covariance Matrix
  308.      */

  309.     public double[] expectedReturns (
  310.         final java.lang.String[] idArray)
  311.     {
  312.         if (null == idArray)
  313.         {
  314.             return null;
  315.         }

  316.         int assetCount = idArray.length;
  317.         double[] expectedReturnsArray = new double[assetCount];

  318.         if (0 == assetCount)
  319.         {
  320.             return null;
  321.         }

  322.         for (int assetIndex = 0;
  323.             assetIndex < assetCount;
  324.             ++assetIndex)
  325.         {
  326.             org.drip.portfolioconstruction.params.AssetStatisticalProperties assetStatisticalProperties =
  327.                 assetStatisticalProperties (
  328.                     idArray[assetIndex]
  329.                 );

  330.             if (null == assetStatisticalProperties)
  331.             {
  332.                 return null;
  333.             }

  334.             expectedReturnsArray[assetIndex] = assetStatisticalProperties.expectedReturn();
  335.         }

  336.         return expectedReturnsArray;
  337.     }

  338.     /**
  339.      * Retrieve the Asset Covariance Matrix
  340.      *
  341.      * @param idArray Array of Asset IDs
  342.      *
  343.      * @return The Asset Covariance Matrix
  344.      */

  345.     public double[][] covariance (
  346.         final java.lang.String[] idArray)
  347.     {
  348.         if (null == idArray)
  349.         {
  350.             return null;
  351.         }

  352.         int assetCount = idArray.length;
  353.         double[][] covarianceMatrix = new double[assetCount][assetCount];

  354.         if (0 == assetCount)
  355.         {
  356.             return null;
  357.         }

  358.         for (int assetIndexI = 0;
  359.             assetIndexI < assetCount;
  360.             ++assetIndexI)
  361.         {
  362.             org.drip.portfolioconstruction.params.AssetStatisticalProperties assetStatisticalPropertiesI =
  363.                 assetStatisticalProperties (idArray[assetIndexI]);

  364.             if (null == assetStatisticalPropertiesI)
  365.             {
  366.                 return null;
  367.             }

  368.             double dblVarianceI = assetStatisticalPropertiesI.variance();

  369.             for (int assetIndexJ = 0;
  370.                 assetIndexJ < assetCount;
  371.                 ++assetIndexJ)
  372.             {
  373.                 org.drip.portfolioconstruction.params.AssetStatisticalProperties assetStatisticalPropertiesJ
  374.                     = assetStatisticalProperties (
  375.                         idArray[assetIndexJ]
  376.                     );

  377.                 if (null == assetStatisticalPropertiesJ)
  378.                 {
  379.                     return null;
  380.                 }

  381.                 try
  382.                 {
  383.                     covarianceMatrix[assetIndexI][assetIndexJ] = java.lang.Math.sqrt (
  384.                         dblVarianceI * assetStatisticalPropertiesJ.variance()
  385.                     ) * correlation (
  386.                         idArray[assetIndexI],
  387.                         idArray[assetIndexJ]
  388.                     );
  389.                 }
  390.                 catch (java.lang.Exception e)
  391.                 {
  392.                     e.printStackTrace();

  393.                     return null;
  394.                 }
  395.             }
  396.         }

  397.         return covarianceMatrix;
  398.     }
  399. }