forked from carp-lang/Carp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.carp
100 lines (96 loc) · 3.08 KB
/
statistics.carp
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
(load "Test.carp")
(load "core/Statistics.carp") ;; specified path to avoid loading itself on case insensitive file systems
(use-all Double Test Statistics)
(defn all-eq [a b]
(if (Int./= (Array.length a) (Array.length b))
false
(let [res true]
(do
(for [i 0 (Array.length a)]
(if (not (Double.= @(Array.unsafe-nth a i) @(Array.unsafe-nth b i)))
(set! res false)
()))
res))))
(defn all-approx [a b]
(if (Int./= (Array.length a) (Array.length b))
false
(let [res true]
(do
(for [i 0 (Array.length a)]
(if (not (Double.approx @(Array.unsafe-nth a i) @(Array.unsafe-nth b i)))
(set! res false)
()))
res))))
(deftest test
(assert-equal test
2.0
(median &[1.0 2.0 3.0])
"median works as expected")
(assert-equal test
2.5
(mean &[1.0 2.0 4.5])
"mean works as expected")
(assert-equal test
2.0
(low-median &[1.0 2.0 4.0 5.0])
"low-median works as expected")
(assert-equal test
4.0
(high-median &[1.0 2.0 4.0 5.0])
"high-median works as expected")
(assert-equal test
3.0
(grouped-median &[1.0 2.0 4.0 5.0] 2)
"grouped-median works as expected I")
(assert-equal test
2.5
(grouped-median &[1.0 2.0 4.0 5.0] 3)
"grouped-median works as expected II")
(assert-equal test
1.0
(variance &[1.0 2.0 4.0 5.0])
"variance works as expected")
(assert-equal test
0.75
(pvariance &[1.0 2.0 4.0 5.0])
"pvariance works as expected")
(assert-equal test
2.0
(stdev &[1.0 1.0 9.0 9.0])
"stdev works as expected")
(assert-equal test
2.0
(pstdev &[1.0 9.0])
"pstdev works as expected")
(assert-op test
&[0.0 0.0 0.0 0.0 8.0]
&(winsorize &[0.0 0.0 0.0 0.0 10.0] 5.0)
"winsorizing works as expected"
all-approx)
(assert-op test
&[2.5 5.0 7.5]
&(quartiles &[0.0 2.5 5.0 7.5 10.0])
"quartiles work as expected"
all-eq)
(assert-equal test
5.0
(iqr &[0.0 2.5 5.0 7.5 10.0])
"iqr works as expected")
(assert-equal test
40.0
(stdev-pct &[1.0 1.0 9.0 9.0])
"stdev-pct works as expected")
(assert-op test
3.7065
(median-abs-dev &[5.0 10.0])
"median-abs-dev works as expected"
Double.approx)
(assert-op test
49.42
(median-abs-dev-pct &[5.0 10.0])
"median-abs-dev-pct works as expected"
Double.approx)
(assert-equal test
2.0
@(Summary.median &(summary &[1.0 2.0 3.0]))
"summary works as expected"))