QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
blackkarasinski.cpp
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
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/models/shortrate/onefactormodels/blackkarasinski.hpp>
21#include <ql/methods/lattices/trinomialtree.hpp>
22#include <ql/math/solvers1d/brent.hpp>
23
24namespace QuantLib {
25
26 // Private function used by solver to determine time-dependent parameter
27 class BlackKarasinski::Helper {
28 public:
29 Helper(Size i, Real xMin, Real dx,
30 Real discountBondPrice,
31 const ext::shared_ptr<ShortRateTree>& tree)
32 : size_(tree->size(i)),
33 dt_(tree->timeGrid().dt(i)),
34 xMin_(xMin), dx_(dx),
35 statePrices_(tree->statePrices(i)),
36 discountBondPrice_(discountBondPrice) {}
37
38 Real operator()(Real theta) const {
39 Real value = discountBondPrice_;
40 Real x = xMin_;
41 for (Size j=0; j<size_; j++) {
42 Real discount = std::exp(-std::exp(theta+x)*dt_);
43 value -= statePrices_[j]*discount;
44 x += dx_;
45 }
46 return value;
47 }
48
49 private:
50 Size size_;
51 Time dt_;
52 Real xMin_, dx_;
53 const Array& statePrices_;
54 Real discountBondPrice_;
55 };
56
58 const Handle<YieldTermStructure>& termStructure,
59 Real a, Real sigma)
61 a_(arguments_[0]), sigma_(arguments_[1]) {
65
67 }
68
69 ext::shared_ptr<Lattice>
70 BlackKarasinski::tree(const TimeGrid& grid) const {
71
72 ext::shared_ptr<ShortRateDynamics> numericDynamics(
73 new Dynamics(phi_, a(), sigma()));
74 ext::shared_ptr<TrinomialTree> trinomial(
75 new TrinomialTree(numericDynamics->process(), grid));
76 ext::shared_ptr<ShortRateTree> numericTree(
77 new ShortRateTree(trinomial, numericDynamics, grid));
78
80 ext::shared_ptr<NumericalImpl> impl =
81 ext::dynamic_pointer_cast<NumericalImpl>(phi_.implementation());
82 impl->reset();
83 Real value = 1.0;
84 Real vMin = -50.0;
85 Real vMax = 50.0;
86 for (Size i=0; i<(grid.size() - 1); i++) {
87 Real discountBond = termStructure()->discount(grid[i+1]);
88 Real xMin = trinomial->underlying(i, 0);
89 Real dx = trinomial->dx(i);
90 Helper finder(i, xMin, dx, discountBond, numericTree);
91 Brent s1d;
92 s1d.setMaxEvaluations(1000);
93 value = s1d.solve(finder, 1e-7, value, vMin, vMax);
94 impl->set(grid[i], value);
95 }
96 return numericTree;
97 }
98
99 ext::shared_ptr<OneFactorModel::ShortRateDynamics>
101 // Calibrate fitting parameter to term structure
102 Size steps = 50;
103 ext::shared_ptr<Lattice> lattice = this->tree(
104 TimeGrid(termStructure()->maxTime(), steps));
105 ext::shared_ptr<ShortRateDynamics> numericDynamics(
106 new Dynamics(phi_, a(), sigma()));
107 return numericDynamics;
108 }
109
110}
Short-rate dynamics in the Black-Karasinski model.
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Return by default a trinomial recombining tree.
BlackKarasinski(const Handle< YieldTermStructure > &termStructure, Real a=0.1, Real sigma=0.1)
ext::shared_ptr< ShortRateDynamics > dynamics() const override
returns the short-rate dynamics
Brent 1-D solver
Definition: brent.hpp:37
Real value(const Array &params, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Definition: model.cpp:117
Standard constant parameter .
Definition: parameter.hpp:71
Shared handle to an observable.
Definition: handle.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Recombining trinomial tree discretizing the state variable.
Single-factor short-rate model abstract class.
const ext::shared_ptr< Impl > & implementation() const
Definition: parameter.hpp:59
Constraint imposing positivity to all arguments
Definition: constraint.hpp:92
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
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:149
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
Recombining trinomial tree class.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35