KernelDensityEstimationL1.java

  1. package org.drip.sequence.custom;

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

  5. /*!
  6.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  11.  *
  12.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  13.  *      calculations, and portfolio construction within and across fixed income, credit, commodity, equity,
  14.  *      FX, and structured products.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three main modules:
  19.  *  
  20.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  23.  *
  24.  *  DROP Analytics Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Asset Backed Analytics
  27.  *  - XVA Analytics
  28.  *  - Exposure and Margin Analytics
  29.  *
  30.  *  DROP Portfolio Core implements libraries for the following:
  31.  *  - Asset Allocation Analytics
  32.  *  - Transaction Cost Analytics
  33.  *
  34.  *  DROP Numerical Core implements libraries for the following:
  35.  *  - Statistical Learning Library
  36.  *  - Numerical Optimizer Library
  37.  *  - Machine Learning Library
  38.  *  - Spline Builder Library
  39.  *
  40.  *  Documentation for DROP is Spread Over:
  41.  *
  42.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  43.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  44.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  45.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  46.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  47.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  48.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  49.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  50.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  51.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  52.  *
  53.  *  Licensed under the Apache License, Version 2.0 (the "License");
  54.  *      you may not use this file except in compliance with the License.
  55.  *  
  56.  *  You may obtain a copy of the License at
  57.  *      http://www.apache.org/licenses/LICENSE-2.0
  58.  *  
  59.  *  Unless required by applicable law or agreed to in writing, software
  60.  *      distributed under the License is distributed on an "AS IS" BASIS,
  61.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  62.  *  
  63.  *  See the License for the specific language governing permissions and
  64.  *      limitations under the License.
  65.  */

  66. /**
  67.  * <i>KernelDensityEstimationL1</i> implements the L1 Error Scheme Estimation for a Multivariate Kernel
  68.  * Density Estimator with Focus on establishing targeted Variate-Specific and Agnostic Bounds.
  69.  *
  70.  * <br><br>
  71.  *  <ul>
  72.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  73.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  74.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sequence">Sequence</a></li>
  75.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sequence/custom">Custom</a></li>
  76.  *  </ul>
  77.  * <br><br>
  78.  *
  79.  * @author Lakshmi Krishnamurthy
  80.  */

  81. public class KernelDensityEstimationL1 extends org.drip.sequence.functional.BoundedMultivariateRandom {
  82.     private int _iSampleSize = -1;
  83.     private double _dblSmoothingParameter = java.lang.Double.NaN;
  84.     private org.drip.function.definition.R1ToR1 _auKernel = null;
  85.     private org.drip.function.definition.R1ToR1 _auResponse = null;

  86.     /**
  87.      * KernelDensityEstimationL1 Constructor
  88.      *
  89.      * @param auKernel The Kernel Function
  90.      * @param dblSmoothingParameter The Smoothing Parameter
  91.      * @param iSampleSize The Sample Size
  92.      * @param auResponse The Response Function
  93.      *
  94.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  95.      */

  96.     public KernelDensityEstimationL1 (
  97.         final org.drip.function.definition.R1ToR1 auKernel,
  98.         final double dblSmoothingParameter,
  99.         final int iSampleSize,
  100.         final org.drip.function.definition.R1ToR1 auResponse)
  101.         throws java.lang.Exception
  102.     {
  103.         if (null == (_auKernel = auKernel) || !org.drip.numerical.common.NumberUtil.IsValid
  104.             (_dblSmoothingParameter = dblSmoothingParameter) || 0 >= (_iSampleSize = iSampleSize) || null ==
  105.                 (_auResponse = auResponse))
  106.             throw new java.lang.Exception ("KernelDensityEstimationL1 Constructor => Invalid Inputs!");
  107.     }

  108.     /**
  109.      * Retrieve the Kernel Function
  110.      *
  111.      * @return The Kernel Function
  112.      */

  113.     public org.drip.function.definition.R1ToR1 kernelFunction()
  114.     {
  115.         return _auKernel;
  116.     }

  117.     /**
  118.      * Retrieve the Smoothing Parameter
  119.      *
  120.      * @return The Smoothing Parameter
  121.      */

  122.     public double smoothingParameter()
  123.     {
  124.         return _dblSmoothingParameter;
  125.     }

  126.     /**
  127.      * Retrieve the Sample Size
  128.      *
  129.      * @return The Sample Size
  130.      */

  131.     public int sampleSize()
  132.     {
  133.         return _iSampleSize;
  134.     }

  135.     /**
  136.      * Retrieve the Response Function
  137.      *
  138.      * @return The Response Function
  139.      */

  140.     public org.drip.function.definition.R1ToR1 responseFunction()
  141.     {
  142.         return _auResponse;
  143.     }

  144.     @Override public int dimension()
  145.     {
  146.         return org.drip.function.definition.RdToR1.DIMENSION_NOT_FIXED;
  147.     }

  148.     @Override public double evaluate (
  149.         final double[] adblVariate)
  150.         throws java.lang.Exception
  151.     {
  152.         double dblMinVariate = org.drip.numerical.common.NumberUtil.Minimum (adblVariate);

  153.         double dblMaxVariate = org.drip.numerical.common.NumberUtil.Maximum (adblVariate);

  154.         double dblKernelIntegral = 0.;
  155.         int iNumVariate = adblVariate.length;

  156.         for (int i = 0; i < iNumVariate; ++i)
  157.             dblKernelIntegral += _auKernel.integrate ((dblMinVariate - adblVariate[i]) /
  158.                 _dblSmoothingParameter, (dblMaxVariate - adblVariate[i]) / _dblSmoothingParameter);

  159.         return dblKernelIntegral / (_iSampleSize * _dblSmoothingParameter) - _auResponse.integrate
  160.             (dblMinVariate, dblMaxVariate);
  161.     }

  162.     @Override public double targetVariateVarianceBound (
  163.         final int iTargetVariateIndex)
  164.         throws java.lang.Exception
  165.     {
  166.         return 4. / (_iSampleSize * _iSampleSize);
  167.     }
  168. }