Coverage Report

Created: 2026-06-23 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/finitedifferences/utilities/squarerootprocessrndcalculator.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2015 Johannes Göttker-Schnetmann
5
 Copyright (C) 2015 Klaus Spanderen
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
22
#include <ql/methods/finitedifferences/utilities/squarerootprocessrndcalculator.hpp>
23
24
#include <boost/math/distributions/non_central_chi_squared.hpp>
25
26
namespace QuantLib {
27
28
    SquareRootProcessRNDCalculator::SquareRootProcessRNDCalculator(
29
        Real v0, Real kappa, Real theta, Real sigma)
30
0
    : v0_(v0), kappa_(kappa), theta_(theta),
31
0
      d_(4*kappa/(sigma*sigma)), df_(d_*theta) {    }
32
33
34
0
    Real SquareRootProcessRNDCalculator::pdf(Real v, Time t) const {
35
0
        const Real e   = std::exp(-kappa_*t);
36
0
        const Real k   = d_/(1-e);
37
0
        const Real ncp = k*v0_*e;
38
39
0
        const boost::math::non_central_chi_squared_distribution<Real>
40
0
            dist(df_, ncp);
41
42
0
        return boost::math::pdf(dist, v*k) * k;
43
0
    }
44
45
0
    Real SquareRootProcessRNDCalculator::cdf(Real v, Time t) const {
46
0
        const Real e   = std::exp(-kappa_*t);
47
0
        const Real k   = d_/(1-e);
48
0
        const Real ncp = k*v0_*e;
49
50
0
        const boost::math::non_central_chi_squared_distribution<Real>
51
0
            dist(df_, ncp);
52
53
0
        return boost::math::cdf(dist, v*k);
54
0
    }
55
56
0
    Real SquareRootProcessRNDCalculator::invcdf(Real q, Time t) const {
57
0
        const Real e   = std::exp(-kappa_*t);
58
0
        const Real k   = d_/(1-e);
59
0
        const Real ncp = k*v0_*e;
60
61
0
        const boost::math::non_central_chi_squared_distribution<Real>
62
0
            dist(df_, ncp);
63
64
0
        return boost::math::quantile(dist, q) / k;
65
0
    }
66
67
0
    Real SquareRootProcessRNDCalculator::stationary_pdf(Real v) const {
68
0
        const Real alpha = 0.5*df_;
69
0
        const Real beta = alpha/theta_;
70
71
0
        return std::pow(beta, alpha)*std::pow(v, alpha-1)
72
0
                *std::exp(-beta*v-boost::math::lgamma(alpha));
73
0
    }
74
75
0
    Real SquareRootProcessRNDCalculator::stationary_cdf(Real v) const {
76
0
        const Real alpha = 0.5*df_;
77
0
        const Real beta = alpha/theta_;
78
79
0
        return boost::math::gamma_p(alpha, beta*v);
80
0
    }
81
82
0
    Real SquareRootProcessRNDCalculator::stationary_invcdf(Real q) const {
83
0
        const Real alpha = 0.5*df_;
84
0
        const Real beta = alpha/theta_;
85
86
0
        return boost::math::gamma_p_inv(alpha, q)/beta;
87
0
    }
88
}