|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic commonSense.stat.basic.Variances
Computes the variance for a specific type VarianceTypes
.
Uses the definitional formula:
variance = sum((x_i - mean)^2) / (n - type)
where mean is the Mean
and n
is the number
of sample observations. When the type
is
VarianceTypes.SAMPLE
it calculates the standard bias free
sample variance with corresponds with n - 1
. When the type
is
VarianceTypes.POPULATION
, n
is left unchanged. In general,
the number provided for type
is subtracted equivalent to n - type
.
The definitional formula does not have good numerical properties, so this implementation uses updating formulas based on West's algorithm as described in Chan, T. F. andJ. G. Lewis 1979, Communications of the ACM, vol. 22 no. 9, pp. 526-531..
Note that this implementation is not synchronized. If
multiple threads access an instance of this class concurrently, and at least
one of the threads invokes the increment()
or
clear()
method, it must be synchronized externally.
Field Summary | |
protected boolean |
incMoment
Boolean test to determine if this Variance should also increment the second moment, this evaluates to false when this Variance is constructed with an external SecondMoment as a parameter. |
protected SecondMoment |
moment
SecondMoment is used in incremental calculation of Variance |
Fields inherited from interface commonSense.stat.VarianceTypes |
POPULATION, SAMPLE |
Constructor Summary | |
Variances()
Constructs a Variance. |
|
Variances(SecondMoment m2)
Constructs a Variance based on an external second moment. |
Method Summary | |
void |
clear()
|
double |
evaluate(double[] values)
Returns the variance of the entries in the input array, or Double.NaN if the array is empty.
|
double |
evaluate(double[] values,
double mean)
Returns the variance of the entries in the input array, using the precomputed mean value. |
double |
evaluate(double[] values,
double mean,
int type)
Returns the variance of the entries in the input array, using the precomputed mean value. |
double |
evaluate(double[] values,
double mean,
int begin,
int length)
Returns the variance of the entries in the specified portion of the input array, using the precomputed mean value. |
double |
evaluate(double[] values,
double mean,
int begin,
int length,
int type)
Returns the variance of the entries in the specified portion of the input array, using the precomputed mean value. |
double |
evaluate(double[] values,
int type)
Returns the variance of the entries in the input array, or Double.NaN if the array is empty.
|
double |
evaluate(double[] values,
int begin,
int length)
Returns the variance of the entries in the specified portion of the input array, or Double.NaN if the designated subarray
is empty.
|
double |
evaluate(double[] values,
int begin,
int length,
int type)
Returns the variance of the entries in the specified portion of the input array, or Double.NaN if the designated subarray
is empty.
|
long |
getN()
|
double |
getResult()
Gives back the sample variance. |
double |
getResult(int type)
Gives back the variance of the specified type. |
void |
increment(double d)
|
Methods inherited from class org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic |
equals, hashCode, incrementAll, incrementAll |
Methods inherited from class org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic |
test |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected SecondMoment moment
protected boolean incMoment
Constructor Detail |
public Variances()
public Variances(SecondMoment m2)
m2
- the SecondMoment (Thrid or Fourth moments work
here as well.)Method Detail |
public void increment(double d)
increment
in interface org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic
StorelessUnivariateStatistic.increment(double)
public double getResult(int type)
type
is
VarianceTypes.SAMPLE
it calculates the standard bias free
sample variance with corresponds with n = N - 1
in which N is the
number of rows in the matrix. When the type
is
VarianceTypes.POPULATION
, N
is left unchanged
(n = N
). In general, the number provided for type
is subtracted equivalent to n = N - type
.
type
- The type of variance that should be calculated, be it
sample or population (co-)variances.
NaN
when the
number of observations is less that the number specified in type
.VarianceTypes
,
StorelessUnivariateStatistic.getResult()
public double getResult()
getResult
in interface org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic
NaN
when the
number of observations is zero.VarianceTypes
,
StorelessUnivariateStatistic.getResult()
public long getN()
getN
in interface org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic
StorelessUnivariateStatistic.getN()
public void clear()
clear
in interface org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic
StorelessUnivariateStatistic.clear()
public double evaluate(double[] values)
Double.NaN
if the array is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws IllegalArgumentException
if the array is null.
Does not change the internal state of the statistic.
evaluate
in interface org.apache.commons.math.stat.univariate.UnivariateStatistic
values
- the input array
java.lang.IllegalArgumentException
- if the array is nullpublic double evaluate(double[] values, int begin, int length)
Double.NaN
if the designated subarray
is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Does not change the internal state of the statistic.
Throws IllegalArgumentException
if the array is null.
evaluate
in interface org.apache.commons.math.stat.univariate.UnivariateStatistic
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to include
java.lang.IllegalArgumentException
- if the array is null or the array index
parameters are not validpublic double evaluate(double[] values, double mean, int begin, int length)
Double.NaN
if the designated subarray is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws IllegalArgumentException
if the array is null.
Does not change the internal state of the statistic.
values
- the input arraymean
- the precomputed mean valuebegin
- index of the first array element to includelength
- the number of elements to include
java.lang.IllegalArgumentException
- if the array is null or the array index
parameters are not validpublic double evaluate(double[] values, double mean)
Double.NaN
if the array
is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws IllegalArgumentException
if the array is null.
Does not change the internal state of the statistic.
values
- the input arraymean
- the precomputed mean value
java.lang.IllegalArgumentException
- if the array is nullpublic double evaluate(double[] values, int type)
Double.NaN
if the array is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws IllegalArgumentException
if the array is null.
Does not change the internal state of the statistic.
values
- the input arraytype
- The type of variance that should be calculated, be it
the sample or population variance.
java.lang.IllegalArgumentException
- if the array is nullVarianceTypes
public double evaluate(double[] values, int begin, int length, int type)
Double.NaN
if the designated subarray
is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Does not change the internal state of the statistic.
Throws IllegalArgumentException
if the array is null.
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includetype
- The type of variance that should be calculated, be it
the sample or population variance.
java.lang.IllegalArgumentException
- if the array is null or the array index
parameters are not validVarianceTypes
public double evaluate(double[] values, double mean, int begin, int length, int type)
Double.NaN
if the designated subarray is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws IllegalArgumentException
if the array is null.
Does not change the internal state of the statistic.
type
should be set to VarianceTypes.SAMPLE
when the precomputed
mean is based on the same sample, but to VarianceTypes.POPULATION
when the precomputed
mean is based on the different sample.
values
- the input arraymean
- the precomputed mean valuebegin
- index of the first array element to includelength
- the number of elements to includetype
- The type of variance that should be calculated, be it
the sample or population variance.
java.lang.IllegalArgumentException
- if the array is null or the array index
parameters are not validVarianceTypes
public double evaluate(double[] values, double mean, int type)
Double.NaN
if the array
is empty.
See Variances
for details on the computing algorithm.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws IllegalArgumentException
if the array is null.
Does not change the internal state of the statistic.
type
should be set to VarianceTypes.SAMPLE
when the precomputed
mean is based on the same sample, but to VarianceTypes.POPULATION
when the precomputed
mean is based on the different sample.
values
- the input arraymean
- the precomputed mean valuetype
- The type of variance that should be calculated, be it
the sample or population variance.
java.lang.IllegalArgumentException
- if the array is nullVarianceTypes
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |