copula-core 0.1.0

A comprehensive Rust library for copula modeling, estimation, and simulation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Vine copulas module.
//!
//! Vine copulas (also called pair-copula constructions) are a flexible way to model
//! high-dimensional dependence structures by decomposing them into bivariate copulas.
//!
//! This module implements:
//! - C-vine (Canonical vine) - star-shaped structure
//! - D-vine (Drawable vine) - path-shaped structure
//!
//! ## Vine Copulas Overview
//!
//! A d-dimensional density can be decomposed into:
//! - d marginal densities
//! - d(d-1)/2 bivariate copulas (pair-copulas)
//!
//! The vine structure determines which variables are coupled and in which order.
//!
//! ## Bibliography
//! - Aas, K., et al. (2009). Pair-copula constructions of multiple dependence. *Insurance: Mathematics and Economics*.
//! - Bedford, T., & Cooke, R. M. (2002). Vines - A new graphical model for dependent random variables.
//! - Joe, H. (2014). *Dependence Modeling with Copulas*. CRC Press.

use crate::archimedean::{ClaytonCopula, FrankCopula, GumbelCopula, JoeCopula, AMHCopula};
use crate::elliptical::{GaussianCopula, StudentTCopula};
use crate::{Copula, CopulaError, Result};
use nalgebra::DMatrix;
use rand::Rng;

/// Enum representing different copula types for vine constructions.
///
/// Since Rust's trait objects cannot be used with traits that have generic methods,
/// we use an enum to represent the different copula types.
#[derive(Clone)]
pub enum CopulaType {
    /// Clayton copula
    Clayton(ClaytonCopula),
    /// Gumbel copula
    Gumbel(GumbelCopula),
    /// Frank copula
    Frank(FrankCopula),
    /// Joe copula
    Joe(JoeCopula),
    /// Ali-Mikhail-Haq copula
    AMH(AMHCopula),
    /// Gaussian copula
    Gaussian(GaussianCopula),
    /// Student-t copula
    StudentT(StudentTCopula),
}

impl CopulaType {
    /// Evaluate the CDF.
    fn cdf(&self, u: &[f64]) -> Result<f64> {
        match self {
            CopulaType::Clayton(c) => c.cdf(u),
            CopulaType::Gumbel(c) => c.cdf(u),
            CopulaType::Frank(c) => c.cdf(u),
            CopulaType::Joe(c) => c.cdf(u),
            CopulaType::AMH(c) => c.cdf(u),
            CopulaType::Gaussian(c) => c.cdf(u),
            CopulaType::StudentT(c) => c.cdf(u),
        }
    }

    /// Evaluate the PDF.
    fn pdf(&self, u: &[f64]) -> Result<f64> {
        match self {
            CopulaType::Clayton(c) => c.pdf(u),
            CopulaType::Gumbel(c) => c.pdf(u),
            CopulaType::Frank(c) => c.pdf(u),
            CopulaType::Joe(c) => c.pdf(u),
            CopulaType::AMH(c) => c.pdf(u),
            CopulaType::Gaussian(c) => c.pdf(u),
            CopulaType::StudentT(c) => c.pdf(u),
        }
    }
}

/// A pair-copula element in the vine structure.
///
/// Contains a copula and the conditioning set information.
#[derive(Clone)]
pub struct PairCopula {
    /// The bivariate copula
    copula: CopulaType,
    /// Index of first variable
    var1: usize,
    /// Index of second variable
    var2: usize,
    /// Indices of conditioning variables
    conditioning_set: Vec<usize>,
}

impl PairCopula {
    /// Create a new pair-copula.
    pub fn new(
        copula: CopulaType,
        var1: usize,
        var2: usize,
        conditioning_set: Vec<usize>,
    ) -> Self {
        Self {
            copula,
            var1,
            var2,
            conditioning_set,
        }
    }

    /// Get the conditional CDF: h(u|v) = ∂C(u,v)/∂v
    fn h_function(&self, u: f64, v: f64) -> Result<f64> {
        let h = 1e-8;
        let c1 = self.copula.cdf(&[u, v + h])?;
        let c2 = self.copula.cdf(&[u, v])?;
        Ok(((c1 - c2) / h).clamp(0.0, 1.0))
    }

    /// Get the inverse h-function for sampling
    fn h_inv(&self, u: f64, v: f64) -> Result<f64> {
        // Binary search to find u2 such that h(u2, v) = u
        let mut u2_low = 1e-10;
        let mut u2_high = 1.0 - 1e-10;

        for _ in 0..50 {
            let u2 = (u2_low + u2_high) / 2.0;
            let h_val = self.h_function(u2, v)?;

            if (h_val - u).abs() < 1e-10 {
                return Ok(u2);
            }

            if h_val < u {
                u2_low = u2;
            } else {
                u2_high = u2;
            }
        }

        Ok((u2_low + u2_high) / 2.0)
    }
}

/// C-vine copula (Canonical vine).
///
/// In a C-vine, each tree has a star structure with one variable as the root.
/// Tree 1: copulas C_{1,j} for j=2,...,d
/// Tree 2: copulas C_{2,j|1} for j=3,...,d
/// etc.
///
/// ## Example Structure (4D)
/// Tree 1: C_{12}, C_{13}, C_{14}
/// Tree 2: C_{23|1}, C_{24|1}
/// Tree 3: C_{34|12}
#[derive(Clone)]
pub struct CVineCopula {
    dimension: usize,
    /// Pair-copulas organized by tree level
    /// trees[i] contains the pair-copulas for tree i+1
    trees: Vec<Vec<PairCopula>>,
}

impl CVineCopula {
    /// Create a new C-vine copula.
    ///
    /// # Arguments
    /// * `dimension` - Number of dimensions
    /// * `trees` - Vector of trees, each containing pair-copulas
    ///
    /// # Returns
    /// A new C-vine copula
    pub fn new(dimension: usize, trees: Vec<Vec<PairCopula>>) -> Result<Self> {
        if dimension < 2 {
            return Err(CopulaError::invalid_parameter(
                "dimension must be >= 2 for vine copulas",
            ));
        }

        // Validate structure: should have (dimension - 1) trees
        if trees.len() != dimension - 1 {
            return Err(CopulaError::invalid_parameter(&format!(
                "C-vine with dimension {} should have {} trees, got {}",
                dimension,
                dimension - 1,
                trees.len()
            )));
        }

        // Validate each tree has correct number of pair-copulas
        for (level, tree) in trees.iter().enumerate() {
            let expected_pairs = dimension - level - 1;
            if tree.len() != expected_pairs {
                return Err(CopulaError::invalid_parameter(&format!(
                    "Tree {} should have {} pair-copulas, got {}",
                    level + 1,
                    expected_pairs,
                    tree.len()
                )));
            }
        }

        Ok(Self { dimension, trees })
    }

    /// Compute conditional distributions for sampling.
    fn compute_conditionals(&self, u: &[f64]) -> Result<Vec<Vec<f64>>> {
        let d = self.dimension;
        let mut v = vec![vec![0.0; d]; d];

        // Initialize first row with uniform samples
        for j in 0..d {
            v[0][j] = u[j];
        }

        // Compute conditional distributions tree by tree
        for level in 0..self.trees.len() {
            for (j, pair_cop) in self.trees[level].iter().enumerate() {
                let idx = level + j + 1;
                if idx < d {
                    v[level + 1][idx] = pair_cop.h_function(v[level][idx], v[level][level])?;
                }
            }
        }

        Ok(v)
    }
}

impl Copula for CVineCopula {
    fn cdf(&self, u: &[f64]) -> Result<f64> {
        if u.len() != self.dimension {
            return Err(CopulaError::dimension_mismatch(self.dimension, u.len()));
        }
        crate::error::validate_unit_range(u)?;

        // CDF computation for vine copulas is complex and typically requires numerical integration
        Err(CopulaError::not_implemented(
            "C-vine CDF requires specialized numerical methods",
        ))
    }

    fn pdf(&self, u: &[f64]) -> Result<f64> {
        if u.len() != self.dimension {
            return Err(CopulaError::dimension_mismatch(self.dimension, u.len()));
        }
        crate::error::validate_unit_range(u)?;

        // PDF can be computed as product of all pair-copula densities
        // But requires computing all conditional values
        Err(CopulaError::not_implemented(
            "C-vine PDF computation not yet implemented",
        ))
    }

    fn sample<R: Rng + ?Sized>(&self, n: usize, rng: &mut R) -> Result<DMatrix<f64>> {
        use rand_distr::{Distribution, Uniform};
        let uniform = Uniform::new(0.0, 1.0);

        let d = self.dimension;
        let mut samples = DMatrix::<f64>::zeros(n, d);

        for i in 0..n {
            // Sample d independent uniforms
            let mut w: Vec<f64> = (0..d).map(|_| uniform.sample(rng)).collect();

            // Transform using vine structure
            let mut v = vec![vec![0.0; d]; d];
            v[0][0] = w[0];

            // First tree
            for j in 1..d {
                v[0][j] = self.trees[0][j - 1].h_inv(w[j], v[0][0])?;
            }

            // Subsequent trees
            for level in 1..self.trees.len() {
                for (j, pair_cop) in self.trees[level].iter().enumerate() {
                    let idx = level + j + 1;
                    if idx < d {
                        let cond_val = pair_cop.h_function(v[level - 1][idx], v[level - 1][level])?;
                        v[level][idx] = pair_cop.h_inv(cond_val, v[level - 1][level])?;
                    }
                }
            }

            // Extract final sample
            for j in 0..d {
                samples[(i, j)] = v[0][j];
            }
        }

        Ok(samples)
    }

    fn dimension(&self) -> usize {
        self.dimension
    }
}

/// D-vine copula (Drawable vine).
///
/// In a D-vine, each tree has a path structure.
/// Tree 1: copulas C_{j,j+1} for j=1,...,d-1
/// Tree 2: copulas C_{j,j+2|j+1} for j=1,...,d-2
/// etc.
///
/// ## Example Structure (4D)
/// Tree 1: C_{12}, C_{23}, C_{34}
/// Tree 2: C_{13|2}, C_{24|3}
/// Tree 3: C_{14|23}
#[derive(Clone)]
pub struct DVineCopula {
    dimension: usize,
    /// Pair-copulas organized by tree level
    trees: Vec<Vec<PairCopula>>,
}

impl DVineCopula {
    /// Create a new D-vine copula.
    ///
    /// # Arguments
    /// * `dimension` - Number of dimensions
    /// * `trees` - Vector of trees, each containing pair-copulas
    ///
    /// # Returns
    /// A new D-vine copula
    pub fn new(dimension: usize, trees: Vec<Vec<PairCopula>>) -> Result<Self> {
        if dimension < 2 {
            return Err(CopulaError::invalid_parameter(
                "dimension must be >= 2 for vine copulas",
            ));
        }

        // Validate structure
        if trees.len() != dimension - 1 {
            return Err(CopulaError::invalid_parameter(&format!(
                "D-vine with dimension {} should have {} trees, got {}",
                dimension,
                dimension - 1,
                trees.len()
            )));
        }

        for (level, tree) in trees.iter().enumerate() {
            let expected_pairs = dimension - level - 1;
            if tree.len() != expected_pairs {
                return Err(CopulaError::invalid_parameter(&format!(
                    "Tree {} should have {} pair-copulas, got {}",
                    level + 1,
                    expected_pairs,
                    tree.len()
                )));
            }
        }

        Ok(Self { dimension, trees })
    }
}

impl Copula for DVineCopula {
    fn cdf(&self, u: &[f64]) -> Result<f64> {
        if u.len() != self.dimension {
            return Err(CopulaError::dimension_mismatch(self.dimension, u.len()));
        }
        crate::error::validate_unit_range(u)?;

        Err(CopulaError::not_implemented(
            "D-vine CDF requires specialized numerical methods",
        ))
    }

    fn pdf(&self, u: &[f64]) -> Result<f64> {
        if u.len() != self.dimension {
            return Err(CopulaError::dimension_mismatch(self.dimension, u.len()));
        }
        crate::error::validate_unit_range(u)?;

        Err(CopulaError::not_implemented(
            "D-vine PDF computation not yet implemented",
        ))
    }

    fn sample<R: Rng + ?Sized>(&self, n: usize, rng: &mut R) -> Result<DMatrix<f64>> {
        use rand_distr::{Distribution, Uniform};
        let uniform = Uniform::new(0.0, 1.0);

        let d = self.dimension;
        let mut samples = DMatrix::<f64>::zeros(n, d);

        for i in 0..n {
            // Sample d independent uniforms
            let mut w: Vec<f64> = (0..d).map(|_| uniform.sample(rng)).collect();

            // Initialize first two variables
            let mut v = vec![vec![0.0; d]; d];
            v[0][0] = w[0];
            v[0][1] = self.trees[0][0].h_inv(w[1], v[0][0])?;

            // Build up the D-vine structure
            for j in 2..d {
                // Use tree 0 to get initial value
                v[0][j] = w[j];

                // Transform through previous trees
                for level in 0..(j.min(self.trees.len())) {
                    if level < self.trees.len() && j - level - 1 < self.trees[level].len() {
                        let pair_cop = &self.trees[level][j - level - 1];
                        let cond_var = v[level][j - level - 1];
                        v[level + 1][j] = pair_cop.h_inv(v[level][j], cond_var)?;
                    }
                }
            }

            // Extract final sample
            for j in 0..d {
                samples[(i, j)] = v[0][j];
            }
        }

        Ok(samples)
    }

    fn dimension(&self) -> usize {
        self.dimension
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pair_copula_creation() {
        let clayton = CopulaType::Clayton(ClaytonCopula::new(2.0).unwrap());
        let pair = PairCopula::new(clayton, 0, 1, vec![]);

        assert_eq!(pair.var1, 0);
        assert_eq!(pair.var2, 1);
    }

    #[test]
    fn test_cvine_creation() {
        // Create a simple 3D C-vine
        let c12 = CopulaType::Clayton(ClaytonCopula::new(2.0).unwrap());
        let c13 = CopulaType::Clayton(ClaytonCopula::new(1.5).unwrap());
        let c23_1 = CopulaType::Clayton(ClaytonCopula::new(1.0).unwrap());

        let tree1 = vec![
            PairCopula::new(c12, 0, 1, vec![]),
            PairCopula::new(c13, 0, 2, vec![]),
        ];

        let tree2 = vec![PairCopula::new(c23_1, 1, 2, vec![0])];

        let cvine = CVineCopula::new(3, vec![tree1, tree2]).unwrap();
        assert_eq!(cvine.dimension(), 3);
    }

    #[test]
    fn test_cvine_wrong_num_trees() {
        let c12 = CopulaType::Clayton(ClaytonCopula::new(2.0).unwrap());
        let tree1 = vec![PairCopula::new(c12, 0, 1, vec![])];

        // 3D C-vine should have 2 trees, not 1
        let result = CVineCopula::new(3, vec![tree1]);
        assert!(result.is_err());
    }

    #[test]
    fn test_dvine_creation() {
        // Create a simple 3D D-vine
        let c12 = CopulaType::Clayton(ClaytonCopula::new(2.0).unwrap());
        let c23 = CopulaType::Clayton(ClaytonCopula::new(1.5).unwrap());
        let c13_2 = CopulaType::Clayton(ClaytonCopula::new(1.0).unwrap());

        let tree1 = vec![
            PairCopula::new(c12, 0, 1, vec![]),
            PairCopula::new(c23, 1, 2, vec![]),
        ];

        let tree2 = vec![PairCopula::new(c13_2, 0, 2, vec![1])];

        let dvine = DVineCopula::new(3, vec![tree1, tree2]).unwrap();
        assert_eq!(dvine.dimension(), 3);
    }

    #[test]
    fn test_cvine_sample() {
        use rand::thread_rng;
        let mut rng = thread_rng();

        // Create a simple 2D C-vine (just one copula)
        let c12 = CopulaType::Clayton(ClaytonCopula::new(2.0).unwrap());
        let tree1 = vec![PairCopula::new(c12, 0, 1, vec![])];

        let cvine = CVineCopula::new(2, vec![tree1]).unwrap();
        let samples = cvine.sample(10, &mut rng).unwrap();

        assert_eq!(samples.nrows(), 10);
        assert_eq!(samples.ncols(), 2);

        // Check all values in [0, 1]
        for i in 0..10 {
            for j in 0..2 {
                assert!(samples[(i, j)] >= 0.0 && samples[(i, j)] <= 1.0);
            }
        }
    }
}