1 USING: assocs debugger hashtables help.markup help.syntax
2 kernel 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 } { "k" integer } { "elt" object } }
20 { $description "Returns the kth smallest element. This is semantically equivalent to " { $snippet "swap natural-sort nth" } ", and is therefore zero-indexed. " { $snippet "k" } " may not be larger than the highest index of " { $snippet "sequence" } "." }
21 { $examples { $example "USING: math.statistics prettyprint ;" "{ 3 1 2 } 1 kth-smallest ." "2" } } ;
24 { $values { "seq" sequence } { "x" "a non-negative real number"} }
25 { $description "Computes the arithmetic mean of the elements in " { $snippet "seq" } "." }
26 { $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } mean ." "2" } }
27 { $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
30 { $values { "seq" sequence } { "x" "a non-negative real number"} }
31 { $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." }
33 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } median ." "2" }
34 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } median ." "2+1/2" } }
35 { $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
38 { $values { "seq" sequence } { "x" "a non-negative real number"} }
39 { $description "Computes the difference of the maximum and minimum values in " { $snippet "seq" } "." }
41 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } range ." "2" }
42 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } range ." "3" } } ;
45 { $values { "seq" sequence } { "min" real } { "max" real } }
46 { $description "Finds the minimum and maximum elements of " { $snippet "seq" } " in one pass. Throws an error on an empty sequence." }
48 { $example "USING: arrays math.statistics prettyprint ;"
49 "{ 1 2 3 } minmax 2array ."
55 { $values { "seq" sequence } { "x" "a non-negative real number"} }
56 { $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." }
58 { $example "USING: math.statistics prettyprint ;" "{ 7 8 9 } sample-std ." "1.0" } } ;
61 { $values { "seq" sequence } { "x" "a non-negative real number"} }
62 { $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." }
64 { $example "USING: math.statistics prettyprint ;" "{ -2 2 } sample-ste ." "2.0" }
68 { $values { "seq" sequence } { "x" "a non-negative real number"} }
69 { $description "Computes the variance of " { $snippet "seq" } ". It's a measurement of the spread of values in a sequence." }
70 { $notes "If the number of elements in " { $snippet "seq" } " is 1 or less, it outputs 0." }
72 { $example "USING: math.statistics prettyprint ;" "{ 1 } sample-var ." "0" }
73 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } sample-var ." "1" }
74 { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } sample-var ." "1+2/3" } } ;
77 { $values { "{x}" sequence } { "{y}" sequence } { "cov" "a real number" } }
78 { $description "Computes the covariance of two sequences, " { $snippet "{x}" } " and " { $snippet "{y}" } "." } ;
81 { $values { "{x}" sequence } { "{y}" sequence } { "corr" "a real number" } }
82 { $description "Computes the correlation of two sequences, " { $snippet "{x}" } " and " { $snippet "{y}" } "." } ;
87 { "hashtable" hashtable }
89 { $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." }
91 { $example "! Count the number of times an element appears in a sequence."
92 "USING: prettyprint math.statistics ;"
93 "\"aaabc\" histogram ."
94 "H{ { 97 3 } { 98 1 } { 99 1 } }"
102 { "quot" { $quotation ( x -- bin ) } }
103 { "hashtable" hashtable }
105 { $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." }
107 { $unchecked-example "! Count the number of times letters and non-letters appear in a sequence."
108 "USING: prettyprint math.statistics unicode.categories ;"
109 "\"aaa123bc\" [ letter? ] histogram-by ."
110 "H{ { t 5 } { f 3 } }"
116 { "hashtable" hashtable } { "seq" sequence }
118 { $description "Takes an existing hashtable and uses " { $link histogram } " to continue counting the number of occurrences of each element." }
120 { $example "! Count the number of times the elements of two sequences appear."
121 "USING: prettyprint math.statistics ;"
122 "\"aaabc\" histogram \"aaaaaabc\" histogram! ."
123 "H{ { 97 9 } { 98 2 } { 99 2 } }"
127 HELP: sorted-histogram
130 { "alist" "an array of key/value pairs" }
132 { $description "Outputs a " { $link histogram } " of a sequence sorted by number of occurrences from lowest to highest." }
134 { $example "USING: prettyprint math.statistics ;"
135 """"abababbbbbbc" sorted-histogram ."""
136 "{ { 99 1 } { 97 3 } { 98 8 } }"
142 { "seq" sequence } { "map-quot" quotation } { "insert-quot" quotation } { "exemplar" "an exemplar assoc" }
145 { $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." }
147 { $example "! Iterate over a sequence and increment the count at each element"
148 "! The first quotation has stack effect ( key -- key ), a no-op"
149 "USING: assocs prettyprint kernel math.statistics ;"
150 "\"aaabc\" [ ] [ inc-at ] H{ } sequence>assoc ."
151 "H{ { 97 3 } { 98 1 } { 99 1 } }"
155 HELP: sequence>assoc!
157 { "assoc" assoc } { "seq" sequence } { "map-quot" quotation } { "insert-quot" quotation } }
158 { $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." }
160 { $example "! Iterate over a sequence and add the counts to an existing assoc"
161 "USING: assocs prettyprint math.statistics kernel ;"
162 "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ ] [ inc-at ] sequence>assoc! ."
163 "H{ { 97 5 } { 98 2 } { 99 1 } }"
167 HELP: sequence>hashtable
169 { "seq" sequence } { "map-quot" quotation } { "insert-quot" quotation }
170 { "hashtable" hashtable }
172 { $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." }
174 { $example "! Count the number of times an element occurs in a sequence"
175 "USING: assocs kernel prettyprint math.statistics ;"
176 "\"aaabc\" [ ] [ inc-at ] sequence>hashtable ."
177 "H{ { 97 3 } { 98 1 } { 99 1 } }"
182 { $values { "seq" sequence } { "seq'" sequence } }
183 { $description "Returns the cumulative sum of " { $snippet "seq" } "." }
185 { $example "USING: math.statistics prettyprint ;"
186 "{ 1 -1 2 -1 4 } cum-sum ."
192 { $values { "seq" sequence } { "quot" quotation } { "seq'" sequence } }
193 { $description "Returns the cumulative count of how many times " { $snippet "quot" } " returns true." }
195 { $example "USING: math math.statistics prettyprint ;"
196 "{ 1 -1 2 -1 4 } [ 0 < ] cum-count ."
203 { $values { "seq" sequence } { "seq'" sequence } }
204 { $description "Returns the cumulative product of " { $snippet "seq" } "." }
206 { $example "USING: math.statistics prettyprint ;"
207 "{ 1 2 3 4 } cum-product ."
213 { $values { "seq" sequence } { "seq'" sequence } }
214 { $description "Returns the cumulative mean of " { $snippet "seq" } "." }
216 { $example "USING: math.statistics prettyprint ;"
217 "{ 1.0 2.0 3.0 } cum-mean ."
223 { $values { "seq" sequence } { "seq'" sequence } }
224 { $description "Returns the cumulative min of " { $snippet "seq" } "." }
226 { $example "USING: math.statistics prettyprint ;"
227 "{ 5 3 4 1 } cum-min ."
233 { $values { "seq" sequence } { "seq'" sequence } }
234 { $description "Returns the cumulative max of " { $snippet "seq" } "." }
236 { $example "USING: math.statistics prettyprint ;"
237 "{ 1 -1 3 5 } cum-max ."
243 { $values { "u" sequence } { "v" sequence } }
244 { $description "Shifts and rescales the elements of " { $snippet "u" } " to have zero mean and unit sample variance." } ;
247 { $values { "u" sequence } { "v" sequence } }
248 { $description "Returns the successive differences of elements in " { $snippet "u" } "." } ;
251 { $values { "u" sequence } { "v" sequence } }
252 { $description "Returns " { $snippet "u" } " rescaled to run from 0 to 1 over the range min to max." } ;
256 { "seq" sequence } { "quot" { $quotation ( ... obj -- ... key ) } }
257 { "hashtable" hashtable }
259 { $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." }
261 "Collect even and odd elements:"
263 "USING: math math.statistics prettyprint ;"
264 "{ 11 12 13 14 14 13 12 11 } [ odd? ] collect-by ."
265 "H{ { t V{ 11 13 13 11 } } { f V{ 12 14 14 12 } } }"
268 { $notes "May be named " { $snippet "group-by" } " in other languages." } ;
270 HELP: collect-index-by
272 { "seq" sequence } { "quot" { $quotation ( ... obj -- ... key ) } }
273 { "hashtable" hashtable }
275 { $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." }
277 "Collect even and odd elements:"
279 "USING: math math.statistics prettyprint ;"
280 "{ 11 12 13 14 14 13 12 11 } [ odd? ] collect-index-by ."
281 "H{ { t V{ 0 2 5 7 } } { f V{ 1 3 4 6 } } }"
286 { $values { "seq" sequence } { "n" number } }
287 { $description "Calculates the Z-Score for " { $snippet "seq" } "." } ;
289 ARTICLE: "histogram" "Computing histograms"
290 "Counting elements in a sequence:"
297 "Combinators for implementing histogram:"
304 ARTICLE: "cumulative" "Computing cumulative sequences"
305 "Cumulative mapping combinators:"
315 "Cumulative comparisons:"
320 "Cumulative counting:"
325 ARTICLE: "math.statistics" "Statistics"
326 "Computing the mean:"
327 { $subsections mean geometric-mean harmonic-mean }
328 "Computing the median:"
329 { $subsections median lower-median upper-median medians }
330 "Computing the mode:"
331 { $subsections mode }
332 "Computing the population standard deviation, standard error, and variance:"
333 { $subsections population-std population-ste population-var }
334 "Computing the sample standard deviation, standard error, and variance:"
335 { $subsections sample-std sample-ste sample-var }
336 "Computing the nth delta-degrees-of-freedom statistics:"
337 { $subsections std-ddof ste-ddof var-ddof }
338 "Computing the range and minimum and maximum elements:"
339 { $subsections range minmax }
340 "Computing the kth smallest element:"
341 { $subsections kth-smallest }
342 "Counting the frequency of occurrence of elements:"
343 { $subsections "histogram" }
344 "Collecting related items:"
345 { $subsections collect-by collect-index-by }
346 "Computing cumulative sequences:"
347 { $subsections "cumulative" } ;
349 ABOUT: "math.statistics"
351 { var-ddof population-var sample-var } related-words
352 { std-ddof population-std sample-std } related-words
353 { ste-ddof population-ste sample-ste } related-words
354 { corr-ddof population-corr sample-corr } related-words
355 { cov-ddof population-cov sample-cov } related-words