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 file gsl_statistics_int.h.

Mean, Standard Deviation and Variance
=====================================

 -- Function: double gsl_stats_mean (const double DATA[], size_t STRIDE, size_t N)
     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.

 -- Function: double gsl_stats_variance (const double DATA[], size_t STRIDE, size_t 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.

 -- Function: double gsl_stats_variance_m (const double DATA[], size_t STRIDE, size_t N, double MEAN)
     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,

          \Hat\sigma^2 = (1/(N-1)) \sum (x_i - mean)^2

 -- Function: double gsl_stats_sd (const double DATA[], size_t STRIDE, size_t N)
 -- Function: double gsl_stats_sd_m (const double DATA[], size_t STRIDE, size_t N, double MEAN)
     The standard deviation is defined as the square root of the variance. These functions return the square root of the corresponding variance functions above.

 -- Function: double gsl_stats_variance_with_fixed_mean (const double DATA[], size_t STRIDE, size_t N, double MEAN)
     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,

          \Hat\sigma^2 = (1/N) \sum (x_i - \mu)^2

 -- Function: double gsl_stats_sd_with_fixed_mean (const double DATA[], size_t STRIDE, size_t N, double MEAN)
     This function calculates the standard deviation of DATA for a fixed population mean MEAN.  The result is the square root of the corresponding variance function.

Absolute deviation
==================

 -- Function: double gsl_stats_absdev (const double DATA[], size_t STRIDE, size_t N)
     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.

 -- Function: double gsl_stats_absdev_m (const double DATA[], size_t STRIDE, size_t N, double 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).

Higher moments (skewness and kurtosis)
======================================

 -- Function: double gsl_stats_skew (const double DATA[], size_t STRIDE, size_t N)
     This function computes the skewness of DATA, a dataset of length N with stride STRIDE.  The skewness is defined as,

          skew = (1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^3

     where x_i are the elements of the dataset DATA.  The skewness measures the asymmetry of the tails of a distribution.

     The function computes the mean and estimated standard deviation of DATA via calls to gsl_stats_mean and gsl_stats_sd.

 -- Function: double gsl_stats_skew_m_sd (const double DATA[], size_t STRIDE, size_t N, double MEAN, double SD)
     This function computes the skewness of the dataset DATA using the given values of the mean MEAN and standard deviation SD,

          skew = (1/N) \sum ((x_i - mean)/sd)^3

     These functions are useful if you have already computed the mean and standard deviation of DATA and want to avoid recomputing them.

 -- Function: double gsl_stats_kurtosis (const double DATA[], size_t STRIDE, size_t N)
     This function computes the kurtosis of DATA, a dataset of length N with stride STRIDE.  The kurtosis is defined as,

          kurtosis = ((1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^4)  - 3

     The kurtosis measures how sharply peaked a distribution is, relative to its width.  The kurtosis is normalized to zero for a gaussian distribution.

 -- Function: double gsl_stats_kurtosis_m_sd (const double DATA[], size_t STRIDE, size_t N, double MEAN, double SD)
     This function computes the kurtosis of the dataset DATA using the given values of the mean MEAN and standard deviation SD,

          kurtosis = ((1/N) \sum ((x_i - mean)/sd)^4) - 3

     This function is useful if you have already computed the mean and standard deviation of DATA and want to avoid recomputing them.

Autocorrelation
===============

 -- Function: double gsl_stats_lag1_autocorrelation (const double DATA[], const size_t STRIDE, const size_t N)
     This function computes the lag-1 autocorrelation of the dataset DATA.

          a_1 = {\sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i-1} - \Hat\mu)
                 \over
                 \sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i} - \Hat\mu)}

 -- Function: double gsl_stats_lag1_autocorrelation_m (const double DATA[], const size_t STRIDE, const size_t N, const double MEAN)
     This function computes the lag-1 autocorrelation of the dataset DATA using the given value of the mean MEAN.

Covariance
==========

 -- Function: double gsl_stats_covariance (const double DATA1[], const size_t STRIDE1, const double DATA2[], const size_t STRIDE2, const size_t N)
     This function computes the covariance of the datasets DATA1 and DATA2 which must both be of the same length N.

          covar = (1/(n - 1)) \sum_{i = 1}^{n} (x_i - \Hat x) (y_i - \Hat y)

 -- Function: double gsl_stats_covariance_m (const double DATA1[], const size_t STRIDE1, const double DATA2[], const size_t STRIDE2, const size_t N, const double MEAN1, const double MEAN2)
     This function computes the covariance of the datasets DATA1 and DATA2 using the given values of the means, MEAN1 and MEAN2.  This is useful if you have already computed the means of DATA1 and DATA2 and want to avoid recomputing them.

Weighted Samples
================

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.

 -- Function: double gsl_stats_wmean (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N)
     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,

          \Hat\mu = (\sum w_i x_i) / (\sum w_i)

 -- Function: double gsl_stats_wvariance (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N)
     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,

          \Hat\sigma^2 = ((\sum w_i)/((\sum w_i)^2 - \sum (w_i^2)))
                          \sum w_i (x_i - \Hat\mu)^2

     Note that this expression reduces to an unweighted variance with the familiar 1/(N-1) factor when there are N equal non-zero weights.

 -- Function: double gsl_stats_wvariance_m (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, double WMEAN)
     This function returns the estimated variance of the weighted dataset DATA using the given weighted mean WMEAN.

 -- Function: double gsl_stats_wsd (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N)
     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.

 -- Function: double gsl_stats_wsd_m (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, double WMEAN)
     This function returns the square root of the corresponding variance function gsl_stats_wvariance_m above.

 -- Function: double gsl_stats_wvariance_with_fixed_mean (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, const double MEAN)
     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,

          \Hat\sigma^2 = (\sum w_i (x_i - \mu)^2) / (\sum w_i)

 -- Function: double gsl_stats_wsd_with_fixed_mean (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, const double MEAN)
     The standard deviation is defined as the square root of the variance. This function returns the square root of the corresponding variance function above.

 -- Function: double gsl_stats_wabsdev (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N)
     This function computes the weighted absolute deviation from the weighted mean of DATA.  The absolute deviation from the mean is defined as,

          absdev = (\sum w_i |x_i - \Hat\mu|) / (\sum w_i)

 -- Function: double gsl_stats_wabsdev_m (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, double WMEAN)
     This function computes the absolute deviation of the weighted dataset DATA about the given weighted mean WMEAN.

 -- Function: double gsl_stats_wskew (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N)
     This function computes the weighted skewness of the dataset DATA.

          skew = (\sum w_i ((x_i - xbar)/\sigma)^3) / (\sum w_i)

 -- Function: double gsl_stats_wskew_m_sd (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, double WMEAN, double WSD)
     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.

 -- Function: double gsl_stats_wkurtosis (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N)
     This function computes the weighted kurtosis of the dataset DATA.

          kurtosis = ((\sum w_i ((x_i - xbar)/sigma)^4) / (\sum w_i)) - 3

 -- Function: double gsl_stats_wkurtosis_m_sd (const double W[], size_t WSTRIDE, const double DATA[], size_t STRIDE, size_t N, double WMEAN, double 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.

Maximum and Minimum values
==========================

The following functions find the maximum and minimum values of a dataset (or their indices).  If the data contains NaNs then a NaN will be returned, since the maximum or minimum value is undefined.  For functions which return an index, the location of the first NaN in the array is returned.

 -- Function: double gsl_stats_max (const double DATA[], size_t STRIDE, size_t N)
     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 \ge x_j$ 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.

 -- Function: double gsl_stats_min (const double DATA[], size_t STRIDE, size_t N)
     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 \le x_j$ 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.

 -- Function: void gsl_stats_minmax (double * MIN, double * MAX, const double DATA[], size_t STRIDE, size_t N)
     This function finds both the minimum and maximum values MIN, MAX in DATA in a single pass.

 -- Function: size_t gsl_stats_max_index (const double DATA[], size_t STRIDE, size_t N)
     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 \ge x_j$ x_i >= x_j for all j.  When there are several equal maximum elements then the first one is chosen.

 -- Function: size_t gsl_stats_min_index (const double DATA[], size_t STRIDE, size_t N)
     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 \ge x_j$ x_i >= x_j for all j.  When there are several equal minimum elements then the first one is chosen.

 -- Function: void gsl_stats_minmax_index (size_t * MIN_INDEX, size_t * MAX_INDEX, const double DATA[], size_t STRIDE, size_t N)
     This function returns the indexes MIN_INDEX, MAX_INDEX of the minimum and maximum values in DATA in a single pass.

Median and Percentiles
======================

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).

 -- Function: double gsl_stats_median_from_sorted_data (const double SORTED_DATA[], size_t STRIDE, size_t N)
     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.

 -- Function: double gsl_stats_quantile_from_sorted_data (const double SORTED_DATA[], size_t STRIDE, size_t N, double F)
     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.

Examples
========

Here is a basic example of how to use the statistical functions:

          [file stat.c not included]

The program should produce the following output,

          [file stat.out not included]

Here is an example using sorted data,

          [file statsort.c not included]

This program should produce the following output,

          [file statsort.out not included]

References and Further Reading
==============================

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)

The Review of Particle Physics is available online at the website http://pdg.lbl.gov/.