Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
StudentTCopula.cpp
Go to the documentation of this file.
1#include "StudentTCopula.h"
2
3StudentTCopula::StudentTCopula(unsigned int seed, RealMatrix R, int nu) : GenericCopula(seed) {
4 if (nu < 2)
5 throw std::invalid_argument("Number of DoF should be at least 2 for finite variance.");
6
7 this->nu = nu;
8 this->R = nearest_psd(R);
9 this->L = cholesky_L(R);
10 this->logdet = R.slogdet().logdet;
11 this->R_inv = R.inv();
12}
13
14std::vector<std::vector<double>> StudentTCopula::sample_u(std::size_t n) {
15 std::vector<std::vector<double>> U;
16
17 for (std::size_t i = 0; i < n; i++) {
18 U.emplace_back(sample_u());
19 }
20
21 return U;
22}
23
24std::vector<double> StudentTCopula::sample_u() {
25 std::size_t d = R.cols();
26
27 RealMatrix z (d, 1);
28 for (std::size_t j = 0; j < d; j++) {
29 z.at(j, 0) = gsl_ran_ugaussian(eng_.get()); // z follows MN(0, 1)
30 }
31
32 z = L * z; // z follows MN(0, R)
33 double w = gsl_ran_chisq(eng_.get(), nu);
34 z /= std::sqrt(w / nu); // z follows Mt(R, nu)
35
36 std::vector<double> u (d, 0.0);
37 for (std::size_t j = 0; j < d; j++) {
38 u[j] = std::clamp(gsl_cdf_tdist_P(z.at(j, 0), nu), CLIP_U, 1 - CLIP_U); // u follows C(R, nu)
39 }
40
41 return u;
42}
43
44double StudentTCopula::log_density(std::vector<double> u) {
45 std::size_t d = u.size();
46 RealMatrix z (d, 1);
47
48 double log_t1 {0.0};
49 for (size_t i = 0; i < d; i++) {
50 z.at(i, 0) = gsl_cdf_tdist_Pinv(std::clamp(u[i], 1e-15, 1. - 1e-15), nu);
51 log_t1 += std::log(gsl_ran_tdist_pdf(z.at(i, 0), nu));
52 }
53
54 double quad = (z.transpose() * R_inv * z).at(0, 0);
55 // double log_td = gsl_sf_lngamma((nu + d) / 2) - gsl_sf_lngamma(nu / 2)
56 // - (d / 2) * std::log(nu * PI) - 0.5 * logdet
57 // - (nu + d) / 2 * gsl_sf_log_1plusx(quad / nu);
58
59 double log_td = -(nu + d) / 2 * gsl_sf_log_1plusx(quad / nu);
60
61 return log_t1 + log_td;
62}
63
65{
66 // TODO
67 return RealMatrix();
68}
69
71{
72 // TODO
73 return RealMatrix();
74}
constexpr double CLIP_U
Small clipping threshold used to avoid exact 0 or 1 uniforms.
RealMatrix nearest_psd(RealMatrix R, double thr)
Projects a matrix to the nearest positive semi-definite correlation-like matrix.
Definition Matrix.cpp:475
RealMatrix cholesky_L(RealMatrix R)
Returns the lower-triangular Cholesky factor of a PSD matrix.
Definition Matrix.cpp:508
Student-t copula implementation.
Partial base implementation of a copula with RNG support.
gsl_rng_sptr eng_
GSL RNG type used by the copula.
double & at(size_t i, size_t j)
Returns a mutable reference to element (i,j) with bounds checking.
Definition Matrix.cpp:587
std::size_t cols() const
Returns the number of columns.
Definition Matrix.cpp:605
RealMatrix transpose() const
Returns the transpose of the matrix.
Definition Matrix.cpp:698
RealMatrix dlog_density(std::vector< double > u) override
Computes the gradient of the Student-t copula log-density.
std::vector< double > sample_u() override
Draws a single sample from the copula.
RealMatrix ddlog_density(std::vector< double > u) override
Computes the Hessian of the Student-t copula log-density.
StudentTCopula(unsigned int seed, RealMatrix R, int nu)
Constructs a Student-t copula.
double log_density(std::vector< double > u) override
Evaluates the log-density of the copula at a point in the unit cube.