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
use std::f64::consts::PI;

use crate::{AvgLLH, Error, Mixable, Parametrizable};
use itertools::izip;
use ndarray::parallel::prelude::*;
use ndarray::prelude::*;

use itertools::Itertools;

use super::utils::{
    get_det_spd, get_shape2, get_shape3, get_weighted_means, get_weighted_sum, invert_spd,
};

/// Represents a gaussian mixture model. The number of components
/// is determined automatically from the responsibilities when first
/// calling `maximize` method.
///
/// The sufficient statistics that can be extracted from
/// the data to maximize the parameters. It is a triple of
/// arrays (names as in [Kimura et al.](https://link.springer.com/article/10.1007/s10044-011-0256-4)):
///
/// $$ \begin{aligned}
/// a_j &= \sum_i^n r_{ij},&& (k) \\\\
/// b_j &= \sum_i^n r_{ij} \cdot x_i ,&& (k \times d) \\\\
/// c_j &= \sum_i^n r_{ij} \cdot x_i^T\cdot x_i, &&(k \times d \times d) \\\\
/// \end{aligned} $$
#[derive(Default, Debug, Clone)]
pub struct Gaussian {
    /// The mean values, $ k\times d $
    pub means: Array2<f64>,
    /// The covariance matrices), $(k\times d\times d)$
    pub covariances: Array3<f64>,
    /// The precision matrices (inverted coariances), $(k\times d\times d)$
    pub precisions: Array3<f64>,
    pub summands: Array1<f64>,
    sufficient_statistics: <Gaussian as Parametrizable>::SufficientStatistics,
}

impl Gaussian {
    pub fn new() -> Gaussian {
        Gaussian {
            ..Default::default()
        }
    }
}

impl Parametrizable for Gaussian {
    type SufficientStatistics = (Array1<f64>, Array2<f64>, Array3<f64>);

    type Likelihood = Array2<f64>;
    type DataIn<'a> = ArrayView2<'a, f64>;

    type DataOut = Array2<f64>;

    fn expect(&self, data: &Self::DataIn<'_>) -> Result<(Self::Likelihood, AvgLLH), Error> {
        let [k, _d, _] = get_shape3(&self.covariances)?;

        // n x k x d
        let adjusted = &data.slice(s![.., NewAxis, ..]) - &self.means.slice(s![NewAxis, .., ..]);

        let [n, _d] = get_shape2(data)?;

        let mut responsibilities = Array2::<f64>::default((n, k));

        (
            adjusted.axis_iter(Axis(1)),
            responsibilities.axis_iter_mut(Axis(1)),
            self.precisions.axis_iter(Axis(0)),
            self.summands.axis_iter(Axis(0)),
        ) // iterate k
            .into_par_iter()
            .for_each(|(samples, mut rsp, precision, summand)| {
                izip!(samples.axis_iter(Axis(0)), rsp.axis_iter_mut(Axis(0))) // iterate n
                    .for_each(|(x, mut r)| {
                        let x = x.slice(s![.., NewAxis]);
                        let x = &x.t().dot(&precision).dot(&x).into_shape(()).unwrap();
                        let x = &summand - x;
                        r.assign(&x);
                    })
            });

        Ok((responsibilities, AvgLLH(f64::NAN)))
    }

    fn compute(
        &self,
        data: &Self::DataIn<'_>,
        responsibilities: &Self::Likelihood,
    ) -> Result<Self::SufficientStatistics, Error> {
        let sum_responsibilities = responsibilities.sum_axis(Axis(0)); // (K)

        let weighted_sum = get_weighted_sum(&data, &responsibilities); // (K x d)

        let [k, d] = get_shape2(&weighted_sum.view())?;

        let mut covs = Array3::<f64>::zeros((k, d, d)); // (K x d x d)

        // Einstein sum: NxD, NxK -> KxDxD ??
        (
            covs.axis_iter_mut(Axis(0)),
            responsibilities.axis_iter(Axis(1)),
        ) // iterate k (can be parallelized)
            .into_par_iter()
            .for_each(|(mut cov, resp)| {
                data.axis_iter(Axis(0)) // iterate n (cannot be parallelized)
                    .zip(resp.axis_iter(Axis(0)))
                    .for_each(|(x, r)| {
                        let x = x.slice(s![NewAxis, ..]);
                        let x = &r.slice(s![NewAxis, NewAxis]) * &x.t().dot(&x);
                        cov += &x;
                    });
            });

        Ok((sum_responsibilities, weighted_sum, covs))
    }

    fn maximize(
        &mut self,
        sufficient_statistics: &Self::SufficientStatistics,
    ) -> Result<(), Error> {
        self.means = get_weighted_means(&sufficient_statistics.1, &sufficient_statistics.0); // (K x D)

        let [k, d, _d] = get_shape3(&sufficient_statistics.2)?;
        let mut product = Array3::<f64>::zeros((k, d, d)); // means.T * means

        (
            product.axis_iter_mut(Axis(0)), // einsum('kd,ke->kde', mean, mean)
            self.means.axis_iter(Axis(0)),
        )
            .into_par_iter()
            .for_each(|(mut prod, mean)| {
                prod.assign(
                    &mean
                        .slice(s![.., NewAxis])
                        .dot(&mean.slice(s![NewAxis, ..])),
                )
            });

        self.covariances = &sufficient_statistics.2
            / &sufficient_statistics.0.slice(s![.., NewAxis, NewAxis])
            - &product;

        // Compute variables needed in the expectation step
        self.precisions = Array3::<f64>::zeros((k, d, d));
        self.summands = Array1::<f64>::zeros(k);

        (
            self.covariances.axis_iter(Axis(0)),
            self.precisions.axis_iter_mut(Axis(0)),
            self.summands.axis_iter_mut(Axis(0)),
        )
            .into_par_iter()
            .for_each(|(cov, mut prec, mut summand)| {
                prec.assign(&invert_spd(&cov).unwrap());
                summand.assign(&arr0(
                    -(k as f64) / 2.0 * (2.0 * PI).ln() - get_det_spd(&cov).unwrap().ln(),
                ))
            });

        // TODO: where are the weights updated. Reminder that this happens in the finite class!

        Ok(())
    }

    fn update(
        &mut self,
        sufficient_statistics: &Self::SufficientStatistics,
        weight: f64,
    ) -> Result<(), Error> {
        // check values of weight
        self.sufficient_statistics.0 =
            &self.sufficient_statistics.0 * (1.0 - weight) + &sufficient_statistics.0 * weight;
        self.sufficient_statistics.1 =
            &self.sufficient_statistics.1 * (1.0 - weight) + &sufficient_statistics.1 * weight;
        self.sufficient_statistics.2 =
            &self.sufficient_statistics.2 * (1.0 - weight) + &sufficient_statistics.2 * weight;
        Ok(())
    }

    fn merge(
        sufficient_statistics: &[&Self::SufficientStatistics],
        weights: &[f64],
    ) -> Result<Self::SufficientStatistics, Error> {
        // if time matters fill arrays initialized outside of the closure
        // Reduct is slower.
        Ok(sufficient_statistics
            .iter()
            .zip(weights.iter())
            .map(|(s, w)| (&s.0 * *w, &s.1 * *w, &s.2 * *w))
            .reduce(|s1, s2| (&s1.0 + &s2.0, &s1.1 + &s2.1, &s1.2 + &s2.2))
            .unwrap())
    }

    fn predict(&self, _data: &Self::DataIn<'_>) -> Result<Self::DataOut, Error> {
        Err(Error::NotImplemented)
    }
}

impl Mixable<Gaussian> for Gaussian {
    fn predict(
        &self,
        _latent_likelihood: <Gaussian as Parametrizable>::Likelihood,
        _data: &<Gaussian as Parametrizable>::DataIn<'_>,
    ) -> Result<<Gaussian as Parametrizable>::DataOut, Error> {
        Err(Error::NotImplemented)
    }
}

/// Function that sorts the parameters (means, covariances) of a Gaussian mixture according to the
/// weights of the components. TODO: This should be part of the Mixable interface
/// but this is difficult right now (type of the pmf is unknown in the interace).
/// see [this blog post](https://www.lemonfold.io/posts/2023/rust/sorting_ndarray/)
pub fn sort_parameters(gmm: &Gaussian, pmf: &ArrayView1<f64>) -> (Array2<f64>, Array3<f64>) {
    let mut means = gmm.means.clone();
    let mut covariances = gmm.covariances.clone();

    pmf.as_slice()
        .unwrap()
        .into_iter()
        .enumerate()
        .sorted_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
        .enumerate()
        .for_each(|(i, (j, _))| {
            means
                .slice_mut(s![i, ..])
                .assign(&gmm.means.slice(s![j, ..]));
            covariances
                .slice_mut(s![i, .., ..])
                .assign(&gmm.covariances.slice(s![j, .., ..]));
        });

    (means, covariances)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::ndarray::utils::{filter_data, generate_samples};
    use tracing::info;
    use tracing_test::traced_test;

    #[traced_test]
    #[test]
    fn check_maximization() {
        let (data, responsibilities, _, covariances) =
            generate_samples(&[100000, 100000, 100000], 2);

        let mut gaussian = Gaussian::new();

        let sufficient_statistics = gaussian.compute(&data.view(), &responsibilities).unwrap();

        gaussian.maximize(&sufficient_statistics).unwrap();

        assert!(covariances.abs_diff_eq(&gaussian.covariances, 1e-1))
    }

    #[traced_test]
    #[test]
    fn check_expectation() {
        // Simulate an expectation maximization step:
        // 1. generate data with responsibility, 2. maximize, 3. expect, 3. maximize again, 4. compare to ground trouth
        let (data, responsibilities, _, covariances) =
            generate_samples(&[200000, 200000, 200000], 2);

        let mut gaussian = Gaussian::new();

        let sufficient_statistics = gaussian.compute(&data.view(), &responsibilities).unwrap();
        gaussian.maximize(&sufficient_statistics).unwrap();
        // assert!(covariances.abs_diff_eq(&gaussian.covariances, 1e-1));
        let (responsibilities, _) = gaussian.expect(&data.view()).unwrap();
        let responsibilities = responsibilities.map(|x| x.exp());
        let responsibilities =
            &responsibilities / &responsibilities.sum_axis(Axis(1)).slice(s![.., NewAxis]);

        let sufficient_statistics = gaussian.compute(&data.view(), &responsibilities).unwrap();
        gaussian.maximize(&sufficient_statistics).unwrap();

        info!(%covariances);
        info!(%gaussian.covariances);
        let diff = &covariances - &gaussian.covariances;
        info!(%diff);
        assert!(covariances.abs_diff_eq(&gaussian.covariances, 2e-1));
    }

    /// Checks whether we can learn on partitioned data! Important for federated learning.
    #[traced_test]
    #[test]
    fn check_merge() {
        let (data, responsibilities, _, covariances) = generate_samples(&[10000, 10000, 10000], 2);
        let (data_1, responsibilities_1) =
            filter_data(&data.view(), &responsibilities.view(), |x, _y| x[1] > 0.5).unwrap();
        let (data_2, responsibilities_2) =
            filter_data(&data.view(), &responsibilities.view(), |x, _y| x[1] <= 0.5).unwrap();

        let mut gaussian = Gaussian::new();

        let sufficient_statistics_1 = gaussian
            .compute(&data_1.view(), &responsibilities_1)
            .unwrap();
        let sufficient_statistics_2 = gaussian
            .compute(&data_2.view(), &responsibilities_2)
            .unwrap();

        gaussian.maximize(&sufficient_statistics_1).unwrap();

        info!("{}", covariances.abs_diff_eq(&gaussian.covariances, 1e-3));

        // This should fail--we ignored much of the data
        assert!(!covariances.abs_diff_eq(&gaussian.covariances, 1e-3));

        gaussian.maximize(&sufficient_statistics_2).unwrap();

        // This should fail--we ignored much of the data
        assert!(!covariances.abs_diff_eq(&gaussian.covariances, 1e-3));

        let sufficient_statistics = Gaussian::merge(
            &[&sufficient_statistics_1, &sufficient_statistics_2],
            &[0.5, 0.5],
        )
        .unwrap();
        gaussian.maximize(&sufficient_statistics).unwrap();

        info!(%gaussian.covariances);
        info!(%covariances);
        // This should fail--we ignored much of the data
        assert!(covariances.abs_diff_eq(&gaussian.covariances, 1e-3));
    }

    // #[traced_test]
    // #[test]
    // fn how_to_deal_with_zero_dim() {
    //     let mut x = arr0(0.0);
    //     x.assign(&arr0(1.2));
    //     let y = x.get(()).unwrap();
    //     x[()] = 4.0;
    // }
}