| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2003, 2004, 2005, 2006 Ferdinando Ametrano |
| 5 | Copyright (C) 2006 StatPro Italia srl |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | /*! \file blackcalculator.hpp |
| 22 | \brief Black-formula calculator class |
| 23 | */ |
| 24 | |
| 25 | #ifndef quantlib_blackcalculator_hpp |
| 26 | #define quantlib_blackcalculator_hpp |
| 27 | |
| 28 | #include <ql/instruments/payoffs.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | //! Black 1976 calculator class |
| 33 | /*! \bug When the variance is null, division by zero occur during |
| 34 | the calculation of delta, delta forward, gamma, gamma |
| 35 | forward, rho, dividend rho, vega, and strike sensitivity. |
| 36 | */ |
| 37 | class BlackCalculator { |
| 38 | private: |
| 39 | class Calculator; |
| 40 | public: |
| 41 | BlackCalculator(const ext::shared_ptr<StrikedTypePayoff>& payoff, |
| 42 | Real forward, |
| 43 | Real stdDev, |
| 44 | Real discount = 1.0); |
| 45 | BlackCalculator(Option::Type optionType, |
| 46 | Real strike, |
| 47 | Real forward, |
| 48 | Real stdDev, |
| 49 | Real discount = 1.0); |
| 50 | virtual ~BlackCalculator() = default; |
| 51 | |
| 52 | Real value() const; |
| 53 | |
| 54 | /*! Sensitivity to change in the underlying forward price. */ |
| 55 | Real deltaForward() const; |
| 56 | /*! Sensitivity to change in the underlying spot price. */ |
| 57 | virtual Real delta(Real spot) const; |
| 58 | |
| 59 | /*! Sensitivity in percent to a percent change in the |
| 60 | underlying forward price. */ |
| 61 | Real elasticityForward() const; |
| 62 | /*! Sensitivity in percent to a percent change in the |
| 63 | underlying spot price. */ |
| 64 | virtual Real elasticity(Real spot) const; |
| 65 | |
| 66 | /*! Second order derivative with respect to change in the |
| 67 | underlying forward price. */ |
| 68 | Real gammaForward() const; |
| 69 | /*! Second order derivative with respect to change in the |
| 70 | underlying spot price. */ |
| 71 | virtual Real gamma(Real spot) const; |
| 72 | |
| 73 | /*! Sensitivity to time to maturity. */ |
| 74 | virtual Real theta(Real spot, |
| 75 | Time maturity) const; |
| 76 | /*! Sensitivity to time to maturity per day, |
| 77 | assuming 365 day per year. */ |
| 78 | virtual Real thetaPerDay(Real spot, |
| 79 | Time maturity) const; |
| 80 | |
| 81 | /*! Sensitivity to volatility. */ |
| 82 | Real vega(Time maturity) const; |
| 83 | |
| 84 | /*! Sensitivity to discounting rate. */ |
| 85 | Real rho(Time maturity) const; |
| 86 | |
| 87 | /*! Sensitivity to dividend/growth rate. */ |
| 88 | Real dividendRho(Time maturity) const; |
| 89 | |
| 90 | /*! Probability of being in the money in the bond martingale |
| 91 | measure, i.e. N(d2). |
| 92 | It is a risk-neutral probability, not the real world one. |
| 93 | */ |
| 94 | Real itmCashProbability() const; |
| 95 | |
| 96 | /*! Probability of being in the money in the asset martingale |
| 97 | measure, i.e. N(d1). |
| 98 | It is a risk-neutral probability, not the real world one. |
| 99 | */ |
| 100 | Real itmAssetProbability() const; |
| 101 | |
| 102 | /*! Sensitivity to strike. */ |
| 103 | Real strikeSensitivity() const; |
| 104 | |
| 105 | /*! gamma w.r.t. strike. */ |
| 106 | Real strikeGamma() const; |
| 107 | |
| 108 | Real alpha() const; |
| 109 | Real beta() const; |
| 110 | protected: |
| 111 | void initialize(const ext::shared_ptr<StrikedTypePayoff>& p); |
| 112 | Real strike_, forward_, stdDev_, discount_, variance_; |
| 113 | Real d1_, d2_; |
| 114 | Real alpha_, beta_, DalphaDd1_, DbetaDd2_; |
| 115 | Real n_d1_, cum_d1_, n_d2_, cum_d2_; |
| 116 | Real x_, DxDs_, DxDstrike_; |
| 117 | }; |
| 118 | |
| 119 | // inline |
| 120 | inline Real BlackCalculator::thetaPerDay(Real spot, |
| 121 | Time maturity) const { |
| 122 | return theta(spot, maturity)/365.0; |
| 123 | } |
| 124 | |
| 125 | inline Real BlackCalculator::itmCashProbability() const { |
| 126 | return cum_d2_; |
| 127 | } |
| 128 | |
| 129 | inline Real BlackCalculator::itmAssetProbability() const { |
| 130 | return cum_d1_; |
| 131 | } |
| 132 | |
| 133 | inline Real BlackCalculator::alpha() const { |
| 134 | return alpha_; |
| 135 | } |
| 136 | |
| 137 | inline Real BlackCalculator::beta() const { |
| 138 | return beta_; |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | #endif |
| 144 | |