QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
twofactormodel.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 twofactormodel.hpp
22 \brief Abstract two-factor interest rate model class
23*/
24
25#ifndef quantlib_two_factor_model_hpp
26#define quantlib_two_factor_model_hpp
27
29#include <ql/models/model.hpp>
30#include <utility>
31
32namespace QuantLib {
33 class StochasticProcess1D;
34 class StochasticProcess;
35 //! Abstract base-class for two-factor models
36 /*! \ingroup shortrate */
38 public:
39 explicit TwoFactorModel(Size nParams);
40
42 class ShortRateTree;
43
44 //! Returns the short-rate dynamics
45 virtual ext::shared_ptr<ShortRateDynamics> dynamics() const = 0;
46
47 //! Returns a two-dimensional trinomial tree
48 ext::shared_ptr<Lattice> tree(const TimeGrid& grid) const override;
49 };
50
51 //! Class describing the dynamics of the two state variables
52 /*! We assume here that the short-rate is a function of two state
53 variables x and y.
54 \f[
55 r_t = f(t, x_t, y_t)
56 \f]
57 of two state variables \f$ x_t \f$ and \f$ y_t \f$. These stochastic
58 processes satisfy
59 \f[
60 x_t = \mu_x(t, x_t)dt + \sigma_x(t, x_t) dW_t^x
61 \f]
62 and
63 \f[
64 y_t = \mu_y(t,y_t)dt + \sigma_y(t, y_t) dW_t^y
65 \f]
66 where \f$ W^x \f$ and \f$ W^y \f$ are two brownian motions
67 satisfying
68 \f[
69 dW^x_t dW^y_t = \rho dt
70 \f].
71 */
73 public:
74 ShortRateDynamics(ext::shared_ptr<StochasticProcess1D> xProcess,
75 ext::shared_ptr<StochasticProcess1D> yProcess,
77 : xProcess_(std::move(xProcess)), yProcess_(std::move(yProcess)),
79 virtual ~ShortRateDynamics() = default;
80
81 virtual Rate shortRate(Time t, Real x, Real y) const = 0;
82
83 //! Risk-neutral dynamics of the first state variable x
84 const ext::shared_ptr<StochasticProcess1D>& xProcess() const {
85 return xProcess_;
86 }
87
88 //! Risk-neutral dynamics of the second state variable y
89 const ext::shared_ptr<StochasticProcess1D>& yProcess() const {
90 return yProcess_;
91 }
92
93 //! Correlation \f$ \rho \f$ between the two brownian motions.
94 Real correlation() const {
95 return correlation_;
96 }
97
98 //! Joint process of the two variables
99 ext::shared_ptr<StochasticProcess> process() const;
100
101 private:
102 ext::shared_ptr<StochasticProcess1D> xProcess_, yProcess_;
104 };
105
106 //! Recombining two-dimensional tree discretizing the state variable
108 : public TreeLattice2D<TwoFactorModel::ShortRateTree,TrinomialTree> {
109 public:
110 //! Plain tree build-up from short-rate dynamics
111 ShortRateTree(const ext::shared_ptr<TrinomialTree>& tree1,
112 const ext::shared_ptr<TrinomialTree>& tree2,
113 const ext::shared_ptr<ShortRateDynamics>& dynamics);
114
116 Size modulo = tree1_->size(i);
117 Size index1 = index % modulo;
118 Size index2 = index / modulo;
119
120 Real x = tree1_->underlying(i, index1);
121 Real y = tree2_->underlying(i, index2);
122
123 Rate r = dynamics_->shortRate(timeGrid()[i], x, y);
124 return std::exp(-r*timeGrid().dt(i));
125 }
126 private:
127 ext::shared_ptr<ShortRateDynamics> dynamics_;
128 };
129
130}
131
132
133#endif
const TimeGrid & timeGrid() const
Abstract short-rate model class.
Definition: model.hpp:141
time grid class
Definition: timegrid.hpp:43
Two-dimensional tree-based lattice.
Definition: lattice2d.hpp:42
Class describing the dynamics of the two state variables.
const ext::shared_ptr< StochasticProcess1D > & xProcess() const
Risk-neutral dynamics of the first state variable x.
ShortRateDynamics(ext::shared_ptr< StochasticProcess1D > xProcess, ext::shared_ptr< StochasticProcess1D > yProcess, Real correlation)
virtual Rate shortRate(Time t, Real x, Real y) const =0
ext::shared_ptr< StochasticProcess > process() const
Joint process of the two variables.
Real correlation() const
Correlation between the two brownian motions.
ext::shared_ptr< StochasticProcess1D > yProcess_
ext::shared_ptr< StochasticProcess1D > xProcess_
const ext::shared_ptr< StochasticProcess1D > & yProcess() const
Risk-neutral dynamics of the second state variable y.
Recombining two-dimensional tree discretizing the state variable.
ext::shared_ptr< ShortRateDynamics > dynamics_
DiscountFactor discount(Size i, Size index) const
Abstract base-class for two-factor models.
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Returns a two-dimensional trinomial tree.
virtual ext::shared_ptr< ShortRateDynamics > dynamics() const =0
Returns the short-rate dynamics.
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 Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Two-dimensional lattice class.
Abstract interest rate model class.
Definition: any.hpp:35
STL namespace.
ext::shared_ptr< YieldTermStructure > r