Coverage Report

Created: 2026-02-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/processes/mfstateprocess.cpp
Line
Count
Source
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
 <https://www.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
22
namespace QuantLib {
23
24
    MfStateProcess::MfStateProcess(Real reversion, Array times, Array vols)
25
0
    : reversion_(reversion), times_(std::move(times)), vols_(std::move(vols)) {
26
0
        if (reversion_ < QL_EPSILON && -reversion_ < QL_EPSILON)
27
0
            reversionZero_ = true;
28
0
        checkTimesVols();
29
0
    }
30
31
0
    void MfStateProcess::setTimes(Array times) {
32
0
        times_ = std::move(times);
33
0
        checkTimesVols();
34
0
        notifyObservers();
35
0
    }
36
37
0
    void MfStateProcess::setVols(Array vols) {
38
0
        vols_ = std::move(vols);
39
0
        checkTimesVols();
40
0
        notifyObservers();
41
0
    }
42
43
0
    void MfStateProcess::checkTimesVols() const {
44
0
        QL_REQUIRE(times_.size() == vols_.size() - 1,
45
0
                   "number of volatilities ("
46
0
                       << vols_.size() << ") compared to number of times ("
47
0
                       << times_.size() << " must be bigger by one");
48
0
        for (int i = 0; i < ((int)times_.size()) - 1; i++)
49
0
            QL_REQUIRE(times_[i] < times_[i + 1], "times must be increasing ("
50
0
                                                    << times_[i] << "@" << i
51
0
                                                    << " , " << times_[i + 1]
52
0
                                                    << "@" << i + 1 << ")");
53
0
        for (Size i = 0; i < vols_.size(); i++)
54
0
            QL_REQUIRE(vols_[i] >= 0.0, "volatilities must be non negative ("
55
0
                                           << vols_[i] << "@" << i << ")");
56
0
    }
57
58
0
    Real MfStateProcess::x0() const { return 0.0; }
59
60
0
    Real MfStateProcess::drift(Time, Real) const { return 0.0; }
61
62
0
    Real MfStateProcess::diffusion(Time t, Real) const {
63
0
        Size i =
64
0
            std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
65
0
        return vols_[i];
66
0
    }
67
68
0
    Real MfStateProcess::expectation(Time, Real x0, Time dt) const {
69
0
        return x0;
70
0
    }
71
72
0
    Real MfStateProcess::stdDeviation(Time t, Real x0, Time dt) const {
73
0
        return std::sqrt(variance(t, x0, dt));
74
0
    }
75
76
0
    Real MfStateProcess::variance(Time t, Real, Time dt) const {
77
78
0
        if (dt < QL_EPSILON)
79
0
            return 0.0;
80
0
        if (times_.empty())
81
0
            return reversionZero_ ? dt
82
0
                                  : 1.0 / (2.0 * reversion_) *
83
0
                                        (std::exp(2.0 * reversion_ * (t + dt)) -
84
0
                                         std::exp(2.0 * reversion_ * t));
85
86
0
        Size i =
87
0
            std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
88
0
        Size j = std::upper_bound(times_.begin(), times_.end(), t + dt) -
89
0
                 times_.begin();
90
91
0
        Real v = 0.0;
92
93
0
        for (Size k = i; k < j; k++) {
94
0
            if (reversionZero_)
95
0
                v += vols_[k] * vols_[k] *
96
0
                     (times_[k] - std::max(k > 0 ? times_[k - 1] : 0.0, t));
97
0
            else
98
0
                v += 1.0 / (2.0 * reversion_) * vols_[k] * vols_[k] *
99
0
                     (std::exp(2.0 * reversion_ * times_[k]) -
100
0
                      std::exp(2.0 * reversion_ *
101
0
                               std::max(k > 0 ? times_[k - 1] : 0.0, t)));
102
0
        }
103
104
0
        if (reversionZero_)
105
0
            v += vols_[j] * vols_[j] *
106
0
                 (t + dt - std::max(j > 0 ? times_[j - 1] : 0.0, t));
107
0
        else
108
0
            v += 1.0 / (2.0 * reversion_) * vols_[j] * vols_[j] *
109
0
                 (std::exp(2.0 * reversion_ * (t + dt)) -
110
0
                  std::exp(2.0 * reversion_ *
111
0
                           (std::max(j > 0 ? times_[j - 1] : 0.0, t))));
112
113
0
        return v;
114
0
    }
115
}