QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mfstateprocess.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2013 Peter Caspers
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/processes/mfstateprocess.hpp>
21
22namespace QuantLib {
23
24 MfStateProcess::MfStateProcess(Real reversion, const Array& times, const Array& vols)
25 : reversion_(reversion), times_(times), vols_(vols) {
27 reversionZero_ = true;
28 QL_REQUIRE(times.size() == vols.size() - 1,
29 "number of volatilities ("
30 << vols.size() << ") compared to number of times ("
31 << times_.size() << " must be bigger by one");
32 for (int i = 0; i < ((int)times.size()) - 1; i++)
33 QL_REQUIRE(times[i] < times[i + 1], "times must be increasing ("
34 << times[i] << "@" << i
35 << " , " << times[i + 1]
36 << "@" << i + 1 << ")");
37 for (Size i = 0; i < vols.size(); i++)
38 QL_REQUIRE(vols[i] >= 0.0, "volatilities must be non negative ("
39 << vols[i] << "@" << i << ")");
40 }
41
42 Real MfStateProcess::x0() const { return 0.0; }
43
44 Real MfStateProcess::drift(Time, Real) const { return 0.0; }
45
47 Size i =
48 std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
49 return vols_[i];
50 }
51
53 return x0;
54 }
55
57 return std::sqrt(variance(t, x0, dt));
58 }
59
61
62 if (dt < QL_EPSILON)
63 return 0.0;
64 if (times_.empty())
65 return reversionZero_ ? dt
66 : 1.0 / (2.0 * reversion_) *
67 (std::exp(2.0 * reversion_ * (t + dt)) -
68 std::exp(2.0 * reversion_ * t));
69
70 Size i =
71 std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
72 Size j = std::upper_bound(times_.begin(), times_.end(), t + dt) -
73 times_.begin();
74
75 Real v = 0.0;
76
77 for (Size k = i; k < j; k++) {
79 v += vols_[k] * vols_[k] *
80 (times_[k] - std::max(k > 0 ? times_[k - 1] : 0.0, t));
81 else
82 v += 1.0 / (2.0 * reversion_) * vols_[k] * vols_[k] *
83 (std::exp(2.0 * reversion_ * times_[k]) -
84 std::exp(2.0 * reversion_ *
85 std::max(k > 0 ? times_[k - 1] : 0.0, t)));
86 }
87
89 v += vols_[j] * vols_[j] *
90 (t + dt - std::max(j > 0 ? times_[j - 1] : 0.0, t));
91 else
92 v += 1.0 / (2.0 * reversion_) * vols_[j] * vols_[j] *
93 (std::exp(2.0 * reversion_ * (t + dt)) -
94 std::exp(2.0 * reversion_ *
95 (std::max(j > 0 ? times_[j - 1] : 0.0, t))));
96
97 return v;
98 }
99}
1-D array used in linear algebra.
Definition: array.hpp:52
bool empty() const
whether the array is empty
Definition: array.hpp:499
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
MfStateProcess(Real reversion, const Array &times, const Array &vols)
Real diffusion(Time t, Real x) const override
returns the diffusion part of the equation, i.e.
Real stdDeviation(Time t0, Real x0, Time dt) const override
Real drift(Time t, Real x) const override
returns the drift part of the equation, i.e.
Real expectation(Time t0, Real x0, Time dt) const override
Real x0() const override
returns the initial value of the state variable
Real variance(Time t0, Real x0, Time dt) const override
#define QL_EPSILON
Definition: qldefines.hpp:178
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35