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 fry arrays grouping sets ;
8 : power-mean ( seq p -- x )
9 [ '[ _ ^ ] map-sum ] [ [ length / ] [ recip ^ ] bi* ] 2bi ; inline
12 [ sum ] [ length ] bi / ; inline
14 : sum-of-squares ( seq -- x )
15 [ sq ] map-sum ; inline
17 : sum-of-squared-errors ( seq -- x )
18 [ mean ] keep [ - sq ] with map-sum ; inline
20 : sum-of-absolute-errors ( seq -- x )
21 [ mean ] keep [ - ] with map-sum ; inline
23 : quadratic-mean ( seq -- x ) ! root-mean-square
24 [ sum-of-squares ] [ length ] bi / sqrt ; inline
26 : geometric-mean ( seq -- x )
27 [ length ] [ product ] bi nth-root ; inline
29 : harmonic-mean ( seq -- x )
30 [ recip ] map-sum recip ; inline
32 : contraharmonic-mean ( seq -- x )
33 [ sum-of-squares ] [ sum ] bi / ; inline
35 : trim-mean ( seq p -- x )
36 swap [ length [ * >integer ] keep over - ] keep <slice> mean ;
40 :: ((kth-object)) ( seq k nth-quot exchange-quot quot: ( x y -- ? ) -- elt )
41 #! Wirth's method, Algorithm's + Data structues = Programs p. 84
42 k seq bounds-check 2drop
55 [ i seq nth-quot call x quot call ] [ i 1 + i! ] while
56 [ x j seq nth-quot call quot call ] [ j 1 - j! ] while
58 i j seq exchange-quot call
69 : (kth-object) ( seq k nth-quot exchange-quot quot: ( x y -- ? ) -- elt )
70 #! The algorithm modifiers seq, so we clone it
71 [ clone ] 4dip ((kth-object)) ; inline
73 : kth-object-unsafe ( seq k quot: ( x y -- ? ) -- elt )
74 [ [ nth-unsafe ] [ exchange-unsafe ] ] dip (kth-object) ; inline
76 : kth-objects-unsafe ( seq kths quot: ( x y -- ? ) -- elts )
78 '[ [ nth-unsafe ] [ exchange-unsafe ] _ ((kth-object)) ] with map ; inline
82 : kth-object ( seq k quot: ( x y -- ? ) -- elt )
83 [ [ nth ] [ exchange ] ] dip (kth-object) ; inline
85 : kth-objects ( seq kths quot: ( x y -- ? ) -- elts )
87 '[ [ nth ] [ exchange ] _ ((kth-object)) ] with map ; inline
89 : kth-smallests ( seq kths -- elts ) [ < ] kth-objects-unsafe ;
91 : kth-smallest ( seq k -- elt ) [ < ] kth-object-unsafe ;
93 : kth-largests ( seq kths -- elts ) [ > ] kth-objects-unsafe ;
95 : kth-largest ( seq k -- elt ) [ > ] kth-object-unsafe ;
97 : count-relative ( seq k -- lt eq gt )
100 { +lt+ [ [ 1 + ] 2dip ] }
102 { +eq+ [ [ 1 + ] dip ] }
106 : minmax-relative ( seq k -- lt eq gt lt-max gt-min )
107 [ 0 0 0 -1/0. 1/0. ] 2dip '[
109 { +lt+ [ [ 1 + ] 5 ndip '[ _ max ] dip ] }
110 { +gt+ [ [ 1 + ] 3dip min ] }
111 { +eq+ [ [ 1 + ] 4dip drop ] }
115 : lower-median-index ( seq -- n )
117 [ length odd? [ 1 - ] unless ] bi ;
119 : lower-median ( seq -- elt )
120 [ ] [ lower-median-index ] bi kth-smallest ;
122 : upper-median ( seq -- elt )
123 dup midpoint@ kth-smallest ;
125 : medians ( seq -- lower upper )
127 [ [ lower-median-index ] [ midpoint@ ] bi 2array ]
128 bi kth-smallests first2 ;
130 : median ( seq -- x )
132 [ lower-median ] [ medians + 2 / ] if ;
134 ! quantile can be any n-tile. quartile is n = 4, percentile is n = 100
135 ! a,b,c,d parameters, N - number of samples, q is quantile (1/2 for median, 1/4 for 1st quartile)
136 ! http://mathworld.wolfram.com/Quantile.html
138 ! could subtract 1 from a
140 : quantile-x ( a b N q -- x )
141 [ + ] dip * + 1 - ; inline
145 >fraction [ /mod nip ] keep / ; inline
147 :: quantile-indices ( seq qs a b c d -- seq )
148 qs [ [ a b seq length ] dip quantile-x ] map ;
150 :: qabcd ( y-floor y-ceiling x c d -- qabcd )
151 y-floor y-ceiling y-floor - c d x frac * + * + ;
153 :: quantile-abcd ( seq qs a b c d -- quantile )
154 seq qs a b c d quantile-indices :> indices
155 indices [ [ floor ] [ ceiling ] bi 2array ] map
156 concat :> index-pairs
158 seq index-pairs kth-smallests
159 2 group indices [ [ first2 ] dip c d qabcd ] 2map ;
161 : quantile1 ( seq qs -- seq' )
162 0 0 1 0 quantile-abcd ;
164 : quantile3 ( seq qs -- seq' )
165 1/2 0 0 0 quantile-abcd ;
167 : quantile4 ( seq qs -- seq' )
168 0 0 0 1 quantile-abcd ;
170 : quantile5 ( seq qs -- seq' )
171 1/2 0 0 1 quantile-abcd ;
173 : quantile6 ( seq qs -- seq' )
174 0 1 0 1 quantile-abcd ;
176 : quantile7 ( seq qs -- seq' )
177 1 -1 0 1 quantile-abcd ;
179 : quantile8 ( seq qs -- seq' )
180 1/3 1/3 0 1 quantile-abcd ;
182 : quantile9 ( seq qs -- seq' )
183 3/8 1/4 0 1 quantile-abcd ;
185 : quartile ( seq -- seq' )
186 { 1/4 1/2 3/4 } quantile5 ;
190 : (sequence>assoc) ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) assoc -- assoc )
191 [ swap curry compose each ] keep ; inline
195 : sequence>assoc! ( assoc seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) -- assoc )
196 4 nrot (sequence>assoc) ; inline
198 : sequence>assoc ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) exemplar -- assoc )
199 clone (sequence>assoc) ; inline
201 : sequence>hashtable ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) -- hashtable )
202 H{ } sequence>assoc ; inline
204 : histogram! ( hashtable seq -- hashtable )
205 [ ] [ inc-at ] sequence>assoc! ;
207 : histogram-by ( seq quot: ( x -- bin ) -- hashtable )
208 [ inc-at ] sequence>hashtable ; inline
210 : histogram ( seq -- hashtable )
213 : sorted-histogram ( seq -- alist )
214 histogram sort-values ;
216 : collect-pairs ( seq quot: ( x -- v k ) -- hashtable )
217 [ push-at ] sequence>hashtable ; inline
219 : collect-by ( seq quot: ( x -- x' ) -- hashtable )
220 [ dup ] prepose collect-pairs ; inline
224 [ ] [ [ [ second ] bi@ > ] most ] map-reduce first ;
226 ERROR: empty-sequence ;
228 : minmax ( seq -- min max )
232 [ first dup ] keep [ [ min ] [ max ] bi-curry bi* ] each
238 : sample-var ( seq -- x )
239 #! normalize by N-1; unbiased
243 [ sum-of-squared-errors ] [ length 1 - ] bi /
246 : full-var ( seq -- x )
250 [ sum-of-squared-errors ] [ length ] bi /
253 ALIAS: var sample-var
255 : sample-std ( seq -- x ) sample-var sqrt ;
257 : full-std ( seq -- x ) full-var sqrt ;
259 ALIAS: std sample-std
261 : signal-to-noise ( seq -- x ) [ mean ] [ std ] bi / ;
263 : mean-dev ( seq -- x ) dup mean v-n vabs mean ;
265 : median-dev ( seq -- x ) dup median v-n vabs mean ;
267 : sample-ste ( seq -- x ) [ sample-std ] [ length ] bi sqrt / ;
269 : full-ste ( seq -- x ) [ full-std ] [ length ] bi sqrt / ;
271 ALIAS: ste sample-ste
273 : ((r)) ( mean(x) mean(y) {x} {y} -- (r) )
274 ! finds sigma((xi-mean(x))(yi-mean(y))
275 0 [ [ [ pick ] dip swap - ] bi@ * + ] 2reduce 2nip ;
277 : (r) ( mean(x) mean(y) {x} {y} sx sy -- r )
278 * recip [ [ ((r)) ] keep length 1 - / ] dip * ;
280 : [r] ( {{x,y}...} -- mean(x) mean(y) {x} {y} sx sy )
281 first2 [ [ [ mean ] bi@ ] 2keep ] 2keep [ std ] bi@ ;
283 : r ( {{x,y}...} -- r )
286 : r^2 ( {{x,y}...} -- r )
289 : least-squares ( {{x,y}...} -- alpha beta )
290 [r] { [ 2dup ] [ ] [ ] [ ] [ ] } spread
291 ! stack is mean(x) mean(y) mean(x) mean(y) {x} {y} sx sy
292 [ (r) ] 2keep ! stack is mean(x) mean(y) r sx sy
293 swap / * ! stack is mean(x) mean(y) beta
296 : cov ( {x} {y} -- cov )
297 [ dup mean v-n ] bi@ v* mean ;
299 : sample-corr ( {x} {y} -- corr )
300 [ cov ] [ [ sample-var ] bi@ * sqrt ] 2bi / ;
302 : full-corr ( {x} {y} -- corr )
303 [ cov ] [ [ full-var ] bi@ * sqrt ] 2bi / ;
305 ALIAS: corr sample-corr
307 : cum-sum ( seq -- seq' )
308 0 swap [ + dup ] map nip ;
310 : cum-product ( seq -- seq' )
311 1 swap [ * dup ] map nip ;
313 : cum-min ( seq -- seq' )
314 [ ?first ] keep [ min dup ] map nip ;
316 : cum-max ( seq -- seq' )
317 [ ?first ] keep [ max dup ] map nip ;
319 : entropy ( seq -- n )
320 dup members [ [ = ] curry count ] with map
321 dup sum v/n dup [ log ] map v* sum neg ;
323 : binary-entropy ( p -- h )
324 [ dup log * ] [ 1 swap - dup log * ] bi + neg 2 log / ;
326 : standardize ( u -- v )
327 [ dup mean v-n ] [ std ] bi v/n ;
329 : differences ( u -- v )
330 [ 1 tail-slice ] keep [ - ] 2map ;
333 dup minmax over - [ v-n ] [ v/n ] bi* ;