Coverage Report

Created: 2026-02-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/lattices/binomialtree.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2003 Ferdinando Ametrano
5
 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
6
 Copyright (C) 2005 StatPro Italia srl
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
/*! \file binomialtree.hpp
23
    \brief Binomial tree class
24
*/
25
26
#ifndef quantlib_binomial_tree_hpp
27
#define quantlib_binomial_tree_hpp
28
29
30
#include <ql/methods/lattices/tree.hpp>
31
#include <ql/instruments/dividendschedule.hpp>
32
#include <ql/stochasticprocess.hpp>
33
34
namespace QuantLib {
35
36
    //! Binomial tree base class
37
    /*! \ingroup lattices */
38
    template <class T>
39
    class BinomialTree : public Tree<T> {
40
      public:
41
        enum Branches { branches = 2 };
42
        BinomialTree(const ext::shared_ptr<StochasticProcess1D>& process,
43
                     Time end,
44
                     Size steps)
45
0
        : Tree<T>(steps+1), x0_(process->x0()), dt_(end/steps) {
46
0
            driftPerStep_ = process->drift(0.0, x0_) * dt_;
47
0
        }
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::JarrowRudd>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::CoxRossRubinstein>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::AdditiveEQPBinomialTree>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::Trigeorgis>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::Tian>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::LeisenReimer>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::BinomialTree<QuantLib::Joshi4>::BinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
48
        Size size(Size i) const {
49
            return i+1;
50
        }
51
        Size descendant(Size, Size index, Size branch) const {
52
            return index + branch;
53
        }
54
      protected:
55
        Real x0_, driftPerStep_;
56
        Time dt_;
57
    };
58
59
60
    //! Base class for equal probabilities binomial tree
61
    /*! \ingroup lattices */
62
    template <class T>
63
    class EqualProbabilitiesBinomialTree : public BinomialTree<T> {
64
      public:
65
        EqualProbabilitiesBinomialTree(
66
                        const ext::shared_ptr<StochasticProcess1D>& process,
67
                        Time end,
68
                        Size steps)
69
0
        : BinomialTree<T>(process, end, steps) {}
Unexecuted instantiation: QuantLib::EqualProbabilitiesBinomialTree<QuantLib::JarrowRudd>::EqualProbabilitiesBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::EqualProbabilitiesBinomialTree<QuantLib::AdditiveEQPBinomialTree>::EqualProbabilitiesBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
70
        Real underlying(Size i, Size index) const {
71
            BigInteger j = 2*BigInteger(index) - BigInteger(i);
72
            // exploiting the forward value tree centering
73
            return this->x0_*std::exp(i*this->driftPerStep_ + j*this->up_);
74
        }
75
        Real probability(Size, Size, Size) const { return 0.5; }
76
      protected:
77
        Real up_;
78
    };
79
80
81
    //! Base class for equal jumps binomial tree
82
    /*! \ingroup lattices */
83
    template <class T>
84
    class EqualJumpsBinomialTree : public BinomialTree<T> {
85
      public:
86
        EqualJumpsBinomialTree(
87
                        const ext::shared_ptr<StochasticProcess1D>& process,
88
                        Time end,
89
                        Size steps)
90
0
        : BinomialTree<T>(process, end, steps) {}
Unexecuted instantiation: QuantLib::EqualJumpsBinomialTree<QuantLib::CoxRossRubinstein>::EqualJumpsBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::EqualJumpsBinomialTree<QuantLib::Trigeorgis>::EqualJumpsBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
91
        Real underlying(Size i, Size index) const {
92
            BigInteger j = 2*BigInteger(index) - BigInteger(i);
93
            // exploiting equal jump and the x0_ tree centering
94
            return this->x0_*std::exp(j*this->dx_);
95
        }
96
        Real probability(Size, Size, Size branch) const {
97
            return (branch == 1 ? pu_ : pd_);
98
        }
99
      protected:
100
        Real dx_, pu_, pd_;
101
    };
102
103
104
    //! Jarrow-Rudd (multiplicative) equal probabilities binomial tree
105
    /*! \ingroup lattices */
106
    class JarrowRudd : public EqualProbabilitiesBinomialTree<JarrowRudd> {
107
      public:
108
        JarrowRudd(const ext::shared_ptr<StochasticProcess1D>&,
109
                   Time end,
110
                   Size steps,
111
                   Real strike);
112
    };
113
114
115
    //! Cox-Ross-Rubinstein (multiplicative) equal jumps binomial tree
116
    /*! \ingroup lattices */
117
    class CoxRossRubinstein
118
        : public EqualJumpsBinomialTree<CoxRossRubinstein> {
119
      public:
120
        CoxRossRubinstein(const ext::shared_ptr<StochasticProcess1D>&,
121
                          Time end,
122
                          Size steps,
123
                          Real strike);
124
    };
125
126
127
    //! Additive equal probabilities binomial tree
128
    /*! \ingroup lattices */
129
    class AdditiveEQPBinomialTree
130
        : public EqualProbabilitiesBinomialTree<AdditiveEQPBinomialTree> {
131
      public:
132
        AdditiveEQPBinomialTree(
133
                        const ext::shared_ptr<StochasticProcess1D>&,
134
                        Time end,
135
                        Size steps,
136
                        Real strike);
137
    };
138
139
140
    //! %Trigeorgis (additive equal jumps) binomial tree
141
    /*! \ingroup lattices */
142
    class Trigeorgis : public EqualJumpsBinomialTree<Trigeorgis> {
143
      public:
144
        Trigeorgis(const ext::shared_ptr<StochasticProcess1D>&,
145
                   Time end,
146
                   Size steps,
147
                   Real strike);
148
    };
149
150
151
    //! %Tian tree: third moment matching, multiplicative approach
152
    /*! \ingroup lattices */
153
    class Tian : public BinomialTree<Tian> {
154
      public:
155
        Tian(const ext::shared_ptr<StochasticProcess1D>&,
156
             Time end,
157
             Size steps,
158
             Real strike);
159
0
        Real underlying(Size i, Size index) const {
160
0
            return x0_ * std::pow(down_, Real(BigInteger(i)-BigInteger(index)))
161
0
                       * std::pow(up_, Real(index));
162
0
        };
163
0
        Real probability(Size, Size, Size branch) const {
164
0
            return (branch == 1 ? pu_ : pd_);
165
0
        }
166
      protected:
167
        Real up_, down_, pu_, pd_;
168
    };
169
170
    //! Leisen & Reimer tree: multiplicative approach
171
    /*! \ingroup lattices */
172
    class LeisenReimer : public BinomialTree<LeisenReimer> {
173
      public:
174
        LeisenReimer(const ext::shared_ptr<StochasticProcess1D>&,
175
                     Time end,
176
                     Size steps,
177
                     Real strike);
178
0
        Real underlying(Size i, Size index) const {
179
0
            return x0_ * std::pow(down_, Real(BigInteger(i)-BigInteger(index)))
180
0
                       * std::pow(up_, Real(index));
181
0
        }
182
0
        Real probability(Size, Size, Size branch) const {
183
0
            return (branch == 1 ? pu_ : pd_);
184
0
        }
185
      protected:
186
        Real up_, down_, pu_, pd_;
187
    };
188
189
190
     class Joshi4 : public BinomialTree<Joshi4> {
191
      public:
192
        Joshi4(const ext::shared_ptr<StochasticProcess1D>&,
193
               Time end,
194
               Size steps,
195
               Real strike);
196
0
        Real underlying(Size i, Size index) const {
197
0
            return x0_ * std::pow(down_, Real(BigInteger(i)-BigInteger(index)))
198
0
                       * std::pow(up_, Real(index));
199
0
        }
200
0
        Real probability(Size, Size, Size branch) const {
201
0
            return (branch == 1 ? pu_ : pd_);
202
0
        }
203
      protected:
204
        Real computeUpProb(Real k, Real dj) const;
205
        Real up_, down_, pu_, pd_;
206
    };
207
208
209
}
210
211
212
#endif