QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
twofactormodel.hpp
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
25#ifndef quantlib_two_factor_model_hpp
26#define quantlib_two_factor_model_hpp
27
28#include <ql/methods/lattices/lattice2d.hpp>
29#include <ql/models/model.hpp>
30#include <utility>
31
32namespace QuantLib {
33 class StochasticProcess1D;
34 class StochasticProcess;
36
38 public:
39 explicit TwoFactorModel(Size nParams);
40
42 class ShortRateTree;
43
45 virtual ext::shared_ptr<ShortRateDynamics> dynamics() const = 0;
46
48 ext::shared_ptr<Lattice> tree(const TimeGrid& grid) const override;
49 };
50
52
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
84 const ext::shared_ptr<StochasticProcess1D>& xProcess() const {
85 return xProcess_;
86 }
87
89 const ext::shared_ptr<StochasticProcess1D>& yProcess() const {
90 return yProcess_;
91 }
92
94 Real correlation() const {
95 return correlation_;
96 }
97
99 ext::shared_ptr<StochasticProcess> process() const;
100
101 private:
102 ext::shared_ptr<StochasticProcess1D> xProcess_, yProcess_;
104 };
105
108 : public TreeLattice2D<TwoFactorModel::ShortRateTree,TrinomialTree> {
109 public:
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.
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
Definition: any.hpp:35
STL namespace.