| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package stats |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmp" |
| 9 |
"math" |
| 10 |
"slices" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestMeanAndStdDev(t *testing.T) { |
| 15 |
tests := []struct { |
| 16 |
name string |
| 17 |
data []float64 |
| 18 |
mean, stddev float64 |
| 19 |
}{ |
| 20 |
{ |
| 21 |
"low count large positive reals", |
| 22 |
[]float64{1.0e10, 2.0e10, 3.0e10, 4.0e10, 5.0e10}, |
| 23 |
3e10, 15811388300.8418960571289062, |
| 24 |
}, |
| 25 |
{ |
| 26 |
"low count large negative reals", |
| 27 |
[]float64{-1.0e10, -2.0e10, -3.0e10, -4.0e10, -5.0e10}, |
| 28 |
-3e10, 15811388300.8418960571289062, |
| 29 |
}, |
| 30 |
{ |
| 31 |
"high count large positive reals", |
| 32 |
[]float64{1e10, 2e10, 3e10, 4e10, 5e10, 6e10, 7e10, 8e10, 9e10, 10e10, 11e10, 12e10, 13e10}, |
| 33 |
7e10, 38944404818.4930725097656250, |
| 34 |
}, |
| 35 |
{ |
| 36 |
"high count large negative reals", |
| 37 |
[]float64{-1e10, -2e10, -3e10, -4e10, -5e10, -6e10, -7e10, -8e10, -9e10, -10e10, -11e10, -12e10, -13e10}, |
| 38 |
-7e10, 38944404818.4930725097656250, |
| 39 |
}, |
| 40 |
{ |
| 41 |
"low count small positive reals", |
| 42 |
[]float64{0.1, 0.2, 0.3, 0.4, 0.5}, |
| 43 |
0.3, 0.1581138830084190, |
| 44 |
}, |
| 45 |
{ |
| 46 |
"low count small negative reals", |
| 47 |
[]float64{-0.1, -0.2, -0.3, -0.4, -0.5}, |
| 48 |
-0.3, 0.1581138830084190, |
| 49 |
}, |
| 50 |
{ |
| 51 |
"high count small positive reals", |
| 52 |
[]float64{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12, 0.13}, |
| 53 |
0.38153846153846155, 0.2902540885111455, |
| 54 |
}, |
| 55 |
{ |
| 56 |
"high count small negative reals", |
| 57 |
[]float64{-0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -0.10, -0.11, -0.12, -0.13}, |
| 58 |
-0.38153846153846155, 0.2902540885111455, |
| 59 |
}, |
| 60 |
{ |
| 61 |
"single value", |
| 62 |
[]float64{20.25}, |
| 63 |
20.25, 0, |
| 64 |
}, |
| 65 |
{ |
| 66 |
"contains nan", |
| 67 |
[]float64{1.0, math.NaN()}, |
| 68 |
math.NaN(), math.NaN(), |
| 69 |
}, |
| 70 |
{ |
| 71 |
"contains +inf and -inf", |
| 72 |
[]float64{math.Inf(1), 0.42, 314, math.Inf(-1)}, |
| 73 |
math.NaN(), math.Inf(1), |
| 74 |
}, |
| 75 |
{ |
| 76 |
"contains -inf", |
| 77 |
[]float64{1.0, math.Inf(-1)}, |
| 78 |
math.Inf(-1), math.Inf(1), |
| 79 |
}, |
| 80 |
{ |
| 81 |
"contains +inf", |
| 82 |
[]float64{1.0, math.Inf(1)}, |
| 83 |
math.Inf(1), math.Inf(1), |
| 84 |
}, |
| 85 |
} |
| 86 |
|
| 87 |
for _, tc := range tests { |
| 88 |
t.Run(tc.name, func(t *testing.T) { |
| 89 |
preimage := slices.Clone(tc.data) |
| 90 |
mean, stddev := MeanAndStdDev(tc.data) |
| 91 |
if !slices.EqualFunc(preimage, tc.data, equateNaN) { |
| 92 |
t.Errorf("input slice cannot be modified\n\tgot:\t%v\n\twant:\t%v", tc.data, preimage) |
| 93 |
} |
| 94 |
if res := compareLastULP(mean, tc.mean); res != 0 { |
| 95 |
t.Errorf("miscalculated mean: got %.16f, want %.16f", mean, tc.mean) |
| 96 |
} |
| 97 |
if res := compareLastULP(stddev, tc.stddev); res != 0 { |
| 98 |
t.Errorf("miscalculated stddev: got %.16f, want %.16f", stddev, tc.stddev) |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestMedian(t *testing.T) { |
| 105 |
tests := []struct { |
| 106 |
name string |
| 107 |
data []float64 |
| 108 |
want float64 |
| 109 |
}{ |
| 110 |
{"low count odd length positive reals", []float64{1, 5, 2, 8, 7}, 5}, |
| 111 |
{"low count even length positive reals", []float64{1, 5, 2, 8, 7, 9}, 6}, |
| 112 |
{"low count odd length negative reals", []float64{-1, -5, -2, -8, -7}, -5}, |
| 113 |
{"low count even length negative reals", []float64{-1, -5, -2, -8, -7, -9}, -6}, |
| 114 |
{"high count odd length positive reals", []float64{1, 5, 2, 8, 7, 9, 3, 4, 6, 11, 10, 12, 13}, 7}, |
| 115 |
{"high count even length positive reals", []float64{1, 5, 2, 8, 7, 9, 3, 4, 6, 11, 10, 12}, 6.5}, |
| 116 |
{"high count odd length negative reals", []float64{-1, -5, -2, -8, -7, -9, -3, -4, -6, -11, -10, -12, -13}, -7}, |
| 117 |
{"high count even length negative reals", []float64{-1, -5, -2, -8, -7, -9, -3, -4, -6, -11, -10, -12}, -6.5}, |
| 118 |
{"contains nan", []float64{1, math.NaN()}, math.NaN()}, |
| 119 |
{"contains +inf", []float64{1, 2, math.Inf(1)}, 2}, |
| 120 |
{"contains -inf", []float64{1, 2, math.Inf(-1)}, 1}, |
| 121 |
} |
| 122 |
|
| 123 |
for _, tc := range tests { |
| 124 |
t.Run(tc.name, func(t *testing.T) { |
| 125 |
preimage := slices.Clone(tc.data) |
| 126 |
got := Median(tc.data) |
| 127 |
if !slices.EqualFunc(preimage, tc.data, equateNaN) { |
| 128 |
t.Errorf("input slice cannot be modified\n\tgot:\t%v\n\twant:\t%v", tc.data, preimage) |
| 129 |
} |
| 130 |
if res := compareLastULP(got, tc.want); res != 0 { |
| 131 |
t.Errorf("miscalculated median: got %.16f, want %.16f", got, tc.want) |
| 132 |
} |
| 133 |
}) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestQuantiles(t *testing.T) { |
| 138 |
tests := []struct { |
| 139 |
name string |
| 140 |
data []float64 |
| 141 |
quantiles []float64 |
| 142 |
want []float64 |
| 143 |
}{ |
| 144 |
{ |
| 145 |
"quartiles positive reals", |
| 146 |
[]float64{1, 2, 3, 4, 5, 6, 7, 8}, |
| 147 |
[]float64{0.25, 0.5, 0.75}, []float64{2.75, 4.5, 6.25}, |
| 148 |
}, |
| 149 |
{ |
| 150 |
"deciles positive reals", |
| 151 |
[]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, |
| 152 |
[]float64{0.1, 0.5, 0.9}, []float64{1.9000000000000001, 5.5, 9.1}, |
| 153 |
}, |
| 154 |
{ |
| 155 |
"quartiles negative reals", |
| 156 |
[]float64{-1, -2, -3, -4, -5, -6, -7, -8}, |
| 157 |
[]float64{0.25, 0.5, 0.75}, []float64{-6.25, -4.5, -2.75}, |
| 158 |
}, |
| 159 |
{ |
| 160 |
"deciles negative reals", |
| 161 |
[]float64{-1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, |
| 162 |
[]float64{0.1, 0.5, 0.9}, []float64{-9.1, -5.5, -1.9000000000000004}, |
| 163 |
}, |
| 164 |
{ |
| 165 |
"small positive floats", |
| 166 |
[]float64{0.000001, 0.000002, 0.000003, 0.000004, 0.000005, 0.000006, 0.000007, 0.000008, 0.000009}, |
| 167 |
[]float64{0.25, 0.5, 0.75}, []float64{0.000003, 0.000005, 0.000007}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
"small negative floats", |
| 171 |
[]float64{-0.000001, -0.000002, -0.000003, -0.000004, -0.000005, -0.000006, -0.000007, -0.000008, -0.000009}, |
| 172 |
[]float64{0.25, 0.5, 0.75}, []float64{-0.000007, -0.000005, -0.000003}, |
| 173 |
}, |
| 174 |
{ |
| 175 |
"large positive floats", |
| 176 |
[]float64{1e10, 2e10, 3e10, 4e10, 5e10, 6e10, 7e10, 8e10, 9e10}, |
| 177 |
[]float64{0.25, 0.5, 0.75}, []float64{3e10, 5e10, 7e10}, |
| 178 |
}, |
| 179 |
{ |
| 180 |
"large negative floats", |
| 181 |
[]float64{-1e10, -2e10, -3e10, -4e10, -5e10, -6e10, -7e10, -8e10, -9e10}, |
| 182 |
[]float64{0.25, 0.5, 0.75}, []float64{-7e10, -5e10, -3e10}, |
| 183 |
}, |
| 184 |
{ |
| 185 |
"contains +inf and -inf", |
| 186 |
[]float64{1.0, math.Inf(1), 0.0, -0.0000001, 2, math.Inf(-1)}, |
| 187 |
[]float64{0.25, 0.5, 0.75}, []float64{-0.000000075, 0.5, 1.75}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
"contains nan", |
| 191 |
[]float64{-1, 0.000001, math.Inf(-1), 0.0, 1, math.NaN()}, |
| 192 |
[]float64{0.5}, []float64{math.NaN()}, |
| 193 |
}, |
| 194 |
} |
| 195 |
|
| 196 |
for _, tc := range tests { |
| 197 |
t.Run(tc.name, func(t *testing.T) { |
| 198 |
preimage := slices.Clone(tc.data) |
| 199 |
res := Quantiles(tc.data, tc.quantiles...) |
| 200 |
if !slices.EqualFunc(preimage, tc.data, equateNaN) { |
| 201 |
t.Errorf("input slice cannot be modified\n\tgot:\t%v\n\twant:\t%v", tc.data, preimage) |
| 202 |
} |
| 203 |
for i := range res { |
| 204 |
if compareLastULP(res[i], tc.want[i]) != 0 { |
| 205 |
t.Errorf("miscalculated quantile: got %.16f, want %.16f", res[i], tc.want[i]) |
| 206 |
} |
| 207 |
} |
| 208 |
}) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
t |
| 214 |
|
| 215 |
func compareLastULP(x, y float64) int { |
| 216 |
switch cmp.Compare(x, y) { |
| 217 |
case -1, 1: |
| 218 |
return cmp.Compare(math.Nextafter(x, y), y) |
| 219 |
} |
| 220 |
return 0 |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
func equateNaN(x, y float64) bool { |
| 227 |
return cmp.Compare(x, y) == 0 |
| 228 |
} |