File: generalizedhullwhite.cpp

package info (click to toggle)
quantlib 1.4-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 34,340 kB
  • ctags: 64,765
  • sloc: cpp: 291,654; ansic: 21,484; sh: 11,209; makefile: 4,923; lisp: 86
file content (359 lines) | stat: -rw-r--r-- 12,641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2010 SunTrust Bank
 Copyright (C) 2010, 2014 Cavit Hafizoglu

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/experimental/shortrate/generalizedhullwhite.hpp>
#include <ql/termstructures/interpolatedcurve.hpp>
#include <ql/math/interpolations/linearinterpolation.hpp>
#include <ql/methods/lattices/trinomialtree.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/pricingengines/blackformula.hpp>

namespace QuantLib {

    namespace {

        class PiecewiseLinearCurve : public InterpolatedCurve<Linear> {
          public:
            PiecewiseLinearCurve(const std::vector<Time>& times,
                                 const std::vector<Real>& data)
            : InterpolatedCurve<Linear>(times, data) {
                setupInterpolation();
            }

            Real operator()(Time t) {
                return interpolation_(t,true);
            }
        };

        class PiecewiseConstantParameter2 : public Parameter {
          private:
            class Impl : public Parameter::Impl {
              public:
                Impl(const std::vector<Time>& times)
                : times_(times) {}

                Real value(const Array& params, Time t) const {
                    Size size = times_.size();
                    for (Size i=0; i<size; i++) {
                        if (t<times_[i])
                            return params[i];
                    }
                    return params[size-1];
                }
              private:
                std::vector<Time> times_;
            };
          public:
            PiecewiseConstantParameter2(const std::vector<Time>& times,
                                        const Constraint& constraint =
                                                             NoConstraint())
            : Parameter(times.size(),
                        boost::shared_ptr<Parameter::Impl>(
                                new PiecewiseConstantParameter2::Impl(times)),
                        constraint)
            {}
        };

        Real identity(Real x) {
            return x;
        }

    }

    /* Private function used by solver to determine time-dependent parameter
       df(r) = [theta(t) - a(t) f(r)]dt + sigma(t) dz
       dg = [theta(t) - a(t) g(t)] dt
       dx = -a(t) x dt + sigma(t) dz
       x = f(r) - g(t)
    */
    class GeneralizedHullWhite::Helper {
      public:
        Helper(const Size i, const Real xMin, const Real dx,
               const Real discountBondPrice,
               const boost::shared_ptr<ShortRateTree>& tree,
               const boost::function<Real(Real)>& fInv)
        : size_(tree->size(i)),
          dt_(tree->timeGrid().dt(i)),
          xMin_(xMin), dx_(dx),
          statePrices_(tree->statePrices(i)),
          discountBondPrice_(discountBondPrice), fInverse_(fInv) {}

        Real operator()(const Real theta) const {
            Real value = discountBondPrice_;
            Real x = xMin_;
            for (Size j=0; j<size_; j++) {
                Real discount = std::exp(- fInverse_(theta+x)*dt_);
                value -= statePrices_[j]*discount;
                x += dx_;
            }

            return value;
        };

      private:
        Size size_;
        Time dt_;
        Real xMin_, dx_;
        const Array& statePrices_;
        Real discountBondPrice_;
        boost::function<Real(Real)> fInverse_;
    };


    GeneralizedHullWhite::GeneralizedHullWhite(
        const Handle<YieldTermStructure>& yieldtermStructure,
        const std::vector<Date>& speedstructure,
        const std::vector<Date>& volstructure,
        const boost::function<Real(Real)>& f,
        const boost::function<Real(Real)>& fInverse)
    : OneFactorAffineModel(2), TermStructureConsistentModel(yieldtermStructure),
      speedstructure_(speedstructure), volstructure_(volstructure),
      a_(arguments_[0]), sigma_(arguments_[1]),
      f_(f), fInverse_(fInverse) {

        if (f_.empty())
            f_ = identity;
        if (fInverse_.empty())
            fInverse_ = identity;

        DayCounter dc = yieldtermStructure->dayCounter();

        speedperiods_.push_back(0.0);
        for (Size i=0;i<speedstructure.size()-1;i++)
            speedperiods_.push_back(dc.yearFraction(speedstructure[0],
                                                    speedstructure[i+1]));

        a_ = PiecewiseConstantParameter2(speedperiods_, PositiveConstraint());

        volperiods_.push_back(0.0);
        for (Size i=0;i<volstructure.size()-1;i++)
            volperiods_.push_back(dc.yearFraction(volstructure[0],
                                                  volstructure[i+1]));

        sigma_ = PiecewiseConstantParameter2(volperiods_, PositiveConstraint());

        a_.setParam(0,0.001);
        sigma_.setParam(0,0.001);

        for (Size i=1; i< a_.size();i++){
            a_.setParam(i,0.01*i+0.07);
        }

        for (Size i=1; i< sigma_.size();i++){
            sigma_.setParam(i,0.01*i+0.01);
        }

        registerWith(yieldtermStructure);
    }

    GeneralizedHullWhite::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 boost::function<Real(Real)>& f,
        const boost::function<Real(Real)>& fInverse)
    : OneFactorAffineModel(2), TermStructureConsistentModel(yieldtermStructure),
      speedstructure_(speedstructure),
      volstructure_(volstructure),
      a_(arguments_[0]), sigma_(arguments_[1]),
      f_(f), fInverse_(fInverse) {

        if (f_.empty())
            f_ = identity;
        if (fInverse_.empty())
            fInverse_ = identity;

        DayCounter dc = yieldtermStructure->dayCounter();
        Date ref = yieldtermStructure->referenceDate();

        for (Size i=0;i<speedstructure.size();i++)
            speedperiods_.push_back(dc.yearFraction(ref,
                                                    speedstructure[i]));

        a_ = PiecewiseConstantParameter2(speedperiods_, PositiveConstraint());

        for (Size i=0;i<volstructure.size();i++)
            volperiods_.push_back(dc.yearFraction(ref,
                                                  volstructure[i]));

        sigma_ = PiecewiseConstantParameter2(volperiods_, PositiveConstraint());

        for (Size i=0; i< sigma_.size();i++) {
            sigma_.setParam(i,vol[i]);
        }
        for (Size i=0; i< a_.size();i++) {
            a_.setParam(i,speed[i]);
        }

        registerWith(yieldtermStructure);
    }

    //classical HW
    GeneralizedHullWhite::GeneralizedHullWhite(
        const Handle<YieldTermStructure>& yieldtermStructure,
        Real a, Real sigma)
    : OneFactorAffineModel(2),
      TermStructureConsistentModel(yieldtermStructure),
      a_(arguments_[0]),
      sigma_(arguments_[1]) {

        a_ = ConstantParameter(a, PositiveConstraint());
        sigma_ = ConstantParameter(sigma, PositiveConstraint());
        a_.setParam(0,a);
        sigma_.setParam(0,sigma);

        f_ = identity;
        fInverse_ = identity;

        Date ref = yieldtermStructure->referenceDate();

        speedperiods_.push_back(0.0);
        volperiods_.push_back(0.0);
        speedstructure_.push_back(ref);
        volstructure_.push_back(ref);

        generateArguments();

        registerWith(yieldtermStructure);
    }

    Real GeneralizedHullWhite::B(Time t, Time T) const {
        Real _a = a();
        if (_a < std::sqrt(QL_EPSILON))
            return (T - t);
        else
            return (1.0 - std::exp(-_a*(T - t)))/_a;
    }

    void GeneralizedHullWhite::generateArguments() {
        phi_ = FittingParameter(termStructure(), a(), sigma());
    }

    Real GeneralizedHullWhite::discountBondOption(Option::Type type, Real strike,
                                                  Time maturity,
                                                  Time bondMaturity) const {

        Real _a = a();
        Real v;
        if (_a < std::sqrt(QL_EPSILON)) {
            v = sigma()*B(maturity, bondMaturity)* std::sqrt(maturity);
        } else {
            v = sigma()*B(maturity, bondMaturity)*
                std::sqrt(0.5*(1.0 - std::exp(-2.0*_a*maturity))/_a);
        }
        Real f = termStructure()->discount(bondMaturity);
        Real k = termStructure()->discount(maturity)*strike;

        return blackFormula(type, k, f, v);
    }

    Real GeneralizedHullWhite::A(Time t, Time T) const {
        DiscountFactor discount1 = termStructure()->discount(t);
        DiscountFactor discount2 = termStructure()->discount(T);
        Rate forward = termStructure()->forwardRate(t, t,
                                                    Continuous, NoFrequency);
        Real temp = sigma()*B(t,T);
        Real value = B(t,T)*forward - 0.25*temp*temp*B(0.0,2.0*t);
        return std::exp(value)*discount2/discount1;
    }


    boost::shared_ptr<Lattice> GeneralizedHullWhite::tree(
                                                  const TimeGrid& grid) const{

        TermStructureFittingParameter phi(termStructure());
        boost::shared_ptr<ShortRateDynamics> numericDynamics(
            new Dynamics(phi, speed(), vol(), f_, fInverse_));
        boost::shared_ptr<TrinomialTree> trinomial(
            new TrinomialTree(numericDynamics->process(), grid));
        boost::shared_ptr<ShortRateTree> numericTree(
            new ShortRateTree(trinomial, numericDynamics, grid));
        typedef TermStructureFittingParameter::NumericalImpl NumericalImpl;
        boost::shared_ptr<NumericalImpl> impl =
            boost::dynamic_pointer_cast<NumericalImpl>(phi.implementation());

        impl->reset();
        Real value = 1.0;
        Real vMin = -50.0;
        Real vMax = 50.0;

        for (Size i=0; i<(grid.size() - 1); i++) {
            Real discountBond = termStructure()->discount(grid[i+1]);
            Real xMin = trinomial->underlying(i, 0);
            Real dx = trinomial->dx(i);
            Helper finder(i, xMin, dx, discountBond, numericTree, fInverse_);
            Brent s1d;
            s1d.setMaxEvaluations(2000);
            value =s1d.solve(finder, 1e-8, value, vMin, vMax);
            impl->set(grid[i], value);
        }

        return numericTree;
    }

    boost::function<Real (Time)> GeneralizedHullWhite::speed() const {

        std::vector<Real> speedvals;
        speedvals.push_back(a_(0.001));

        if (a_.size()==1) {
            std::vector<Time> speedper;
            speedper.push_back(0.0);
            speedper.push_back(200); // 200 years to match the
                                     // constant a and sigma case
            speedvals.push_back(a_(0.001));
            return PiecewiseLinearCurve(speedper, speedvals);
        }

        for (Size i=0;i<a_.size()-1;i++)
            speedvals.push_back(
            a_(
            (speedstructure_[i+1]- Settings::instance().evaluationDate() )/365.0
            - 0.001));

        return PiecewiseLinearCurve(speedperiods_, speedvals);
    }

    boost::function<Real (Time)> GeneralizedHullWhite::vol() const {

        std::vector<Real> volvals;
        volvals.push_back(sigma_(0.001));

        if (sigma_.size()==1) {
            std::vector<Time> volper;
            volper.push_back(0.0);
            volper.push_back(200); //200 years to approximate the
                                   //constant coefficient model
            volvals.push_back(sigma_(0.001));
            return PiecewiseLinearCurve(volper, volvals);
        }

        for (Size i=0;i<sigma_.size()-1;i++)
            volvals.push_back(
            sigma_(
            (speedstructure_[i+1]-Settings::instance().evaluationDate() )/365.0
            - 0.001));

        return PiecewiseLinearCurve(volperiods_, volvals);
    }

}