Merge branch 'master' of git://factorcode.org/git/factor
commit
d2f0d577c3
|
@ -1,4 +1,5 @@
|
||||||
USING: help.markup help.syntax debugger ;
|
USING: assocs debugger hashtables help.markup help.syntax
|
||||||
|
quotations sequences ;
|
||||||
IN: math.statistics
|
IN: math.statistics
|
||||||
|
|
||||||
HELP: geometric-mean
|
HELP: geometric-mean
|
||||||
|
@ -58,3 +59,104 @@ HELP: var
|
||||||
{ $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } var ." "1" }
|
{ $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } var ." "1" }
|
||||||
{ $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } var ." "1+2/3" } } ;
|
{ $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } var ." "1+2/3" } } ;
|
||||||
|
|
||||||
|
|
||||||
|
HELP: histogram
|
||||||
|
{ $values
|
||||||
|
{ "seq" sequence }
|
||||||
|
{ "hashtable" hashtable }
|
||||||
|
}
|
||||||
|
{ $examples
|
||||||
|
{ $example "! Count the number of times an element appears in a sequence."
|
||||||
|
"USING: prettyprint histogram ;"
|
||||||
|
"\"aaabc\" histogram ."
|
||||||
|
"H{ { 97 3 } { 98 1 } { 99 1 } }"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ $description "Returns a hashtable where the keys are the elements of the sequence and the values are the number of times they appeared in that sequence." } ;
|
||||||
|
|
||||||
|
HELP: histogram*
|
||||||
|
{ $values
|
||||||
|
{ "hashtable" hashtable } { "seq" sequence }
|
||||||
|
{ "hashtable" hashtable }
|
||||||
|
}
|
||||||
|
{ $examples
|
||||||
|
{ $example "! Count the number of times the elements of two sequences appear."
|
||||||
|
"USING: prettyprint histogram ;"
|
||||||
|
"\"aaabc\" histogram \"aaaaaabc\" histogram* ."
|
||||||
|
"H{ { 97 9 } { 98 2 } { 99 2 } }"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ $description "Takes an existing hashtable and uses " { $link histogram } " to continue counting the number of occurences of each element." } ;
|
||||||
|
|
||||||
|
HELP: sequence>assoc
|
||||||
|
{ $values
|
||||||
|
{ "seq" sequence } { "quot" quotation } { "exemplar" "an exemplar assoc" }
|
||||||
|
{ "assoc" assoc }
|
||||||
|
}
|
||||||
|
{ $examples
|
||||||
|
{ $example "! Iterate over a sequence and increment the count at each element"
|
||||||
|
"USING: assocs prettyprint histogram ;"
|
||||||
|
"\"aaabc\" [ inc-at ] H{ } sequence>assoc ."
|
||||||
|
"H{ { 97 3 } { 98 1 } { 99 1 } }"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } " according to the passed quotation." } ;
|
||||||
|
|
||||||
|
HELP: sequence>assoc*
|
||||||
|
{ $values
|
||||||
|
{ "assoc" assoc } { "seq" sequence } { "quot" quotation }
|
||||||
|
{ "assoc" assoc }
|
||||||
|
}
|
||||||
|
{ $examples
|
||||||
|
{ $example "! Iterate over a sequence and add the counts to an existing assoc"
|
||||||
|
"USING: assocs prettyprint histogram kernel ;"
|
||||||
|
"H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ inc-at ] sequence>assoc* ."
|
||||||
|
"H{ { 97 5 } { 98 2 } { 99 1 } }"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } " according to the passed quotation." } ;
|
||||||
|
|
||||||
|
HELP: sequence>hashtable
|
||||||
|
{ $values
|
||||||
|
{ "seq" sequence } { "quot" quotation }
|
||||||
|
{ "hashtable" hashtable }
|
||||||
|
}
|
||||||
|
{ $examples
|
||||||
|
{ $example "! Count the number of times an element occurs in a sequence"
|
||||||
|
"USING: assocs prettyprint histogram ;"
|
||||||
|
"\"aaabc\" [ inc-at ] sequence>hashtable ."
|
||||||
|
"H{ { 97 3 } { 98 1 } { 99 1 } }"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according to the passed quotation." } ;
|
||||||
|
|
||||||
|
ARTICLE: "histogram" "Computing histograms"
|
||||||
|
"Counting elements in a sequence:"
|
||||||
|
{ $subsections
|
||||||
|
histogram
|
||||||
|
histogram*
|
||||||
|
}
|
||||||
|
"Combinators for implementing histogram:"
|
||||||
|
{ $subsections
|
||||||
|
sequence>assoc
|
||||||
|
sequence>assoc*
|
||||||
|
sequence>hashtable
|
||||||
|
} ;
|
||||||
|
|
||||||
|
ARTICLE: "math.statistics" "Statistics"
|
||||||
|
"Computing the mean:"
|
||||||
|
{ $subsections mean geometric-mean harmonic-mean }
|
||||||
|
"Computing the median:"
|
||||||
|
{ $subsections median lower-median upper-median medians }
|
||||||
|
"Computing the mode:"
|
||||||
|
{ $subsections mode }
|
||||||
|
"Computing the standard deviation and variance:"
|
||||||
|
{ $subsections std var }
|
||||||
|
"Computing the range and minimum and maximum elements:"
|
||||||
|
{ $subsections range minmax }
|
||||||
|
"Computing the kth smallest element:"
|
||||||
|
{ $subsections kth-smallest }
|
||||||
|
"Counting the frequency of occurrence of elements:"
|
||||||
|
{ $subsection "histogram" } ;
|
||||||
|
|
||||||
|
ABOUT: "math.statistics"
|
||||||
|
|
|
@ -43,3 +43,13 @@ IN: math.statistics.tests
|
||||||
[ 0 ] [ { 1 } var ] unit-test
|
[ 0 ] [ { 1 } var ] unit-test
|
||||||
[ 0.0 ] [ { 1 } std ] unit-test
|
[ 0.0 ] [ { 1 } std ] unit-test
|
||||||
[ 0.0 ] [ { 1 } ste ] unit-test
|
[ 0.0 ] [ { 1 } ste ] unit-test
|
||||||
|
|
||||||
|
[
|
||||||
|
H{
|
||||||
|
{ 97 2 }
|
||||||
|
{ 98 2 }
|
||||||
|
{ 99 2 }
|
||||||
|
}
|
||||||
|
] [
|
||||||
|
"aabbcc" histogram
|
||||||
|
] unit-test
|
||||||
|
|
|
@ -45,7 +45,8 @@ IN: math.statistics
|
||||||
k seq nth ; inline
|
k seq nth ; inline
|
||||||
|
|
||||||
: lower-median ( seq -- elt )
|
: lower-median ( seq -- elt )
|
||||||
dup dup length odd? [ midpoint@ ] [ midpoint@ 1 - ] if kth-smallest ;
|
[ ] [ ] [ length odd? ] tri
|
||||||
|
[ midpoint@ ] [ midpoint@ 1 - ] if kth-smallest ;
|
||||||
|
|
||||||
: upper-median ( seq -- elt )
|
: upper-median ( seq -- elt )
|
||||||
dup midpoint@ kth-smallest ;
|
dup midpoint@ kth-smallest ;
|
||||||
|
@ -54,13 +55,35 @@ IN: math.statistics
|
||||||
[ lower-median ] [ upper-median ] bi ;
|
[ lower-median ] [ upper-median ] bi ;
|
||||||
|
|
||||||
: median ( seq -- x )
|
: median ( seq -- x )
|
||||||
dup length odd? [ lower-median ] [ medians + 2 / ] if ;
|
[ ] [ length odd? ] bi [ lower-median ] [ medians + 2 / ] if ;
|
||||||
|
|
||||||
: frequency ( seq -- hashtable )
|
<PRIVATE
|
||||||
H{ } clone [ '[ _ inc-at ] each ] keep ;
|
|
||||||
|
: (sequence>assoc) ( seq quot assoc -- assoc )
|
||||||
|
[ swap curry each ] keep ; inline
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
: sequence>assoc* ( assoc seq quot: ( obj assoc -- ) -- assoc )
|
||||||
|
rot (sequence>assoc) ; inline
|
||||||
|
|
||||||
|
: sequence>assoc ( seq quot: ( obj assoc -- ) exemplar -- assoc )
|
||||||
|
clone (sequence>assoc) ; inline
|
||||||
|
|
||||||
|
: sequence>hashtable ( seq quot: ( obj hashtable -- ) -- hashtable )
|
||||||
|
H{ } sequence>assoc ; inline
|
||||||
|
|
||||||
|
: histogram* ( hashtable seq -- hashtable )
|
||||||
|
[ inc-at ] sequence>assoc* ;
|
||||||
|
|
||||||
|
: histogram ( seq -- hashtable )
|
||||||
|
[ inc-at ] sequence>hashtable ;
|
||||||
|
|
||||||
|
: collect-values ( seq quot: ( obj hashtable -- ) -- hash )
|
||||||
|
'[ [ dup @ ] dip push-at ] sequence>hashtable ; inline
|
||||||
|
|
||||||
: mode ( seq -- x )
|
: mode ( seq -- x )
|
||||||
frequency >alist
|
histogram >alist
|
||||||
[ ] [ [ [ second ] bi@ > ] 2keep ? ] map-reduce first ;
|
[ ] [ [ [ second ] bi@ > ] 2keep ? ] map-reduce first ;
|
||||||
|
|
||||||
: minmax ( seq -- min max )
|
: minmax ( seq -- min max )
|
||||||
|
|
|
@ -1,87 +0,0 @@
|
||||||
IN: histogram
|
|
||||||
USING: help.markup help.syntax sequences hashtables quotations assocs ;
|
|
||||||
|
|
||||||
HELP: histogram
|
|
||||||
{ $values
|
|
||||||
{ "seq" sequence }
|
|
||||||
{ "hashtable" hashtable }
|
|
||||||
}
|
|
||||||
{ $examples
|
|
||||||
{ $example "! Count the number of times an element appears in a sequence."
|
|
||||||
"USING: prettyprint histogram ;"
|
|
||||||
"\"aaabc\" histogram ."
|
|
||||||
"H{ { 97 3 } { 98 1 } { 99 1 } }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{ $description "Returns a hashtable where the keys are the elements of the sequence and the values are the number of times they appeared in that sequence." } ;
|
|
||||||
|
|
||||||
HELP: histogram*
|
|
||||||
{ $values
|
|
||||||
{ "hashtable" hashtable } { "seq" sequence }
|
|
||||||
{ "hashtable" hashtable }
|
|
||||||
}
|
|
||||||
{ $examples
|
|
||||||
{ $example "! Count the number of times the elements of two sequences appear."
|
|
||||||
"USING: prettyprint histogram ;"
|
|
||||||
"\"aaabc\" histogram \"aaaaaabc\" histogram* ."
|
|
||||||
"H{ { 97 9 } { 98 2 } { 99 2 } }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{ $description "Takes an existing hashtable and uses " { $link histogram } " to continue counting the number of occurences of each element." } ;
|
|
||||||
|
|
||||||
HELP: sequence>assoc
|
|
||||||
{ $values
|
|
||||||
{ "seq" sequence } { "quot" quotation } { "exemplar" "an exemplar assoc" }
|
|
||||||
{ "assoc" assoc }
|
|
||||||
}
|
|
||||||
{ $examples
|
|
||||||
{ $example "! Iterate over a sequence and increment the count at each element"
|
|
||||||
"USING: assocs prettyprint histogram ;"
|
|
||||||
"\"aaabc\" [ inc-at ] H{ } sequence>assoc ."
|
|
||||||
"H{ { 97 3 } { 98 1 } { 99 1 } }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } " according to the passed quotation." } ;
|
|
||||||
|
|
||||||
HELP: sequence>assoc*
|
|
||||||
{ $values
|
|
||||||
{ "assoc" assoc } { "seq" sequence } { "quot" quotation }
|
|
||||||
{ "assoc" assoc }
|
|
||||||
}
|
|
||||||
{ $examples
|
|
||||||
{ $example "! Iterate over a sequence and add the counts to an existing assoc"
|
|
||||||
"USING: assocs prettyprint histogram kernel ;"
|
|
||||||
"H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ inc-at ] sequence>assoc* ."
|
|
||||||
"H{ { 97 5 } { 98 2 } { 99 1 } }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } " according to the passed quotation." } ;
|
|
||||||
|
|
||||||
HELP: sequence>hashtable
|
|
||||||
{ $values
|
|
||||||
{ "seq" sequence } { "quot" quotation }
|
|
||||||
{ "hashtable" hashtable }
|
|
||||||
}
|
|
||||||
{ $examples
|
|
||||||
{ $example "! Count the number of times an element occurs in a sequence"
|
|
||||||
"USING: assocs prettyprint histogram ;"
|
|
||||||
"\"aaabc\" [ inc-at ] sequence>hashtable ."
|
|
||||||
"H{ { 97 3 } { 98 1 } { 99 1 } }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according to the passed quotation." } ;
|
|
||||||
|
|
||||||
ARTICLE: "histogram" "Computing histograms"
|
|
||||||
"Counting elements in a sequence:"
|
|
||||||
{ $subsections
|
|
||||||
histogram
|
|
||||||
histogram*
|
|
||||||
}
|
|
||||||
"Combinators for implementing histogram:"
|
|
||||||
{ $subsections
|
|
||||||
sequence>assoc
|
|
||||||
sequence>assoc*
|
|
||||||
sequence>hashtable
|
|
||||||
} ;
|
|
||||||
|
|
||||||
ABOUT: "histogram"
|
|
|
@ -1,12 +0,0 @@
|
||||||
IN: histogram.tests
|
|
||||||
USING: help.markup help.syntax tools.test histogram ;
|
|
||||||
|
|
||||||
[
|
|
||||||
H{
|
|
||||||
{ 97 2 }
|
|
||||||
{ 98 2 }
|
|
||||||
{ 99 2 }
|
|
||||||
}
|
|
||||||
] [
|
|
||||||
"aabbcc" histogram
|
|
||||||
] unit-test
|
|
|
@ -1,29 +0,0 @@
|
||||||
! Copyright (C) 2009 Doug Coleman.
|
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
|
||||||
USING: kernel sequences assocs fry ;
|
|
||||||
IN: histogram
|
|
||||||
|
|
||||||
<PRIVATE
|
|
||||||
|
|
||||||
: (sequence>assoc) ( seq quot assoc -- assoc )
|
|
||||||
[ swap curry each ] keep ; inline
|
|
||||||
|
|
||||||
PRIVATE>
|
|
||||||
|
|
||||||
: sequence>assoc* ( assoc seq quot: ( obj assoc -- ) -- assoc )
|
|
||||||
rot (sequence>assoc) ; inline
|
|
||||||
|
|
||||||
: sequence>assoc ( seq quot: ( obj assoc -- ) exemplar -- assoc )
|
|
||||||
clone (sequence>assoc) ; inline
|
|
||||||
|
|
||||||
: sequence>hashtable ( seq quot: ( obj hashtable -- ) -- hashtable )
|
|
||||||
H{ } sequence>assoc ; inline
|
|
||||||
|
|
||||||
: histogram* ( hashtable seq -- hashtable )
|
|
||||||
[ inc-at ] sequence>assoc* ;
|
|
||||||
|
|
||||||
: histogram ( seq -- hashtable )
|
|
||||||
[ inc-at ] sequence>hashtable ;
|
|
||||||
|
|
||||||
: collect-values ( seq quot: ( obj hashtable -- ) -- hash )
|
|
||||||
'[ [ dup @ ] dip push-at ] sequence>hashtable ; inline
|
|
Loading…
Reference in New Issue