use crate::bvn::bvn_cdf;
use solow_distributions::{norm_ppf, t_pdf};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GaussianCopula {
pub rho: f64,
}
impl GaussianCopula {
pub fn new(rho: f64) -> Self {
Self { rho }
}
pub fn from_corr(corr: [[f64; 2]; 2]) -> Self {
Self { rho: corr[0][1] }
}
pub fn pdf(&self, u: f64, v: f64) -> f64 {
let rho = self.rho;
let x = norm_ppf(u);
let y = norm_ppf(v);
let det = 1.0 - rho * rho;
let quad = (x * x + y * y - 2.0 * rho * x * y) / det - (x * x + y * y);
(-0.5 * quad).exp() / det.sqrt()
}
pub fn logpdf(&self, u: f64, v: f64) -> f64 {
let rho = self.rho;
let x = norm_ppf(u);
let y = norm_ppf(v);
let det = 1.0 - rho * rho;
let quad = (x * x + y * y - 2.0 * rho * x * y) / det - (x * x + y * y);
-0.5 * quad - 0.5 * det.ln()
}
pub fn cdf(&self, u: f64, v: f64) -> f64 {
let x = norm_ppf(u);
let y = norm_ppf(v);
bvn_cdf(x, y, self.rho)
}
pub fn tau(&self) -> f64 {
std::f64::consts::FRAC_2_PI * self.rho.asin()
}
pub fn spearmans_rho(&self) -> f64 {
6.0 / std::f64::consts::PI * (self.rho / 2.0).asin()
}
pub fn rho_from_tau(tau: f64) -> f64 {
(std::f64::consts::FRAC_PI_2 * tau).sin()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StudentTCopula {
pub rho: f64,
pub df: f64,
}
impl StudentTCopula {
pub fn new(rho: f64, df: f64) -> Self {
Self { rho, df }
}
pub fn pdf(&self, u: f64, v: f64) -> f64 {
use solow_distributions::t_ppf;
let nu = self.df;
let rho = self.rho;
let x = t_ppf(u, nu);
let y = t_ppf(v, nu);
let det = 1.0 - rho * rho;
let lgamma = solow_distributions::lgamma;
let d = 2.0;
let log_c = lgamma((nu + d) / 2.0)
- lgamma(nu / 2.0)
- (d / 2.0) * (nu * std::f64::consts::PI).ln()
- 0.5 * det.ln();
let quad = (x * x - 2.0 * rho * x * y + y * y) / det;
let log_joint = log_c - (nu + d) / 2.0 * (1.0 + quad / nu).ln();
let log_marg = t_pdf(x, nu).ln() + t_pdf(y, nu).ln();
(log_joint - log_marg).exp()
}
pub fn tau(&self) -> f64 {
std::f64::consts::FRAC_2_PI * self.rho.asin()
}
}
#[allow(dead_code)]
fn bvn_pdf(x: f64, y: f64, rho: f64) -> f64 {
let det = 1.0 - rho * rho;
let quad = (x * x + y * y - 2.0 * rho * x * y) / det;
(-0.5 * quad).exp() / (std::f64::consts::TAU * det.sqrt())
}
#[cfg(test)]
mod tests {
use super::*;
use solow_distributions::norm_pdf;
#[test]
fn gaussian_pdf_via_bvn_pdf() {
let g = GaussianCopula::new(0.5);
for (u, v) in [(0.3, 0.7), (0.1, 0.9), (0.5, 0.5)] {
let x = norm_ppf(u);
let y = norm_ppf(v);
let direct = bvn_pdf(x, y, 0.5) / (norm_pdf(x) * norm_pdf(y));
assert!((g.pdf(u, v) - direct).abs() < 1e-12);
}
}
#[test]
fn gaussian_independence_pdf_is_one() {
let g = GaussianCopula::new(0.0);
for (u, v) in [(0.2, 0.8), (0.5, 0.5), (0.95, 0.05)] {
assert!((g.pdf(u, v) - 1.0).abs() < 1e-12);
}
}
#[test]
fn gaussian_tau_spearman_roundtrip() {
for &rho in &[-0.6, 0.25, 0.8] {
let g = GaussianCopula::new(rho);
let back = GaussianCopula::rho_from_tau(g.tau());
assert!((back - rho).abs() < 1e-12);
}
}
#[test]
fn gaussian_logpdf_matches_pdf() {
let g = GaussianCopula::new(-0.4);
for (u, v) in [(0.3, 0.6), (0.8, 0.2)] {
assert!((g.logpdf(u, v).exp() - g.pdf(u, v)).abs() < 1e-12);
}
}
#[test]
fn studentt_independence_pdf_is_one() {
let t = StudentTCopula::new(0.0, 5.0);
assert!((t.tau()).abs() < 1e-15);
let p = t.pdf(0.3, 0.7);
assert!(p.is_finite() && p > 0.0);
}
}