This chapter describes the statistical functions in the library. The
basic statistical functions include routines to compute the mean,
variance and standard deviation. More advanced functions allow you to
calculate absolute deviations, skewness, and kurtosis as well as the
median and arbitrary percentiles. The algorithms use recurrence
relations to compute average quantities in a stable way, without large
intermediate values that might overflow.
The functions are available in versions for datasets in the standard
floating-point and integer types. The versions for double precision
floating-point data have the prefix gsl_stats and are declared in
the header file `gsl_statistics_double.h'. The versions for integer
data have the prefix gsl_stats_int and are declared in the header
files `gsl_statistics_int.h'.
This function returns the arithmetic mean of data, a dataset of
length n with stride stride. The arithmetic mean, or
sample mean, is denoted by \Hat\mu and defined as,
\Hat\mu = (1/N) \sum x_i
where x_i are the elements of the dataset data. For
samples drawn from a gaussian distribution the variance of
\Hat\mu is \sigma^2 / N.
This function returns the estimated, or sample, variance of
data, a dataset of length n with stride stride. The
estimated variance is denoted by \Hat\sigma^2 and is defined by,
\Hat\sigma^2 = (1/(N-1)) \sum (x_i - \Hat\mu)^2
where x_i are the elements of the dataset data. Note that
the normalization factor of 1/(N-1) results from the derivation
of \Hat\sigma^2 as an unbiased estimator of the population
variance \sigma^2. For samples drawn from a gaussian distribution
the variance of \Hat\sigma^2 itself is 2 \sigma^4 / N.
This function computes the mean via a call to gsl_stats_mean. If
you have already computed the mean then you can pass it directly to
gsl_stats_variance_m.
This function returns the sample variance of data relative to the
given value of mean. The function is computed with \Hat\mu
replaced by the value of mean that you supply,
The standard deviation is defined as the square root of the variance.
These functions return the square root of the corresponding variance
functions above.
This function computes an unbiased estimate of the variance of
data when the population mean mean of the underlying
distribution is known a priori. In this case the estimator for
the variance uses the factor 1/N and the sample mean
\Hat\mu is replaced by the known population mean \mu,
This function calculates the standard deviation of data for a a
fixed population mean mean. The result is the square root of the
corresponding variance function.
This function computes the absolute deviation from the mean of
data, a dataset of length n with stride stride. The
absolute deviation from the mean is defined as,
absdev = (1/N) \sum |x_i - \Hat\mu|
where x_i are the elements of the dataset data. The
absolute deviation from the mean provides a more robust measure of the
width of a distribution than the variance. This function computes the
mean of data via a call to gsl_stats_mean.
This function computes the absolute deviation of the dataset data
relative to the given value of mean,
absdev = (1/N) \sum |x_i - mean|
This function is useful if you have already computed the mean of
data (and want to avoid recomputing it), or wish to calculate the
absolute deviation relative to another value (such as zero, or the
median).
The functions described in this section allow the computation of
statistics for weighted samples. The functions accept an array of
samples, x_i, with associated weights, w_i. Each sample
x_i is considered as having been drawn from a Gaussian
distribution with variance \sigma_i^2. The sample weight
w_i is defined as the reciprocal of this variance, w_i =
1/\sigma_i^2. Setting a weight to zero corresponds to removing a
sample from a dataset.
This function returns the weighted mean of the dataset data with
stride stride and length n, using the set of weights w
with stride wstride and length n. The weighted mean is defined as,
This function returns the estimated variance of the dataset data
with stride stride and length n, using the set of weights
w with stride wstride and length n. The estimated
variance of a weighted dataset is defined as,
The standard deviation is defined as the square root of the variance.
This function returns the square root of the corresponding variance
function gsl_stats_wvariance above.
This function computes an unbiased estimate of the variance of weighted
dataset data when the population mean mean of the underlying
distribution is known a priori. In this case the estimator for
the variance replaces the sample mean \Hat\mu by the known
population mean \mu,
The standard deviation is defined as the square root of the variance.
This function returns the square root of the corresponding variance
function above.
This function computes the weighted skewness of the dataset data
using the given values of the weighted mean and weighted standard
deviation, wmean and wsd.
This function computes the weighted kurtosis of the dataset data
using the given values of the weighted mean and weighted standard
deviation, wmean and wsd.
This function returns the maximum value in data, a dataset of
length n with stride stride. The maximum value is defined
as the value of the element x_i which satisfies
x_i >= x_j for all j.
If you want instead to find the element with the largest absolute
magnitude you will need to apply fabs or abs to your data
before calling this function.
This function returns the minimum value in data, a dataset of
length n with stride stride. The minimum value is defined
as the value of the element x_i which satisfies
x_i <= x_j for all j.
If you want instead to find the element with the smallest absolute
magnitude you will need to apply fabs or abs to your data
before calling this function.
This function returns the index of the maximum value in data, a
dataset of length n with stride stride. The maximum value is
defined as the value of the element x_i which satisfies
x_i >= x_j for all j. When there are several equal maximum
elements then the first one is chosen.
This function returns the index of the minimum value in data, a
dataset of length n with stride stride. The minimum value
is defined as the value of the element x_i which satisfies
x_i >= x_j for all j. When there are several equal
minimum elements then the first one is chosen.
The median and percentile functions described in this section operate on
sorted data. For convenience we use quantiles, measured on a scale
of 0 to 1, instead of percentiles (which use a scale of 0 to 100).
This function returns the median value of sorted_data, a dataset
of length n with stride stride. The elements of the array
must be in ascending numerical order. There are no checks to see
whether the data are sorted, so the function gsl_sort should
always be used first.
When the dataset has an odd number of elements the median is the value
of element (n-1)/2. When the dataset has an even number of
elements the median is the mean of the two nearest middle values,
elements (n-1)/2 and n/2. Since the algorithm for
computing the median involves interpolation this function always returns
a floating-point number, even for integer data types.
This function returns a quantile value of sorted_data, a
double-precision array of length n with stride stride. The
elements of the array must be in ascending numerical order. The
quantile is determined by the f, a fraction between 0 and 1. For
example, to compute the value of the 75th percentile f should have
the value 0.75.
There are no checks to see whether the data are sorted, so the function
gsl_sort should always be used first.
The quantile is found by interpolation, using the formula
quantile = (1 - \delta) x_i + \delta x_{i+1}
where i is floor((n - 1)f) and \delta is
(n-1)f - i.
Thus the minimum value of the array (data[0*stride]) is given by
f equal to zero, the maximum value (data[(n-1)*stride]) is
given by f equal to one and the median value is given by f
equal to 0.5. Since the algorithm for computing quantiles involves
interpolation this function always returns a floating-point number, even
for integer data types.
Here is a basic example of how to use the statistical functions:
#include <stdio.h>
#include <gsl/gsl_statistics.h>
int
main(void)
{
double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6};
double mean, variance, largest, smallest;
mean = gsl_stats_mean(data, 1, 5);
variance = gsl_stats_variance(data, 1, 5);
largest = gsl_stats_max(data, 1, 5);
smallest = gsl_stats_min(data, 1, 5);
printf("The dataset is %g, %g, %g, %g, %g\n",
data[0], data[1], data[2], data[3], data[4]);
printf("The sample mean is %g\n", mean);
printf("The estimated variance is %g\n", variance);
printf("The largest value is %g\n", largest);
printf("The smallest value is %g\n", smallest);
return 0;
}
The program should produce the following output,
The dataset is 17.2, 18.1, 16.5, 18.3, 12.6
The sample mean is 16.54
The estimated variance is 4.2984
The largest value is 18.3
The smallest value is 12.6
Original dataset: 17.2, 18.1, 16.5, 18.3, 12.6
Sorted dataset: 12.6, 16.5, 17.2, 18.1, 18.3
The median is 17.2
The upper quartile is 18.1
The lower quartile is 16.5
The standard reference for almost any topic in statistics is the
multi-volume Advanced Theory of Statistics by Kendall and Stuart.
Maurice Kendall, Alan Stuart, and J. Keith Ord.
The Advanced Theory of Statistics (multiple volumes)
reprinted as Kendall's Advanced Theory of Statistics.
Wiley, ISBN 047023380X.
Many statistical concepts can be more easily understood by a Bayesian
approach. The following book by Gelman, Carlin, Stern and Rubin gives a
comprehensive coverage of the subject.
Andrew Gelman, John B. Carlin, Hal S. Stern, Donald B. Rubin.
Bayesian Data Analysis.
Chapman & Hall, ISBN 0412039915.
For physicists the Particle Data Group provides useful reviews of
Probability and Statistics in the "Mathematical Tools" section of its
Annual Review of Particle Physics.
Review of Particle Properties
R.M. Barnett et al., Physical Review D54, 1 (1996)