Coverage Report

Created: 2026-03-11 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/models/shortrate/onefactormodel.hpp
Line
Count
Source
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
 <https://www.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 onefactormodel.hpp
22
    \brief Abstract one-factor interest rate model class
23
*/
24
25
#ifndef quantlib_one_factor_model_hpp
26
#define quantlib_one_factor_model_hpp
27
28
#include <ql/methods/lattices/lattice1d.hpp>
29
#include <ql/methods/lattices/trinomialtree.hpp>
30
#include <ql/models/model.hpp>
31
#include <utility>
32
33
namespace QuantLib {
34
    class StochasticProcess1D;
35
36
    //! Single-factor short-rate model abstract class
37
    /*! \ingroup shortrate */
38
    class OneFactorModel : public ShortRateModel {
39
      public:
40
        explicit OneFactorModel(Size nArguments);
41
0
        ~OneFactorModel() override = default;
42
43
        class ShortRateDynamics;
44
        class ShortRateTree;
45
46
        //! returns the short-rate dynamics
47
        virtual ext::shared_ptr<ShortRateDynamics> dynamics() const = 0;
48
49
        //! Return by default a trinomial recombining tree
50
        ext::shared_ptr<Lattice> tree(const TimeGrid& grid) const override;
51
    };
52
53
    //! Base class describing the short-rate dynamics
54
    class OneFactorModel::ShortRateDynamics {
55
      public:
56
        explicit ShortRateDynamics(ext::shared_ptr<StochasticProcess1D> process)
57
0
        : process_(std::move(process)) {}
58
0
        virtual ~ShortRateDynamics() = default;
59
60
        //! Compute state variable from short rate
61
        virtual Real variable(Time t, Rate r) const = 0;
62
63
        //! Compute short rate from state variable
64
        virtual Rate shortRate(Time t, Real variable) const = 0;
65
66
        //! Returns the risk-neutral dynamics of the state variable
67
0
        const ext::shared_ptr<StochasticProcess1D>& process() {
68
0
            return process_;
69
0
        }
70
      private:
71
        ext::shared_ptr<StochasticProcess1D> process_;
72
    };
73
74
    //! Recombining trinomial tree discretizing the state variable
75
    class OneFactorModel::ShortRateTree
76
        : public TreeLattice1D<OneFactorModel::ShortRateTree> {
77
      public:
78
        //! Plain tree build-up from short-rate dynamics
79
        ShortRateTree(const ext::shared_ptr<TrinomialTree>& tree,
80
                      ext::shared_ptr<ShortRateDynamics> dynamics,
81
                      const TimeGrid& timeGrid);
82
        //! Tree build-up + numerical fitting to term-structure
83
        ShortRateTree(const ext::shared_ptr<TrinomialTree>& tree,
84
                      ext::shared_ptr<ShortRateDynamics> dynamics,
85
                      const ext::shared_ptr<TermStructureFittingParameter::NumericalImpl>& phi,
86
                      const TimeGrid& timeGrid);
87
88
0
        Size size(Size i) const {
89
0
            return tree_->size(i);
90
0
        }
91
0
        DiscountFactor discount(Size i, Size index) const {
92
0
            Real x = tree_->underlying(i, index);
93
0
            Rate r = dynamics_->shortRate(timeGrid()[i], x) +spread_;
94
0
            return std::exp(-r*timeGrid().dt(i));
95
0
        }
96
0
        Real underlying(Size i, Size index) const {
97
0
            return tree_->underlying(i, index);
98
0
        }
99
0
        Size descendant(Size i, Size index, Size branch) const {
100
0
            return tree_->descendant(i, index, branch);
101
0
        }
102
0
        Real probability(Size i, Size index, Size branch) const {
103
0
            return tree_->probability(i, index, branch);
104
0
        }
105
        void setSpread(Spread spread)
106
0
        {
107
0
            spread_=spread;
108
0
        }
109
      private:
110
        ext::shared_ptr<TrinomialTree> tree_;
111
        ext::shared_ptr<ShortRateDynamics> dynamics_;
112
        class Helper;
113
        Spread spread_;
114
    };
115
116
    //! Single-factor affine base class
117
    /*! Single-factor models with an analytical formula for discount bonds
118
        should inherit from this class. They must then implement the
119
        functions \f$ A(t,T) \f$ and \f$ B(t,T) \f$ such that
120
        \f[
121
            P(t, T, r_t) = A(t,T)e^{ -B(t,T) r_t}.
122
        \f]
123
124
        \ingroup shortrate
125
    */
126
    class OneFactorAffineModel : public OneFactorModel,
127
                                 public AffineModel {
128
      public:
129
        explicit OneFactorAffineModel(Size nArguments)
130
0
        : OneFactorModel(nArguments) {}
131
132
0
        Real discountBond(Time now, Time maturity, Array factors) const override {
133
0
            return discountBond(now, maturity, factors[0]);
134
0
        }
135
136
0
        Real discountBond(Time now, Time maturity, Rate rate) const {
137
0
            return A(now, maturity)*std::exp(-B(now, maturity)*rate);
138
0
        }
139
140
        DiscountFactor discount(Time t) const override;
141
142
      protected:
143
        virtual Real A(Time t, Time T) const = 0;
144
        virtual Real B(Time t, Time T) const = 0;
145
    };
146
147
}
148
149
#endif
150