QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
hullwhite.cpp
Go to the documentation of this file.
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
25
26using std::exp;
27using std::sqrt;
28
29namespace QuantLib {
30
32 Real a, Real sigma)
33 : Vasicek(termStructure->forwardRate(0.0, 0.0, Continuous, NoFrequency),
34 a, 0.0, sigma, 0.0),
35 TermStructureConsistentModel(termStructure) {
36 b_ = NullParameter();
39
41 }
42
43 ext::shared_ptr<Lattice> HullWhite::tree(const TimeGrid& grid) const {
44
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
54 ext::shared_ptr<NumericalImpl> impl =
55 ext::dynamic_pointer_cast<NumericalImpl>(phi.implementation());
56 impl->reset();
57 for (Size i=0; i<(grid.size() - 1); i++) {
58 Real discountBond = termStructure()->discount(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,0);
64 Real value = 0.0;
65 for (Size j=0; j<size; j++) {
66 value += statePrices[j]*std::exp(-x*dt);
67 x += dx;
68 }
69 value = std::log(value/discountBond)/dt;
70 impl->set(grid[i], value);
71 }
72 return numericTree;
73 }
74
76 DiscountFactor discount1 = termStructure()->discount(t);
77 DiscountFactor discount2 = termStructure()->discount(T);
78 Rate forward = termStructure()->forwardRate(t, t,
80 Real temp = sigma()*B(t,T);
81 Real value = B(t,T)*forward - 0.25*temp*temp*B(0.0,2.0*t);
82 return std::exp(value)*discount2/discount1;
83 }
84
87 }
88
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(maturity, bondMaturity)* std::sqrt(maturity);
97 } else {
98 v = sigma()*B(maturity, bondMaturity)*
99 std::sqrt(0.5*(1.0 - std::exp(-2.0*_a*maturity))/_a);
100 }
101 Real f = termStructure()->discount(bondMaturity);
102 Real k = termStructure()->discount(maturity)*strike;
103
104 return blackFormula(type, k, f, v);
105 }
106
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(bondStart, bondMaturity)* std::sqrt(maturity);
115 } else {
116 Real c = exp(-2.0*_a*(bondStart-maturity))
117 - exp(-2.0*_a*bondStart)
118 -2.0*(exp(-_a*(bondStart+bondMaturity-2.0*maturity))
119 - exp(-_a*(bondStart+bondMaturity)))
120 + exp(-2.0*_a*(bondMaturity-maturity))
121 - exp(-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(2.0*_a)) * sqrt(std::max(c, 0.0));
126 }
127 Real f = termStructure()->discount(bondMaturity);
128 Real k = termStructure()->discount(bondStart)*strike;
129
130 return blackFormula(type, k, f, v);
131 }
132
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(-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(-2.0*a*t)) / a *
155 tempDeltaT * tempDeltaT;
156
157 Real tempT = (1.0 - std::exp(-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(-z)) * (futureRate + 1.0/(T-t));
167 }
168
169}
170
Black formula.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Real value(const Array &params, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Definition: model.cpp:117
Shared handle to an observable.
Definition: handle.hpp:41
Short-rate dynamics in the Hull-White model.
Definition: hullwhite.hpp:110
Analytical term-structure fitting parameter .
Definition: hullwhite.hpp:132
HullWhite(const Handle< YieldTermStructure > &termStructure, Real a=0.1, Real sigma=0.01)
Definition: hullwhite.cpp:31
static Rate convexityBias(Real futurePrice, Time t, Time T, Real sigma, Real a)
Definition: hullwhite.cpp:133
Real discountBondOption(Option::Type type, Real strike, Time maturity, Time bondMaturity) const override
Definition: hullwhite.cpp:89
void generateArguments() override
Definition: hullwhite.cpp:85
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Return by default a trinomial recombining tree.
Definition: hullwhite.cpp:43
Real A(Time t, Time T) const override
Definition: hullwhite.cpp:75
Parameter which is always zero
Definition: parameter.hpp:99
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Real discountBond(Time now, Time maturity, Array factors) const override
Recombining trinomial tree discretizing the state variable.
const ext::shared_ptr< Impl > & implementation() const
Definition: parameter.hpp:59
Term-structure consistent model class.
Definition: model.hpp:73
const Handle< YieldTermStructure > & termStructure() const
Definition: model.hpp:77
Deterministic time-dependent parameter used for yield-curve fitting.
Definition: parameter.hpp:145
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
Recombining trinomial tree class.
Vasicek model class
Definition: vasicek.hpp:42
Parameter & b_
Definition: vasicek.hpp:66
Parameter & lambda_
Definition: vasicek.hpp:68
Real B(Time t, Time T) const override
Definition: vasicek.cpp:49
Real sigma() const
Definition: vasicek.hpp:57
Real lambda() const
Definition: vasicek.hpp:56
Real a() const
Definition: vasicek.hpp:54
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
@ NoFrequency
null frequency
Definition: frequency.hpp:37
#define QL_EPSILON
Definition: qldefines.hpp:178
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Real sigma
Hull & White (HW) model.
Definition: any.hpp:35
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
ext::shared_ptr< BlackVolTermStructure > v
Trinomial tree class.