Coverage Report

Created: 2026-06-08 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/credit/onefactorstudentcopula.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Roland Lichters
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
/*! \file onefactorstudentcopula.hpp
21
    \brief One-factor Student-t copula
22
*/
23
24
#ifndef quantlib_one_factor_student_copula_hpp
25
#define quantlib_one_factor_student_copula_hpp
26
27
#include <ql/experimental/credit/onefactorcopula.hpp>
28
#include <ql/math/distributions/studenttdistribution.hpp>
29
#include <ql/math/distributions/normaldistribution.hpp>
30
31
namespace QuantLib {
32
33
    //! One-factor Double Student t-Copula
34
    /*! The copula model
35
        \f[ Y_i = a_i\,M+\sqrt{1-a_i^2}\:Z_i \f]
36
37
        is specified here by setting the probability density functions
38
        for \f$ Z_i \f$ (\f$ D_Z \f$) and \f$ M \f$ (\f$ D_M \f$) to
39
        Student t-distributions with \f$ N_z \f$ and \f$ N_m \f$
40
        degrees of freedom, respectively.
41
42
        The variance of the Student t-distribution with \f$ \nu \f$
43
        degrees of freedom is \f$ \nu / (\nu - 2) \f$. Since the
44
        copula approach requires zero mean and unit variance
45
        distributions, variables \f$ Z \f$ and \f$ M \f$ are scaled by
46
        \f$ \sqrt{(N_z - 2) / N_z} \f$ and \f$ \sqrt{(N_m - 2) / N_m}, \f$
47
        respectively.
48
49
        \todo Improve performance/accuracy of the calculation of
50
              inverse cumulative Y. Tabulate and store it for selected
51
              correlations?
52
    */
53
    class OneFactorStudentCopula : public OneFactorCopula {
54
      public:
55
        OneFactorStudentCopula (const Handle<Quote>& correlation,
56
                                int nz, int nm,
57
                                Real maximum = 10, Size integrationSteps = 200);
58
59
        Real density(Real m) const override;
60
        Real cumulativeZ(Real z) const override;
61
62
      private:
63
        //! Observer interface
64
        void performCalculations() const override;
65
66
        StudentDistribution density_;              // density of M
67
        CumulativeStudentDistribution cumulative_; // cumulated density of Z
68
        int nz_;                                   // degrees of freedom of Z
69
        int nm_;                                   // degrees of freedom of M
70
71
        Real scaleM_; // scaling for m to ensure unit variance
72
        Real scaleZ_; // scaling for z to ensure unit variance
73
74
        // This function is used to update the table of the cumulative
75
        // distribution of Y. It is invoked by performCalculations() when the
76
        // correlation handle is amended.
77
        Real cumulativeYintegral (Real y) const;
78
    };
79
80
0
    inline Real OneFactorStudentCopula::density (Real m) const {
81
0
        return density_(m / scaleM_) / scaleM_;
82
0
    }
83
84
0
    inline Real OneFactorStudentCopula::cumulativeZ (Real z) const {
85
0
        return cumulative_(z / scaleZ_);
86
0
    }
87
88
89
    //! One-factor Gaussian-Student t-Copula
90
    /*! The copula model
91
        \f[ Y_i = a_i\,M+\sqrt{1-a_i^2}\:Z_i \f]
92
93
        is specified here by setting the probability density functions
94
        for \f$ Z_i \f$ (\f$ D_Z \f$) to a Student t-distributions
95
        with \f$ N_z \f$ degrees of freedom, and for \f$ M \f$
96
        (\f$ D_M \f$) to a Gaussian.
97
98
        The variance of the Student t-distribution with \f$ \nu \f$
99
        degrees of freedom is \f$ \nu / (\nu - 2) \f$. Since the
100
        copula approach requires zero mean and unit variance
101
        distributions, \f$ Z \f$ is scaled by \f$ \sqrt{(N_z - 2) /
102
        N_z}.\f$
103
104
        \todo Improve performance/accuracy of the calculation of
105
              inverse cumulative Y. Tabulate and store it for selected
106
              correlations?
107
    */
108
    class OneFactorGaussianStudentCopula : public OneFactorCopula {
109
      public:
110
        OneFactorGaussianStudentCopula (const Handle<Quote>& correlation,
111
                                        int nz,
112
                                        Real maximum = 10,
113
                                        Size integrationSteps = 200);
114
115
        Real density(Real m) const override;
116
        Real cumulativeZ(Real z) const override;
117
118
      private:
119
        //! Observer interface
120
        void performCalculations() const override;
121
122
        NormalDistribution density_;               // density of M
123
        CumulativeStudentDistribution cumulative_; // cumulated density of Z
124
        int nz_;                                   // degrees of freedom of Z
125
126
        Real scaleZ_; // scaling for z to ensure unit variance
127
128
        // This function is used to update the table of the cumulative
129
        // distribution of Y. It is invoked by performCalculations() when the
130
        // correlation handle is amended.
131
        Real cumulativeYintegral (Real y) const;
132
    };
133
134
0
    inline Real OneFactorGaussianStudentCopula::density (Real m) const {
135
0
        return density_(m);
136
0
    }
137
138
0
    inline Real OneFactorGaussianStudentCopula::cumulativeZ (Real z) const {
139
0
        return cumulative_(z / scaleZ_);
140
0
    }
141
142
143
    //! One-factor Student t - Gaussian Copula
144
    /*! The copula model
145
        \f[ Y_i = a_i\,M+\sqrt{1-a_i^2}\:Z_i \f]
146
        is specified here by setting the probability density functions
147
        for \f$ Z_i \f$ (\f$ D_Z \f$) to a Gaussian and for \f$ M \f$
148
        (\f$ D_M \f$) to a Student t-distribution with \f$ N_m \f$
149
        degrees of freedom.
150
151
        The variance of the Student t-distribution with \f$ \nu \f$
152
        degrees of freedom is \f$ \nu / (\nu - 2) \f$. Since the
153
        copula approach requires zero mean and unit variance
154
        distributions, \f$ M \f$ is scaled by \f$ \sqrt{(N_m - 2) /
155
        N_m}. \f$
156
157
        \todo Improve performance/accuracy of the calculation of
158
              inverse cumulative Y. Tabulate and store it for selected
159
              correlations?
160
    */
161
    class OneFactorStudentGaussianCopula : public OneFactorCopula {
162
      public:
163
        OneFactorStudentGaussianCopula (const Handle<Quote>& correlation,
164
                                        int nm,
165
                                        Real maximum = 10,
166
                                        Size integrationSteps = 200);
167
168
        Real density(Real m) const override;
169
        Real cumulativeZ(Real z) const override;
170
171
      private:
172
        //! Observer interface
173
        void performCalculations() const override;
174
175
        StudentDistribution density_;              // density of M
176
        CumulativeNormalDistribution cumulative_;  // cumulated density of Z
177
        int nm_;                                   // degrees of freedom of M
178
179
        Real scaleM_; // scaling for m to ensure unit variance
180
181
        // This function is used to update the table of the cumulative
182
        // distribution of Y. It is invoked by performCalculations() when the
183
        // correlation handle is amended.
184
        Real cumulativeYintegral (Real y) const;
185
    };
186
187
0
    inline Real OneFactorStudentGaussianCopula::density (Real m) const {
188
0
        return density_(m / scaleM_) / scaleM_;
189
0
    }
190
191
0
    inline Real OneFactorStudentGaussianCopula::cumulativeZ (Real z) const {
192
0
        return cumulative_(z);
193
0
    }
194
195
}
196
197
198
#endif