OptionHelper.java

  1. package org.drip.analytics.support;

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

  79. /**
  80.  * <i>OptionHelper</i> contains the collection of the option valuation related utility functions used by the
  81.  * modules.
  82.  *
  83.  *  <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  87.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/support/README.md">Assorted Support and Helper Utilities</a></li>
  89.  *  </ul>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class OptionHelper {

  94.     static class CrossVolatilityQuantoProduct extends org.drip.function.definition.R1ToR1 {
  95.         org.drip.state.volatility.VolatilityCurve _vcForward = null;
  96.         org.drip.state.volatility.VolatilityCurve _vcForwardToDiscount = null;
  97.         org.drip.function.definition.R1ToR1 _r1r1ForwardForwardToDiscountCorrelation = null;

  98.         CrossVolatilityQuantoProduct (
  99.             final org.drip.state.volatility.VolatilityCurve vcForward,
  100.             final org.drip.state.volatility.VolatilityCurve vcForwardToDiscount,
  101.             final org.drip.function.definition.R1ToR1 r1r1ForwardForwardToDiscountCorrelation)
  102.         {
  103.             super (null);

  104.             _vcForward = vcForward;
  105.             _vcForwardToDiscount = vcForwardToDiscount;
  106.             _r1r1ForwardForwardToDiscountCorrelation = r1r1ForwardForwardToDiscountCorrelation;
  107.         }

  108.         @Override public double evaluate (
  109.             final double dblVariate)
  110.             throws java.lang.Exception
  111.         {
  112.             return _vcForward.impliedVol ((int) dblVariate) * _vcForwardToDiscount.impliedVol ((int)
  113.                 dblVariate) * _r1r1ForwardForwardToDiscountCorrelation.evaluate (dblVariate);
  114.         }
  115.     }

  116.     static class CrossVolatilityConvexityExponent extends org.drip.function.definition.R1ToR1 {
  117.         org.drip.state.volatility.VolatilityCurve _vcForward = null;
  118.         org.drip.state.volatility.VolatilityCurve _vcFunding = null;
  119.         double _dblForwardShiftedLogNormalScaler = java.lang.Double.NaN;
  120.         double _dblFundingShiftedLogNormalScaler = java.lang.Double.NaN;
  121.         org.drip.function.definition.R1ToR1 _r1r1ForwardFundingCorrelation = null;

  122.         CrossVolatilityConvexityExponent (
  123.             final org.drip.state.volatility.VolatilityCurve vcForward,
  124.             final double dblForwardShiftedLogNormalScaler,
  125.             final org.drip.state.volatility.VolatilityCurve vcFunding,
  126.             final double dblFundingShiftedLogNormalScaler,
  127.             final org.drip.function.definition.R1ToR1 r1r1ForwardFundingCorrelation)
  128.         {
  129.             super (null);

  130.             _vcForward = vcForward;
  131.             _vcFunding = vcFunding;
  132.             _r1r1ForwardFundingCorrelation = r1r1ForwardFundingCorrelation;
  133.             _dblForwardShiftedLogNormalScaler = dblForwardShiftedLogNormalScaler;
  134.             _dblFundingShiftedLogNormalScaler = dblFundingShiftedLogNormalScaler;
  135.         }

  136.         @Override public double evaluate (
  137.             final double dblVariate)
  138.             throws java.lang.Exception
  139.         {
  140.             double dblForwardShiftedLogNormalScaler = java.lang.Double.isNaN
  141.                 (_dblForwardShiftedLogNormalScaler) ? 1. : _dblForwardShiftedLogNormalScaler;
  142.             double dblFundingShiftedLogNormalScaler = java.lang.Double.isNaN
  143.                 (_dblFundingShiftedLogNormalScaler) ? 1. : _dblFundingShiftedLogNormalScaler;

  144.             return _r1r1ForwardFundingCorrelation.evaluate (dblVariate) * _vcFunding.impliedVol ((int)
  145.                 dblVariate) * _vcFunding.impliedVol ((int) dblVariate) * dblFundingShiftedLogNormalScaler *
  146.                     dblForwardShiftedLogNormalScaler - _vcForward.impliedVol ((int) dblVariate) *
  147.                         _vcForward.impliedVol ((int) dblVariate) * dblForwardShiftedLogNormalScaler *
  148.                             dblForwardShiftedLogNormalScaler;
  149.         }
  150.     }

  151.     static class PeriodVariance extends org.drip.function.definition.R1ToR1 {
  152.         org.drip.state.volatility.VolatilityCurve _vc = null;

  153.         PeriodVariance (
  154.             final org.drip.state.volatility.VolatilityCurve vc)
  155.         {
  156.             super (null);

  157.             _vc = vc;
  158.         }

  159.         @Override public double evaluate (
  160.             final double dblVariate)
  161.             throws java.lang.Exception
  162.         {
  163.             return _vc.impliedVol ((int) dblVariate) * _vc.impliedVol ((int) dblVariate);
  164.         }
  165.     }

  166.     /**
  167.      * Compute the Integrated Surface Variance given the corresponding volatility and the date spans
  168.      *
  169.      * @param csqs Market Parameters
  170.      * @param strCustomMetricLabel Custom Metric Label
  171.      * @param iStartDate Evolution Start Date
  172.      * @param iEndDate Evolution End Date
  173.      *
  174.      * @return The Integrated Volatility Surface
  175.      *
  176.      * @throws java.lang.Exception Thrown if inputs are invalid
  177.      */

  178.     public static final double IntegratedSurfaceVariance (
  179.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  180.         final java.lang.String strCustomMetricLabel,
  181.         final int iStartDate,
  182.         final int iEndDate)
  183.         throws java.lang.Exception
  184.     {
  185.         if (iEndDate < iStartDate)
  186.             throw new java.lang.Exception ("OptionHelper::IntegratedSurfaceVariance => Invalid Inputs");

  187.         if (null == csqs || null == strCustomMetricLabel || strCustomMetricLabel.isEmpty() || iEndDate ==
  188.             iStartDate)
  189.             return 0.;

  190.         org.drip.state.volatility.VolatilityCurve vc = csqs.customVolatility
  191.             (org.drip.state.identifier.CustomLabel.Standard (strCustomMetricLabel));

  192.         return null != vc ? new PeriodVariance (vc).integrate (iStartDate, iEndDate) / 365.25 : 0.;
  193.     }

  194.     /**
  195.      * Compute the Integrated Surface Variance given the corresponding volatility and the date spans
  196.      *
  197.      * @param vc The Volatility Curve
  198.      * @param iStartDate Evolution Start Date
  199.      * @param iEndDate Evolution End Date
  200.      *
  201.      * @return The Integrated Volatility Surface
  202.      *
  203.      * @throws java.lang.Exception Thrown if inputs are invalid
  204.      */

  205.     public static final double IntegratedSurfaceVariance (
  206.         final org.drip.state.volatility.VolatilityCurve vc,
  207.         final int iStartDate,
  208.         final int iEndDate)
  209.         throws java.lang.Exception
  210.     {
  211.         if (null == vc || iEndDate < iStartDate)
  212.             throw new java.lang.Exception ("OptionHelper::IntegratedSurfaceVariance => Invalid Inputs");

  213.         return null != vc ? new PeriodVariance (vc).integrate (iStartDate, iEndDate) / 365.25 : 0.;
  214.     }

  215.     /**
  216.      * Compute the Integrated Cross Volatility Quanto Product given the corresponding volatility and the
  217.      *  correlation curves, and the date spans
  218.      *
  219.      * @param vc1 Volatility Curve #1
  220.      * @param vc2 Volatility Curve #2
  221.      * @param r1r1Correlation Correlation Curve
  222.      * @param iStartDate Evolution Start Date
  223.      * @param iEndDate Evolution End Date
  224.      *
  225.      * @return The Integrated Cross Volatility Quanto Product
  226.      *
  227.      * @throws java.lang.Exception Thrown if inputs are invalid
  228.      */

  229.     public static final double IntegratedCrossVolQuanto (
  230.         final org.drip.state.volatility.VolatilityCurve vc1,
  231.         final org.drip.state.volatility.VolatilityCurve vc2,
  232.         final org.drip.function.definition.R1ToR1 r1r1Correlation,
  233.         final int iStartDate,
  234.         final int iEndDate)
  235.         throws java.lang.Exception
  236.     {
  237.         if (iEndDate < iStartDate)
  238.             throw new java.lang.Exception ("OptionHelper::IntegratedCrossVolQuanto => Invalid Inputs");

  239.         return null == vc1 || null == vc2 || null == r1r1Correlation ? 0. : new CrossVolatilityQuantoProduct
  240.             (vc1, vc2, r1r1Correlation).integrate (iStartDate, iEndDate) / 365.25;
  241.     }

  242.     /**
  243.      * Compute the Integrated FRA Cross Volatility Convexity Exponent given the corresponding volatility and
  244.      *  the correlation Curves, and the date spans
  245.      *
  246.      * @param vcForward Volatility Term Structure of the Funding Rate
  247.      * @param vcFunding Volatility Term Structure of the Forward Rate
  248.      * @param r1r1ForwardFundingCorrelation Correlation Term Structure between the Forward and the Funding
  249.      *  States
  250.      * @param dblForwardShiftedLogNormalScaler Scaling for the Forward Log Normal Volatility
  251.      * @param dblFundingShiftedLogNormalScaler Scaling for the Funding Log Normal Volatility
  252.      * @param iStartDate Evolution Start Date
  253.      * @param iEndDate Evolution End Date
  254.      *
  255.      * @return The Integrated FRA Cross Volatility Convexity Exponent
  256.      *
  257.      * @throws java.lang.Exception Thrown if inputs are invalid
  258.      */

  259.     public static final double IntegratedFRACrossVolConvexityExponent (
  260.         final org.drip.state.volatility.VolatilityCurve vcForward,
  261.         final org.drip.state.volatility.VolatilityCurve vcFunding,
  262.         final org.drip.function.definition.R1ToR1 r1r1ForwardFundingCorrelation,
  263.         final double dblForwardShiftedLogNormalScaler,
  264.         final double dblFundingShiftedLogNormalScaler,
  265.         final int iStartDate,
  266.         final int iEndDate)
  267.         throws java.lang.Exception
  268.     {
  269.         if (iEndDate < iStartDate)
  270.             throw new java.lang.Exception
  271.                 ("OptionHelper::IntegratedFRACrossVolConvexityExponent => Invalid Inputs");

  272.         return null == vcFunding || null == vcForward || null == r1r1ForwardFundingCorrelation ? 0. : new
  273.             CrossVolatilityConvexityExponent (vcForward, dblForwardShiftedLogNormalScaler, vcFunding,
  274.                 dblFundingShiftedLogNormalScaler, r1r1ForwardFundingCorrelation).integrate (iStartDate,
  275.                     iEndDate) / 365.25;
  276.     }

  277.     /**
  278.      * Compute the Integrated Cross Volatility Quanto Product given the corresponding volatility and the
  279.      *  correlation Curves and the date spans
  280.      *
  281.      * @param csqs Market Parameters
  282.      * @param strCustomMetricLabel1 Custom Metric Label #1
  283.      * @param strCustomMetricLabel2 Custom Metric Label #2
  284.      * @param iStartDate Evolution Start Date
  285.      * @param iEndDate Evolution End Date
  286.      *
  287.      * @return The Integrated Cross Volatility Quanto Product
  288.      *
  289.      * @throws java.lang.Exception Thrown if inputs are invalid
  290.      */

  291.     public static final double IntegratedCrossVolQuanto (
  292.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  293.         final java.lang.String strCustomMetricLabel1,
  294.         final java.lang.String strCustomMetricLabel2,
  295.         final int iStartDate,
  296.         final int iEndDate)
  297.         throws java.lang.Exception
  298.     {
  299.         if (iEndDate < iStartDate)
  300.             throw new java.lang.Exception ("OptionHelper::IntegratedCrossVolQuanto => Invalid Inputs");

  301.         if (null == csqs || null == strCustomMetricLabel1 || strCustomMetricLabel1.isEmpty() || null ==
  302.             strCustomMetricLabel2 || strCustomMetricLabel2.isEmpty() || iEndDate == iStartDate)
  303.             return 0.;

  304.         org.drip.state.identifier.CustomLabel cml1 =
  305.             org.drip.state.identifier.CustomLabel.Standard (strCustomMetricLabel1);

  306.         org.drip.state.identifier.CustomLabel cml2 =
  307.             org.drip.state.identifier.CustomLabel.Standard (strCustomMetricLabel2);

  308.         return null == cml1 || null == cml2 ? 0. : IntegratedCrossVolQuanto (csqs.customVolatility (cml1),
  309.             csqs.customVolatility (cml2), csqs.customCustomCorrelation (cml1, cml2), iStartDate, iEndDate);
  310.     }

  311.     /**
  312.      * Compute the Multiplicative Cross Volatility Quanto Product given the corresponding volatility and the
  313.      *  correlation Curves, and the date spans
  314.      *
  315.      * @param csqs Market Parameters
  316.      * @param strCustomMetricLabel1 Custom Metric Label #1
  317.      * @param strCustomMetricLabel2 Custom Metric Label #2
  318.      * @param iStartDate Evolution Start Date
  319.      * @param iEndDate Evolution End Date
  320.      *
  321.      * @return The Multiplicative Cross Volatility Quanto Product
  322.      *
  323.      * @throws java.lang.Exception Thrown if inputs are invalid
  324.      */

  325.     public static final double MultiplicativeCrossVolQuanto (
  326.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  327.         final java.lang.String strCustomMetricLabel1,
  328.         final java.lang.String strCustomMetricLabel2,
  329.         final int iStartDate,
  330.         final int iEndDate)
  331.         throws java.lang.Exception
  332.     {
  333.         return java.lang.Math.exp (-1. * IntegratedCrossVolQuanto (csqs, strCustomMetricLabel1,
  334.             strCustomMetricLabel2, iStartDate, iEndDate));
  335.     }

  336.     /**
  337.      * Compute the Integrated FRA Cross Volatility Convexity Adjuster given the corresponding volatility and
  338.      *  the correlation Curves and the date spans
  339.      *
  340.      * @param csqs Market Parameters
  341.      * @param forwardLabel Forward Latent State Label
  342.      * @param fundingLabel Funding Latent State Label
  343.      * @param dblForwardShiftedLogNormalScaler Scaling for the Forward Log Normal Volatility
  344.      * @param dblFundingShiftedLogNormalScaler Scaling for the Funding Log Normal Volatility
  345.      * @param iStartDate Evolution Start Date
  346.      * @param iEndDate Evolution End Date
  347.      *
  348.      * @return The Integrated FRA Cross Volatility Convexity Adjuster
  349.      *
  350.      * @throws java.lang.Exception Thrown if inputs are invalid
  351.      */

  352.     public static final double IntegratedFRACrossVolConvexityAdjuster (
  353.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  354.         final org.drip.state.identifier.ForwardLabel forwardLabel,
  355.         final org.drip.state.identifier.FundingLabel fundingLabel,
  356.         final double dblForwardShiftedLogNormalScaler,
  357.         final double dblFundingShiftedLogNormalScaler,
  358.         final int iStartDate,
  359.         final int iEndDate)
  360.         throws java.lang.Exception
  361.     {
  362.         if (iEndDate < iStartDate)
  363.             throw new java.lang.Exception
  364.                 ("OptionHelper::IntegratedFRACrossVolConvexityAdjuster => Invalid Inputs");

  365.         return null == csqs || null == forwardLabel || null == fundingLabel || iEndDate == iStartDate ? 0. :
  366.             IntegratedFRACrossVolConvexityExponent (csqs.fundingVolatility (fundingLabel),
  367.                 csqs.forwardVolatility (forwardLabel), csqs.forwardFundingCorrelation (forwardLabel,
  368.                     fundingLabel), dblFundingShiftedLogNormalScaler, dblForwardShiftedLogNormalScaler,
  369.                         iStartDate, iEndDate);
  370.     }
  371. }