]> gitweb.factorcode.org Git - factor.git/blob - basis/math/statistics/statistics.factor
math.statistics: change 'var' to 'sample-var' and implement variance, covariance...
[factor.git] / basis / math / statistics / statistics.factor
1 ! Copyright (C) 2008 Doug Coleman, Michael Judge.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs combinators generalizations kernel locals math
4 math.functions math.order math.vectors sequences
5 sequences.private sorting ;
6 IN: math.statistics
7
8 : mean ( seq -- x )
9     [ sum ] [ length ] bi / ;
10
11 : geometric-mean ( seq -- x )
12     [ length ] [ product ] bi nth-root ;
13
14 : harmonic-mean ( seq -- x )
15     [ recip ] map-sum recip ;
16
17 :: kth-smallest ( seq k -- elt )
18     #! Wirth's method, Algorithm's + Data structues = Programs p. 84
19     #! The algorithm modifiers seq, so we clone it
20     seq clone :> seq
21     0 :> i!
22     0 :> j!
23     0 :> l!
24     0 :> x!
25     seq length 1 - :> m!
26     [ l m < ]
27     [
28         k seq nth x!
29         l i!
30         m j!
31         [ i j <= ]
32         [
33             [ i seq nth-unsafe x < ] [ i 1 + i! ] while
34             [ x j seq nth-unsafe < ] [ j 1 - j! ] while
35             i j <= [
36                 i j seq exchange-unsafe
37                 i 1 + i!
38                 j 1 - j!
39             ] when
40         ] do while
41
42         j k < [ i l! ] when
43         k i < [ j m! ] when
44     ] while
45     k seq nth ; inline
46
47 : lower-median ( seq -- elt )
48     [ ] [ ] [ length odd? ] tri
49     [ midpoint@ ] [ midpoint@ 1 - ] if kth-smallest ;
50
51 : upper-median ( seq -- elt )
52     dup midpoint@ kth-smallest ;
53
54 : medians ( seq -- lower upper )
55     [ lower-median ] [ upper-median ] bi ;
56
57 : median ( seq -- x )
58     [ ] [ length odd? ] bi [ lower-median ] [ medians + 2 / ] if ;
59
60 <PRIVATE
61
62 : (sequence>assoc) ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) assoc -- assoc )
63     [ swap curry compose each ] keep ; inline
64
65 PRIVATE>
66
67 : sequence>assoc! ( assoc seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) -- assoc )
68     4 nrot (sequence>assoc) ; inline
69
70 : sequence>assoc ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) exemplar -- assoc )
71     clone (sequence>assoc) ; inline
72
73 : sequence>hashtable ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) -- hashtable )
74     H{ } sequence>assoc ; inline
75
76 : histogram! ( hashtable seq -- hashtable )
77     [ ] [ inc-at ] sequence>assoc! ;
78
79 : histogram-by ( seq quot: ( x -- bin ) -- hashtable )
80     [ inc-at ] sequence>hashtable ; inline
81
82 : histogram ( seq -- hashtable )
83     [ ] histogram-by ;
84
85 : sorted-histogram ( seq -- alist )
86     histogram sort-values ;
87
88 : collect-pairs ( seq quot: ( x -- v k ) -- hashtable )
89     [ push-at ] sequence>hashtable ; inline
90
91 : collect-by ( seq quot: ( x -- x' ) -- hashtable )
92     [ dup ] prepose collect-pairs ; inline
93
94 : mode ( seq -- x )
95     histogram >alist
96     [ ] [ [ [ second ] bi@ > ] most ] map-reduce first ;
97
98 ERROR: empty-sequence ;
99
100 : minmax ( seq -- min max )
101     [
102         empty-sequence
103     ] [
104         [ first dup ] keep [ [ min ] [ max ] bi-curry bi* ] each
105     ] if-empty ;
106
107 : range ( seq -- x )
108     minmax swap - ;
109
110 : sample-var ( seq -- x )
111     #! normalize by N-1
112     dup length 1 <= [
113         drop 0
114     ] [
115         [ [ mean ] keep [ - sq ] with map-sum ]
116         [ length 1 - ] bi /
117     ] if ;
118
119 : var ( seq -- x )
120     dup length 1 <= [
121         drop 0
122     ] [
123         [ [ mean ] keep [ - sq ] with map-sum ]
124         [ length ] bi /
125     ] if ;
126
127 : std ( seq -- x ) var sqrt ;
128
129 : ste ( seq -- x ) [ std ] [ length ] bi sqrt / ;
130
131 : ((r)) ( mean(x) mean(y) {x} {y} -- (r) )
132     ! finds sigma((xi-mean(x))(yi-mean(y))
133     0 [ [ [ pick ] dip swap - ] bi@ * + ] 2reduce 2nip ;
134
135 : (r) ( mean(x) mean(y) {x} {y} sx sy -- r )
136     * recip [ [ ((r)) ] keep length 1 - / ] dip * ;
137
138 : [r] ( {{x,y}...} -- mean(x) mean(y) {x} {y} sx sy )
139     first2 [ [ [ mean ] bi@ ] 2keep ] 2keep [ std ] bi@ ;
140
141 : r ( {{x,y}...} -- r )
142     [r] (r) ;
143
144 : r^2 ( {{x,y}...} -- r )
145     r sq ;
146
147 : least-squares ( {{x,y}...} -- alpha beta )
148     [r] { [ 2dup ] [ ] [ ] [ ] [ ] } spread
149     ! stack is mean(x) mean(y) mean(x) mean(y) {x} {y} sx sy
150     [ (r) ] 2keep ! stack is mean(x) mean(y) r sx sy
151     swap / * ! stack is mean(x) mean(y) beta
152     [ swapd * - ] keep ;
153
154 : cov ( {x} {y} -- cov )
155     [ dup mean v-n ] bi@ v* mean ;
156
157 : corr ( {x} {y} -- corr )
158      [ cov ] [ [ var ] bi@ * sqrt ] 2bi / ;