Statistics

Header file stats.h

average of doubles

double averagea(const Array<double>& x)
Calculates the average of an array of doubles.

Mean, variance and standard deviation

N.B. Uses 1/(N-1) form of variance.

void meanvarsd(
   const Array<double>& x // data values
   double& xbar, // mean
   double& var, // variance
   double& sigma // standard deviation
)

Running mean and variance

This is intended for use with volumes of data too large to be stored in an array, when the data is read gradually over a long period so that it is necessary to calculate interim values of the mean etc, or to avoid creating temporary array storage. This routine must be called each time a data value is read, and the calling program must increment the point numbers itself (starting at 1) and preserve the previously calculated mean and variance. N.B. Uses 1/(N-1) form of variance.

The standard deviation can be calculated as the square root of the variance.

void runmeanvar(
   const int n, // sequence number of current point (starting at 1)
   const double x, // current data value 
   double& mean, // old average in, new average out
   double& var // old variance in, new variance out
)

N'th moment of a set of data

Two versions are provided, for use with or without calculating the mean and standard deviation separately.

double moment(
   const double xbar, // mean, calculated previously
   const double sigma, // standard deviation, calculated previously
   const Array<double>& x, // data values
   const int mom // moment required (greater than 2)
)

double moment(
   const Array<double>& x, // data values
   const int mom // moment required (greater than 2)
)

Linear correlation coefficient between two sets of data

double lincor(
   const Array<double>& x, const Array<double>& y // datasets
)