QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lattice.hpp
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) 2004, 2005 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
21/*! \file lattice.hpp
22 \brief Tree-based lattice-method class
23*/
24
25#ifndef quantlib_tree_based_lattice_hpp
26#define quantlib_tree_based_lattice_hpp
27
31
32namespace QuantLib {
33
34 //! Tree-based lattice-method base class
35 /*! This class defines a lattice method that is able to rollback
36 (with discount) a discretized asset object. It will be based
37 on one or more trees.
38
39 Derived classes must implement the following interface:
40 \code
41 public:
42 DiscountFactor discount(Size i, Size index) const;
43 Size descendant(Size i, Size index, Size branch) const;
44 Real probability(Size i, Size index, Size branch) const;
45 \endcode
46 and may implement the following:
47 \code
48 public:
49 void stepback(Size i,
50 const Array& values,
51 Array& newValues) const;
52 \endcode
53
54 \ingroup lattices
55 */
56 template <class Impl>
57 class TreeLattice : public Lattice,
58 public CuriouslyRecurringTemplate<Impl> {
59 public:
61 Size n)
62 : Lattice(timeGrid), n_(n) {
63 QL_REQUIRE(n>0, "there is no zeronomial lattice!");
64 statePrices_ = std::vector<Array>(1, Array(1, 1.0));
66 }
67
68 //! \name Lattice interface
69 //@{
70 void initialize(DiscretizedAsset&, Time t) const override;
71 void rollback(DiscretizedAsset&, Time to) const override;
72 void partialRollback(DiscretizedAsset&, Time to) const override;
73 //! Computes the present value of an asset using Arrow-Debrew prices
75 //@}
76
77 const Array& statePrices(Size i) const;
78
79 void stepback(Size i,
80 const Array& values,
81 Array& newValues) const;
82
83 protected:
84 void computeStatePrices(Size until) const;
85
86 // Arrow-Debrew state prices
87 mutable std::vector<Array> statePrices_;
88
89 private:
92 };
93
94
95 // template definitions
96
97 template <class Impl>
99 for (Size i=statePricesLimit_; i<until; i++) {
100 statePrices_.push_back(Array(this->impl().size(i+1), 0.0));
101 for (Size j=0; j<this->impl().size(i); j++) {
102 DiscountFactor disc = this->impl().discount(i,j);
103 Real statePrice = statePrices_[i][j];
104 for (Size l=0; l<n_; l++) {
105 statePrices_[i+1][this->impl().descendant(i,j,l)] +=
106 statePrice*disc*this->impl().probability(i,j,l);
107 }
108 }
109 }
110 statePricesLimit_ = until;
111 }
112
113 template <class Impl>
115 if (i>statePricesLimit_)
116 computeStatePrices(i);
117 return statePrices_[i];
118 }
119
120 template <class Impl>
122 Size i = t_.index(asset.time());
123 return DotProduct(asset.values(), statePrices(i));
124 }
125
126 template <class Impl>
128 Size i = t_.index(t);
129 asset.time() = t;
130 asset.reset(this->impl().size(i));
131 }
132
133 template <class Impl>
134 inline void TreeLattice<Impl>::rollback(DiscretizedAsset& asset, Time to) const {
135 partialRollback(asset,to);
136 asset.adjustValues();
137 }
138
139 template <class Impl>
141 Time to) const {
142
143 Time from = asset.time();
144
145 if (close(from,to))
146 return;
147
148 QL_REQUIRE(from > to,
149 "cannot roll the asset back to" << to
150 << " (it is already at t = " << from << ")");
151
152 auto iFrom = Integer(t_.index(from));
153 auto iTo = Integer(t_.index(to));
154
155 for (Integer i=iFrom-1; i>=iTo; --i) {
156 Array newValues(this->impl().size(i));
157 this->impl().stepback(i, asset.values(), newValues);
158 asset.time() = t_[i];
159 asset.values() = newValues;
160 // skip the very last adjustment
161 if (i != iTo)
162 asset.adjustValues();
163 }
164 }
165
166 template <class Impl>
168 Array& newValues) const {
169 #pragma omp parallel for
170 for (long j=0; j<(long)this->impl().size(i); j++) {
171 Real value = 0.0;
172 for (Size l=0; l<n_; l++) {
173 value += this->impl().probability(i,j,l) *
174 values[this->impl().descendant(i,j,l)];
175 }
176 value *= this->impl().discount(i,j);
177 newValues[j] = value;
178 }
179 }
180
181}
182
183
184#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Support for the curiously recurring template pattern.
Discretized asset class used by numerical methods.
const Array & values() const
virtual void reset(Size size)=0
Lattice (tree, finite-differences) base class
const TimeGrid & timeGrid() const
time grid class
Definition: timegrid.hpp:43
Tree-based lattice-method base class.
Definition: lattice.hpp:58
Real presentValue(DiscretizedAsset &) const override
Computes the present value of an asset using Arrow-Debrew prices.
Definition: lattice.hpp:121
std::vector< Array > statePrices_
Definition: lattice.hpp:87
void partialRollback(DiscretizedAsset &, Time to) const override
Definition: lattice.hpp:140
void rollback(DiscretizedAsset &, Time to) const override
Definition: lattice.hpp:134
void initialize(DiscretizedAsset &, Time t) const override
initialize an asset at the given time.
Definition: lattice.hpp:127
const Array & statePrices(Size i) const
Definition: lattice.hpp:114
void computeStatePrices(Size until) const
Definition: lattice.hpp:98
void stepback(Size i, const Array &values, Array &newValues) const
Definition: lattice.hpp:167
TreeLattice(const TimeGrid &timeGrid, Size n)
Definition: lattice.hpp:60
Curiously recurring template pattern.
const DefaultType & t
Discretized asset classes.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
QL_INTEGER Integer
integer number
Definition: types.hpp:35
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
Real DotProduct(const Array &v1, const Array &v2)
Definition: array.hpp:553
Numerical method class.