QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
onefactormodel.hpp
Go to the documentation of this file.
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 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
30#include <ql/models/model.hpp>
31#include <utility>
32
33namespace QuantLib {
34 class StochasticProcess1D;
35
36 //! Single-factor short-rate model abstract class
37 /*! \ingroup shortrate */
39 public:
40 explicit OneFactorModel(Size nArguments);
41 ~OneFactorModel() override = default;
42
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
55 public:
56 explicit ShortRateDynamics(ext::shared_ptr<StochasticProcess1D> process)
57 : process_(std::move(process)) {}
58 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 const ext::shared_ptr<StochasticProcess1D>& process() {
68 return process_;
69 }
70 private:
71 ext::shared_ptr<StochasticProcess1D> process_;
72 };
73
74 //! Recombining trinomial tree discretizing the state variable
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 Size size(Size i) const {
89 return tree_->size(i);
90 }
91 DiscountFactor discount(Size i, Size index) const {
92 Real x = tree_->underlying(i, index);
93 Rate r = dynamics_->shortRate(timeGrid()[i], x) +spread_;
94 return std::exp(-r*timeGrid().dt(i));
95 }
96 Real underlying(Size i, Size index) const {
97 return tree_->underlying(i, index);
98 }
99 Size descendant(Size i, Size index, Size branch) const {
100 return tree_->descendant(i, index, branch);
101 }
102 Real probability(Size i, Size index, Size branch) const {
103 return tree_->probability(i, index, branch);
104 }
105 void setSpread(Spread spread)
106 {
107 spread_=spread;
108 }
109 private:
110 ext::shared_ptr<TrinomialTree> tree_;
111 ext::shared_ptr<ShortRateDynamics> dynamics_;
112 class Helper;
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 */
127 public AffineModel {
128 public:
129 explicit OneFactorAffineModel(Size nArguments)
130 : OneFactorModel(nArguments) {}
131
132 Real discountBond(Time now, Time maturity, Array factors) const override {
133 return discountBond(now, maturity, factors[0]);
134 }
135
136 Real discountBond(Time now, Time maturity, Rate rate) const {
137 return A(now, maturity)*std::exp(-B(now, maturity)*rate);
138 }
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
Affine model class.
Definition: model.hpp:45
1-D array used in linear algebra.
Definition: array.hpp:52
const TimeGrid & timeGrid() const
Single-factor affine base class.
Real discountBond(Time now, Time maturity, Array factors) const override
DiscountFactor discount(Time t) const override
Implied discount curve.
virtual Real A(Time t, Time T) const =0
Real discountBond(Time now, Time maturity, Rate rate) const
virtual Real B(Time t, Time T) const =0
Base class describing the short-rate dynamics.
virtual Rate shortRate(Time t, Real variable) const =0
Compute short rate from state variable.
ext::shared_ptr< StochasticProcess1D > process_
virtual Real variable(Time t, Rate r) const =0
Compute state variable from short rate.
ShortRateDynamics(ext::shared_ptr< StochasticProcess1D > process)
const ext::shared_ptr< StochasticProcess1D > & process()
Returns the risk-neutral dynamics of the state variable.
Recombining trinomial tree discretizing the state variable.
ext::shared_ptr< ShortRateDynamics > dynamics_
Size descendant(Size i, Size index, Size branch) const
DiscountFactor discount(Size i, Size index) const
ext::shared_ptr< TrinomialTree > tree_
Real probability(Size i, Size index, Size branch) const
Real underlying(Size i, Size index) const
Single-factor short-rate model abstract class.
~OneFactorModel() override=default
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Return by default a trinomial recombining tree.
virtual ext::shared_ptr< ShortRateDynamics > dynamics() const =0
returns the short-rate dynamics
Abstract short-rate model class.
Definition: model.hpp:141
time grid class
Definition: timegrid.hpp:43
One-dimensional tree-based lattice.
Definition: lattice1d.hpp:39
const DefaultType & t
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
One-dimensional lattice class.
Abstract interest rate model class.
Definition: any.hpp:35
STL namespace.
ext::shared_ptr< YieldTermStructure > r
Trinomial tree class.