1 USING: assocs debugger hashtables help.markup help.syntax
2 quotations sequences math ;
6 { $values { "seq" sequence } { "x" "a non-negative real number"} }
7 { $description "Computes the geometric mean of all elements in " { $snippet "seq" } ". The geometric mean measures the central tendency of a data set and minimizes the effects of extreme values." }
8 { $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } geometric-mean ." "1.81712059283214" } }
9 { $errors "Throws a " { $link signal-error. } " (square-root of 0) if the sequence is empty." } ;
12 { $values { "seq" sequence } { "x" "a non-negative real number"} }
13 { $description "Computes the harmonic mean of the elements in " { $snippet "seq" } ". The harmonic mean is appropriate when the average of rates is desired." }
14 { $notes "Positive reals only." }
15 { $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } harmonic-mean ." "6/11" } }
16 { $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
19 { $values { "seq" sequence } { "x" "a non-negative real number"} }
20 { $description "Computes the arithmetic mean of the elements in " { $snippet "seq" } "." }
21 { $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } mean ." "2" } }
22 { $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
25 { $values { "seq" sequence } { "x" "a non-negative real number"} }
26 { $description "Computes the median of " { $snippet "seq" } " by finding the middle element of the sequence using " { $link kth-smallest } ". If there is an even number of elements in the sequence, the median is not unique, so the mean of the two middle values is output." }
28 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } median ." "2" }
29 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } median ." "2+1/2" } }
30 { $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
33 { $values { "seq" sequence } { "x" "a non-negative real number"} }
34 { $description "Computes the difference of the maximum and minimum values in " { $snippet "seq" } "." }
36 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } range ." "2" }
37 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } range ." "3" } } ;
40 { $values { "seq" sequence } { "min" real } { "max" real } }
41 { $description "Finds the minimum and maximum elements of " { $snippet "seq" } " in one pass. Throws an error on an empty sequence." }
43 { $example "USING: arrays math.statistics prettyprint ;"
44 "{ 1 2 3 } minmax 2array ."
50 { $values { "seq" sequence } { "x" "a non-negative real number"} }
51 { $description "Computes the sample standard deviation of " { $snippet "seq" } ", which is the square root of the sample variance. It measures how widely spread the values in a sequence are about the mean for a random subset of a dataset." }
53 { $example "USING: math.statistics prettyprint ;" "{ 7 8 9 } sample-std ." "1.0" } } ;
56 { $values { "seq" sequence } { "x" "a non-negative real number"} }
57 { $description "Computes the standard error of the mean for " { $snippet "seq" } ". It's defined as the standard deviation divided by the square root of the length of the sequence, and measures uncertainty associated with the estimate of the mean." }
59 { $example "USING: math.statistics prettyprint ;" "{ -2 2 } sample-ste ." "2.0" }
63 { $values { "seq" sequence } { "x" "a non-negative real number"} }
64 { $description "Computes the variance of " { $snippet "seq" } ". It's a measurement of the spread of values in a sequence." }
65 { $notes "If the number of elements in " { $snippet "seq" } " is 1 or less, it outputs 0." }
67 { $example "USING: math.statistics prettyprint ;" "{ 1 } sample-var ." "0" }
68 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } sample-var ." "1" }
69 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } sample-var ." "1+2/3" } } ;
72 { $values { "{x}" sequence } { "{y}" sequence } { "cov" "a real number" } }
73 { $description "Computes the covariance of two sequences, " { $snippet "{x}" } " and " { $snippet "{y}" } "." } ;
76 { $values { "{x}" sequence } { "{y}" sequence } { "corr" "a real number" } }
77 { $description "Computes the correlation of two sequences, " { $snippet "{x}" } " and " { $snippet "{y}" } "." } ;
82 { "hashtable" hashtable }
84 { $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." }
86 { $example "! Count the number of times an element appears in a sequence."
87 "USING: prettyprint math.statistics ;"
88 "\"aaabc\" histogram ."
89 "H{ { 97 3 } { 98 1 } { 99 1 } }"
97 { "quot" { $quotation "( x -- bin )" } }
98 { "hashtable" hashtable }
100 { $description "Returns a hashtable where the keys are the elements of the sequence binned by being passed through " { $snippet "quot" } ", and the values are the number of times members of each bin appeared in that sequence." }
102 { $unchecked-example "! Count the number of times letters and non-letters appear in a sequence."
103 "USING: prettyprint math.statistics unicode.categories ;"
104 "\"aaa123bc\" [ letter? ] histogram-by ."
105 "H{ { t 5 } { f 3 } }"
111 { "hashtable" hashtable } { "seq" sequence }
113 { $description "Takes an existing hashtable and uses " { $link histogram } " to continue counting the number of occurrences of each element." }
115 { $example "! Count the number of times the elements of two sequences appear."
116 "USING: prettyprint math.statistics ;"
117 "\"aaabc\" histogram \"aaaaaabc\" histogram! ."
118 "H{ { 97 9 } { 98 2 } { 99 2 } }"
122 HELP: sorted-histogram
125 { "alist" "an array of key/value pairs" }
127 { $description "Outputs a " { $link histogram } " of a sequence sorted by number of occurrences from lowest to highest." }
129 { $example "USING: prettyprint math.statistics ;"
130 """"abababbbbbbc" sorted-histogram ."""
131 "{ { 99 1 } { 97 3 } { 98 8 } }"
137 { "seq" sequence } { "map-quot" $quotation } { "insert-quot" quotation } { "exemplar" "an exemplar assoc" }
140 { $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } ". The " { $snippet "map-quot" } " gets passed each element from the sequence. Its outputs are passed along with the assoc being constructed to the " { $snippet "insert-quot" } ", which can modify the assoc in response." }
142 { $example "! Iterate over a sequence and increment the count at each element"
143 "! The first quotation has stack effect ( key -- key ), a no-op"
144 "USING: assocs prettyprint kernel math.statistics ;"
145 "\"aaabc\" [ ] [ inc-at ] H{ } sequence>assoc ."
146 "H{ { 97 3 } { 98 1 } { 99 1 } }"
150 HELP: sequence>assoc!
152 { "assoc" assoc } { "seq" sequence } { "map-quot" quotation } { "insert-quot" quotation } }
153 { $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } ". The " { $snippet "map-quot" } " gets passed each element from the sequence. Its outputs are passed along with the assoc being constructed to the " { $snippet "insert-quot" } ", which can modify the assoc in response." }
155 { $example "! Iterate over a sequence and add the counts to an existing assoc"
156 "USING: assocs prettyprint math.statistics kernel ;"
157 "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ ] [ inc-at ] sequence>assoc! ."
158 "H{ { 97 5 } { 98 2 } { 99 1 } }"
162 HELP: sequence>hashtable
164 { "seq" sequence } { "map-quot" quotation } { "insert-quot" quotation }
165 { "hashtable" hashtable }
167 { $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created hashtable. The " { $snippet "map-quot" } " gets passed each element from the sequence. Its outputs are passed along with the assoc being constructed to the " { $snippet "insert-quot" } ", which can modify the assoc in response." }
169 { $example "! Count the number of times an element occurs in a sequence"
170 "USING: assocs kernel prettyprint math.statistics ;"
171 "\"aaabc\" [ ] [ inc-at ] sequence>hashtable ."
172 "H{ { 97 3 } { 98 1 } { 99 1 } }"
177 { $values { "seq" sequence } { "seq'" sequence } }
178 { $description "Returns the cumulative sum of " { $snippet "seq" } "." }
180 { $example "USING: math.statistics prettyprint ;"
181 "{ 1 -1 2 -1 4 } cum-sum ."
187 { $values { "seq" sequence } { "quot" quotation } { "seq'" sequence } }
188 { $description "Returns the cumulative count of how many times " { $snippet "quot" } " returns true." }
190 { $example "USING: math math.statistics prettyprint ;"
191 "{ 1 -1 2 -1 4 } [ 0 < ] cum-count ."
198 { $values { "seq" sequence } { "seq'" sequence } }
199 { $description "Returns the cumulative product of " { $snippet "seq" } "." }
201 { $example "USING: math.statistics prettyprint ;"
202 "{ 1 2 3 4 } cum-product ."
208 { $values { "seq" sequence } { "seq'" sequence } }
209 { $description "Returns the cumulative mean of " { $snippet "seq" } "." }
211 { $example "USING: math.statistics prettyprint ;"
212 "{ 1.0 2.0 3.0 } cum-mean ."
218 { $values { "seq" sequence } { "seq'" sequence } }
219 { $description "Returns the cumulative min of " { $snippet "seq" } "." }
221 { $example "USING: math.statistics prettyprint ;"
222 "{ 5 3 4 1 } cum-min ."
228 { $values { "seq" sequence } { "seq'" sequence } }
229 { $description "Returns the cumulative max of " { $snippet "seq" } "." }
231 { $example "USING: math.statistics prettyprint ;"
232 "{ 1 -1 3 5 } cum-max ."
238 { $values { "u" sequence } { "v" sequence } }
239 { $description "Shifts and rescales the elements of " { $snippet "u" } " to have zero mean and unit sample variance." } ;
242 { $values { "u" sequence } { "v" sequence } }
243 { $description "Returns the successive differences of elements in " { $snippet "u" } "." } ;
246 { $values { "u" sequence } { "v" sequence } }
247 { $description "Returns " { $snippet "u" } " rescaled to run from 0 to 1 over the range min to max." } ;
251 { "seq" sequence } { "quot" { $quotation "( ... obj -- ... key )" } }
252 { "hashtable" hashtable }
254 { $description "Applies a quotation to each element in the input sequence and returns a " { $snippet "hashtable" } " of like elements. The keys of this " { $snippet "hashtable" } " are the output of " { $snippet "quot" } " and the values at each key are the elements that transformed to that key." }
256 "Collect even and odd elements:"
258 "USING: math math.statistics prettyprint ;"
259 "{ 11 12 13 14 14 13 12 11 } [ odd? ] collect-by ."
260 "H{ { f V{ 12 14 14 12 } } { t V{ 11 13 13 11 } } }"
263 { $notes "May be named " { $snippet "group-by" } " in other languages." } ;
265 HELP: collect-index-by
267 { "seq" sequence } { "quot" { $quotation "( ... obj -- ... key )" } }
268 { "hashtable" hashtable }
270 { $description "Applies a quotation to each element in the input sequence and returns a " { $snippet "hashtable" } " of like elements. The keys of this " { $snippet "hashtable" } " are the output of " { $snippet "quot" } " and the values at each key are the indices for the elements that transformed to that key." }
272 "Collect even and odd elements:"
274 "USING: math math.statistics prettyprint ;"
275 "{ 11 12 13 14 14 13 12 11 } [ odd? ] collect-index-by ."
276 "H{ { f V{ 1 3 4 6 } } { t V{ 0 2 5 7 } } }"
281 { $values { "seq" sequence } { "n" number } }
282 { $description "Calculates the Z-Score for " { $snippet "seq" } "." } ;
284 ARTICLE: "histogram" "Computing histograms"
285 "Counting elements in a sequence:"
292 "Combinators for implementing histogram:"
299 ARTICLE: "cumulative" "Computing cumulative sequences"
300 "Cumulative mapping combinators:"
310 "Cumulative comparisons:"
315 "Cumulative counting:"
320 ARTICLE: "math.statistics" "Statistics"
321 "Computing the mean:"
322 { $subsections mean geometric-mean harmonic-mean }
323 "Computing the median:"
324 { $subsections median lower-median upper-median medians }
325 "Computing the mode:"
326 { $subsections mode }
327 "Computing the population standard deviation, standard error, and variance:"
328 { $subsections population-std population-ste population-var }
329 "Computing the sample standard deviation, standard error, and variance:"
330 { $subsections sample-std sample-ste sample-var }
331 "Computing the nth delta-degrees-of-freedom statistics:"
332 { $subsections std-ddof ste-ddof var-ddof }
333 "Computing the range and minimum and maximum elements:"
334 { $subsections range minmax }
335 "Computing the kth smallest element:"
336 { $subsections kth-smallest }
337 "Counting the frequency of occurrence of elements:"
338 { $subsections "histogram" }
339 "Collecting related items:"
340 { $subsections collect-by collect-index-by }
341 "Computing cumulative sequences:"
342 { $subsections "cumulative" } ;
344 ABOUT: "math.statistics"
346 { var-ddof population-var sample-var } related-words
347 { std-ddof population-std sample-std } related-words
348 { ste-ddof population-ste sample-ste } related-words
349 { corr-ddof population-corr sample-corr } related-words
350 { cov-ddof population-cov sample-cov } related-words