| |
- __builtin__.object
-
- Mean
- gMean
class Mean(__builtin__.object) |
|
On-line algorithm to compute the sample arithmetic mean (average), plus
the corresponding sample variance.
To be specific, the algorithm calculates an estimator for the sample
average and an estimator for the sample variance.
After creating an instance of this class, add values to it using the method
"add". After at least one value has been added, the result can be obtained
by calling the instance or the method "mean". Sample variance, sample
standard deviation and sample standard error can be obtained by calling
the methods "var", "std" and "stderr".
Working principle
-----------------
Besides the sample count N, the class only has to remember two other values
(scalar or nd-array):
1. The sum of all added values, sigma.
2. The sum of all squared values, sigma2.
With this information, it is easy to calculate the arithmetic mean and
the sample variance:
mean = sigma/N
var = 1/(N-1)*sigma2-sigma**2/(N*(N-1)) |
|
Methods defined here:
- __call__(self)
- Alias for mean().
- __init__(self, shape=None, dtype=None, init_value=None, init_var=None, init_count=None)
- Initialize the instance. The desired data shape and data type can be
set, otherwise they are infered from the first added value, or from the
given initial values. A previously stopped calculation can be continued
by setting the three needed initial values:
init_value : sample mean of the old calculation
init_count : sample count of the old calculation
init_var : sample variance of the old calculation
- __repr__(self)
- Return complete string representation.
- __str__(self)
- Return short string representation.
- add(self, value)
- Add a value. It can be a scalar or an n-dimensional array, but it
must be consistent with values that were already added.
- ci(self, n=1)
- Return confidence interval (CI) of order n. Two length-N arrays are
returned, containing the value of the lower and the upper part of the
CI, respectively, relative to the mean value. In other words, the CI
stretches from mean-array1 to mean+array2. The width of the CI is
array1+array2.
- mean(self)
- Calculate the sample arithmetic mean.
- mspair(self)
- Return the pair of sample mean and sample standard error (2-tuple).
- sem(self)
- Alias for "stderr".
- std(self)
- Calculate the sample standard deviation, which is the square root of
the sample variance.
- stderr(self)
- Calculate the sample standard error of the mean, which is the sample
standard deviation devided by sqrt(N).
- var(self)
- Calculate the sample variance.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
- count
- Return the current sample count.
- dtype
- Return data type.
- ndim
- Return number of dimensions of the allowed values.
- shape
- Return data shape.
- size
- Return number of elements of the allowed values.
|
class gMean(__builtin__.object) |
|
On-line algorithm to compute the sample geometric mean, plus the
corresponding variance.
To be specific, the algorithm calculates an estimator for the sample
geometric mean and an estimator for the corresponding sample variance.
After creating an instance of this class, add values to it using the method
"add". After at least one value has been added, the result can be obtained
by calling the instance or the method "gmean". Sample variance, sample
standard deviation and sample standard error can be obtained by calling the
methods "var", "std" and "stderr".
Working principle
-----------------
Besides the sample count N, the class only has to remember two other values
(scalar or nd-array):
1. The sum of the logarithm of all added values, gamma.
2. The sum of the squared logarithms of all added values, gamma2.
With this information, the sample geometric mean and
the sample variance can be calculated as follows:
mean = exp(gamma/N)
var = exp(2*gamma/N)*(1/(N-1)*gamma2-gamma**2/(N*(N-1))) |
|
Methods defined here:
- __call__(self)
- Alias for the method mean().
- __init__(self, shape=None, dtype=None, init_value=None, init_var=None, init_count=None)
- Initialize the instance. The desired data shape and data type can be
set, otherwise they are infered from the first added value, or from the
given initial values. A previously stopped calculation can be continued
by setting the three needed initial values:
init_value : the result for the sample mean of the old calculation
init_count : the sample count of the old calculation
init_var : sample variance of the old calculation
- __repr__(self)
- Return complete string representation.
- __str__(self)
- Return short string representation.
- add(self, value)
- Add a value. It can be a scalar or an n-dimensional array, but it
must be consistent with values that were already added.
- ci(self, n=1)
- Return confidence interval (CI) of order n. Two arrays with the same
shape as the input data are returned, containing the value of the lower
and the upper part of the CI for every array element, respectively,
relative to the mean value. In other words, the CI stretches from
mean-array1 to mean+array2. The width of the CI is array1+array2.
- mean(self)
- Calculate the sample geometric mean.
- mspair(self)
- Return the pair of mean and standard error (2-tuple).
- sem(self)
- Alias for "stderr".
- std(self)
- Calculate the sample standard deviation, which is the square root
of the sample variance.
- stderr(self)
- Calculate the standard error of the geometric mean.
According to [Norris1940], the sample standard error is equal to the
sample standard deviation devided by sqrt(N-1).
- var(self)
- Calculate the sample variance.
Note: Because a numerically unstable algorithm for the variance is
used, sometimes the variance takes small negative values. To circumvent
this, those values are set to zero. Tests show that even with a
million samples, the error is still of the order 1e-10.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
- count
- Return the current sample count.
- dtype
- Return data type.
- ndim
- Return number of dimensions of the allowed values.
- shape
- Return data shape.
- size
- Return number of elements of the allowed values.
| |