5 throw std::invalid_argument(
"Number of DoF should be at least 2 for finite variance.");
10 this->logdet = R.slogdet().logdet;
11 this->R_inv = R.inv();
15 std::vector<std::vector<double>> U;
17 for (std::size_t i = 0; i < n; i++) {
25 std::size_t d = R.
cols();
28 for (std::size_t j = 0; j < d; j++) {
29 z.
at(j, 0) = gsl_ran_ugaussian(
eng_.get());
33 double w = gsl_ran_chisq(
eng_.get(), nu);
34 z /= std::sqrt(w / nu);
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);
45 std::size_t d = u.size();
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));
54 double quad = (z.
transpose() * R_inv * z).at(0, 0);
59 double log_td = -(nu + d) / 2 * gsl_sf_log_1plusx(quad / nu);
61 return log_t1 + log_td;
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.
RealMatrix cholesky_L(RealMatrix R)
Returns the lower-triangular Cholesky factor of a PSD matrix.
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.
std::size_t cols() const
Returns the number of columns.
RealMatrix transpose() const
Returns the transpose of the matrix.
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.