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
#[macro_export]
macro_rules! joint_entropy {
($prob:expr) => {
$prob.iter()
.fold(0.0, |acc, p| {
if *p == 0.0 {
acc
} else {
acc - (p * (*p as f64).ln())
}
})
};
}
#[cfg(test)]
mod testing {
use approx::assert_relative_eq;
use ndarray::{array, Array2, Array1, Array3, Array4};
use ndarray_rand::{RandomExt, rand_distr::Uniform};
use crate::{entropy::entropy, prob::{prob1d, prob2d}, joint_entropy};
const N_ITER: usize = 1000;
const ARRAY_SIZE: usize = 100;
#[test]
fn test_joint_macro() {
for _ in 0..N_ITER {
let c_x = Array1::random(ARRAY_SIZE, Uniform::new(0.1, 0.8));
let p_x = &c_x / c_x.sum();
let h = joint_entropy!(&p_x);
assert!(h >= 0.0);
assert_relative_eq!(h, entropy(&p_x));
let c_xy = Array2::random((2, ARRAY_SIZE), Uniform::new(0.1, 0.8));
let p_xy = &c_xy / c_xy.sum();
let h = joint_entropy!(&p_xy);
assert!(h >= 0.0);
let c_xy = Array3::random((2, 2, ARRAY_SIZE), Uniform::new(0.1, 0.8));
let p_xy = &c_xy / c_xy.sum();
let h = joint_entropy!(&p_xy);
assert!(h >= 0.0);
let c_xy = Array4::random((2, 2, 2, ARRAY_SIZE), Uniform::new(0.1, 0.8));
let p_xy = &c_xy / c_xy.sum();
let h = joint_entropy!(&p_xy);
assert!(h >= 0.0);
}
}
#[test]
fn test_joint() {
let px = array![[0.5, 0.0], [0.25, 0.25]];
let hx = joint_entropy!(&px);
assert_eq!(hx, 1.0397207708399179);
}
#[test]
fn test_nonnegative() {
for _ in 0..N_ITER {
let c_xy = Array2::random((2, ARRAY_SIZE), Uniform::new(0.1, 0.8));
let p_xy = &c_xy / c_xy.sum();
let h = joint_entropy!(&p_xy);
assert!(h >= 0.0);
}
}
#[test]
fn test_nonnegative_3d() {
for _ in 0..N_ITER {
let c_xyz = Array3::random((2, 2, ARRAY_SIZE), Uniform::new(0.1, 0.8));
let p_xyz = &c_xyz / c_xyz.sum();
let h = joint_entropy!(&p_xyz);
assert!(h >= 0.0);
}
}
#[test]
fn test_gte_individual_entropies() {
for _ in 0..N_ITER {
let c_x = Array1::random(ARRAY_SIZE, Uniform::new(0, 3));
let c_y = Array1::random(ARRAY_SIZE, Uniform::new(0, 3));
let p_xy = prob2d(&c_x, &c_y, 4, 4).unwrap();
let p_x = prob1d(&c_x, 4).unwrap();
let p_y = prob1d(&c_y, 4).unwrap();
let h_xy = joint_entropy!(&p_xy);
let h_x = entropy(&p_x);
let h_y = entropy(&p_y);
assert!(h_xy >= h_x);
assert!(h_xy >= h_y);
}
}
#[test]
fn test_lte_individual_entropies() {
for _ in 0..N_ITER {
let c_x = Array1::random(ARRAY_SIZE, Uniform::new(0, 3));
let c_y = Array1::random(ARRAY_SIZE, Uniform::new(0, 3));
let p_xy = prob2d(&c_x, &c_y, 4, 4).unwrap();
let p_x = prob1d(&c_x, 4).unwrap();
let p_y = prob1d(&c_y, 4).unwrap();
let h_xy = joint_entropy!(&p_xy);
let h_x = entropy(&p_x);
let h_y = entropy(&p_y);
assert!(h_xy <= h_x + h_y);
}
}
}