1 ! Copyright (C) 2008 Doug Coleman, Michael Judge, Loryn Jenkins.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators fry generalizations grouping
4 kernel locals math math.functions math.order math.vectors
5 sequences sequences.private sorting ;
8 : power-mean ( seq p -- x )
9 [ '[ _ ^ ] map-sum ] [ [ length / ] [ recip ^ ] bi* ] 2bi ; inline
11 ! Delta in degrees-of-freedom
12 : mean-ddof ( seq ddof -- x )
13 [ [ sum ] [ length ] bi ] dip -
14 [ drop 0 ] [ / ] if-zero ; inline
19 : unbiased-mean ( seq -- x )
22 : sum-of-squares ( seq -- x )
23 [ sq ] map-sum ; inline
25 : sum-of-squared-errors ( seq -- x )
26 [ mean ] keep [ - sq ] with map-sum ; inline
28 : sum-of-absolute-errors ( seq -- x )
29 [ mean ] keep [ - ] with map-sum ; inline
31 : quadratic-mean ( seq -- x ) ! root-mean-square
32 [ sum-of-squares ] [ length ] bi / sqrt ; inline
34 : geometric-mean ( seq -- x )
35 [ [ log ] map-sum ] [ length ] bi /f e^ ; inline
37 : harmonic-mean ( seq -- x )
38 [ recip ] map-sum recip ; inline
40 : contraharmonic-mean ( seq -- x )
41 [ sum-of-squares ] [ sum ] bi / ; inline
45 : trim-points ( p seq -- from to seq )
46 [ length [ * >integer ] keep over - ] keep ;
50 : trimmed-mean ( seq p -- x )
51 swap natural-sort trim-points <slice> mean ;
53 : winsorized-mean ( seq p -- x )
54 swap natural-sort trim-points
56 [ nip dupd nth <array> ]
57 [ [ 1 - ] dip nth <array> ] 3tri
62 :: kth-object-impl ( seq k nth-quot exchange-quot quot: ( x y -- ? ) -- elt )
63 ! Wirth's method, Algorithm's + Data structues = Programs p. 84
64 k seq bounds-check 2drop
77 [ i seq nth-quot call x quot call ] [ i 1 + i! ] while
78 [ x j seq nth-quot call quot call ] [ j 1 - j! ] while
80 i j seq exchange-quot call
89 k seq nth-unsafe ; inline
91 : (kth-object) ( seq k nth-quot exchange-quot quot: ( x y -- ? ) -- elt )
92 ! The algorithm modifiers seq, so we clone it
93 [ >array ] 4dip kth-object-impl ; inline
95 : kth-object-unsafe ( seq k quot: ( x y -- ? ) -- elt )
96 [ [ nth-unsafe ] [ exchange-unsafe ] ] dip (kth-object) ; inline
98 : kth-objects-unsafe ( seq kths quot: ( x y -- ? ) -- elts )
99 '[ _ kth-object-unsafe ] with map ; inline
103 : kth-object ( seq k quot: ( x y -- ? ) -- elt )
104 [ [ nth ] [ exchange ] ] dip (kth-object) ; inline
106 : kth-objects ( seq kths quot: ( x y -- ? ) -- elts )
107 '[ _ kth-object ] with map ; inline
109 : kth-smallests ( seq kths -- elts ) [ < ] kth-objects-unsafe ;
111 : kth-smallest ( seq k -- elt ) [ < ] kth-object-unsafe ;
113 : kth-largests ( seq kths -- elts ) [ > ] kth-objects-unsafe ;
115 : kth-largest ( seq k -- elt ) [ > ] kth-object-unsafe ;
117 : count-relative ( seq k -- lt eq gt )
120 { +lt+ [ [ 1 + ] 2dip ] }
122 { +eq+ [ [ 1 + ] dip ] }
126 : minmax-relative ( seq k -- lt eq gt lt-max gt-min )
127 [ 0 0 0 -1/0. 1/0. ] 2dip '[
129 { +lt+ [ [ 1 + ] 5 ndip '[ _ max ] dip ] }
130 { +gt+ [ [ 1 + ] 3dip min ] }
131 { +eq+ [ [ 1 + ] 4dip drop ] }
135 : lower-median-index ( seq -- n )
137 [ length odd? [ 1 - ] unless ] bi ;
139 : lower-median ( seq -- elt )
140 [ ] [ lower-median-index ] bi kth-smallest ;
142 : upper-median ( seq -- elt )
143 dup midpoint@ kth-smallest ;
145 : medians ( seq -- lower upper )
147 [ [ lower-median-index ] [ midpoint@ ] bi 2array ]
148 bi kth-smallests first2 ;
150 : median ( seq -- x )
151 dup length odd? [ lower-median ] [ medians + 2 / ] if ;
153 ! quantile can be any n-tile. quartile is n = 4, percentile is n = 100
154 ! a,b,c,d parameters, N - number of samples, q is quantile (1/2 for median, 1/4 for 1st quartile)
155 ! http://mathworld.wolfram.com/Quantile.html
157 ! could subtract 1 from a
159 : quantile-x ( a b N q -- x )
160 [ + ] dip * + 1 - ; inline
164 >fraction [ /mod nip ] keep / ; inline
166 :: quantile-indices ( seq qs a b -- seq )
167 qs [ [ a b seq length ] dip quantile-x ] map ;
169 :: qabcd ( y-floor y-ceiling x c d -- qabcd )
170 y-floor y-ceiling y-floor - c d x frac * + * + ;
172 :: quantile-abcd ( seq qs a b c d -- quantile )
173 seq qs a b quantile-indices :> indices
174 indices [ [ floor 0 max ] [ ceiling seq length 1 - min ] bi 2array ] map
175 concat :> index-pairs
177 seq index-pairs kth-smallests
178 2 group indices [ [ first2 ] dip c d qabcd ] 2map ;
180 : quantile1 ( seq qs -- seq' )
181 0 0 1 0 quantile-abcd ;
183 : quantile3 ( seq qs -- seq' )
184 1/2 0 0 0 quantile-abcd ;
186 : quantile4 ( seq qs -- seq' )
187 0 0 0 1 quantile-abcd ;
189 : quantile5 ( seq qs -- seq' )
190 1/2 0 0 1 quantile-abcd ;
192 : quantile6 ( seq qs -- seq' )
193 0 1 0 1 quantile-abcd ;
195 : quantile7 ( seq qs -- seq' )
196 1 -1 0 1 quantile-abcd ;
198 : quantile8 ( seq qs -- seq' )
199 1/3 1/3 0 1 quantile-abcd ;
201 : quantile9 ( seq qs -- seq' )
202 3/8 1/4 0 1 quantile-abcd ;
204 : quartile ( seq -- seq' )
205 { 1/4 1/2 3/4 } quantile5 ;
207 : trimean ( seq -- x )
208 quartile first3 [ 2 * ] dip + + 4 / ;
210 : histogram! ( hashtable seq -- hashtable )
211 over '[ _ inc-at ] each ;
213 : histogram-by ( seq quot: ( x -- bin ) -- hashtable )
214 H{ } clone [ '[ @ _ inc-at ] each ] keep ; inline
216 : histogram ( seq -- hashtable )
219 : sorted-histogram ( seq -- alist )
220 histogram sort-values ;
222 : normalized-histogram ( seq -- alist )
223 [ histogram ] [ length ] bi '[ _ / ] assoc-map ;
225 : equal-probabilities ( n -- array )
226 dup recip <array> ; inline
229 histogram >alist [ second ] supremum-by first ;
231 : minmax ( seq -- min max )
232 [ first dup ] keep [ [ min ] [ max ] bi-curry bi* ] each ;
237 : var-ddof ( seq n -- x )
238 2dup [ length ] dip - 0 <= [
241 [ [ sum-of-squared-errors ] [ length ] bi ] dip - /
244 : population-var ( seq -- x ) 0 var-ddof ; inline
246 : sample-var ( seq -- x ) 1 var-ddof ; inline
248 : std-ddof ( seq n -- x )
249 var-ddof sqrt ; inline
251 : population-std ( seq -- x ) 0 std-ddof ; inline
253 : sample-std ( seq -- x ) 1 std-ddof ; inline
255 ALIAS: std sample-std
257 : signal-to-noise ( seq -- x ) [ mean ] [ population-std ] bi / ;
259 : demean ( seq -- seq' ) dup mean v-n ;
261 : mean-dev ( seq -- x ) demean vabs mean ;
263 : demedian ( seq -- seq' ) dup median v-n ;
265 : median-dev ( seq -- x ) demedian vabs mean ;
267 : ste-ddof ( seq n -- x ) '[ _ std-ddof ] [ length ] bi sqrt / ;
269 : population-ste ( seq -- x ) 0 ste-ddof ;
271 : sample-ste ( seq -- x ) 1 ste-ddof ;
274 : r-sum-diffs ( x-mean y-mean x-seq y-seq -- (r) )
275 ! finds sigma((xi-mean(x))(yi-mean(y))
276 0 [ [ [ pick ] dip swap - ] bi@ * + ] 2reduce 2nip ;
278 : (r) ( x-mean y-mean x-seq y-seq x-std y-std -- r )
279 * recip [ [ r-sum-diffs ] keep length 1 - / ] dip * ;
281 : r-stats ( xy-pairs -- x-mean y-mean x-seq y-seq x-std y-std )
282 first2 [ [ [ mean ] bi@ ] 2keep ] 2keep [ population-std ] bi@ ;
285 : pearson-r ( xy-pairs -- r ) r-stats (r) ;
287 : least-squares ( xy-pairs -- alpha beta )
288 r-stats [ 2dup ] 4 ndip
289 ! stack is x-mean y-mean x-mean y-mean x-seq y-seq x-std y-std
290 [ (r) ] 2keep ! stack is mean(x) mean(y) r sx sy
291 swap / * ! stack is mean(x) mean(y) beta
294 : cov-ddof ( x-seq y-seq ddof -- cov )
295 [ [ demean ] bi@ v* ] dip mean-ddof ;
297 : population-cov ( x-seq y-seq -- cov ) 0 cov-ddof ; inline
299 : sample-cov ( x-seq y-seq -- cov ) 1 cov-ddof ; inline
301 : corr-ddof ( x-seq y-seq n -- corr )
302 [ [ population-cov ] ] dip
303 '[ [ _ var-ddof ] bi@ * sqrt ] 2bi / ;
305 : population-corr ( x-seq y-seq -- corr ) 0 corr-ddof ; inline
307 : sample-corr ( x-seq y-seq -- corr ) 1 corr-ddof ; inline
309 : cum-sum ( seq -- seq' )
310 0 [ + ] accumulate* ;
312 : cum-sum0 ( seq -- seq' )
313 0 [ + ] accumulate nip ;
315 : cum-product ( seq -- seq' )
316 1 [ * ] accumulate* ;
318 : cum-product1 ( seq -- seq' )
319 1 [ * ] accumulate nip ;
321 : cum-mean ( seq -- seq' )
322 0 swap [ [ + dup ] dip 1 + / ] map-index nip ;
324 : cum-count ( seq quot: ( elt -- ? ) -- seq' )
325 [ 0 ] dip '[ @ [ 1 + ] when ] accumulate* ; inline
327 : cum-min ( seq -- seq' )
328 dup ?first [ min ] accumulate* ;
330 : cum-max ( seq -- seq' )
331 dup ?first [ max ] accumulate* ;
333 : entropy ( probabilities -- n )
334 dup sum '[ _ / dup log * ] map-sum neg ;
336 : maximum-entropy ( probabilities -- n )
339 : normalized-entropy ( probabilities -- n )
340 [ entropy ] [ maximum-entropy ] bi / ;
342 : binary-entropy ( p -- h )
343 [ dup log * ] [ 1 swap - dup log * ] bi + neg 2 log / ;
345 : standardize ( u -- v )
346 [ demean ] [ sample-std ] bi [ v/n ] unless-zero ;
348 : standardize-2d ( u -- v )
349 flip [ standardize ] map flip ;
351 : differences ( u -- v )
352 [ rest-slice ] keep v- ;
355 dup minmax over - [ v-n ] [ v/n ] bi* ;
357 : rankings ( histogram -- assoc )
358 sort-keys 0 swap [ rot [ + ] keep swapd ] H{ } assoc-map-as nip ;
360 : rank-values ( seq -- seq' )
361 dup histogram rankings '[ _ at ] map ;
363 : z-score ( seq -- n )
364 [ demean ] [ sample-std ] bi v/n ;