Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/experimental/lattices/extendedbinomialtree.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) 2003 Ferdinando Ametrano
6
 Copyright (C) 2005 StatPro Italia srl
7
 Copyright (C) 2008 John Maiden
8
9
 This file is part of QuantLib, a free-software/open-source library
10
 for financial quantitative analysts and developers - http://quantlib.org/
11
12
 QuantLib is free software: you can redistribute it and/or modify it
13
 under the terms of the QuantLib license.  You should have received a
14
 copy of the license along with this program; if not, please email
15
 <quantlib-dev@lists.sf.net>. The license is also available online at
16
 <http://quantlib.org/license.shtml>.
17
18
 This program is distributed in the hope that it will be useful, but WITHOUT
19
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20
 FOR A PARTICULAR PURPOSE.  See the license for more details.
21
*/
22
23
/*! \file extendedbinomialtree.hpp
24
    \brief Time-dependent binomial tree class
25
*/
26
27
#ifndef quantlib_extended_binomial_tree_hpp
28
#define quantlib_extended_binomial_tree_hpp
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 ExtendedBinomialTree : public Tree<T> {
40
      public:
41
        enum Branches { branches = 2 };
42
        ExtendedBinomialTree(
43
                        const ext::shared_ptr<StochasticProcess1D>& process,
44
                        Time end,
45
                        Size steps)
46
0
        : Tree<T>(steps+1), x0_(process->x0()), dt_(end/steps),
47
0
          treeProcess_(process) {}
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedJarrowRudd>::ExtendedBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedCoxRossRubinstein>::ExtendedBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedAdditiveEQPBinomialTree>::ExtendedBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedTrigeorgis>::ExtendedBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedTian>::ExtendedBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedLeisenReimer>::ExtendedBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedJoshi4>::ExtendedBinomialTree(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
        //time dependent drift per step
56
0
        Real driftStep(Time driftTime) const {
57
0
            return this->treeProcess_->drift(driftTime, x0_) * dt_;
58
0
        }
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedCoxRossRubinstein>::driftStep(double) const
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedAdditiveEQPBinomialTree>::driftStep(double) const
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedTrigeorgis>::driftStep(double) const
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedTian>::driftStep(double) const
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedLeisenReimer>::driftStep(double) const
Unexecuted instantiation: QuantLib::ExtendedBinomialTree<QuantLib::ExtendedJoshi4>::driftStep(double) const
59
60
        Real x0_;
61
        Time dt_;
62
63
64
        ext::shared_ptr<StochasticProcess1D> treeProcess_;
65
    };
66
67
68
    //! Base class for equal probabilities binomial tree
69
    /*! \ingroup lattices */
70
    template <class T>
71
    class ExtendedEqualProbabilitiesBinomialTree
72
        : public ExtendedBinomialTree<T> {
73
      public:
74
        ExtendedEqualProbabilitiesBinomialTree(
75
                        const ext::shared_ptr<StochasticProcess1D>& process,
76
                        Time end,
77
                        Size steps)
78
0
        : ExtendedBinomialTree<T>(process, end, steps) {}
Unexecuted instantiation: QuantLib::ExtendedEqualProbabilitiesBinomialTree<QuantLib::ExtendedJarrowRudd>::ExtendedEqualProbabilitiesBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedEqualProbabilitiesBinomialTree<QuantLib::ExtendedAdditiveEQPBinomialTree>::ExtendedEqualProbabilitiesBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
79
0
        virtual ~ExtendedEqualProbabilitiesBinomialTree() = default;
Unexecuted instantiation: QuantLib::ExtendedEqualProbabilitiesBinomialTree<QuantLib::ExtendedJarrowRudd>::~ExtendedEqualProbabilitiesBinomialTree()
Unexecuted instantiation: QuantLib::ExtendedEqualProbabilitiesBinomialTree<QuantLib::ExtendedAdditiveEQPBinomialTree>::~ExtendedEqualProbabilitiesBinomialTree()
80
81
        Real underlying(Size i, Size index) const {
82
            Time stepTime = i*this->dt_;
83
            BigInteger j = 2*BigInteger(index) - BigInteger(i);
84
            // exploiting the forward value tree centering
85
            return this->x0_*std::exp(i*this->driftStep(stepTime) + j*this->upStep(stepTime));
86
        }
87
88
        Real probability(Size, Size, Size) const { return 0.5; }
89
      protected:
90
        //the tree dependent up move term at time stepTime
91
        virtual Real upStep(Time stepTime) const = 0;
92
        Real up_;
93
    };
94
95
96
    //! Base class for equal jumps binomial tree
97
    /*! \ingroup lattices */
98
    template <class T>
99
    class ExtendedEqualJumpsBinomialTree : public ExtendedBinomialTree<T> {
100
      public:
101
        ExtendedEqualJumpsBinomialTree(
102
                        const ext::shared_ptr<StochasticProcess1D>& process,
103
                        Time end,
104
                        Size steps)
105
0
        : ExtendedBinomialTree<T>(process, end, steps) {}
Unexecuted instantiation: QuantLib::ExtendedEqualJumpsBinomialTree<QuantLib::ExtendedCoxRossRubinstein>::ExtendedEqualJumpsBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
Unexecuted instantiation: QuantLib::ExtendedEqualJumpsBinomialTree<QuantLib::ExtendedTrigeorgis>::ExtendedEqualJumpsBinomialTree(boost::shared_ptr<QuantLib::StochasticProcess1D> const&, double, unsigned long)
106
0
        virtual ~ExtendedEqualJumpsBinomialTree() = default;
Unexecuted instantiation: QuantLib::ExtendedEqualJumpsBinomialTree<QuantLib::ExtendedCoxRossRubinstein>::~ExtendedEqualJumpsBinomialTree()
Unexecuted instantiation: QuantLib::ExtendedEqualJumpsBinomialTree<QuantLib::ExtendedTrigeorgis>::~ExtendedEqualJumpsBinomialTree()
107
108
        Real underlying(Size i, Size index) const {
109
            Time stepTime = i*this->dt_;
110
            BigInteger j = 2*BigInteger(index) - BigInteger(i);
111
            // exploiting equal jump and the x0_ tree centering
112
            return this->x0_*std::exp(j*this->dxStep(stepTime));
113
        }
114
115
        Real probability(Size i, Size, Size branch) const {
116
            Time stepTime = i*this->dt_;
117
            Real upProb = this->probUp(stepTime);
118
            Real downProb = 1 - upProb;
119
            return (branch == 1 ? upProb : downProb);
120
        }
121
      protected:
122
        //probability of a up move
123
        virtual Real probUp(Time stepTime) const = 0;
124
        //time dependent term dx_
125
        virtual Real dxStep(Time stepTime) const = 0;
126
127
        Real dx_, pu_, pd_;
128
    };
129
130
131
    //! Jarrow-Rudd (multiplicative) equal probabilities binomial tree
132
    /*! \ingroup lattices */
133
    class ExtendedJarrowRudd
134
        : public ExtendedEqualProbabilitiesBinomialTree<ExtendedJarrowRudd> {
135
      public:
136
        ExtendedJarrowRudd(const ext::shared_ptr<StochasticProcess1D>&,
137
                           Time end,
138
                           Size steps,
139
                           Real strike);
140
      protected:
141
        Real upStep(Time stepTime) const override;
142
    };
143
144
145
    //! Cox-Ross-Rubinstein (multiplicative) equal jumps binomial tree
146
    /*! \ingroup lattices */
147
    class ExtendedCoxRossRubinstein
148
        : public ExtendedEqualJumpsBinomialTree<ExtendedCoxRossRubinstein> {
149
      public:
150
        ExtendedCoxRossRubinstein(const ext::shared_ptr<StochasticProcess1D>&,
151
                                  Time end,
152
                                  Size steps,
153
                                  Real strike);
154
      protected:
155
        Real dxStep(Time stepTime) const override;
156
        Real probUp(Time stepTime) const override;
157
    };
158
159
160
    //! Additive equal probabilities binomial tree
161
    /*! \ingroup lattices */
162
    class ExtendedAdditiveEQPBinomialTree
163
        : public ExtendedEqualProbabilitiesBinomialTree<
164
                                            ExtendedAdditiveEQPBinomialTree> {
165
      public:
166
        ExtendedAdditiveEQPBinomialTree(
167
                        const ext::shared_ptr<StochasticProcess1D>&,
168
                        Time end,
169
                        Size steps,
170
                        Real strike);
171
172
      protected:
173
        Real upStep(Time stepTime) const override;
174
    };
175
176
177
    //! %Trigeorgis (additive equal jumps) binomial tree
178
    /*! \ingroup lattices */
179
    class ExtendedTrigeorgis
180
        : public ExtendedEqualJumpsBinomialTree<ExtendedTrigeorgis> {
181
      public:
182
        ExtendedTrigeorgis(const ext::shared_ptr<StochasticProcess1D>&,
183
                           Time end,
184
                           Size steps,
185
                           Real strike);
186
    protected:
187
      Real dxStep(Time stepTime) const override;
188
      Real probUp(Time stepTime) const override;
189
    };
190
191
192
    //! %Tian tree: third moment matching, multiplicative approach
193
    /*! \ingroup lattices */
194
    class ExtendedTian : public ExtendedBinomialTree<ExtendedTian> {
195
      public:
196
        ExtendedTian(const ext::shared_ptr<StochasticProcess1D>&,
197
                     Time end,
198
                     Size steps,
199
                     Real strike);
200
201
        Real underlying(Size i, Size index) const;
202
        Real probability(Size, Size, Size branch) const;
203
      protected:
204
        Real up_, down_, pu_, pd_;
205
    };
206
207
    //! Leisen & Reimer tree: multiplicative approach
208
    /*! \ingroup lattices */
209
    class ExtendedLeisenReimer
210
        : public ExtendedBinomialTree<ExtendedLeisenReimer> {
211
      public:
212
        ExtendedLeisenReimer(const ext::shared_ptr<StochasticProcess1D>&,
213
                             Time end,
214
                             Size steps,
215
                             Real strike);
216
217
        Real underlying(Size i, Size index) const;
218
        Real probability(Size, Size, Size branch) const;
219
      protected:
220
        Time end_;
221
        Size oddSteps_;
222
        Real strike_, up_, down_, pu_, pd_;
223
    };
224
225
226
     class ExtendedJoshi4 : public ExtendedBinomialTree<ExtendedJoshi4> {
227
      public:
228
        ExtendedJoshi4(const ext::shared_ptr<StochasticProcess1D>&,
229
                       Time end,
230
                       Size steps,
231
                       Real strike);
232
233
        Real underlying(Size i, Size index) const;
234
        Real probability(Size, Size, Size branch) const;
235
      protected:
236
        Real computeUpProb(Real k, Real dj) const;
237
        Time end_;
238
        Size oddSteps_;
239
        Real strike_, up_, down_, pu_, pd_;
240
    };
241
242
243
}
244
245
246
#endif