| 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 | Real dx(Size i) const { return dx_[i]; } |
| 49 | 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 | inline Size TrinomialTree::size(Size i) const { |
| 85 | return i==0 ? 1 : branchings_[i-1].size(); |
| 86 | } |
| 87 | |
| 88 | inline Real TrinomialTree::underlying(Size i, Size index) const { |
| 89 | if (i==0) |
| 90 | return x0_; |
| 91 | else |
| 92 | return x0_ + (branchings_[i-1].jMin() + |
| 93 | static_cast<Real>(index))*dx(i); |
| 94 | } |
| 95 | |
| 96 | inline Size TrinomialTree::descendant(Size i, Size index, |
| 97 | Size branch) const { |
| 98 | return branchings_[i].descendant(index, branch); |
| 99 | } |
| 100 | |
| 101 | inline Real TrinomialTree::probability(Size i, Size j, Size b) const { |
| 102 | return branchings_[i].probability(index: j, branch: b); |
| 103 | } |
| 104 | |
| 105 | inline TrinomialTree::Branching::Branching() |
| 106 | : probs_(3), kMin_(QL_MAX_INTEGER), jMin_(QL_MAX_INTEGER), |
| 107 | kMax_(QL_MIN_INTEGER), jMax_(QL_MIN_INTEGER) {} |
| 108 | |
| 109 | inline Size TrinomialTree::Branching::descendant(Size index, |
| 110 | Size branch) const { |
| 111 | return k_[index] - jMin_ - 1 + branch; |
| 112 | } |
| 113 | |
| 114 | inline Real TrinomialTree::Branching::probability(Size index, |
| 115 | Size branch) const { |
| 116 | return probs_[branch][index]; |
| 117 | } |
| 118 | |
| 119 | inline Size TrinomialTree::Branching::size() const { |
| 120 | return jMax_ - jMin_ + 1; |
| 121 | } |
| 122 | |
| 123 | inline Integer TrinomialTree::Branching::jMin() const { |
| 124 | return jMin_; |
| 125 | } |
| 126 | |
| 127 | inline Integer TrinomialTree::Branching::jMax() const { |
| 128 | return jMax_; |
| 129 | } |
| 130 | |
| 131 | inline void TrinomialTree::Branching::add(Integer k, |
| 132 | Real p1, Real p2, Real p3) { |
| 133 | // store |
| 134 | k_.push_back(x: k); |
| 135 | probs_[0].push_back(x: p1); |
| 136 | probs_[1].push_back(x: p2); |
| 137 | probs_[2].push_back(x: p3); |
| 138 | // maintain invariants |
| 139 | kMin_ = std::min(a: kMin_, b: k); |
| 140 | jMin_ = kMin_ - 1; |
| 141 | kMax_ = std::max(a: kMax_, b: k); |
| 142 | jMax_ = kMax_ + 1; |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | |
| 148 | #endif |
| 149 | |