/src/quantlib/ql/methods/lattices/trinomialtree.hpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
21 | | /*! \file trinomialtree.hpp |
22 | | \brief Trinomial tree class |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_trinomial_tree_hpp |
26 | | #define quantlib_trinomial_tree_hpp |
27 | | |
28 | | #include <ql/methods/lattices/tree.hpp> |
29 | | #include <ql/timegrid.hpp> |
30 | | |
31 | | namespace QuantLib { |
32 | | class StochasticProcess1D; |
33 | | //! Recombining trinomial tree class |
34 | | /*! This class defines a recombining trinomial tree approximating a |
35 | | 1-D stochastic process. |
36 | | \warning The diffusion term of the SDE must be independent of the |
37 | | underlying process. |
38 | | |
39 | | \ingroup lattices |
40 | | */ |
41 | | class TrinomialTree : public Tree<TrinomialTree> { |
42 | | class Branching; |
43 | | public: |
44 | | enum Branches { branches = 3 }; |
45 | | TrinomialTree(const ext::shared_ptr<StochasticProcess1D>& process, |
46 | | const TimeGrid& timeGrid, |
47 | | bool isPositive = false); |
48 | 0 | Real dx(Size i) const { return dx_[i]; } |
49 | 0 | const TimeGrid& timeGrid() const { return timeGrid_; } |
50 | | |
51 | | Size size(Size i) const; |
52 | | Real underlying(Size i, Size index) const; |
53 | | Size descendant(Size i, Size index, Size branch) const; |
54 | | Real probability(Size i, Size index, Size branch) const; |
55 | | |
56 | | protected: |
57 | | std::vector<Branching> branchings_; |
58 | | Real x0_; |
59 | | std::vector<Real> dx_; |
60 | | TimeGrid timeGrid_; |
61 | | |
62 | | private: |
63 | | /* Branching scheme for a trinomial node. Each node has three |
64 | | descendants, with the middle branch linked to the node |
65 | | which is closest to the expectation of the variable. */ |
66 | | class Branching { |
67 | | public: |
68 | | Branching(); |
69 | | Size descendant(Size index, Size branch) const; |
70 | | Real probability(Size index, Size branch) const; |
71 | | Size size() const; |
72 | | Integer jMin() const; |
73 | | Integer jMax() const; |
74 | | void add(Integer k, Real p1, Real p2, Real p3); |
75 | | private: |
76 | | std::vector<Integer> k_; |
77 | | std::vector<std::vector<Real> > probs_; |
78 | | Integer kMin_, jMin_, kMax_, jMax_; |
79 | | }; |
80 | | }; |
81 | | |
82 | | // inline definitions |
83 | | |
84 | 0 | inline Size TrinomialTree::size(Size i) const { |
85 | 0 | return i==0 ? 1 : branchings_[i-1].size(); |
86 | 0 | } |
87 | | |
88 | 0 | inline Real TrinomialTree::underlying(Size i, Size index) const { |
89 | 0 | if (i==0) |
90 | 0 | return x0_; |
91 | 0 | else |
92 | 0 | return x0_ + (branchings_[i-1].jMin() + |
93 | 0 | static_cast<Real>(index))*dx(i); |
94 | 0 | } |
95 | | |
96 | | inline Size TrinomialTree::descendant(Size i, Size index, |
97 | 0 | Size branch) const { |
98 | 0 | return branchings_[i].descendant(index, branch); |
99 | 0 | } |
100 | | |
101 | 0 | inline Real TrinomialTree::probability(Size i, Size j, Size b) const { |
102 | 0 | return branchings_[i].probability(j, b); |
103 | 0 | } |
104 | | |
105 | | inline TrinomialTree::Branching::Branching() |
106 | 0 | : probs_(3), kMin_(QL_MAX_INTEGER), jMin_(QL_MAX_INTEGER), |
107 | 0 | kMax_(QL_MIN_INTEGER), jMax_(QL_MIN_INTEGER) {} |
108 | | |
109 | | inline Size TrinomialTree::Branching::descendant(Size index, |
110 | 0 | Size branch) const { |
111 | 0 | return k_[index] - jMin_ - 1 + branch; |
112 | 0 | } |
113 | | |
114 | | inline Real TrinomialTree::Branching::probability(Size index, |
115 | 0 | Size branch) const { |
116 | 0 | return probs_[branch][index]; |
117 | 0 | } |
118 | | |
119 | 0 | inline Size TrinomialTree::Branching::size() const { |
120 | 0 | return jMax_ - jMin_ + 1; |
121 | 0 | } |
122 | | |
123 | 0 | inline Integer TrinomialTree::Branching::jMin() const { |
124 | 0 | return jMin_; |
125 | 0 | } |
126 | | |
127 | 0 | inline Integer TrinomialTree::Branching::jMax() const { |
128 | 0 | return jMax_; |
129 | 0 | } |
130 | | |
131 | | inline void TrinomialTree::Branching::add(Integer k, |
132 | 0 | Real p1, Real p2, Real p3) { |
133 | | // store |
134 | 0 | k_.push_back(k); |
135 | 0 | probs_[0].push_back(p1); |
136 | 0 | probs_[1].push_back(p2); |
137 | 0 | probs_[2].push_back(p3); |
138 | | // maintain invariants |
139 | 0 | kMin_ = std::min(kMin_, k); |
140 | 0 | jMin_ = kMin_ - 1; |
141 | 0 | kMax_ = std::max(kMax_, k); |
142 | 0 | jMax_ = kMax_ + 1; |
143 | 0 | } |
144 | | |
145 | | } |
146 | | |
147 | | |
148 | | #endif |