1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
@cindex running statistics
@cindex online statistics
This chapter describes routines for computing running statistics,
also known as online statistics, of data. These routines are
suitable for handling large datasets for which it may be
inconvenient or impractical to store in memory all at once.
The data can be processed in a single pass, one point at a time.
Each time a data point is added to the accumulator, internal
parameters are updated in order to compute the current mean, variance,
standard deviation, skewness, and kurtosis. These statistics are
exact, and are updated with numerically stable single-pass algorithms.
The median and arbitrary quantiles are also available, however these
calculations use algorithms which provide approximations, and grow
more accurate as more data is added to the accumulator.
The functions described in this chapter are declared in the header file
@file{gsl_rstat.h}.
@menu
* Running Statistics Initializing the Accumulator::
* Running Statistics Adding Data to the Accumulator::
* Running Statistics Current Statistics::
* Running Statistics Quantiles::
* Running Statistics Example programs::
* Running Statistics References and Further Reading::
@end menu
@node Running Statistics Initializing the Accumulator
@section Initializing the Accumulator
@deftypefun {gsl_rstat_workspace *} gsl_rstat_alloc (void)
This function allocates a workspace for computing running statistics.
The size of the workspace is @math{O(1)}.
@end deftypefun
@deftypefun void gsl_rstat_free (gsl_rstat_workspace * @var{w})
This function frees the memory associated with the workspace @var{w}.
@end deftypefun
@deftypefun int gsl_rstat_reset (gsl_rstat_workspace * @var{w})
This function resets the workspace @var{w} to its initial state,
so it can begin working on a new set of data.
@end deftypefun
@node Running Statistics Adding Data to the Accumulator
@section Adding Data to the Accumulator
@deftypefun int gsl_rstat_add (const double @var{x}, gsl_rstat_workspace * @var{w})
This function adds the data point @var{x} to the statistical
accumulator, updating calculations of the mean, variance,
standard deviation, skewness, kurtosis, and median.
@end deftypefun
@deftypefun size_t gsl_rstat_n (gsl_rstat_workspace * @var{w})
This function returns the number of data so far added to the accumulator.
@end deftypefun
@node Running Statistics Current Statistics
@section Current Statistics
@deftypefun double gsl_rstat_min (gsl_rstat_workspace * @var{w})
This function returns the minimum value added to the accumulator.
@end deftypefun
@deftypefun double gsl_rstat_max (gsl_rstat_workspace * @var{w})
This function returns the maximum value added to the accumulator.
@end deftypefun
@deftypefun double gsl_rstat_mean (gsl_rstat_workspace * @var{w})
This function returns the mean of all data added to the accumulator,
defined as
@tex
\beforedisplay
$$
{\Hat\mu} = {1 \over N} \sum x_i
$$
\afterdisplay
@end tex
@ifinfo
@example
\Hat\mu = (1/N) \sum x_i
@end example
@end ifinfo
@end deftypefun
@deftypefun double gsl_rstat_variance (gsl_rstat_workspace * @var{w})
This function returns the variance of all data added to the accumulator,
defined as
@tex
\beforedisplay
$$
{\Hat\sigma}^2 = {1 \over (N-1)} \sum (x_i - {\Hat\mu})^2
$$
\afterdisplay
@end tex
@ifinfo
@example
\Hat\sigma^2 = (1/(N-1)) \sum (x_i - \Hat\mu)^2
@end example
@end ifinfo
@end deftypefun
@deftypefun double gsl_rstat_sd (gsl_rstat_workspace * @var{w})
This function returns the standard deviation of all data added to the
accumulator, defined as the square root of the variance given above.
@end deftypefun
@deftypefun double gsl_rstat_sd_mean (gsl_rstat_workspace * @var{w})
This function returns the standard deviation of the mean, defined as
@tex
\beforedisplay
$$
\Hat\sigma_{\Hat\mu} = {\Hat\sigma \over \sqrt{N}}
$$
\afterdisplay
@end tex
@ifinfo
@example
sd_mean = \Hat\sigma / \sqrt@{N@}
@end example
@end ifinfo
@end deftypefun
@deftypefun double gsl_rstat_rms (gsl_rstat_workspace * @var{w})
This function returns the root mean square of all data added to the
accumulator, defined as
@tex
\beforedisplay
$$
rms = \sqrt{{1 \over N} \sum x_i^2}
$$
\afterdisplay
@end tex
@ifinfo
@example
rms = \sqrt ( 1/N \sum x_i^2 )
@end example
@end ifinfo
@end deftypefun
@deftypefun double gsl_rstat_skew (gsl_rstat_workspace * @var{w})
This function returns the skewness of all data added to the accumulator,
defined as
@tex
\beforedisplay
$$
skew = {1 \over N} \sum
{\left( x_i - {\Hat\mu} \over {\Hat\sigma} \right)}^3
$$
\afterdisplay
@end tex
@ifinfo
@example
skew = (1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^3
@end example
@end ifinfo
@end deftypefun
@deftypefun double gsl_rstat_kurtosis (gsl_rstat_workspace * @var{w})
This function returns the kurtosis of all data added to the accumulator,
defined as
@tex
\beforedisplay
$$
kurtosis = \left( {1 \over N} \sum
{\left(x_i - {\Hat\mu} \over {\Hat\sigma} \right)}^4
\right)
- 3
$$
\afterdisplay
@end tex
@ifinfo
@example
kurtosis = ((1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^4) - 3
@end example
@end ifinfo
@end deftypefun
@deftypefun double gsl_rstat_median (gsl_rstat_workspace * @var{w})
This function returns an estimate of the median of the data added to
the accumulator.
@end deftypefun
@node Running Statistics Quantiles
@section Quantiles
The functions in this section estimate quantiles dynamically without
storing the entire dataset, using the algorithm of Jain and Chlamtec, 1985.
Only five points (markers) are stored which represent the minimum
and maximum of the data, as well as current estimates of the
@math{p/2}-, @math{p}-, and @math{(1+p)/2}-quantiles. Each time
a new data point is added, the marker positions and heights are
updated.
@deftypefun {gsl_rstat_quantile_workspace *} gsl_rstat_quantile_alloc (const double @var{p})
This function allocates a workspace for the dynamic estimation of
@var{p}-quantiles, where @var{p} is between @math{0} and @math{1}.
The median corresponds to @math{p = 0.5}. The size of the workspace
is @math{O(1)}.
@end deftypefun
@deftypefun void gsl_rstat_quantile_free (gsl_rstat_quantile_workspace * @var{w})
This function frees the memory associated with the workspace @var{w}.
@end deftypefun
@deftypefun int gsl_rstat_quantile_reset (gsl_rstat_quantile_workspace * @var{w})
This function resets the workspace @var{w} to its initial state,
so it can begin working on a new set of data.
@end deftypefun
@deftypefun int gsl_rstat_quantile_add (const double @var{x}, gsl_rstat_quantile_workspace * @var{w})
This function updates the estimate of the @math{p}-quantile with
the new data point @var{x}.
@end deftypefun
@deftypefun double gsl_rstat_quantile_get (gsl_rstat_quantile_workspace * @var{w})
This function returns the current estimate of the @math{p}-quantile.
@end deftypefun
@node Running Statistics Example programs
@section Examples
Here is a basic example of how to use the statistical functions:
@example
@verbatiminclude examples/rstat.c
@end example
The program should produce the following output,
@example
@verbatiminclude examples/rstat.txt
@end example
@noindent
This next program estimates the lower quartile, median and upper
quartile from 10,000 samples of a random Rayleigh distribution,
using the @math{P^2} algorithm of Jain and Chlamtec. For
comparison, the exact values are also computed from the sorted
dataset.
@example
@verbatiminclude examples/rquantile.c
@end example
The program should produce the following output,
@example
@verbatiminclude examples/rquantile.txt
@end example
@node Running Statistics References and Further Reading
@section References and Further Reading
The algorithm used to dynamically estimate @math{p}-quantiles is described
in the paper,
@itemize @w{}
@item
R. Jain and I. Chlamtac.
@cite{The P^2 algorithm for dynamic calculation of quantiles and histograms without storing observations},
Communications of the ACM, Volume 28 (October), Number 10, 1985,
p. 1076-1085.
@end itemize
|