21#include <ql/experimental/shortrate/generalizedhullwhite.hpp>
22#include <ql/math/integrals/simpsonintegral.hpp>
23#include <ql/math/interpolations/backwardflatinterpolation.hpp>
24#include <ql/math/solvers1d/brent.hpp>
25#include <ql/methods/lattices/trinomialtree.hpp>
26#include <ql/pricingengines/blackformula.hpp>
34 Real integrateMeanReversion(
const Interpolation &a,
Real t,
Real T) {
37 SimpsonIntegral integrator(1e-5, 1000);
38 Real mr = integrator(a,t,T);
50 class GeneralizedHullWhite::Helper {
55 const Real discountBondPrice,
56 const ext::shared_ptr<ShortRateTree>&
tree,
58 : size_(
tree->size(i)), dt_(
tree->timeGrid().dt(i)), xMin_(xMin), dx_(dx),
59 statePrices_(
tree->statePrices(i)), discountBondPrice_(discountBondPrice),
60 fInverse_(
std::move(fInv)) {}
62 Real operator()(
const Real theta)
const {
65 for (
Size j=0; j<size_; j++) {
77 const Array& statePrices_;
78 Real discountBondPrice_;
84 const std::vector<Date>& speedstructure,
85 const std::vector<Date>& volstructure,
86 const std::vector<Real>& speed,
87 const std::vector<Real>& vol,
89 const ext::function<
Real(
Real)>& fInverse)
91 speedstructure_(speedstructure),
92 volstructure_(volstructure),
93 a_(arguments_[0]), sigma_(arguments_[1]),
94 f_(f), fInverse_(fInverse) {
97 initialize(yieldtermStructure,speedstructure,volstructure,
98 speed,
vol, traits, traits, f, fInverse);
108 sigma_(arguments_[1])
110 Date ref = yieldtermStructure->referenceDate();
111 std::vector<Date> speedstructure,volstructure;
112 std::vector<Real> _a, _sigma;
114 _sigma.push_back(
sigma);
115 speedstructure.push_back(ref);
116 volstructure.push_back(ref);
118 initialize(yieldtermStructure,speedstructure,volstructure,
130 Real lnEt = integrateMeanReversion(
speed_,0,t);
133 Size N = std::min<Size>(
Size((T-t)*365), 2000);
135 Real dt = 0.5*(T-t)/N;
136 Real a,b,c,_t,total=0;
140 for (
Size i=0; i<N; i++) {
144 total += (dt*(2.0/6.0))*(
a+4*b+c);
145 B += (2*dt) / exp(lnEt+total);
154 Real lnEt = integrateMeanReversion(
speed_,0,t);
156 Size N = std::min<Size>(
Size((T-t)*365), 2000);
158 Real dt = 0.5*(T-t)/N;
159 Real a,b,c,_t,lnE=lnEt;
165 for (
Size i=0; i<N; i++) {
175 V += (dt*(2.0/6.0))*(
a+4*b+c);
183 Time bondMaturity)
const
190 Real BtT =
B(maturity,bondMaturity);
191 Real Vr =
V(0,maturity);
192 Real Vp = Vr*BtT*BtT;
206 Real AtT = log(discount2/discount1) + BtT*forward - 0.5*BtT*BtT*Vr;
215 ext::shared_ptr<ShortRateDynamics> numericDynamics(
217 ext::shared_ptr<TrinomialTree> trinomial(
219 ext::shared_ptr<ShortRateTree> numericTree(
222 ext::shared_ptr<NumericalImpl> impl =
230 for (
Size i=0; i<(grid.
size() - 1); i++) {
232 Real xMin = trinomial->underlying(i, 0);
233 Real dx = trinomial->dx(i);
238 impl->set(grid[i],
value);
256 std::vector<bool> fixr(na+nsigma,
false);
257 std::fill(fixr.begin(),fixr.begin()+na,
true);
Size size() const
dimension of the array
Backward-flat interpolation factory and traits.
Real value(const Array ¶ms, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Short-rate dynamics in the generalized Hull-White model.
Analytical term-structure fitting parameter .
Real V(Time t, Time T) const
GeneralizedHullWhite(const Handle< YieldTermStructure > &yieldtermStructure, const std::vector< Date > &speedstructure, const std::vector< Date > &volstructure, const std::vector< Real > &speed, const std::vector< Real > &vol, const ext::function< Real(Real)> &f={}, const ext::function< Real(Real)> &fInverse={})
ext::function< Real(Time)> speed() const
ext::function< Real(Real)> fInverse_
Real B(Time t, Time T) const override
std::vector< bool > fixedReversion() const
vector to pass to 'calibrate' to fit only volatility
Real discountBondOption(Option::Type type, Real strike, Time maturity, Time bondMaturity) const override
Only valid under Hull-White model.
void generateArguments() override
ext::function< Real(Time)> vol() const
ext::function< Real(Real)> f_
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Real A(Time t, Time T) const override
static Real identity(Real x)
void initialize(const Handle< YieldTermStructure > &yieldtermStructure, const std::vector< Date > &speedstructure, const std::vector< Date > &volstructure, const std::vector< Real > &speed, const std::vector< Real > &vol, const SpeedInterpolationTraits &speedtraits, const VolInterpolationTraits &voltraits, const ext::function< Real(Real)> &f, const ext::function< Real(Real)> &fInverse)
Shared handle to an observable.
Linear-interpolation with flat-extrapolation factory and traits
Single-factor affine base class.
Real discountBond(Time now, Time maturity, Array factors) const override
DiscountFactor discount(Time t) const override
Implied discount curve.
Recombining trinomial tree discretizing the state variable.
const ext::shared_ptr< Impl > & implementation() const
const Array & params() const
void setMaxEvaluations(Size evaluations)
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Term-structure consistent model class.
const Handle< YieldTermStructure > & termStructure() const
Deterministic time-dependent parameter used for yield-curve fitting.
Recombining trinomial tree class.
@ NoFrequency
null frequency
Real Time
continuous quantity with 1-year units
Real DiscountFactor
discount factor between dates
std::size_t Size
size of a container
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)