QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
trinomialtree.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_trinomial_tree_hpp
26#define quantlib_trinomial_tree_hpp
27
28#include <ql/methods/lattices/tree.hpp>
29#include <ql/timegrid.hpp>
30
31namespace QuantLib {
32 class StochasticProcess1D;
34
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_;
59 std::vector<Real> dx_;
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_;
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
97 Size branch) const {
98 return branchings_[i].descendant(index, branch);
99 }
100
102 return branchings_[i].probability(j, b);
103 }
104
106 : probs_(3), kMin_(QL_MAX_INTEGER), jMin_(QL_MAX_INTEGER),
107 kMax_(QL_MIN_INTEGER), jMax_(QL_MIN_INTEGER) {}
108
110 Size branch) const {
111 return k_[index] - jMin_ - 1 + branch;
112 }
113
115 Size branch) const {
116 return probs_[branch][index];
117 }
118
120 return jMax_ - jMin_ + 1;
121 }
122
124 return jMin_;
125 }
126
128 return jMax_;
129 }
130
132 Real p1, Real p2, Real p3) {
133 // store
134 k_.push_back(k);
135 probs_[0].push_back(p1);
136 probs_[1].push_back(p2);
137 probs_[2].push_back(p3);
138 // maintain invariants
139 kMin_ = std::min(kMin_, k);
140 jMin_ = kMin_ - 1;
141 kMax_ = std::max(kMax_, k);
142 jMax_ = kMax_ + 1;
143 }
144
145}
146
147
148#endif
time grid class
Definition: timegrid.hpp:43
Tree approximating a single-factor diffusion
Definition: tree.hpp:51
std::vector< std::vector< Real > > probs_
void add(Integer k, Real p1, Real p2, Real p3)
Size descendant(Size index, Size branch) const
Real probability(Size index, Size branch) const
Recombining trinomial tree class.
Size descendant(Size i, Size index, Size branch) const
std::vector< Branching > branchings_
Real dx(Size i) const
Real probability(Size i, Size index, Size branch) const
Real underlying(Size i, Size index) const
const TimeGrid & timeGrid() const
Size size(Size i) const
std::vector< Real > dx_
#define QL_MIN_INTEGER
Definition: qldefines.hpp:173
#define QL_MAX_INTEGER
Definition: qldefines.hpp:174
QL_REAL Real
real number
Definition: types.hpp:50
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