7 this->R_inv = this->R.
inv();
11 std::vector<std::vector<double>> U;
13 for (std::size_t i = 0; i < n; i++) {
21 std::size_t d = L.
cols();
24 for (std::size_t j = 0; j < d; j++) {
25 z.
at(j, 0) = gsl_ran_ugaussian(
eng_.get());
29 std::vector<double> u (d, 0.0);
30 for (std::size_t j = 0; j < d; j++) {
31 u[j] = std::clamp(gsl_cdf_ugaussian_P(z.
at(j, 0)),
CLIP_U, 1 -
CLIP_U);
38 std::size_t d = u.size();
41 for (
size_t i = 0; i < d; i++) {
42 z.
at(i, 0) = gsl_cdf_ugaussian_Pinv(std::clamp(u[i], 1e-13, 1. - 1e-13));
46 return -0.5 * (z.
transpose() * (R_inv -
eye(d)) * z).at(0, 0);
50 std::size_t d = u.size();
53 for (
size_t i = 0; i < d; i++) {
54 z.
at(i, 0) = gsl_cdf_ugaussian_Pinv(std::clamp(u[i], 1e-13, 1. - 1e-13));
59 for (
size_t i = 0; i < d; i++) {
60 dlogc.
at(i, 0) /= gsl_ran_ugaussian_pdf(z.
at(i, 0));
67 std::size_t d = u.size();
70 for (
size_t i = 0; i < d; i++) {
71 z.
at(i, 0) = gsl_cdf_ugaussian_Pinv(std::clamp(u[i], 1e-13, 1. - 1e-13));
77 for (
size_t i = 0; i < d; i++) {
78 for (
size_t j = 0; j < d; j++) {
79 double num = -A.
at(i, j);
80 if (i == j) num -= Az.
at(i, 0) * z.
at(i, 0);
81 double den = gsl_ran_ugaussian_pdf(z.
at(i, 0)) * gsl_ran_ugaussian_pdf(z.
at(j, 0));
82 ddlogc.
at(i, j) = num / den;
90 std::size_t d = u.size();
94 for (
size_t i = 0; i < d; i++) {
95 z.
at(i, 0) = gsl_cdf_ugaussian_Pinv(std::clamp(u[i], 1e-13, 1. - 1e-13));
96 phi_z[i] = gsl_ran_ugaussian_pdf(z.
at(i, 0));
104 double log_c = -0.5 * (z.
transpose() * Az).at(0, 0);
105 for (
size_t i = 0; i < d; i++) {
106 dlogc.
at(i, 0) = -Az.
at(i, 0) / phi_z[i];
108 for (
size_t j = 0; j < d; j++) {
109 double num = -A.
at(i, j);
110 if (i == j) num -= Az.
at(i, 0) * z.
at(i, 0);
111 double den = phi_z[i] * phi_z[j];
112 ddlogc.
at(i, j) = num / den;
116 return {log_c, dlogc, ddlogc};
Gaussian copula implementation.
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 eye(std::size_t n)
Returns the identity matrix of size n.
RealMatrix cholesky_L(RealMatrix R)
Returns the lower-triangular Cholesky factor of a PSD matrix.
Vector sample_u() override
Draws a single sample from the copula.
LogDensityDiff log_c_dc_ddc(std::vector< double > u) override
Computes the log-density and its first and second derivatives.
double log_density(Vector u) override
Evaluates the log-density of the copula at a point in the unit cube.
GaussianCopula(unsigned int seed, RealMatrix R)
Constructs a Gaussian copula from a correlation matrix.
RealMatrix ddlog_density(std::vector< double > u) override
Computes the Hessian of the Gaussian copula log-density.
RealMatrix dlog_density(std::vector< double > u) override
Computes the gradient of the Gaussian copula log-density.
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.
SignedLogDet slogdet() const
Computes the signed logarithmic determinant via LU decomposition.
std::size_t cols() const
Returns the number of columns.
RealMatrix transpose() const
Returns the transpose of the matrix.
RealMatrix inv() const
Computes the inverse of the matrix via LU decomposition.