Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/shortrate/generalizedornsteinuhlenbeckprocess.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2010 SunTrust Bank
5
 Copyright (C) 2010 Cavit Hafizoglu
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
 <https://www.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
#include <ql/experimental/shortrate/generalizedornsteinuhlenbeckprocess.hpp>
22
#include <utility>
23
24
namespace QuantLib {
25
26
    GeneralizedOrnsteinUhlenbeckProcess::GeneralizedOrnsteinUhlenbeckProcess(
27
        std::function<Real(Time)> speed, std::function<Real(Time)> vol, Real x0, Real level)
28
0
    : x0_(x0), level_(level), speed_(std::move(speed)), volatility_(std::move(vol)) {
29
30
0
        QL_REQUIRE(x0 >= 0.0, "negative initial data given");
31
0
        QL_REQUIRE(level >= 0.0, "negative level given");
32
0
    }
33
34
0
    Real GeneralizedOrnsteinUhlenbeckProcess::x0() const {
35
0
        return x0_;
36
0
    }
37
38
0
    Real GeneralizedOrnsteinUhlenbeckProcess::drift(Time t, Real x) const {
39
0
        return speed_(t) * (level_ - x);;
40
0
    }
41
42
0
    Real GeneralizedOrnsteinUhlenbeckProcess::diffusion(Time t, Real) const {
43
0
        return volatility_(t);
44
0
    }
45
46
    Real GeneralizedOrnsteinUhlenbeckProcess::expectation(
47
0
                                             Time t, Real x0, Time dt) const {
48
0
        return level_ + (x0 - level_) * std::exp(-speed_(t)*dt);
49
0
    }
50
51
    Real GeneralizedOrnsteinUhlenbeckProcess::stdDeviation(
52
0
                                             Time t, Real x0, Time dt) const {
53
0
        return std::sqrt(variance(t,x0,dt));
54
0
    }
55
56
    Real GeneralizedOrnsteinUhlenbeckProcess::variance(
57
0
                                              Time t, Real, Time dt) const {
58
0
        Real speed = speed_(t);
59
0
        Volatility vol = volatility_(t);
60
61
0
        if (speed < std::sqrt(QL_EPSILON)) {
62
            // algebraic limit for small speed
63
0
            return vol*vol*dt;
64
0
        } else {
65
0
            return 0.5*vol*vol/speed*(1.0 - std::exp(-2.0*speed*dt));
66
0
        }
67
0
    }
68
69
70
0
    Real GeneralizedOrnsteinUhlenbeckProcess::speed(Time t) const {
71
0
        return speed_(t);
72
0
    }
73
74
0
    Real GeneralizedOrnsteinUhlenbeckProcess::volatility(Time t) const {
75
0
        return volatility_(t);
76
0
    }
77
78
0
    Real GeneralizedOrnsteinUhlenbeckProcess::level() const {
79
0
        return level_;
80
0
    }
81
82
}
83