factor/basis/math/statistics/statistics.factor

204 lines
5.2 KiB
Factor
Raw Normal View History

2008-10-03 03:19:03 -04:00
! Copyright (C) 2008 Doug Coleman, Michael Judge.
! See http://factorcode.org/license.txt for BSD license.
USING: assocs combinators generalizations kernel locals math
math.functions math.order math.vectors sequences
sequences.private sorting fry ;
2007-09-20 18:09:08 -04:00
IN: math.statistics
2009-05-18 03:41:58 -04:00
: mean ( seq -- x )
2008-09-22 22:31:15 -04:00
[ sum ] [ length ] bi / ;
2007-09-20 18:09:08 -04:00
2009-05-18 03:41:58 -04:00
: geometric-mean ( seq -- x )
2008-09-22 22:31:15 -04:00
[ length ] [ product ] bi nth-root ;
2007-09-20 18:09:08 -04:00
2009-05-18 03:41:58 -04:00
: harmonic-mean ( seq -- x )
2009-10-29 15:34:04 -04:00
[ recip ] map-sum recip ;
2007-09-20 18:09:08 -04:00
<PRIVATE
:: (kth-object) ( seq k quot: ( x y -- ? ) nth-quot exchange-quot -- elt )
#! Wirth's method, Algorithm's + Data structues = Programs p. 84
#! The algorithm modifiers seq, so we clone it
k seq bounds-check 2drop
seq clone :> seq
0 :> i!
0 :> j!
0 :> l!
0 :> x!
seq length 1 - :> m!
[ l m < ]
[
k seq nth x!
l i!
m j!
[ i j <= ]
[
[ i seq nth-quot call x quot call ] [ i 1 + i! ] while
[ x j seq nth-quot call quot call ] [ j 1 - j! ] while
i j <= [
i j seq exchange-quot call
i 1 + i!
j 1 - j!
] when
] do while
j k < [ i l! ] when
k i < [ j m! ] when
] while
k seq nth ; inline
: kth-object-unsafe ( seq k quot: ( x y -- ? ) -- elt )
[ nth-unsafe ] [ exchange-unsafe ] (kth-object) ; inline
PRIVATE>
: kth-object ( seq k quot: ( x y -- ? ) -- elt )
[ nth ] [ exchange ] (kth-object) ; inline
: kth-smallest ( seq k -- elt ) [ < ] kth-object-unsafe ;
: kth-largest ( seq k -- elt ) [ > ] kth-object-unsafe ;
: count-relative ( seq k -- lt eq gt )
[ 0 0 0 ] 2dip '[
_ <=> {
{ +lt+ [ [ 1 + ] 2dip ] }
{ +gt+ [ 1 + ] }
{ +eq+ [ [ 1 + ] dip ] }
} case
] each ;
: minmax-relative ( seq k -- lt eq gt lt-max gt-min )
[ 0 0 0 -1/0. 1/0. ] 2dip '[
dup _ <=> {
{ +lt+ [ [ 1 + ] 5 ndip '[ _ max ] dip ] }
{ +gt+ [ [ 1 + ] 3dip min ] }
{ +eq+ [ [ 1 + ] 4dip drop ] }
} case
] each ;
: lower-median ( seq -- elt )
[ ] [ ] [ length odd? ] tri
[ midpoint@ ] [ midpoint@ 1 - ] if kth-smallest ;
: upper-median ( seq -- elt )
dup midpoint@ kth-smallest ;
: medians ( seq -- lower upper )
[ lower-median ] [ upper-median ] bi ;
: median ( seq -- x )
[ ] [ length odd? ] bi [ lower-median ] [ medians + 2 / ] if ;
<PRIVATE
: (sequence>assoc) ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) assoc -- assoc )
[ swap curry compose each ] keep ; inline
PRIVATE>
: sequence>assoc! ( assoc seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) -- assoc )
4 nrot (sequence>assoc) ; inline
: sequence>assoc ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) exemplar -- assoc )
clone (sequence>assoc) ; inline
: sequence>hashtable ( seq map-quot: ( x -- ..y ) insert-quot: ( ..y assoc -- ) -- hashtable )
H{ } sequence>assoc ; inline
: histogram! ( hashtable seq -- hashtable )
[ ] [ inc-at ] sequence>assoc! ;
: histogram-by ( seq quot: ( x -- bin ) -- hashtable )
[ inc-at ] sequence>hashtable ; inline
: histogram ( seq -- hashtable )
[ ] histogram-by ;
2009-11-02 00:16:26 -05:00
: sorted-histogram ( seq -- alist )
histogram sort-values ;
2009-11-02 00:16:26 -05:00
: collect-pairs ( seq quot: ( x -- v k ) -- hashtable )
[ push-at ] sequence>hashtable ; inline
: collect-by ( seq quot: ( x -- x' ) -- hashtable )
[ dup ] prepose collect-pairs ; inline
2009-05-24 16:45:25 -04:00
: mode ( seq -- x )
histogram >alist
2011-10-15 22:19:44 -04:00
[ ] [ [ [ second ] bi@ > ] most ] map-reduce first ;
2009-05-24 16:45:25 -04:00
2009-12-07 18:26:33 -05:00
ERROR: empty-sequence ;
: minmax ( seq -- min max )
2009-12-07 18:26:33 -05:00
[
empty-sequence
] [
[ first dup ] keep [ [ min ] [ max ] bi-curry bi* ] each
] if-empty ;
2009-05-18 03:41:58 -04:00
: range ( seq -- x )
2007-09-20 18:09:08 -04:00
minmax swap - ;
: sample-var ( seq -- x )
#! normalize by N-1
2007-09-20 18:09:08 -04:00
dup length 1 <= [
drop 0
] [
2009-10-29 15:34:04 -04:00
[ [ mean ] keep [ - sq ] with map-sum ]
[ length 1 - ] bi /
2007-09-20 18:09:08 -04:00
] if ;
: var ( seq -- x )
dup length 1 <= [
drop 0
] [
[ [ mean ] keep [ - sq ] with map-sum ]
[ length ] bi /
] if ;
: std ( seq -- x ) var sqrt ;
2007-09-20 18:09:08 -04:00
: ste ( seq -- x ) [ std ] [ length ] bi sqrt / ;
2007-09-20 18:09:08 -04:00
: ((r)) ( mean(x) mean(y) {x} {y} -- (r) )
! finds sigma((xi-mean(x))(yi-mean(y))
2008-11-09 17:16:30 -05:00
0 [ [ [ pick ] dip swap - ] bi@ * + ] 2reduce 2nip ;
2007-09-20 18:09:08 -04:00
: (r) ( mean(x) mean(y) {x} {y} sx sy -- r )
2009-05-06 00:32:23 -04:00
* recip [ [ ((r)) ] keep length 1 - / ] dip * ;
2007-09-20 18:09:08 -04:00
: [r] ( {{x,y}...} -- mean(x) mean(y) {x} {y} sx sy )
2008-03-29 21:36:58 -04:00
first2 [ [ [ mean ] bi@ ] 2keep ] 2keep [ std ] bi@ ;
2007-09-20 18:09:08 -04:00
: r ( {{x,y}...} -- r )
[r] (r) ;
: r^2 ( {{x,y}...} -- r )
r sq ;
: least-squares ( {{x,y}...} -- alpha beta )
[r] { [ 2dup ] [ ] [ ] [ ] [ ] } spread
2007-09-20 18:09:08 -04:00
! stack is mean(x) mean(y) mean(x) mean(y) {x} {y} sx sy
[ (r) ] 2keep ! stack is mean(x) mean(y) r sx sy
swap / * ! stack is mean(x) mean(y) beta
[ swapd * - ] keep ;
: cov ( {x} {y} -- cov )
[ dup mean v-n ] bi@ v* mean ;
: corr ( {x} {y} -- corr )
[ cov ] [ [ var ] bi@ * sqrt ] 2bi / ;
: cum-sum ( seq -- seq' )
0 swap [ + dup ] map nip ;
: cum-product ( seq -- seq' )
1 swap [ * dup ] map nip ;
: cum-min ( seq -- seq' )
[ ?first ] keep [ min dup ] map nip ;
: cum-max ( seq -- seq' )
[ ?first ] keep [ max dup ] map nip ;