| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb |
| 5 | Copyright (C) 2006 Chiara Fornarola |
| 6 | Copyright (C) 2007 StatPro Italia srl |
| 7 | |
| 8 | This file is part of QuantLib, a free-software/open-source library |
| 9 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 10 | |
| 11 | QuantLib is free software: you can redistribute it and/or modify it |
| 12 | under the terms of the QuantLib license. You should have received a |
| 13 | copy of the license along with this program; if not, please email |
| 14 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 15 | <http://quantlib.org/license.shtml>. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 20 | */ |
| 21 | |
| 22 | /*! \file hullwhite.hpp |
| 23 | \brief Hull & White (HW) model |
| 24 | */ |
| 25 | |
| 26 | #ifndef quantlib_hull_white_hpp |
| 27 | #define quantlib_hull_white_hpp |
| 28 | |
| 29 | #include <ql/models/shortrate/onefactormodels/vasicek.hpp> |
| 30 | #include <utility> |
| 31 | |
| 32 | namespace QuantLib { |
| 33 | |
| 34 | //! Single-factor Hull-White (extended %Vasicek) model class. |
| 35 | /*! This class implements the standard single-factor Hull-White model |
| 36 | defined by |
| 37 | \f[ |
| 38 | dr_t = (\theta(t) - \alpha r_t)dt + \sigma dW_t |
| 39 | \f] |
| 40 | where \f$ \alpha \f$ and \f$ \sigma \f$ are constants. |
| 41 | |
| 42 | \test calibration results are tested against cached values |
| 43 | |
| 44 | \bug When the term structure is relinked, the r0 parameter of |
| 45 | the underlying Vasicek model is not updated. |
| 46 | |
| 47 | \ingroup shortrate |
| 48 | */ |
| 49 | class HullWhite : public Vasicek, public TermStructureConsistentModel { |
| 50 | public: |
| 51 | HullWhite(const Handle<YieldTermStructure>& termStructure, |
| 52 | Real a = 0.1, Real sigma = 0.01); |
| 53 | |
| 54 | ext::shared_ptr<Lattice> tree(const TimeGrid& grid) const override; |
| 55 | |
| 56 | ext::shared_ptr<ShortRateDynamics> dynamics() const override; |
| 57 | |
| 58 | Real discountBondOption(Option::Type type, |
| 59 | Real strike, |
| 60 | Time maturity, |
| 61 | Time bondMaturity) const override; |
| 62 | |
| 63 | Real discountBondOption(Option::Type type, |
| 64 | Real strike, |
| 65 | Time maturity, |
| 66 | Time bondStart, |
| 67 | Time bondMaturity) const override; |
| 68 | |
| 69 | /*! Futures convexity bias (i.e., the difference between |
| 70 | futures implied rate and forward rate) calculated as in |
| 71 | G. Kirikos, D. Novak, "Convexity Conundrums", Risk |
| 72 | Magazine, March 1997. |
| 73 | |
| 74 | \note t and T should be expressed in yearfraction using |
| 75 | deposit day counter, F_quoted is futures' market price. |
| 76 | */ |
| 77 | static Rate convexityBias(Real futurePrice, |
| 78 | Time t, |
| 79 | Time T, |
| 80 | Real sigma, |
| 81 | Real a); |
| 82 | |
| 83 | static std::vector<bool> FixedReversion() { |
| 84 | std::vector<bool> c(2); |
| 85 | c[0] = true; c[1] = false; |
| 86 | return c; |
| 87 | } |
| 88 | |
| 89 | protected: |
| 90 | void generateArguments() override; |
| 91 | |
| 92 | Real A(Time t, Time T) const override; |
| 93 | |
| 94 | private: |
| 95 | class Dynamics; |
| 96 | class FittingParameter; |
| 97 | |
| 98 | Parameter phi_; |
| 99 | }; |
| 100 | |
| 101 | //! Short-rate dynamics in the Hull-White model |
| 102 | /*! The short-rate is here |
| 103 | \f[ |
| 104 | r_t = \varphi(t) + x_t |
| 105 | \f] |
| 106 | where \f$ \varphi(t) \f$ is the deterministic time-dependent |
| 107 | parameter used for term-structure fitting and \f$ x_t \f$ is the |
| 108 | state variable following an Ornstein-Uhlenbeck process. |
| 109 | */ |
| 110 | class HullWhite::Dynamics : public OneFactorModel::ShortRateDynamics { |
| 111 | public: |
| 112 | Dynamics(Parameter fitting, Real a, Real sigma) |
| 113 | : ShortRateDynamics( |
| 114 | ext::shared_ptr<StochasticProcess1D>(new OrnsteinUhlenbeckProcess(a, sigma))), |
| 115 | fitting_(std::move(fitting)) {} |
| 116 | |
| 117 | Real variable(Time t, Rate r) const override { return r - fitting_(t); } |
| 118 | Real shortRate(Time t, Real x) const override { return x + fitting_(t); } |
| 119 | |
| 120 | private: |
| 121 | Parameter fitting_; |
| 122 | }; |
| 123 | |
| 124 | //! Analytical term-structure fitting parameter \f$ \varphi(t) \f$. |
| 125 | /*! \f$ \varphi(t) \f$ is analytically defined by |
| 126 | \f[ |
| 127 | \varphi(t) = f(t) + \frac{1}{2}[\frac{\sigma(1-e^{-at})}{a}]^2, |
| 128 | \f] |
| 129 | where \f$ f(t) \f$ is the instantaneous forward rate at \f$ t \f$. |
| 130 | */ |
| 131 | class HullWhite::FittingParameter |
| 132 | : public TermStructureFittingParameter { |
| 133 | private: |
| 134 | class Impl final : public Parameter::Impl { |
| 135 | public: |
| 136 | Impl(Handle<YieldTermStructure> termStructure, Real a, Real sigma) |
| 137 | : termStructure_(std::move(termStructure)), a_(a), sigma_(sigma) {} |
| 138 | |
| 139 | Real value(const Array&, Time t) const override { |
| 140 | Rate forwardRate = |
| 141 | termStructure_->forwardRate(t1: t, t2: t, comp: Continuous, freq: NoFrequency); |
| 142 | Real temp = a_ < std::sqrt(QL_EPSILON) ? |
| 143 | Real(sigma_*t) : |
| 144 | Real(sigma_*(1.0 - std::exp(x: -a_*t))/a_); |
| 145 | return (forwardRate + 0.5*temp*temp); |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | Handle<YieldTermStructure> termStructure_; |
| 150 | Real a_, sigma_; |
| 151 | }; |
| 152 | public: |
| 153 | FittingParameter(const Handle<YieldTermStructure>& termStructure, |
| 154 | Real a, Real sigma) |
| 155 | : TermStructureFittingParameter(ext::shared_ptr<Parameter::Impl>( |
| 156 | new FittingParameter::Impl(termStructure, a, sigma))) {} |
| 157 | }; |
| 158 | |
| 159 | |
| 160 | // inline definitions |
| 161 | |
| 162 | inline ext::shared_ptr<OneFactorModel::ShortRateDynamics> |
| 163 | HullWhite::dynamics() const { |
| 164 | return ext::shared_ptr<ShortRateDynamics>( |
| 165 | new Dynamics(phi_, a(), sigma())); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | |
| 171 | #endif |
| 172 | |
| 173 | |