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#include <ql/models/shortrate/onefactormodels/hullwhite.hpp>
23#include <ql/methods/lattices/trinomialtree.hpp>
24#include <ql/pricingengines/blackformula.hpp>
25
26using std::exp;
27using std::sqrt;
28
29namespace QuantLib {
30
31 HullWhite::HullWhite(const Handle<YieldTermStructure>& termStructure,
32 Real a, Real sigma)
33 : Vasicek(termStructure->forwardRate(t1: 0.0, t2: 0.0, comp: Continuous, freq: NoFrequency),
34 a, 0.0, sigma, 0.0),
35 TermStructureConsistentModel(termStructure) {
36 b_ = NullParameter();
37 lambda_ = NullParameter();
38 HullWhite::generateArguments();
39
40 registerWith(h: termStructure);
41 }
42
43 ext::shared_ptr<Lattice> HullWhite::tree(const TimeGrid& grid) const {
44
45 TermStructureFittingParameter phi(termStructure());
46 ext::shared_ptr<ShortRateDynamics> numericDynamics(
47 new Dynamics(phi, a(), sigma()));
48 ext::shared_ptr<TrinomialTree> trinomial(
49 new TrinomialTree(numericDynamics->process(), grid));
50 ext::shared_ptr<ShortRateTree> numericTree(
51 new ShortRateTree(trinomial, numericDynamics, grid));
52
53 typedef TermStructureFittingParameter::NumericalImpl NumericalImpl;
54 ext::shared_ptr<NumericalImpl> impl =
55 ext::dynamic_pointer_cast<NumericalImpl>(r: phi.implementation());
56 impl->reset();
57 for (Size i=0; i<(grid.size() - 1); i++) {
58 Real discountBond = termStructure()->discount(t: grid[i+1]);
59 const Array& statePrices = numericTree->statePrices(i);
60 Size size = numericTree->size(i);
61 Time dt = numericTree->timeGrid().dt(i);
62 Real dx = trinomial->dx(i);
63 Real x = trinomial->underlying(i,index: 0);
64 Real value = 0.0;
65 for (Size j=0; j<size; j++) {
66 value += statePrices[j]*std::exp(x: -x*dt);
67 x += dx;
68 }
69 value = std::log(x: value/discountBond)/dt;
70 impl->set(t: grid[i], x: value);
71 }
72 return numericTree;
73 }
74
75 Real HullWhite::A(Time t, Time T) const {
76 DiscountFactor discount1 = termStructure()->discount(t);
77 DiscountFactor discount2 = termStructure()->discount(t: T);
78 Rate forward = termStructure()->forwardRate(t1: t, t2: t,
79 comp: Continuous, freq: NoFrequency);
80 Real temp = sigma()*B(t,T);
81 Real value = B(t,T)*forward - 0.25*temp*temp*B(t: 0.0,T: 2.0*t);
82 return std::exp(x: value)*discount2/discount1;
83 }
84
85 void HullWhite::generateArguments() {
86 phi_ = FittingParameter(termStructure(), a(), sigma());
87 }
88
89 Real HullWhite::discountBondOption(Option::Type type, Real strike,
90 Time maturity,
91 Time bondMaturity) const {
92
93 Real _a = a();
94 Real v;
95 if (_a < std::sqrt(QL_EPSILON)) {
96 v = sigma()*B(t: maturity, T: bondMaturity)* std::sqrt(x: maturity);
97 } else {
98 v = sigma()*B(t: maturity, T: bondMaturity)*
99 std::sqrt(x: 0.5*(1.0 - std::exp(x: -2.0*_a*maturity))/_a);
100 }
101 Real f = termStructure()->discount(t: bondMaturity);
102 Real k = termStructure()->discount(t: maturity)*strike;
103
104 return blackFormula(optionType: type, strike: k, forward: f, stdDev: v);
105 }
106
107 Real HullWhite::discountBondOption(Option::Type type, Real strike,
108 Time maturity, Time bondStart,
109 Time bondMaturity) const {
110
111 Real _a = a();
112 Real v;
113 if (_a < std::sqrt(QL_EPSILON)) {
114 v = sigma()*B(t: bondStart, T: bondMaturity)* std::sqrt(x: maturity);
115 } else {
116 Real c = exp(x: -2.0*_a*(bondStart-maturity))
117 - exp(x: -2.0*_a*bondStart)
118 -2.0*(exp(x: -_a*(bondStart+bondMaturity-2.0*maturity))
119 - exp(x: -_a*(bondStart+bondMaturity)))
120 + exp(x: -2.0*_a*(bondMaturity-maturity))
121 - exp(x: -2.0*_a*bondMaturity);
122 // The above should always be positive, but due to
123 // numerical errors it can be a very small negative number.
124 // We floor it at 0 to avoid NaNs.
125 v = sigma()/(_a*sqrt(x: 2.0*_a)) * sqrt(x: std::max(a: c, b: 0.0));
126 }
127 Real f = termStructure()->discount(t: bondMaturity);
128 Real k = termStructure()->discount(t: bondStart)*strike;
129
130 return blackFormula(optionType: type, strike: k, forward: f, stdDev: v);
131 }
132
133 Rate HullWhite::convexityBias(Real futuresPrice,
134 Time t,
135 Time T,
136 Real sigma,
137 Real a) {
138 QL_REQUIRE(futuresPrice>=0.0,
139 "negative futures price (" << futuresPrice << ") not allowed");
140 QL_REQUIRE(t>=0.0,
141 "negative t (" << t << ") not allowed");
142 QL_REQUIRE(T>=t,
143 "T (" << T << ") must not be less than t (" << t << ")");
144 QL_REQUIRE(sigma>=0.0,
145 "negative sigma (" << sigma << ") not allowed");
146 QL_REQUIRE(a>=0.0,
147 "negative a (" << a << ") not allowed");
148
149 Time deltaT = (T-t);
150 Real tempDeltaT = (1.-std::exp(x: -a*deltaT)) / a;
151 Real halfSigmaSquare = sigma*sigma/2.0;
152
153 // lambda adjusts for the fact that the underlying is an interest rate
154 Real lambda = halfSigmaSquare * (1.-std::exp(x: -2.0*a*t)) / a *
155 tempDeltaT * tempDeltaT;
156
157 Real tempT = (1.0 - std::exp(x: -a*t)) / a;
158
159 // phi is the MtM adjustment
160 Real phi = halfSigmaSquare * tempDeltaT * tempT * tempT;
161
162 // the adjustment
163 Real z = lambda + phi;
164
165 Rate futureRate = (100.0-futuresPrice)/100.0;
166 return (1.0-std::exp(x: -z)) * (futureRate + 1.0/(T-t));
167 }
168
169}
170
171

source code of quantlib/ql/models/shortrate/onefactormodels/hullwhite.cpp