QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
lattice2d.hpp
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) 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
25#ifndef quantlib_tree_lattice_2d_hpp
26#define quantlib_tree_lattice_2d_hpp
27
28#include <ql/math/matrix.hpp>
29#include <ql/methods/lattices/lattice.hpp>
30#include <ql/methods/lattices/trinomialtree.hpp>
31#include <utility>
32
33namespace QuantLib {
34
36
41 template <class Impl, class T = TrinomialTree>
42 class TreeLattice2D : public TreeLattice<Impl> {
43 public:
44 TreeLattice2D(const ext::shared_ptr<T>& tree1, ext::shared_ptr<T> tree2, Real correlation);
45
46 Size size(Size i) const;
47 Size descendant(Size i, Size index, Size branch) const;
48 Real probability(Size i, Size index, Size branch) const;
49 protected:
50 ext::shared_ptr<T> tree1_, tree2_;
51 // smelly
52 Array grid(Time) const override { QL_FAIL("not implemented"); }
53
54 private:
57 };
58
59
60 // inline definitions
61
62 template <class Impl, class T>
64 return tree1_->size(i)*tree2_->size(i);
65 }
66
67
68 // template definitions
69
70 template <class Impl, class T>
71 TreeLattice2D<Impl, T>::TreeLattice2D(const ext::shared_ptr<T>& tree1,
72 ext::shared_ptr<T> tree2,
73 Real correlation)
74 : TreeLattice<Impl>(tree1->timeGrid(), T::branches * T::branches), tree1_(tree1),
75 tree2_(std::move(tree2)), m_(T::branches, T::branches), rho_(std::fabs(correlation)) {
76
77 // what happens here?
78 if (correlation < 0.0 && T::branches == 3) {
79 m_[0][0] = -1.0;
80 m_[0][1] = -4.0;
81 m_[0][2] = 5.0;
82 m_[1][0] = -4.0;
83 m_[1][1] = 8.0;
84 m_[1][2] = -4.0;
85 m_[2][0] = 5.0;
86 m_[2][1] = -4.0;
87 m_[2][2] = -1.0;
88 } else {
89 m_[0][0] = 5.0;
90 m_[0][1] = -4.0;
91 m_[0][2] = -1.0;
92 m_[1][0] = -4.0;
93 m_[1][1] = 8.0;
94 m_[1][2] = -4.0;
95 m_[2][0] = -1.0;
96 m_[2][1] = -4.0;
97 m_[2][2] = 5.0;
98 }
99 }
100
101
102 template <class Impl, class T>
104 Size branch) const {
105 Size modulo = tree1_->size(i);
106
107 Size index1 = index % modulo;
108 Size index2 = index / modulo;
109 Size branch1 = branch % T::branches;
110 Size branch2 = branch / T::branches;
111
112 modulo = tree1_->size(i+1);
113 return tree1_->descendant(i, index1, branch1) +
114 tree2_->descendant(i, index2, branch2)*modulo;
115 }
116
117 template <class Impl, class T>
119 Size branch) const {
120 Size modulo = tree1_->size(i);
121
122 Size index1 = index % modulo;
123 Size index2 = index / modulo;
124 Size branch1 = branch % T::branches;
125 Size branch2 = branch / T::branches;
126
127 Real prob1 = tree1_->probability(i, index1, branch1);
128 Real prob2 = tree2_->probability(i, index2, branch2);
129 // does the 36 below depend on T::branches?
130 return prob1*prob2 + rho_*(m_[branch1][branch2])/36.0;
131 }
132
133}
134
135
136#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Matrix used in linear algebra.
Definition: matrix.hpp:41
Two-dimensional tree-based lattice.
Definition: lattice2d.hpp:42
ext::shared_ptr< T > tree1_
Definition: lattice2d.hpp:50
Size descendant(Size i, Size index, Size branch) const
Definition: lattice2d.hpp:103
Real probability(Size i, Size index, Size branch) const
Definition: lattice2d.hpp:118
Array grid(Time) const override
Definition: lattice2d.hpp:52
ext::shared_ptr< T > tree2_
Definition: lattice2d.hpp:50
TreeLattice2D(const ext::shared_ptr< T > &tree1, ext::shared_ptr< T > tree2, Real correlation)
Definition: lattice2d.hpp:71
Size size(Size i) const
Definition: lattice2d.hpp:63
Tree-based lattice-method base class.
Definition: lattice.hpp:58
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
STL namespace.