• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 20991647558

14 Jan 2026 10:57AM UTC coverage: 74.155% (-0.02%) from 74.172%
20991647558

Pull #2418

github

web-flow
Merge db21e8c8f into b64f97dc2
Pull Request #2418: avoid storing references in gsr and markov functional state processes

52 of 57 new or added lines in 8 files covered. (91.23%)

161 existing lines in 16 files now uncovered.

57330 of 77311 relevant lines covered (74.16%)

8755692.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/ql/processes/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
 <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)
15✔
25
    : reversion_(reversion), times_(std::move(times)), vols_(std::move(vols)) {
15✔
26
        if (reversion_ < QL_EPSILON && -reversion_ < QL_EPSILON)
15✔
27
            reversionZero_ = true;
2✔
28
        checkTimesVols();
15✔
29
    }
15✔
30

31
    void MfStateProcess::setTimes(Array times) {
12✔
32
        times_ = std::move(times);
33
        checkTimesVols();
12✔
34
    }
12✔
35

36
    void MfStateProcess::setVols(Array vols) {
142✔
37
        vols_ = std::move(vols);
38
        checkTimesVols();
142✔
39
    }
142✔
40

41
    void MfStateProcess::checkTimesVols() const {
169✔
42
        QL_REQUIRE(times_.size() == vols_.size() - 1,
169✔
43
                   "number of volatilities ("
44
                       << vols_.size() << ") compared to number of times ("
45
                       << times_.size() << " must be bigger by one");
46
        for (int i = 0; i < ((int)times_.size()) - 1; i++)
1,040✔
47
            QL_REQUIRE(times_[i] < times_[i + 1], "times must be increasing ("
871✔
48
                                                    << times_[i] << "@" << i
49
                                                    << " , " << times_[i + 1]
50
                                                    << "@" << i + 1 << ")");
51
        for (Size i = 0; i < vols_.size(); i++)
1,359✔
52
            QL_REQUIRE(vols_[i] >= 0.0, "volatilities must be non negative ("
1,190✔
53
                                           << vols_[i] << "@" << i << ")");
54
    }
169✔
55

UNCOV
56
    Real MfStateProcess::x0() const { return 0.0; }
×
57

58
    Real MfStateProcess::drift(Time, Real) const { return 0.0; }
×
59

60
    Real MfStateProcess::diffusion(Time t, Real) const {
7✔
61
        Size i =
62
            std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
7✔
63
        return vols_[i];
7✔
64
    }
65

66
    Real MfStateProcess::expectation(Time, Real x0, Time dt) const {
13,666✔
67
        return x0;
13,666✔
68
    }
69

70
    Real MfStateProcess::stdDeviation(Time t, Real x0, Time dt) const {
19,855,825✔
71
        return std::sqrt(variance(t, x0, dt));
19,855,825✔
72
    }
73

74
    Real MfStateProcess::variance(Time t, Real, Time dt) const {
19,855,841✔
75

76
        if (dt < QL_EPSILON)
19,855,841✔
77
            return 0.0;
78
        if (times_.empty())
19,571,209✔
79
            return reversionZero_ ? dt
15,591,311✔
80
                                  : 1.0 / (2.0 * reversion_) *
15,591,309✔
81
                                        (std::exp(2.0 * reversion_ * (t + dt)) -
15,591,309✔
82
                                         std::exp(2.0 * reversion_ * t));
15,591,309✔
83

84
        Size i =
85
            std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
3,979,898✔
86
        Size j = std::upper_bound(times_.begin(), times_.end(), t + dt) -
3,979,898✔
87
                 times_.begin();
3,979,898✔
88

89
        Real v = 0.0;
90

91
        for (Size k = i; k < j; k++) {
17,104,631✔
92
            if (reversionZero_)
13,124,733✔
93
                v += vols_[k] * vols_[k] *
14✔
94
                     (times_[k] - std::max(k > 0 ? times_[k - 1] : 0.0, t));
8✔
95
            else
96
                v += 1.0 / (2.0 * reversion_) * vols_[k] * vols_[k] *
13,124,726✔
97
                     (std::exp(2.0 * reversion_ * times_[k]) -
26,249,452✔
98
                      std::exp(2.0 * reversion_ *
13,124,726✔
99
                               std::max(k > 0 ? times_[k - 1] : 0.0, t)));
13,127,103✔
100
        }
101

102
        if (reversionZero_)
3,979,898✔
103
            v += vols_[j] * vols_[j] *
12✔
104
                 (t + dt - std::max(j > 0 ? times_[j - 1] : 0.0, t));
11✔
105
        else
106
            v += 1.0 / (2.0 * reversion_) * vols_[j] * vols_[j] *
3,979,892✔
107
                 (std::exp(2.0 * reversion_ * (t + dt)) -
7,959,784✔
108
                  std::exp(2.0 * reversion_ *
3,979,892✔
109
                           (std::max(j > 0 ? times_[j - 1] : 0.0, t))));
7,962,054✔
110

111
        return v;
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc