QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
tflattice.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006 Theo Boafo
5 Copyright (C) 2006 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_lattices_tf_lattice_hpp
26#define quantlib_lattices_tf_lattice_hpp
27
28#include <ql/methods/lattices/bsmlattice.hpp>
29#include <ql/pricingengines/bond/discretizedconvertible.hpp>
30
31namespace QuantLib {
32
34
38 template <class T>
40 public:
41 TsiveriotisFernandesLattice(const ext::shared_ptr<T>& tree,
43 Time end,
44 Size steps,
46 Volatility volatility,
47 Spread divYield);
48
49 Spread creditSpread() const { return creditSpread_; };
50
51 protected:
52 void stepback(Size i,
53 const Array& values,
54 const Array& conversionProbability,
55 const Array& spreadAdjustedRate,
56 Array& newValues,
57 Array& newConversionProbability,
58 Array& newSpreadAdjustedRate) const;
59 void rollback(DiscretizedAsset&, Time to) const override;
60 void partialRollback(DiscretizedAsset&, Time to) const override;
61
62 private:
64 };
65
66
67 // template definitions
68
69 template <class T>
71 const ext::shared_ptr<T>& tree,
72 Rate riskFreeRate,
73 Time end,
74 Size steps,
75 Spread creditSpread,
76 Volatility sigma,
77 Spread divYield)
78 : BlackScholesLattice<T>(tree, riskFreeRate, end, steps),
79 creditSpread_(creditSpread) {
80 QL_REQUIRE(this->pu_<=1.0,
81 "probability (" << this->pu_ << ") higher than one");
82 QL_REQUIRE(this->pu_>=0.0,
83 "negative (" << this->pu_ << ") probability");
84 }
85
86 template <class T>
88 Size i,
89 const Array& values,
90 const Array& conversionProbability,
91 const Array& spreadAdjustedRate,
92 Array& newValues,
93 Array& newConversionProbability,
94 Array& newSpreadAdjustedRate) const {
95
96 for (Size j=0; j<this->size(i); j++) {
97
98 // new conversion probability is calculated via backward
99 // induction using up and down probabilities on tree on
100 // previous conversion probabilities, ie weighted average
101 // of previous probabilities.
102 newConversionProbability[j] =
103 this->pd_*conversionProbability[j] +
104 this->pu_*conversionProbability[j+1];
105
106 // Use blended discounting rate
107 newSpreadAdjustedRate[j] =
108 newConversionProbability[j] * this->riskFreeRate_ +
109 (1-newConversionProbability[j])*(this->riskFreeRate_+creditSpread_);
110
111 newValues[j] =
112 (this->pd_*values[j]/(1+(spreadAdjustedRate[j]*this->dt_)))
113 + (this->pu_*values[j+1]/(1+(spreadAdjustedRate[j+1]*this->dt_)));
114
115 }
116 }
117
118 template <class T>
120 Time to) const {
121 partialRollback(asset,to);
122 asset.adjustValues();
123 }
124
125
126 template <class T>
128 Time to) const {
129
130 Time from = asset.time();
131
132 if (close(from,to))
133 return;
134
135 QL_REQUIRE(from > to,
136 "cannot roll the asset back to" << to
137 << " (it is already at t = " << from << ")");
138
139 auto& convertible = dynamic_cast<DiscretizedConvertible&>(asset);
140
141 auto iFrom = Integer(this->t_.index(from));
142 auto iTo = Integer(this->t_.index(to));
143
144 for (Integer i=iFrom-1; i>=iTo; --i) {
145
146 Array newValues(this->size(i));
147 Array newSpreadAdjustedRate(this->size(i));
148 Array newConversionProbability(this->size(i));
149
150 stepback(i, convertible.values(),
151 convertible.conversionProbability(),
152 convertible.spreadAdjustedRate(), newValues,
153 newConversionProbability,newSpreadAdjustedRate);
154
155 convertible.time() = this->t_[i];
156 convertible.values() = newValues;
157 convertible.spreadAdjustedRate() = newSpreadAdjustedRate;
158 convertible.conversionProbability() = newConversionProbability;
159
160 // skip the very last adjustment
161 if (i != iTo)
162 convertible.adjustValues();
163 }
164 }
165
166}
167
168#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Simple binomial lattice approximating the Black-Scholes model.
Definition: bsmlattice.hpp:36
Discretized asset class used by numerical methods.
Binomial lattice approximating the Tsiveriotis-Fernandes model.
Definition: tflattice.hpp:39
void partialRollback(DiscretizedAsset &, Time to) const override
Definition: tflattice.hpp:127
void rollback(DiscretizedAsset &, Time to) const override
Definition: tflattice.hpp:119
void stepback(Size i, const Array &values, const Array &conversionProbability, const Array &spreadAdjustedRate, Array &newValues, Array &newConversionProbability, Array &newSpreadAdjustedRate) const
Definition: tflattice.hpp:87
TsiveriotisFernandesLattice(const ext::shared_ptr< T > &tree, Rate riskFreeRate, Time end, Size steps, Spread creditSpread, Volatility volatility, Spread divYield)
Definition: tflattice.hpp:70
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Real Volatility
volatility
Definition: types.hpp:78
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163