- Statistics
- Statistical Distributions
- Provided Functions
- Authors
- Repository
- License
- Version History
Statistics
This library is a port of Larry Hunter's Lisp statistics library to CHICKEN Scheme.
The library provides a number of formulae and methods taken from the book "Fundamentals of Biostatistics" by Bernard Rosner (5th edition).
Statistical Distributions
To use this library, you need to understand the underlying statistics. In brief:
The Binomial distribution is used when counting discrete events in a series of trials, each of which events has a probability p of producing a positive outcome. An example would be tossing a coin n times: the probability of a head is p, and the distribution gives the expected number of heads in the n trials. The binomial distribution is defined as B(n, p).
The Poisson distribution is used to count discrete events which occur with a known average rate. A typical example is the decay of radioactive elements. A poisson distribution is defined Pois(mu).
The Normal distribution is used for real-valued events which cluster around a specific mean with a symmetric variance. A typical example would be the distribution of people's heights. A normal distribution is defined N(mean, variance).
Provided Functions
Most procedures that take a sequence accept a list, a vector, a SRFI-4 numeric vector such as an f64vector, or any other collection of the yasos egg's yasos-collections module; numeric vectors need yasos 1.16 or later. The exceptions are chi-square-test-rxc, which takes a SRFI-63 array, and chi-square-test-for-trend, which takes lists.
Utilities
[procedure] (average-rank value sorted-values)returns the average position of given value in the list of sorted values: the rank is based from 1.
> (average-rank 2 '(1 2 2 3 4)) 5/2[procedure] (beta-incomplete x a b)
[procedure] (bin-and-count items n)
Divides the range of the list of items into n bins, and returns a vector of the number of items which fall into each bin.
> (bin-and-count '(1 1 2 3 3 4 5) 5) #(2 1 2 1 1)[procedure] (combinations n k)
returns the number of ways to select k items from n, where the order does not matter.
[procedure] (factorial n)returns the factorial of n.
[procedure] (find-critical-value p-function p-value #:increasing?)given a monotonic function p-function taking a single value x to y, returns the value of x which makes (p-function x) closest to p-value. A boolean keyword parameter #:increasing? determines if function should be increasing or decreasing (the default).
[procedure] (fisher-z-transform r)returns the transformation of a correlation coefficient r into an approximately normal distribution.
[procedure] (gamma-incomplete a x)[procedure] (gamma-ln x)
[procedure] (permutations n k)
returns the number of ways to select k items from n, where the order does matter.
[procedure] (random-normal mean sd)returns a random number distributed with specified mean and standard deviation.
[procedure] (random-sample n items)returns a random sample of size n from items, without replacement, as a vector. Every subset of size n is equally likely. If n is at least the number of items, all of them are returned.
[procedure] (random-weighted-sample n items weights)returns a random sample from the list of items without replacement of size n, where each sample has a defined probability of selection (weight).
[procedure] (sign n)returns 0, 1 or -1 according to if n is zero, positive or negative.
[procedure] (square n)[procedure] (cumsum sequences)
returns the cumulative sum of a sequence.
[procedure] (outliers sequence #:coef)Identifies outliers using Tukey's method. Values beyond #:coef times the IQR from Q1/Q3 are considered outliers. Returns two values: a list of outlier values and a list of non-outlier values (clean sequence). The #:coef parameter defaults to 1.5 (standard outliers); use 3.0 for extreme outliers.
> (outliers '(10 12 14 15 16 18 20 100) #:coef 1.5) (100) (10 12 14 15 16 18 20) ; 2 values[procedure] (bin-means x-sequence y-sequence n)
Computes the mean of y-sequence values within each of n bins of x-sequence values. Returns a list of (bin-center . mean-y) pairs.
> (bin-means '(1 2 3 4 5 6 7 8 9 10)
'(10 20 30 40 50 60 70 80 90 100)
2)
((3.0 . 30.0) (8.0 . 80.0))[procedure] (bin-sds x-sequence y-sequence n)
Computes the standard deviation of y-sequence values within each of n bins of x-sequence values. Returns a list of (bin-center . sd-y) pairs.
> (bin-sds '(1 2 3 4 5 6 7 8 9 10)
'(10 20 30 40 50 60 70 80 90 100)
2)
((3.0 . 15.811388300841896) (8.0 . 15.811388300841896))
Descriptive statistics
These functions provide information on a given list of numbers, the items. Note, the list does not have to be sorted.
[procedure] (mean items)returns the arithmetic mean of the items (the sum of the numbers divided by the number of numbers).
(mean '(1 2 3 4 5)) => 3[procedure] (median items)
returns the value which separates the upper and lower halves of the list of numbers.
(median '(1 2 3 4)) => 5/2[procedure] (mode items)
returns two values. The first is a list of the modes and the second is the frequency. (A mode of a list of numbers is the most frequently occurring value.)
> (mode '(1 2 3 4)) (1 2 3 4) 1 > (mode '(1 2 2 3 4)) (2) 2 > (mode '(1 2 2 3 3 4)) (2 3) 2[procedure] (geometric-mean items)
returns the geometric mean of the items (the result of multiplying the items together and then taking the nth root, where n is the number of items).
(geometric-mean '(1 2 3 4 5)) => 2.60517108469735[procedure] (range items)
returns the difference between the biggest and the smallest value from the list of items.
(range '(5 1 2 3 4)) => 4[procedure] (percentile items percent)
returns the item closest to the percent value if the items are sorted into order; the returned item may be in the list, or the average of adjacent values.
(percentile '(1 2 3 4) 50) => 5/2 (percentile '(1 2 3 4) 67) => 3[procedure] (variance items)
[procedure] (standard-deviation items)
[procedure] (coefficient-of-variation items)
returns 100 * (std-dev / mean) of the items.
(coefficient-of-variation '(1 2 3 4)) => 51.6397779494322[procedure] (standard-error-of-the-mean items)
returns std-dev / sqrt(length items).
(standard-error-of-the-mean '(1 2 3 4)) => 0.645497224367903[procedure] (mean-sd-n items)
returns three values, one for the mean, one for the standard deviation, and one for the length of the list.
> (mean-sd-n '(1 2 3 4)) 5/2 1.29099444873581 4[procedure] (iqr sequence)
Computes the interquartile range (IQR = Q3 - Q1) of the given sequence. The IQR is a robust measure of statistical dispersion. Requires at least 4 elements.
> (iqr '(1 2 3 4 5 6 7 8 9 10)) 4.5[procedure] (fivenum sequence)
Computes Tukey's five-number summary: (min Q1 median Q3 max). This is the basis for box plots. Requires at least 5 elements.
> (fivenum '(1 2 3 4 5 6 7 8 9 10)) (1 3.0 5.5 8.0 10)[procedure] (skewness sequence)
Returns the bias-adjusted sample skewness G1 = sqrt(n (n - 1)) / (n - 2) * m3 / m2^(3/2), where m2 and m3 are the second and third central moments (Joanes and Gill, 1998). It is 0 for a symmetric sample and positive when the right tail is longer. This is the skewness that Excel, SAS and SPSS report. Needs at least three values, not all equal.
> (skewness '(2 4 4 5 7 9 10 15)) 0.919724445119016[procedure] (kurtosis sequence)
Returns the bias-adjusted sample excess kurtosis G2 = ((n + 1) g2 + 6) (n - 1) / ((n - 2) (n - 3)), with g2 = m4 / m2^2 - 3. It is near 0 for a sample from a normal distribution and positive when the tails are heavier. This is the kurtosis that Excel, SAS and SPSS report. Needs at least four values, not all equal.
> (kurtosis '(2 4 4 5 7 9 10 15)) 0.558688865764828[procedure] (covariance xs ys)
Returns the sample covariance of the paired values xs and ys: sum (x - mean x) (y - mean y) / (n - 1). The two sequences must have the same size, at least two. Exact inputs give an exact result.
> (covariance '(2 4 4 5 7 9 10 15) '(1 3 2 5 6 8 11 13)) 123/7
Distributional functions
[procedure] (binomial-probability n k p)returns the probability that the number of positive outcomes for a binomial distribution B(n, p) is k.
> (do-ec (: i 0 11)
(format #t "i = ~d P = ~f~&" i (binomial-probability 10 i 0.5)))
i = 0 P = 0.0009765625
i = 1 P = 0.009765625
i = 2 P = 0.0439453125
i = 3 P = 0.1171875
i = 4 P = 0.205078125
i = 5 P = 0.24609375
i = 6 P = 0.205078125
i = 7 P = 0.1171875
i = 8 P = 0.0439453125
i = 9 P = 0.009765625
i = 10 P = 0.0009765625[procedure] (binomial-cumulative-probability n k p)
returns the probability that less than k positive outcomes occur for a binomial distribution B(n, p).
> (do-ec (: i 0 11)
(format #t "i = ~d P = ~f~&" i (binomial-cumulative-probability 10 i 0.5)))
i = 0 P = 0.0
i = 1 P = 0.0009765625
i = 2 P = 0.0107421875
i = 3 P = 0.0546875
i = 4 P = 0.171875
i = 5 P = 0.376953125
i = 6 P = 0.623046875
i = 7 P = 0.828125
i = 8 P = 0.9453125
i = 9 P = 0.9892578125
i = 10 P = 0.9990234375[procedure] (binomial-ge-probability n k p)
returns the probability of k or more positive outcomes for a binomial distribution B(n, p).
[procedure] (binomial-le-probability n k p)returns the probability k or fewer positive outcomes for a binomial distribution B(n, p).
[procedure] (poisson-probability mu k)returns the probability of k events occurring when the average is mu.
> (do-ec (: i 0 20)
(format #t "P(X=~2d) = ~,4f~&" i (poisson-probability 10 i)))
P(X= 0) = 0.0000
P(X= 1) = 0.0005
P(X= 2) = 0.0023
P(X= 3) = 0.0076
P(X= 4) = 0.0189
P(X= 5) = 0.0378
P(X= 6) = 0.0631
P(X= 7) = 0.0901
P(X= 8) = 0.1126
P(X= 9) = 0.1251
P(X=10) = 0.1251
P(X=11) = 0.1137
P(X=12) = 0.0948
P(X=13) = 0.0729
P(X=14) = 0.0521
P(X=15) = 0.0347
P(X=16) = 0.0217
P(X=17) = 0.0128
P(X=18) = 0.0071
P(X=19) = 0.0037[procedure] (poisson-cumulative-probability mu k)
returns the probability of less than k events occurring when the average is mu.
> (do-ec (: i 0 20)
(format #t "P(X=~2d) = ~,4f~&" i (poisson-cumulative-probability 10 i)))
P(X= 0) = 0.0000
P(X= 1) = 0.0000
P(X= 2) = 0.0005
P(X= 3) = 0.0028
P(X= 4) = 0.0103
P(X= 5) = 0.0293
P(X= 6) = 0.0671
P(X= 7) = 0.1301
P(X= 8) = 0.2202
P(X= 9) = 0.3328
P(X=10) = 0.4579
P(X=11) = 0.5830
P(X=12) = 0.6968
P(X=13) = 0.7916
P(X=14) = 0.8645
P(X=15) = 0.9165
P(X=16) = 0.9513
P(X=17) = 0.9730
P(X=18) = 0.9857
P(X=19) = 0.9928[procedure] (poisson-ge-probability mu k)
returns the probability of k or more events occurring when the average is mu.
[procedure] (normal-pdf x mean variance)returns the likelihood of x given a normal distribution with stated mean and variance.
> (do-ec (: i 0 11)
(format #t "~3d ~,4f~&" i (normal-pdf i 5 4)))
0 0.0088
1 0.0270
2 0.0648
3 0.1210
4 0.1760
5 0.1995
6 0.1760
7 0.1210
8 0.0648
9 0.0270
10 0.0088[procedure] (convert-to-standard-normal x mean variance)
returns a value for x rescaling the given normal distribution to a standard N(0, 1).
> (convert-to-standard-normal 5 6 2) -1/2[procedure] (phi x)
returns the cumulative distribution function (CDF) of the standard normal distribution.
> (do-ec (: x -2 2 0.4)
(format #t "~4,1f ~,4f~&" x (phi x)))
-2.0 0.0228
-1.6 0.0548
-1.2 0.1151
-0.8 0.2119
-0.4 0.3446
0.0 0.5000
0.4 0.6554
0.8 0.7881
1.2 0.8849
1.6 0.9452[procedure] (z percentile)
returns the inverse of the standard normal distribution. Input is a percentile, between 0.0 and 1.0.
[procedure] ( t-distribution degrees-of-freedom percentile)returns the point in the t-distribution given the degrees-of-freedom and percentile. degrees-of-freedom must be a positive integer, and percentile a value between 0.0 and 1.0.
[procedure] (chi-square degrees-of-freedom percentile)returns the point at which chi-square distribution has percentile to its left, using given degrees-of-freedom.
[procedure] (chi-square-cdf x degrees-of-freedom)returns the probability that a random variable is to the left of x using the chi-square distribution with given degrees-of-freedom.
Histogram and Density Estimation
These functions support the creation of histograms and kernel density estimates.
[procedure] (histogram-breaks sequence n #:method)Computes histogram bin edges using various methods. Returns a vector of n+1 breakpoints (edges). The #:method keyword determines the binning strategy:
- 'equal-width - Equal width bins (default for specified n)
- 'sturges - Sturges' formula: 1 + log2(n) bins
- 'scott - Scott's normal reference rule
- 'fd - Freedman-Diaconis rule: 2*IQR/n^(1/3) (robust to outliers)
> (histogram-breaks '(1 2 3 4 5 6 7 8 9 10) 5 #:method 'equal-width) #(1.0 2.8 4.6 6.4 8.2 10.0) > (histogram-breaks '(1 2 3 4 5 6 7 8 9 10) 10 #:method 'sturges) #(1.0 2.25 3.5 4.75 6.0 7.25 8.5 9.75)[procedure] (histogram sequence breaks)
Computes histogram counts given breakpoints. Returns two values: the bin edges (same as input breaks) and a vector of counts for each bin.
> (let ((breaks (vector 0.5 1.5 2.5 3.5 4.5)))
(histogram '(1 1 2 2 2 3 3 3 3 4) breaks))
#(0.5 1.5 2.5 3.5 4.5)
#(2 3 4 1)
; 2 values[procedure] (kde sequence bandwidth kernel #:n)
Computes kernel density estimate for the given sequence using specified bandwidth (smoothing parameter) and kernel function. Returns a list of (x . density) pairs with n evaluation points (default 512).
Available kernels:
- 'gaussian or 'normal - Gaussian kernel (most common)
- 'epanechnikov - Epanechnikov kernel (most efficient)
- 'rectangular or 'uniform - Rectangular kernel
- 'triangular - Triangular kernel
- 'biweight or 'quartic - Biweight kernel
> (let* ((data '(1 2 3 4 5))
(bw (kde-bandwidth-nrd data))
(density (kde data bw 'gaussian 10)))
(length density))
10
> (kde '(1 2 3) 1.0 'gaussian 5)
((-2.0 . 0.006649110640673518)
(-0.75 . 0.07266464304354425)
(0.5 . 0.25725941161130127)
(1.75 . 0.2968430843637567)
(3.0 . 0.1123450687407796))[procedure] (kde-bandwidth-nrd sequence)
Computes bandwidth using Silverman's normal reference distribution rule of thumb: h = 0.9 * min(sd, IQR/1.34) * n^(-1/5)
This is a good default choice for Gaussian-like distributions.
> (kde-bandwidth-nrd '(1 2 3 4 5 6 7 8 9 10)) 1.3185617394731743[procedure] (kde-bandwidth-scott sequence)
Computes bandwidth using Scott's normal reference rule: h = 1.06 * sd * n^(-1/5)
> (kde-bandwidth-scott '(1 2 3 4 5 6 7 8 9 10)) 1.2850170567715422[procedure] (kde-bandwidth-fd sequence)
Computes bandwidth using the Freedman-Diaconis rule: h = 2 * IQR * n^(-1/3)
This method is more robust to outliers than the normal reference rules.
> (kde-bandwidth-fd '(1 2 3 4 5 6 7 8 9 10)) 4.179339196872841
Confidence intervals
These functions report bounds for an observed property of a distribution: the bounds are tighter as the confidence level, alpha, varies from 0.0 to 1.0.
[procedure] (binomial-probability-ci n p alpha)returns two values, the upper and lower bounds on an observed probability p from n trials with confidence (1-alpha).
> (binomial-probability-ci 10 0.8 0.9) 0.724273681640625 0.851547241210938 ; 2 values[procedure] (poisson-mu-ci k alpha)
returns two values, the upper and lower bounds on the poisson parameter if k events are observed; the bound is for confidence (1-alpha).
> (poisson-mu-ci 10 0.9) 8.305419921875 10.0635986328125 ; 2 values[procedure] (normal-mean-ci mean standard-deviation k alpha)
returns two values, the upper and lower bounds on the mean of the normal distibution of k events are observed; the bound is for confidence (1-alpha).
> (normal-mean-ci 0.5 0.1 10 0.8) 0.491747852700165 0.508252147299835 ; 2 values[procedure] (normal-mean-ci-on-sequence items alpha)
returns two values, the upper and lower bounds on the mean of the given items, assuming they are normally distributed; the bound is for confidence (1-alpha).
> (normal-mean-ci-on-sequence '(1 2 3 4 5) 0.9) 2.90535368828478 3.09464631171522 ; 2 values[procedure] (normal-variance-ci variance n alpha)
returns two values, the upper and lower bounds on the variance of a normal distribution given an observed variance from a sample of size n; the bound is for confidence (1-alpha).
> (normal-variance-ci 2.5 10 0.1) 6.76651982378855 1.32987012987013 ; 2 values[procedure] (normal-variance-ci-on-sequence items alpha)
returns two values, the upper and lower bounds on the variance of the given items, assuming they are normally distributed; the bound is for confidence (1-alpha).
[procedure] (normal-sd-ci sd n alpha)returns two values, the upper and lower bounds on the standard deviation of a normal distribution given an observed standard deviation sd from a sample of size n; the bound is for confidence (1-alpha).
[procedure] (normal-sd-ci-on-sequence items alpha)returns two values, the upper and lower bounds on the standard deviation of the given items, assuming they are normally distributed; the bound is for confidence (1-alpha).
Hypothesis testing
These functions report on the significance of an observed sample against a given distribution.
(parametric)
[procedure] (z-test x-bar n #:mu #:sigma #:tails)Given x-bar the sample mean, n the number in the sample, #:mu the distribution mean (defaults to 0), #:sigma the distribution standard deviation (defaults to 1), and #:tails the significance to report on:
- ':both, the probability of the difference between x-bar and #:mu
- ':positive, the probability that observation is >= x-bar
- ':negative, the probability that observation is <= x-bar
e.g. given a distribution with mean 50 and standard deviation 10
; probability that a single observation is <= 40 > (z-test 40 1 #:mu 50 #:sigma 10 #:tails ':negative) 0.158655 ; probability that 10 observations are <= 40 > (z-test 40 10 #:mu 50 #:sigma 10 #:tails ':negative) 0.000783 ; probability that 5 observations give a mean of 40 > (z-test 40 5 #:mu 50 #:sigma 10) 0.025347[procedure] (z-test-on-sequence observations #:mu #:sigma #:tails)
As for z-test except x-bar and n are computed from given observations.
[procedure] (t-test-one-sample x-bar sd n mu #:tails)Given observed data with mean x-bar, standard devation sd and number of observations n (n < 30), returns the significance of the sample compared with the population mean mu. #:tails is one of:
- ':both two-sided (default)
- ':positive one-sided, x-bar >= mu
- ':negative one-sided, x-bar <= mu
As for t-test-one-sample except x-bar, sd and n are computed from given observations.
[procedure] (t-test-paired t-bar sd n #:tails)Computes the significance of the differences between two sequences of data: the differences are given as their mean, t-bar, standard deviation, sd, and number of measurements, n.
[procedure] (t-test-paired-on-sequences before after #:tails)Computes the significance of the difference between two sequences of data: one before an experimental change and one after. #:tails is as for t-significance.
> (t-test-paired-on-sequences '(4 3 5) '(1 1 3)) 0.0198039411803931[procedure] (t-test-two-sample mean-1 sd-1 n-1 mean-2 sd-2 n-2 #:variances-equal? #:variance-significance-cutoff #:tails)
Computes the significance of the difference of two means given the sample standard deviations and sizes.
[procedure] (t-test-two-sample-on-sequences sequence-1 sequence-2 #:tails)Significance of difference of two sequences of observations.
[procedure] (f-test variance-1 n1 variance-2 n2 #:tails)Tests for the equality of two variances.
[procedure] (chi-square-test-one-sample observed-variance sample-size test-variance #:tails)Tests for significance of difference between an observed and a test variance.
[procedure] (binomial-test-one-sample p-hat n p #:tails #:exact?)Returns the significance of a one sample test with n observations, observed probability p-hat and expected probability p.
[procedure] (binomial-test-two-sample p-hat-1 n-1 p-hat-2 n-2 #:tails #:exact?)Returns the significance of a two sample test.
[procedure] (fisher-exact-test a b c d #:tails)Given a 2x2 contingency table, returns a p value using Fisher's exact test. a and b form the first row of the contingency table, c and d the second row.
[procedure] (mcnemars-test a-discordant-count b-discordant-count #:exact?)For measuring effectiveness of, e.g., one treatment over another. a-discordant-count is the number of times when A worked, b-discordant-count the number of times B worked.
[procedure] (poisson-test-one-sample observed mu #:tails #:approximate?)Computes significance of the number of observed events under a Poisson distribution against mu expected events.
(non parametric)
[procedure] (sign-test plus-count minus-count #:exact? #:tails)[procedure] (sign-test-on-sequence sequence-1 sequence-2 #:exact? #:tails)
Takes two equal-sized sequences of observations, and reports if the entries of one are different to those in the other.
[procedure] (wilcoxon-signed-rank-test differences #:tails)Given at least 16 differences, reports if the positive differences are significantly larger or smaller than the negative differences.
[procedure] (wilcoxon-signed-rank-test-on-sequences sequence-1 sequence-2 #:tails)Given two sequences of at least 16 observations, computes wilcoxon-signed-rank-test on the differences.
[procedure] (chi-square-test-rxc contingency-table)Given a contingency table (a SRFI-63 array), returns significance of relation between row and column variable.
[procedure] (chi-square-test-for-trend row1-counts row2-counts)Returns p significance of trend, and prints a string to show if increasing or decreasing.
Analysis of variance
These procedures compare more than two groups. A group, or the collection of groups, can be any sequence: a list, a vector or another collection. Each returns several values.
[procedure] (one-way-anova means sds ns)One-way analysis of variance from group summaries: tests whether the means of k groups are all equal, assuming normal values with the same variance in every group. means, sds and ns hold the mean, sample standard deviation and size of each group. With N values in all, F is the between-group mean square, on k - 1 degrees of freedom, divided by the within-group mean square, on N - k. Returns four values: F, its significance p (the upper tail of the F distribution), k - 1 and N - k.
> (one-way-anova '(2 5.5 5.5) '(1.0 1.29 4.95) '(3 4 2)) 2.33371858211514 0.177939952801275 2 6[procedure] (one-way-anova-on-sequences groups)
As one-way-anova, but groups holds the samples themselves. A group may hold a single value; no group may be empty. The example uses the PlantGrowth data set of R and gives the same result as aov there.
> (define ctrl '(4.17 5.58 5.18 6.11 4.50 4.61 5.17 4.53 5.33 5.14)) > (define trt1 '(4.81 4.17 4.41 3.59 5.87 3.83 6.03 4.89 4.32 4.69)) > (define trt2 '(6.31 5.12 5.54 5.50 5.37 5.29 4.92 6.15 5.80 5.26)) > (one-way-anova-on-sequences (list ctrl trt1 trt2)) 4.84608786238014 0.0159099583059651 2 27[procedure] (two-way-anova table)
Two-way analysis of variance for two crossed factors A and B in a balanced design. table has one element for each level of A; each of these has one cell for each level of B; and each cell holds the r values observed at that pair of levels. Every cell must hold the same number of values.
Returns three values, for A, for B and for their interaction, each a list (F p df df-error). With r of 2 or more, the error term is the within-cell sum of squares on ab(r - 1) degrees of freedom. With r = 1 the interaction cannot be told apart from error: the model is additive, the interaction sum of squares serves as the error term on (a - 1)(b - 1) degrees of freedom, and the third value is #f.
> (two-way-anova '(((12 14 13) (18 20 19) (15 14 16))
((9 10 11) (22 24 23) (12 11 13))))
(2.0 0.182716762688545 1 12)
(150.5 3.17556824919085e-09 2 12)
(24.5 5.79573428394943e-05 2 12)[procedure] (kruskal-wallis-test groups)
The Kruskal-Wallis test: the rank-based counterpart of one-way-anova-on-sequences, which tests whether k groups come from the same distribution without assuming one. All values are ranked together, ties sharing the mean rank, and the statistic H is corrected for ties. Returns three values: H, its significance p from the chi-square distribution on k - 1 degrees of freedom, and k - 1. The approximation needs about five values per group to be reliable. With the PlantGrowth groups above, the result is the same as that of kruskal.test in R:
> (kruskal-wallis-test (list ctrl trt1 trt2)) 7.9882287494437 0.018423755731472 2
Sample size estimates
[procedure] (t-test-one-sample-sse mean-1 mean-2 sigma-1 #:alpha #:1-beta #:tails)Returns the size of sample necessary to distinguish a normally distributed sample with mean-2 from a population mean-1 standard deviation sigma-1. The significance #:alpha (defaults to 0.05), power #:1-beta (0.95) and sides #:tails (':both) may be altered.
> (t-test-one-sample-sse 5.0 5.2 0.5) 163[procedure] (t-test-two-sample-sse mean-1 sigma-1 mean-2 sigma-2 #:alpha #:1-beta #:tails #:sample-ratio)
Returns the size of sample necessary to distinguish a normally distributed sample N(mean-1, sigma-1) from a normally distributed sample N(mean-2, sigma-2). The significance #:alpha (defaults to 0.05), power #:1-beta (0.95), sides #:tails (':both) and sample-ratio #:sample-ratio (1) may be altered.
[procedure] (t-test-paired-sse difference-mean difference-sigma #:alpha #:1-beta #:tails)Returns the size of sample to produce a given mean and standard deviation in the differences of two samples.
[procedure] (binomial-test-one-sample-sse p-estimated p-null #:alpha #:1-beta #:tails)Returns the size of sample needed to test whether an observed probability is significantly different from a particular binomial null hypothesis with a significance alpha and a power 1-beta.
[procedure] (binomial-test-two-sample-sse p-one p-two #:alpha #:1-beta #:tails #:sample-ratio)Returns the size of sample needed to test if given two binomial probabilities are significantly different. #:sample-ratio can be given if the two samples differ in size.
[procedure] (binomial-test-paired-sse pd pa #:alpha #:1-beta #:tails)Sample size estimate for McNemar's discordant pairs test.
[procedure] (correlation-sse rho #:alpha #:1-beta)Returns the size of sample necessary to find a correlation of value rho with significance #:alpha (defaults to 0.05) and power #:1-beta (defaults to 0.95).
> (correlation-sse 0.80 #:alpha 0.05 #:1-beta 0.9) 11
Correlation and regression
[procedure] (linear-regression xs ys #:print?)Given a line definition as lists of point coordinates, first prints to the terminal and then returns 5 values for the best fitting line through the points (with #:print? #f it only returns them):
- the y-intercept
- the slope
- the correlation coefficient, r
- the square of the correlation coefficient, r^2
- the significance of the difference of the slope from zero, p
(This is also called the Pearson correlation; used when relation expected to be linear. Also see spearman-rank-correlation.)
> (linear-regression '(1.0 2.0 3.0) '(0.1 0.3 0.8)) Intercept = -0.3, slope = 0.35, r = 0.970725343394151, R^2 = 0.942307692307692, p = 0.154420958311267 -0.3 0.35 0.970725343394151 0.942307692307692 0.154420958311267 ; 5 values[procedure] (correlation-coefficient xs ys)
As above, but only returns the value of r:
> (correlation-coefficient '(1.0 2.0 3.0) '(0.1 0.3 0.8)) 0.970725343394151[procedure] (correlation-test-two-sample r1 n1 r2 n2 #:tails)
Returns the significance of the similarity between two correlations. #:tails determines how the comparison is made: ':both measures the difference, ':negative if r1 < r2 and #':positive if r2 > r1.
[procedure] (correlation-test-two-sample-on-sequences xs1 ys1 xs2 ys2 #:tails)As above, but computes the two correlations directly from two pairs of sequences: xs1 and ys1 for the first sample, xs2 and ys2 for the second.
> (correlation-test-two-sample-on-sequences
'(1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
'(1.2 2.1 2.9 4.3 4.8 6.1 7.2 7.8 9.1 10.2)
'(1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0)
'(2.0 2.5 2.2 3.8 3.5 5.0 4.8 6.0))
0.00464138365448497[procedure] (spearman-rank-correlation xs ys #:print?)
Prints and returns two values, the Spearman Rank measure of correlation between the given lists of point coordinates, and the p-significance of the correlation. With #:print? #f the values are only returned. (This correlation is used for non-linear relations; compare with linear-regression.)
Ranks and classification
These procedures take lists or vectors. Labels are booleans, #t marking a positive case; scores and probabilities are real numbers, a higher score meaning a more likely positive.
[procedure] (ranks sequence)Returns the 1-based ranks of the elements of sequence, in their original order. Tied values share the mean of the ranks they occupy. Sorts once, so it takes O(n log n) time.
> (ranks '(10 20 20 30)) (1 5/2 5/2 4)[procedure] (mann-whitney-u-test xs ys #:tails)
The Mann-Whitney U test (Wilcoxon rank-sum test): do the values of xs tend to be larger or smaller than those of ys? Returns two values: U, the number of pairs with x > y plus half the number of ties, and the significance p. The significance uses the normal approximation with a correction for ties and a continuity correction, reliable from about ten values per sample. #:tails is ':both (the default), ':positive (xs larger) or ':negative (xs smaller).
> (mann-whitney-u-test '(3 5 7 9) '(1 2 4 6 8)) 14 0.391267279282639[procedure] (roc-auc scores labels)
The area under the receiver operating characteristic curve: the probability that a random positive case scores higher than a random negative one, ties counting one half. It equals U / (n1 n2) of mann-whitney-u-test on the positive and negative scores. Returns #f when either class is empty.
> (roc-auc '(0.9 0.5 0.5 0.3) '(#t #t #f #f)) 7/8[procedure] (roc-auc-ci scores labels #:alpha)
Returns three values: the AUC and the bounds of its 1 - #:alpha confidence interval (#:alpha defaults to 0.05), using the standard error of Hanley and McNeil (1982) and clamping the interval to [0, 1]. All three are #f when either class is empty.
[procedure] (roc-curve scores labels)The ROC curve as a list of (threshold false-positive-rate true-positive-rate): a first point (+inf.0 0 0), then one point for each distinct score taken as threshold, from the highest down (a score at or above the threshold counts as positive). The last point is (1 1). #f when either class is empty.
[procedure] (confusion-counts predicted actual)For boolean sequences of equal length, returns four values: the numbers of true positives, false positives, false negatives and true negatives.
[procedure] (precision predicted actual)[procedure] (recall predicted actual)
[procedure] (f1-score predicted actual)
Precision TP / (TP + FP), recall TP / (TP + FN), and their harmonic mean 2 TP / (2 TP + FP + FN). Each returns #f when its denominator is zero.
> (precision '(#t #t #f #f #t #f) '(#t #f #f #t #t #f)) 2/3[procedure] (calibration-table probabilities labels #:bins)
Compares predicted probabilities with outcomes in #:bins (default 10) equal bins of [0, 1]. Returns, for each bin that holds a prediction, (low high count mean-probability observed-fraction). A calibrated predictor has the mean probability close to the observed fraction of positives in every bin.
> (calibration-table '(0.05 0.15 0.12 0.95 1.0) '(#f #f #t #t #t)) ((0 1/10 1 0.05 0) (1/10 1/5 2 0.135 1/2) (9/10 1 2 0.975 1))[procedure] (brier-score probabilities labels)
The mean squared difference between the probabilities and the outcomes (1 for a positive label, 0 otherwise).
[procedure] (expected-calibration-error probabilities labels #:bins)The sum over the bins of calibration-table of (count / n) times the absolute difference between the mean probability and the observed fraction.
Effect sizes
These procedures measure the difference between the means of two independent groups in units of standard deviation. Each returns three values: the estimate and the lower and upper bounds of its 1 - #:alpha confidence interval (#:alpha defaults to 0.05). The intervals use the normal approximation of Hedges and Olkin (1985), which needs about ten values in each group to be reliable. Each group must have at least two values, and the pooled standard deviation must not be zero; otherwise an error is signalled.
[procedure] (cohens-d mean-1 sd-1 n-1 mean-2 sd-2 n-2 #:alpha)Cohen's d: the difference mean-1 - mean-2 divided by the pooled standard deviation sqrt(((n1 - 1) sd1^2 + (n2 - 1) sd2^2) / (n1 + n2 - 2)). The arguments are the sample means, sample standard deviations and sizes of the two groups, in the same order as for t-test-two-sample. The interval is d -+ z SE, with SE = sqrt((n1 + n2) / (n1 n2) + d^2 / (2 (n1 + n2))).
> (cohens-d 10 2 20 8 2 20) 1.0 0.34260809456756 1.65739190543244[procedure] (cohens-d-on-sequences sequence-1 sequence-2 #:alpha)
As cohens-d, but computes the means, standard deviations and sizes from the two samples, given as lists or vectors.
> (cohens-d-on-sequences '(5 7 8 6 9 7) '(4 5 3 6 5 4 3)) 2.15635871412464 0.786674150877201 3.52604327737208[procedure] (hedges-g mean-1 sd-1 n-1 mean-2 sd-2 n-2 #:alpha)
Hedges' g: Cohen's d multiplied by the correction factor J = Gamma(df / 2) / (sqrt(df / 2) Gamma((df - 1) / 2)), with df = n1 + n2 - 2. J is a little below 1 and removes the upward bias of d in small samples. The bounds of the interval are those of d multiplied by J.
> (hedges-g 10 2 20 8 2 20) 0.980110402130947 0.335793757339929 1.62442704692197[procedure] (hedges-g-on-sequences sequence-1 sequence-2 #:alpha)
As hedges-g, but computes the summaries from the two samples, given as lists or vectors.
Multiple comparisons
These procedures take a list or vector of p-values, each in [0, 1]; a value outside that range signals an error.
[procedure] (holm-adjust p-values)Returns the Holm (1979) adjusted p-values as a list, in the order of the input; the result is the same as that of p.adjust(p, "holm") in R. With m p-values sorted from smallest to largest, the k-th one is multiplied by m - k + 1 and capped at 1, and a running maximum then keeps the adjusted values in the same order as the raw ones. Rejecting the hypotheses whose adjusted p-value is at most alpha controls the family-wise error rate at alpha, and never rejects fewer hypotheses than the Bonferroni correction. Exact inputs give exact results.
> (holm-adjust '(1/100 4/100 3/100 5/1000)) (3/100 3/50 3/50 1/50)[procedure] (holm-reject p-values #:alpha)
Returns a list of booleans in the order of the input: #t for each hypothesis that the Holm procedure rejects at level #:alpha (default 0.05), that is, whose adjusted p-value is at most #:alpha.
> (holm-reject '(0.01 0.04 0.03 0.005)) (#t #f #f #t)[procedure] (benjamini-hochberg-adjust p-values)
Returns the Benjamini-Hochberg (1995) adjusted p-values as a list, in the order of the input; the result is the same as that of p.adjust(p, "BH") in R. With m p-values sorted from smallest to largest, the k-th one is multiplied by m / k and capped at 1, and a running minimum taken from the largest down then keeps the adjusted values in the same order as the raw ones. Rejecting the hypotheses whose adjusted p-value is at most alpha keeps the false discovery rate (the expected share of false rejections among all rejections) at or below alpha for independent tests. It rejects at least as many hypotheses as the Holm procedure. Exact inputs give exact results.
> (benjamini-hochberg-adjust '(1/100 4/100 3/100 5/1000)) (1/50 1/25 1/25 1/50)[procedure] (benjamini-hochberg-reject p-values #:alpha)
Returns a list of booleans in the order of the input: #t for each hypothesis that the Benjamini-Hochberg procedure rejects at false discovery rate #:alpha (default 0.05).
> (benjamini-hochberg-reject '(0.01 0.04 0.03 0.005) #:alpha 0.03) (#t #f #f #t)
Significance test functions
[procedure] (t-significance t-value degrees-of-freedom #:tails)returns the probability of t-value for given degrees-of-freedom. The keyword #:tails modifies the calculation to be two-sided (the default) with ':both, or one-sided, ':positive or ':negative.
> (t-significance 0.2 5) 0.849360513995829 > (t-significance 0.2 5 #:tails ':positive) 0.424680256997915 > (t-significance 0.2 5 #:tails ':negative) 0.575319743002086[procedure] (f-significance f-value numerator-dof denominator-dof #:one-tailed?)
returns the probability of f-value for given numerator-dof and denominator-dof. The boolean keyword #:one-tailed? indicates if calculation is two-sided (the default) or not.
> (f-significance 1.5 8 2) 0.920449812578091 > (f-significance 1.5 8 2 #:one-tailed? #t) 0.460224906289046
Authors
Peter Lane wrote the Scheme version of this library, which was later extended by Ivan Raikov The original Lisp version was written by Larry Hunter.
Repository
This egg is hosted on the CHICKEN Subversion repository:
https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/statistics
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
License
GPL version 3.0.
Version History
- 0.18: added skewness, kurtosis and covariance; one-way and balanced two-way analysis of variance and the Kruskal-Wallis test; the Benjamini-Hochberg adjustment (benjamini-hochberg-adjust, benjamini-hochberg-reject). Sequence arguments may now be any yasos collection, including SRFI-4 numeric vectors (needs yasos 1.16).
- 0.17: added effect sizes (Cohen's d and Hedges' g, with confidence intervals) and the Holm correction for multiple comparisons (holm-adjust, holm-reject).
- 0.16: added ranks, the Mann-Whitney U test, ROC analysis (AUC with confidence interval, ROC curve), classification counts with precision, recall and F1, and calibration (calibration table, Brier score, expected calibration error); #:print? for linear-regression and spearman-rank-correlation.
- 0.15: ported to CHICKEN 6; replaced the SRFI-25 dependency of chi-square-test-rxc with SRFI-63.
- 0.14: extensions for interquartile range, outlier detection, histograms, kernel density estimation
- 0.12: removed GSL dependency
- 0.11: refactoring correlation and regression interface to take two separate dataset arguments
- 0.9: ported to CHICKEN 5
- 0.8: added cumsum and random-weighted-sample
- 0.5: fixed warning in compilation (thanks to Felix for pointing it out)
- 0.4: all functions should now be working
- 0.3: some error fixes and addition of tests for majority of functions
- 0.2: fixed some errors in keywords and find-critical-value
- 0.1: initial package