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
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

use crate::data::DataOrSuffStat;
use crate::dist::InvGamma;
use crate::traits::SuffStat;

/// Inverse Gamma sufficient statistic.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", serde(rename_all = "snake_case"))]
pub struct InvGammaSuffStat {
    /// Number of observations
    n: usize,
    /// sum of `ln(x)`
    sum_ln_x: f64,
    /// sum of `1/x`
    sum_inv_x: f64,
}

impl InvGammaSuffStat {
    #[inline]
    pub fn new() -> Self {
        InvGammaSuffStat {
            n: 0,
            sum_ln_x: 0.0,
            sum_inv_x: 0.0,
        }
    }

    /// Create a sufficient statistic from components without checking whether
    /// they are valid.
    #[inline]
    pub fn from_parts_unchecked(
        n: usize,
        sum_ln_x: f64,
        sum_inv_x: f64,
    ) -> Self {
        InvGammaSuffStat {
            n,
            sum_ln_x,
            sum_inv_x,
        }
    }

    /// Get the number of observations
    #[inline]
    pub fn n(&self) -> usize {
        self.n
    }

    /// Sum of `ln(x)`
    #[inline]
    pub fn sum_ln_x(&self) -> f64 {
        self.sum_ln_x
    }

    /// Sum of `1/x`
    #[inline]
    pub fn sum_inv_x(&self) -> f64 {
        self.sum_inv_x
    }
}

impl Default for InvGammaSuffStat {
    fn default() -> Self {
        InvGammaSuffStat::new()
    }
}

macro_rules! impl_suffstat {
    ($kind:ty) => {
        impl<'a> From<&'a InvGammaSuffStat>
            for DataOrSuffStat<'a, $kind, InvGamma>
        {
            fn from(stat: &'a InvGammaSuffStat) -> Self {
                DataOrSuffStat::SuffStat(stat)
            }
        }

        impl<'a> From<&'a Vec<$kind>> for DataOrSuffStat<'a, $kind, InvGamma> {
            fn from(xs: &'a Vec<$kind>) -> Self {
                DataOrSuffStat::Data(xs.as_slice())
            }
        }

        impl<'a> From<&'a [$kind]> for DataOrSuffStat<'a, $kind, InvGamma> {
            fn from(xs: &'a [$kind]) -> Self {
                DataOrSuffStat::Data(xs)
            }
        }

        impl From<&Vec<$kind>> for InvGammaSuffStat {
            fn from(xs: &Vec<$kind>) -> Self {
                let mut stat = InvGammaSuffStat::new();
                stat.observe_many(xs);
                stat
            }
        }

        impl From<&[$kind]> for InvGammaSuffStat {
            fn from(xs: &[$kind]) -> Self {
                let mut stat = InvGammaSuffStat::new();
                stat.observe_many(xs);
                stat
            }
        }

        impl SuffStat<$kind> for InvGammaSuffStat {
            fn n(&self) -> usize {
                self.n
            }

            fn observe(&mut self, x: &$kind) {
                let xf = f64::from(*x);

                self.n += 1;

                self.sum_ln_x += xf.ln();
                self.sum_inv_x += xf.recip();
            }

            fn forget(&mut self, x: &$kind) {
                if self.n > 1 {
                    let xf = f64::from(*x);

                    self.sum_ln_x -= xf.ln();
                    self.sum_inv_x -= xf.recip();
                    self.n -= 1;
                } else {
                    self.n = 0;
                    self.sum_ln_x = 0.0;
                    self.sum_inv_x = 0.0;
                }
            }
        }
    };
}

impl_suffstat!(f32);
impl_suffstat!(f64);