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
use super::wishart::Wishart;
use crate::{
DependentJoint, Distribution, IndependentJoint, RandomVariable, SampleableDistribution,
WishartParams,
};
use crate::{DistributionError, InverseWishartParams};
use opensrdk_linear_algebra::pp::trf::PPTRF;
use rand::prelude::*;
use std::{ops::BitAnd, ops::Mul};
#[derive(Clone, Debug)]
pub struct InverseWishart;
#[derive(thiserror::Error, Debug)]
pub enum InverseWishartError {
#[error("Dimension mismatch")]
DimensionMismatch,
#[error("'ν' must be >= dimension")]
NuMustBeGTEDimension,
#[error("Unknown error")]
Unknown,
}
impl Distribution for InverseWishart {
type Value = PPTRF;
type Condition = InverseWishartParams;
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let lpsi = theta.lpsi().0.to_mat();
let nu = theta.nu();
let p = x.0.dim() as f64;
let lx = x.0.to_mat();
Ok(lx.trdet().powf(-(nu + p + 1.0) / 2.0)
* (-0.5 * x.clone().pptrs(&lpsi * lpsi.t())?.tr()).exp())
}
}
impl<Rhs, TRhs> Mul<Rhs> for InverseWishart
where
Rhs: Distribution<Value = TRhs, Condition = InverseWishartParams>,
TRhs: RandomVariable,
{
type Output = IndependentJoint<Self, Rhs, PPTRF, TRhs, InverseWishartParams>;
fn mul(self, rhs: Rhs) -> Self::Output {
IndependentJoint::new(self, rhs)
}
}
impl<Rhs, URhs> BitAnd<Rhs> for InverseWishart
where
Rhs: Distribution<Value = InverseWishartParams, Condition = URhs>,
URhs: RandomVariable,
{
type Output = DependentJoint<Self, Rhs, PPTRF, InverseWishartParams, URhs>;
fn bitand(self, rhs: Rhs) -> Self::Output {
DependentJoint::new(self, rhs)
}
}
impl SampleableDistribution for InverseWishart {
fn sample(
&self,
theta: &Self::Condition,
rng: &mut dyn RngCore,
) -> Result<Self::Value, DistributionError> {
let lpsi = theta.lpsi();
let nu = theta.nu();
let lpsi_inv = lpsi.clone().pptri()?;
let w = Wishart;
let w_params = WishartParams::new(PPTRF(lpsi_inv), nu)?;
let x = w.sample(&w_params, rng)?;
let x_inv = x.pptri()?;
Ok(x_inv.pptrf().unwrap())
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}