-
Notifications
You must be signed in to change notification settings - Fork 7
/
utest.go
71 lines (55 loc) · 1.43 KB
/
utest.go
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
package onlinestats
// https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U
import (
"math"
"sort"
)
// MannWhitney performs a Matt-Whitney U test for the two samples xs and ys.
// It returns the two-tailed p-value for the null hypothesis that the medians
// of the two samples are the same. This uses the normal approximation which
// is more accurate if the number of samples is >30.
func MannWhitney(xs, ys []float64) float64 {
// floats in a map.. this feels dubious?
data := make(map[float64][]int)
for _, x := range xs {
data[x] = append(data[x], 0)
}
for _, y := range ys {
data[y] = append(data[y], 1)
}
floats := make([]float64, 0, len(data))
for k := range data {
floats = append(floats, k)
}
sort.Float64s(floats)
var r [2]float64
var idx = 1
for _, f := range floats {
dataf := data[f]
l := len(dataf)
var rank float64
if l == 1 {
rank = float64(idx)
} else {
rank = float64(idx) + float64(l-1)/2.0
}
for _, xy := range dataf {
r[xy] += rank
}
idx += l
}
n1n2 := len(xs) * len(ys)
idx = 0
u := float64(n1n2+(len(xs)*(len(xs)+1))/2.0) - r[0]
if u1 := float64(n1n2+(len(ys)*(len(ys)+1))/2.0) - r[1]; u > u1 {
idx = 1
u = u1
}
mu := float64(n1n2) / 2.0
sigu := math.Sqrt(float64(n1n2*(len(xs)+len(ys)+1)) / 12.0)
zu := math.Abs(u-mu) / sigu
return 2 - 2*cdf(0, 1, zu)
}
func cdf(mean, stddev, x float64) float64 {
return 0.5 + 0.5*math.Erf((x-mean)/(stddev*math.Sqrt2))
}