prodef 0.1.0

A simple Rust crate for handling probability distributions.
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! A module that implements a multivariate normal probability density function.

use crate::{Density, Domain};
use itertools::{Itertools, zip_eq};
use nalgebra::{
    Const, DMatrix, DVector, DefaultAllocator, Dim, Dyn, MatrixView, OMatrix, OVector, RealField,
    Scalar, U1, VectorView, allocator::Allocator,
};
use rand::Rng;
use rand_distr::{Distribution, StandardNormal};
use serde::{Deserialize, Serialize};
use std::{
    cmp::Ordering,
    iter::Sum,
    ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
};

/// A multivariate normal probability density function.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(bound(
    serialize = "D: Serialize, B: Serialize, OVector<T, D>: Serialize, OMatrix<T, D, D>: Serialize"
))]
#[serde(bound(
    deserialize = "D: Serialize, B: Deserialize<'de>, OVector<T, D>: Deserialize<'de>, OMatrix<T, D, D>: Deserialize<'de>"
))]
pub struct MultiNormalDensity<T, D, B>
where
    T: Scalar,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    matrix: OMatrix<T, D, D>,
    inverse: OMatrix<T, D, D>,
    ltm: OMatrix<T, D, D>,
    domain: B,
    mean: OVector<T, D>,
}

impl<T, D, B> MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    B: 'static + Domain<T, D>,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    /// Returns the determinant of the covariance matrix.
    pub fn determinant(&self) -> T {
        self.ltm
            .diagonal()
            .iter()
            .fold(T::one(), |acc, next| {
                if *next != T::zero() {
                    acc * next.clone()
                } else {
                    acc
                }
            })
            .powi(2)
    }

    /// Returns the amount of Fisher information for derivatves of the mean `da` and `db`.
    pub fn fisher_information<RStride: Dim, CStride: Dim>(
        &self,
        da: &VectorView<T, D, RStride, CStride>,
        db: &VectorView<T, D, RStride, CStride>,
    ) -> T {
        (da.transpose() * &self.inverse * db)[(0, 0)].clone()
    }

    /// Create a [`MultiNormalDensity`] from a covariance matrix.
    pub fn from_matrix(matrix: OMatrix<T, D, D>, mean: OVector<T, D>, domain: B) -> Option<Self> {
        let n_dim = matrix.shape_generic().0;

        let inverse = {
            // Convert input matrix into a DMatrix to bypass annoying ToTypenum domain.
            let dmatrix =
                DMatrix::from_iterator(matrix.nrows(), matrix.ncols(), matrix.iter().cloned());

            let mut pinv = dmatrix
                .clone_owned()
                .pseudo_inverse(T::default_epsilon())
                .expect("failed to compute pseudo inverse");

            dmatrix
                .diagonal()
                .iter()
                .enumerate()
                .for_each(|(idx, value)| {
                    if matches!(
                        value
                            .partial_cmp(&T::zero())
                            .expect("covariance matrix contains NaN values"),
                        Ordering::Equal
                    ) {
                        pinv.set_column(idx, &DVector::<T>::zeros(matrix.ncols()));
                        pinv.set_row(idx, &DVector::<T>::zeros(matrix.ncols()).transpose());
                    }
                });

            let n_dim = matrix.shape_generic().0;

            OMatrix::<T, D, D>::from_iterator_generic(n_dim, n_dim, pinv.iter().cloned())
        };

        let mut d = OVector::<T, D>::zeros_generic(n_dim, Const::<1>);
        let mut l = OMatrix::<T, D, D>::zeros_generic(n_dim, n_dim);

        for cdx in 0..n_dim.value() {
            let mut d_j = matrix[(cdx, cdx)].clone();

            if cdx > 0 {
                for k in 0..cdx {
                    d_j -= d[k].clone() * l[(cdx, k)].clone().powi(2);
                }
            }

            d[cdx] = d_j;

            for rdx in cdx..n_dim.value() {
                let mut l_ij = matrix[(rdx, cdx)].clone();

                for k in 0..cdx {
                    l_ij -= d[k].clone() * l[(cdx, k)].clone() * l[(rdx, k)].clone();
                }

                if matches!(
                    d[cdx]
                        .partial_cmp(&T::zero())
                        .expect("matrix contains NaN values"),
                    Ordering::Equal
                ) {
                    l[(rdx, cdx)] = T::zero();
                } else {
                    l[(rdx, cdx)] = l_ij / d[cdx].clone();
                }
            }
        }

        let lsqrtd = l * OMatrix::from_diagonal(&OVector::from_iterator_generic(
            n_dim,
            Const::<1>,
            d.iter().map(|value| value.clone().sqrt()),
        ));

        Some(Self {
            matrix,
            inverse,
            ltm: lsqrtd,
            domain,
            mean,
        })
    }

    /// Create a [`MultiNormalDensity`] from a set of vectors, with optional weights.
    pub fn from_view<RStride: Dim, CStride: Dim>(
        vectors: &MatrixView<T, D, Dyn, RStride, CStride>,
        domain: B,
        opt_weights: Option<&[T]>,
    ) -> Option<Self>
    where
        T: Sum,
    {
        let n_dim = vectors.shape_generic().0;

        // Construct the covariance matrix.
        let mut matrix = OMatrix::<T, D, D>::from_iterator_generic(
            n_dim,
            n_dim,
            (0..(vectors.nrows().pow(2))).map(|idx| {
                let jdx = idx / n_dim.value();
                let kdx = idx % n_dim.value();

                if jdx <= kdx {
                    let x = vectors.row(jdx);
                    let y = vectors.row(kdx);

                    if !x.iter().all_equal() && !y.iter().all_equal() {
                        match opt_weights {
                            Some(w) => covariance_with_weights(x, y, w),
                            None => covariance(x, y),
                        }
                    } else {
                        T::zero()
                    }
                } else {
                    T::zero()
                }
            }),
        );

        // Fill up the other side of the covariance matrix.
        matrix += matrix.transpose() - OMatrix::from_diagonal(&matrix.diagonal());

        let mut mean = vectors.column_mean();

        // Set mean to first particle value if covariance is zero.
        // This fixes numerical issues where taking the mean over many
        // particles does not equal the constant value.
        matrix
            .diagonal()
            .iter()
            .zip(mean.iter_mut())
            .enumerate()
            .for_each(|(idx, (cov, value))| {
                if cov.eq(&T::zero()) {
                    *value = vectors[(idx, 0)].clone();
                }
            });

        Self::from_matrix(matrix, mean, domain)
    }

    /// Compute the Kullback-Leibler divergence between two [`MultiNormalDensity`]'s.
    pub fn kl_div(&self, other: &MultiNormalDensity<T, D, B>) -> Option<T>
    where
        T: Sum,
    {
        let mut l_0 = self.ltm.clone();
        let mu_0 = &self.mean;

        let mut l_1 = other.ltm.clone();
        let mu_1 = &other.mean;

        let mut n_dim = self.matrix.shape_generic().0.value();

        // Detect zero'd columns/rows that need to be modified.
        (0..l_0.nrows()).for_each(|idx| {
            if l_0[(idx, idx)].eq(&T::zero()) {
                l_0[(idx, idx)] = T::one() / T::zero();

                n_dim -= 1;

                // Set off diagonals to zero.
                for jdx in 0..l_0.ncols() {
                    if jdx != idx {
                        l_0[(idx, jdx)] = T::zero();
                        l_0[(jdx, idx)] = T::zero();
                    }
                }
            };
        });

        // Detect zero'd columns/rows that need to be modified.
        (0..l_1.nrows()).for_each(|idx| {
            if l_1[(idx, idx)].eq(&T::zero()) {
                l_1[(idx, idx)] = T::one() / T::zero();

                // Set off diagonals to zero.
                for jdx in 0..l_1.ncols() {
                    if jdx != idx {
                        l_1[(idx, jdx)] = T::zero();
                        l_1[(jdx, idx)] = T::zero();
                    }
                }
            };
        });

        let mut m = l_1.clone().solve_lower_triangular(&l_0).unwrap();

        // Detect NaN's and zero them out.
        m.iter_mut().for_each(|value| {
            if !value.is_finite() {
                *value = T::zero()
            }
        });

        let y = l_1.clone().solve_lower_triangular(&(mu_1 - mu_0)).unwrap();

        Some(
            (m.iter().cloned().sum::<T>() - T::from_usize(n_dim).unwrap()
                + y.norm()
                + T::from_usize(2).unwrap()
                    * l_1
                        .diagonal()
                        .iter()
                        .zip(l_0.diagonal().iter())
                        .map(|(a, b)| {
                            if a.is_finite() && b.is_finite() {
                                (a.clone() / b.clone()).ln()
                            } else {
                                T::zero()
                            }
                        })
                        .sum::<T>())
                / T::from_usize(2).unwrap(),
        )
    }

    /// Returns the log density value for a given sample.
    pub fn log_density<RStride: Dim, CStride: Dim>(
        &self,
        sample: &VectorView<T, D, RStride, CStride>,
    ) -> Option<T> {
        if !self.domain.contains(sample) {
            return None;
        }

        let diff = sample - self.mean.clone();
        let value = (diff.transpose() * &self.inverse * diff)[(0, 0)].clone();

        let p_nonzero: usize = self.matrix.diagonal().iter().fold(0, |acc, next| {
            if next.abs() > T::zero() { acc + 1 } else { acc }
        });

        Some(
            -(self.determinant().ln()
                + value
                + T::from_usize(p_nonzero).unwrap() * T::two_pi().ln())
                / T::from_usize(2).unwrap(),
        )
    }

    /// Returns the squared Mahalanobis distance.
    pub fn mahalanobis_distance_sq<RStride: Dim, CStride: Dim>(
        &self,
        x: &VectorView<T, D, RStride, CStride>,
    ) -> T {
        (&(x - &self.mean).transpose() * &self.inverse * (x - &self.mean))[(0, 0)].clone()
    }

    /// Returns the normalization factor for the multivariate distribution.
    pub fn normalization_factor(&self) -> T {
        T::one()
            / (T::two_pi()).powf(T::from_usize(self.rank()).unwrap() / T::from_usize(2).unwrap())
            / self.determinant().sqrt()
    }

    /// Returns the rank of the underlying covariance matrix.
    pub fn rank(&self) -> usize {
        self.ltm
            .diagonal()
            .fold(0, |acc, next| if next != T::zero() { acc + 1 } else { acc })
    }

    /// Sets the mean of the distribution.
    pub fn set_mean(&mut self, mean: OVector<T, D>) {
        self.mean = mean;
    }
}

impl<T, D, B> Density<T, D> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    B: 'static + Domain<T, D>,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
    StandardNormal: Distribution<T>,
{
    fn density<RStride: Dim, CStride: Dim>(
        &self,
        sample: &VectorView<T, D, RStride, CStride>,
    ) -> Option<T> {
        (&self).density(sample)
    }

    fn domain(&self) -> impl Domain<T, D> + 'static {
        self.domain.clone()
    }

    fn center(&self) -> OVector<T, D> {
        (&self).center()
    }

    fn is_constant(&self) -> OVector<bool, D> {
        (&self).is_constant()
    }

    fn sample(&self, rng: &mut impl Rng, max_attempts: usize) -> Option<OVector<T, D>> {
        (&self).sample(rng, max_attempts)
    }
}

impl<T, D, B> Density<T, D> for &MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    B: 'static + Domain<T, D>,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
    StandardNormal: Distribution<T>,
{
    fn density<RStride: Dim, CStride: Dim>(
        &self,
        sample: &VectorView<T, D, RStride, CStride>,
    ) -> Option<T> {
        if !self.domain.contains(sample) {
            return None;
        }

        let diff = sample - self.mean.clone();
        let value = (diff.transpose() * &self.inverse * diff)[(0, 0)].clone();

        let p_nonzero: usize = self.matrix.diagonal().iter().fold(0, |acc, next| {
            if next.abs() > T::zero() { acc + 1 } else { acc }
        });

        Some(
            (-value / T::from_usize(2).unwrap()).exp()
                / ((T::two_pi()).powi(p_nonzero as i32) * self.determinant()).sqrt(),
        )
    }

    fn domain(&self) -> impl Domain<T, D> + 'static {
        self.domain.clone()
    }

    fn center(&self) -> OVector<T, D> {
        self.mean.clone()
    }

    fn is_constant(&self) -> OVector<bool, D> {
        OVector::from_iterator_generic(
            self.matrix.shape_generic().0,
            U1,
            self.matrix
                .diagonal()
                .iter()
                .map(|value| value.eq(&T::zero())),
        )
    }

    fn sample(&self, rng: &mut impl Rng, max_attempts: usize) -> Option<OVector<T, D>> {
        let normal = StandardNormal;
        let n_dim = self.matrix.shape_generic().0;

        let mut proposal = self.mean.clone()
            + &self.ltm
                * OVector::<T, D>::from_iterator_generic(
                    n_dim,
                    U1,
                    (0..n_dim.value()).map(|_| rng.sample(normal)),
                );

        // Counter for rejected proposals.
        let mut attempts = 0;

        while !self.domain.contains(&proposal.as_view()) {
            proposal = self.mean.clone()
                + &self.ltm
                    * OVector::<T, D>::from_iterator_generic(
                        n_dim,
                        U1,
                        (0..n_dim.value()).map(|_| rng.sample(normal)),
                    );

            attempts += 1;

            if attempts > max_attempts {
                return None;
            }
        }

        Some(proposal)
    }
}

impl<T, D, B> Add<OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    type Output = MultiNormalDensity<T, D, B>;

    fn add(self, rhs: OVector<T, D>) -> Self::Output {
        Self {
            matrix: self.matrix,
            inverse: self.inverse,
            ltm: self.ltm,
            domain: self.domain,
            mean: self.mean + rhs,
        }
    }
}

impl<T, D, B> Add<&OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    type Output = MultiNormalDensity<T, D, B>;

    fn add(self, rhs: &OVector<T, D>) -> Self::Output {
        Self {
            matrix: self.matrix,
            inverse: self.inverse,
            ltm: self.ltm,
            domain: self.domain,
            mean: self.mean + rhs,
        }
    }
}

impl<T, D, B> AddAssign<OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    fn add_assign(&mut self, rhs: OVector<T, D>) {
        self.mean += rhs
    }
}

impl<T, D, B> AddAssign<&OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    fn add_assign(&mut self, rhs: &OVector<T, D>) {
        self.mean += rhs
    }
}

impl<T, D, B> Mul<T> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    type Output = MultiNormalDensity<T, D, B>;

    fn mul(self, rhs: T) -> Self::Output {
        Self {
            matrix: self.matrix * rhs.clone(),
            inverse: self.inverse / rhs.clone(),
            ltm: self.ltm * rhs.sqrt(),
            domain: self.domain,
            mean: self.mean,
        }
    }
}

impl<T, D, B> MulAssign<T> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    fn mul_assign(&mut self, rhs: T) {
        self.matrix *= rhs.clone();
        self.inverse /= rhs.clone();
        self.ltm *= rhs.sqrt();
    }
}

impl<T, D, B> Sub<OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    type Output = MultiNormalDensity<T, D, B>;

    fn sub(self, rhs: OVector<T, D>) -> Self::Output {
        Self {
            matrix: self.matrix,
            inverse: self.inverse,
            ltm: self.ltm,
            domain: self.domain,
            mean: self.mean - rhs,
        }
    }
}

impl<T, D, B> Sub<&OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    type Output = MultiNormalDensity<T, D, B>;

    fn sub(self, rhs: &OVector<T, D>) -> Self::Output {
        Self {
            matrix: self.matrix,
            inverse: self.inverse,
            ltm: self.ltm,
            domain: self.domain,
            mean: self.mean - rhs,
        }
    }
}

impl<T, D, B> SubAssign<OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    fn sub_assign(&mut self, rhs: OVector<T, D>) {
        self.mean -= rhs
    }
}

impl<T, D, B> SubAssign<&OVector<T, D>> for MultiNormalDensity<T, D, B>
where
    T: RealField,
    D: Dim,
    DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
    fn sub_assign(&mut self, rhs: &OVector<T, D>) {
        self.mean -= rhs
    }
}

/// Computes the unbiased covariance over two slices.
///
/// The length of both iterators must be equal (panic).
pub fn covariance<'a, T, I>(x: I, y: I) -> T
where
    T: RealField + Sum,
    I: IntoIterator<Item = &'a T>,
    <I as IntoIterator>::IntoIter: Clone,
{
    let x_iter = x.into_iter();
    let y_iter = y.into_iter();

    let length = x_iter.clone().fold(0, |acc, _| acc + 1);

    let mu_x = x_iter.clone().cloned().sum::<T>() / T::from_usize(length).unwrap();
    let mu_y = x_iter.clone().cloned().sum::<T>() / T::from_usize(length).unwrap();

    zip_eq(x_iter, y_iter)
        .map(|(val_x, val_y)| (mu_x.clone() - val_x.clone()) * (mu_y.clone() - val_y.clone()))
        .sum::<T>()
        / T::from_usize(length - 1).unwrap()
}

/// Computes the unbiased covariance over two slices with weights.
///
/// The length of all three iterators must be equal (panic).
pub fn covariance_with_weights<'a, T, IV, IW>(x: IV, y: IV, w: IW) -> T
where
    T: RealField + Sum,
    IV: IntoIterator<Item = &'a T>,
    IW: IntoIterator<Item = &'a T>,
    <IV as IntoIterator>::IntoIter: Clone,
    <IW as IntoIterator>::IntoIter: Clone,
{
    let x_iter = x.into_iter();
    let y_iter = y.into_iter();
    let w_iter = w.into_iter();

    let wsum = w_iter.clone().cloned().sum::<T>();
    let wsumsq = w_iter.clone().map(|val_w| val_w.clone().powi(2)).sum::<T>();

    let wfac = wsum.clone() - wsumsq / wsum.clone();

    let mu_x = zip_eq(x_iter.clone(), w_iter.clone())
        .map(|(val_x, val_w)| val_x.clone() * val_w.clone())
        .sum::<T>()
        / wsum.clone();

    let mu_y = zip_eq(y_iter.clone(), w_iter.clone())
        .map(|(val_y, val_w)| val_y.clone() * val_w.clone())
        .sum::<T>()
        / wsum.clone();

    zip_eq(x_iter, zip_eq(y_iter, w_iter))
        .map(|(val_x, (val_y, val_w))| {
            (mu_x.clone() - val_x.clone()) * (mu_y.clone() - val_y.clone()) * val_w.clone()
        })
        .sum::<T>()
        / wfac
}

#[cfg(test)]
mod tests {
    use crate::domain::{MDomain, SDomain};

    use super::*;
    use approx::ulps_eq;
    use nalgebra::{Matrix, SVector, U3, VecStorage};
    use rand::{Rng, SeedableRng};
    use rand_xoshiro::Xoshiro256PlusPlus;

    #[test]
    fn test_multinormal_density() {
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(1);
        let uniform = StandardNormal;

        let mut array = Matrix::<f64, U3, Dyn, VecStorage<f64, U3, Dyn>>::from_iterator(
            10000,
            (0..30000).map(|idx| {
                if idx % 3 == 1 {
                    0.0
                } else {
                    rng.sample::<f64, StandardNormal>(uniform)
                }
            }),
        );

        array.row_mut(0).iter_mut().for_each(|value| {
            *value += 0.1;
        });

        array.row_mut(2).iter_mut().for_each(|value| {
            *value += 0.25;
        });

        let mvnpdf = &MultiNormalDensity::from_view::<Dyn, U3>(
            &array.as_view(),
            MDomain::new(SVector::from([
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
            ])),
            None,
        )
        .unwrap();

        assert!(ulps_eq!(
            mvnpdf
                .density::<U1, U3>(&SVector::from([0.2, 0.0, 0.35]).as_view())
                .unwrap(),
            0.16128485,
            epsilon = 1e-5,
            max_ulps = 5
        ));

        assert!(
            mvnpdf
                .density::<U1, U3>(&SVector::from([0.2f64, 1.0, 0.35]).as_view())
                .is_none()
        );

        assert!(ulps_eq!(
            mvnpdf.sample(&mut rng, 100).unwrap(),
            SVector::from([-0.418523, 0.0, 0.4995714]),
            epsilon = 1e-5,
            max_ulps = 5
        ));

        assert!(
            mvnpdf
                .domain()
                .contains::<U1, U3>(&mvnpdf.sample(&mut rng, 100).unwrap().as_view())
        );

        let mvpdf_ensbl = MultiNormalDensity::from_view::<Dyn, U3>(
            &array.as_view(),
            MDomain::new(SVector::from([
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
            ])),
            None,
        )
        .unwrap();

        assert!(ulps_eq!(
            mvnpdf.ltm,
            mvpdf_ensbl.ltm,
            epsilon = 1e-5,
            max_ulps = 5
        ));
    }

    #[test]
    fn test_multinormal_density_kld() {
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(1);
        let uniform = StandardNormal;

        let array_1 = Matrix::<f64, U3, Dyn, VecStorage<f64, U3, Dyn>>::from_iterator(
            10000,
            (0..30000).map(|idx| {
                if idx % 3 == 1 {
                    0.0
                } else {
                    rng.sample::<f64, StandardNormal>(uniform)
                }
            }),
        );

        let array_2 = Matrix::<f64, U3, Dyn, VecStorage<f64, U3, Dyn>>::from_iterator(
            10000,
            (0..30000).map(|idx| {
                if idx % 3 == 1 {
                    0.0
                } else {
                    0.25 + rng.sample::<f64, StandardNormal>(uniform)
                }
            }),
        );

        let mvpdf_1 = MultiNormalDensity::from_view::<Dyn, U3>(
            &array_1.as_view(),
            MDomain::new(SVector::from([
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
            ])),
            None,
        )
        .unwrap();
        let mvpdf_2 = MultiNormalDensity::from_view::<Dyn, U3>(
            &array_2.as_view(),
            MDomain::new(SVector::from([
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
                SDomain::new(Some(-0.75), Some(0.75)).unwrap(),
            ])),
            None,
        )
        .unwrap();

        assert!(ulps_eq!(
            mvpdf_1.kl_div(&mvpdf_2).unwrap(),
            0.181914,
            epsilon = 1e-5,
            max_ulps = 5
        ));

        assert!(ulps_eq!(
            mvpdf_2.kl_div(&mvpdf_1).unwrap(),
            0.166584,
            epsilon = 1e-5,
            max_ulps = 5
        ));
    }
}