Class Variance
- java.lang.Object
-
- org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic
-
- org.apache.commons.math4.legacy.stat.descriptive.moment.Variance
-
- All Implemented Interfaces:
MathArrays.Function
,StorelessUnivariateStatistic
,UnivariateStatistic
,WeightedEvaluation
public class Variance extends AbstractStorelessUnivariateStatistic implements WeightedEvaluation
Computes the variance of the available values. By default, the unbiased "sample variance" definitional formula is used:variance = sum((x_i - mean)^2) / (n - 1)
where mean is the
Mean
andn
is the number of sample observations.The definitional formula does not have good numerical properties, so this implementation does not compute the statistic using the definitional formula.
- The
getResult
method computes the variance using updating formulas based on West's algorithm, as described in Chan, T. F. and J. G. Lewis 1979, Communications of the ACM, vol. 22 no. 9, pp. 526-531. - The
evaluate
methods leverage the fact that they have the full array of values in memory to execute a two-pass algorithm. Specifically, these methods use the "corrected two-pass algorithm" from Chan, Golub, Levesque, Algorithms for Computing the Sample Variance, American Statistician, vol. 37, no. 3 (1983) pp. 242-247.
increment
orincrementAll
and then executinggetResult
will sometimes give a different, less accurate, result than executingevaluate
with the full array of values. The former approach should only be used when the full array of values is not available.The "population variance" ( sum((x_i - mean)^2) / n ) can also be computed using this statistic. The
isBiasCorrected
property determines whether the "population" or "sample" value is returned by theevaluate
andgetResult
methods. To compute population variances, set this property tofalse.
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()
orclear()
method, it must be synchronized externally.
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
incMoment
Whether or notincrement(double)
should increment the internal second moment.protected SecondMoment
moment
SecondMoment is used in incremental calculation of Variance.
-
Constructor Summary
Constructors Constructor Description Variance()
Constructs a Variance with default (true)isBiasCorrected
property.Variance(boolean isBiasCorrected)
Constructs a Variance with the specifiedisBiasCorrected
property.Variance(boolean isBiasCorrected, SecondMoment m2)
Constructs a Variance with the specifiedisBiasCorrected
property and the supplied external second moment.Variance(SecondMoment m2)
Constructs a Variance based on an external second moment.Variance(Variance original)
Copy constructor, creates a newVariance
identical to theoriginal
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the internal state of the Statistic.Variance
copy()
Returns a copy of the statistic with the same internal state.static void
copy(Variance source, Variance dest)
Copies source to dest.double
evaluate(double[] values)
Returns the variance of the entries in the input array, orDouble.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[] weights)
Returns the weighted variance of the entries in the input array.double
evaluate(double[] values, double[] weights, double mean)
Returns the weighted variance of the values in the input array, using the precomputed weighted mean value.double
evaluate(double[] values, double[] weights, double mean, int begin, int length)
Returns the weighted variance of the entries in the specified portion of the input array, using the precomputed weighted mean value.double
evaluate(double[] values, double[] weights, int begin, int length)
Returns the weighted variance of the entries in the specified portion of the input array, orDouble.NaN
if the designated subarray is empty.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, int begin, int length)
Returns the variance of the entries in the specified portion of the input array, orDouble.NaN
if the designated subarray is empty.long
getN()
Returns the number of values that have been added.double
getResult()
Returns the current value of the Statistic.void
increment(double d)
Updates the internal state of the statistic to reflect the addition of the new value.boolean
isBiasCorrected()
void
setBiasCorrected(boolean biasCorrected)
-
Methods inherited from class org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic
equals, hashCode, incrementAll, incrementAll
-
-
-
-
Field Detail
-
moment
protected SecondMoment moment
SecondMoment is used in incremental calculation of Variance.
-
incMoment
protected boolean incMoment
Whether or notincrement(double)
should increment the internal second moment. When a Variance is constructed with an external SecondMoment as a constructor parameter, this property is set to false and increments must be applied to the second moment directly.
-
-
Constructor Detail
-
Variance
public Variance()
Constructs a Variance with default (true)isBiasCorrected
property.
-
Variance
public Variance(SecondMoment m2)
Constructs a Variance based on an external second moment.When this constructor is used, the statistic may only be incremented via the moment, i.e.,
increment(double)
does nothing; whereasm2.increment(value)
increments bothm2
and the Variance instance constructed from it.- Parameters:
m2
- the SecondMoment (Third or Fourth moments work here as well.)
-
Variance
public Variance(boolean isBiasCorrected)
Constructs a Variance with the specifiedisBiasCorrected
property.- Parameters:
isBiasCorrected
- setting for bias correction - true means bias will be corrected and is equivalent to using the "no arg" constructor
-
Variance
public Variance(boolean isBiasCorrected, SecondMoment m2)
Constructs a Variance with the specifiedisBiasCorrected
property and the supplied external second moment.- Parameters:
isBiasCorrected
- setting for bias correction - true means bias will be correctedm2
- the SecondMoment (Third or Fourth moments work here as well.)
-
Variance
public Variance(Variance original) throws NullArgumentException
Copy constructor, creates a newVariance
identical to theoriginal
.- Parameters:
original
- theVariance
instance to copy- Throws:
NullArgumentException
- if original is null
-
-
Method Detail
-
increment
public void increment(double d)
Updates the internal state of the statistic to reflect the addition of the new value.If all values are available, it is more accurate to use
evaluate(double[])
rather than adding values one at a time using this method and then executinggetResult()
, sinceevaluate
leverages the fact that is has the full list of values together to execute a two-pass algorithm. SeeVariance
.Note also that when
Variance(SecondMoment)
is used to create a Variance, this method does nothing. In that case, the SecondMoment should be incremented directly.- Specified by:
increment
in interfaceStorelessUnivariateStatistic
- Specified by:
increment
in classAbstractStorelessUnivariateStatistic
- Parameters:
d
- the new value.
-
getResult
public double getResult()
Returns the current value of the Statistic.- Specified by:
getResult
in interfaceStorelessUnivariateStatistic
- Specified by:
getResult
in classAbstractStorelessUnivariateStatistic
- Returns:
- value of the statistic,
Double.NaN
if it has been cleared or just instantiated.
-
getN
public long getN()
Returns the number of values that have been added.- Specified by:
getN
in interfaceStorelessUnivariateStatistic
- Returns:
- the number of values.
-
clear
public void clear()
Clears the internal state of the Statistic.- Specified by:
clear
in interfaceStorelessUnivariateStatistic
- Specified by:
clear
in classAbstractStorelessUnivariateStatistic
-
evaluate
public double evaluate(double[] values) throws MathIllegalArgumentException
Returns the variance of the entries in the input array, orDouble.NaN
if the array is empty.See
Variance
for details on the computing algorithm.Returns 0 for a single-value (i.e. length = 1) sample.
Throws
MathIllegalArgumentException
if the array is null.Does not change the internal state of the statistic.
- Specified by:
evaluate
in interfaceMathArrays.Function
- Specified by:
evaluate
in interfaceUnivariateStatistic
- Overrides:
evaluate
in classAbstractStorelessUnivariateStatistic
- Parameters:
values
- the input array- Returns:
- the variance of the values or Double.NaN if length = 0
- Throws:
MathIllegalArgumentException
- if the array is null- See Also:
UnivariateStatistic.evaluate(double[])
-
evaluate
public double evaluate(double[] values, int begin, int length) throws MathIllegalArgumentException
Returns the variance of the entries in the specified portion of the input array, orDouble.NaN
if the designated subarray is empty. Note that Double.NaN may also be returned if the input includes NaN and / or infinite values.See
Variance
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
MathIllegalArgumentException
if the array is null.- Specified by:
evaluate
in interfaceMathArrays.Function
- Specified by:
evaluate
in interfaceUnivariateStatistic
- Overrides:
evaluate
in classAbstractStorelessUnivariateStatistic
- Parameters:
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to include- Returns:
- the variance of the values or Double.NaN if length = 0
- Throws:
MathIllegalArgumentException
- if the array is null or the array index parameters are not valid- See Also:
UnivariateStatistic.evaluate(double[], int, int)
-
evaluate
public double evaluate(double[] values, double[] weights, int begin, int length) throws MathIllegalArgumentException
Returns the weighted variance of the entries in the specified portion of the input array, or
Double.NaN
if the designated subarray is empty.Uses the formula
where weightedMean is the weighted meanΣ(weights[i]*(values[i] - weightedMean)2)/(Σ(weights[i]) - 1)
This formula will not return the same result as the unweighted variance when all weights are equal, unless all weights are equal to 1. The formula assumes that weights are to be treated as "expansion values," as will be the case if for example the weights represent frequency counts. To normalize weights so that the denominator in the variance computation equals the length of the input vector minus one, use
evaluate(values, MathArrays.normalizeArray(weights, values.length));
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
IllegalArgumentException
if any of the following are true:- the values array is null
- the weights array is null
- the weights array does not have the same length as the values array
- the weights array contains one or more infinite values
- the weights array contains one or more NaN values
- the weights array contains negative values
- the weights array does not contain at least one non-zero value (applies when length is non zero)
- the start and length arguments do not determine a valid array
Does not change the internal state of the statistic.
Throws
MathIllegalArgumentException
if either array is null.- Specified by:
evaluate
in interfaceWeightedEvaluation
- Parameters:
values
- the input arrayweights
- the weights arraybegin
- index of the first array element to includelength
- the number of elements to include- Returns:
- the weighted variance of the values or Double.NaN if length = 0
- Throws:
MathIllegalArgumentException
- if the parameters are not valid- Since:
- 2.1
-
evaluate
public double evaluate(double[] values, double[] weights) throws MathIllegalArgumentException
Returns the weighted variance of the entries in the input array.
Uses the formula
where weightedMean is the weighted meanΣ(weights[i]*(values[i] - weightedMean)2)/(Σ(weights[i]) - 1)
This formula will not return the same result as the unweighted variance when all weights are equal, unless all weights are equal to 1. The formula assumes that weights are to be treated as "expansion values," as will be the case if for example the weights represent frequency counts. To normalize weights so that the denominator in the variance computation equals the length of the input vector minus one, use
evaluate(values, MathArrays.normalizeArray(weights, values.length));
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
MathIllegalArgumentException
if any of the following are true:- the values array is null
- the weights array is null
- the weights array does not have the same length as the values array
- the weights array contains one or more infinite values
- the weights array contains one or more NaN values
- the weights array contains negative values
- the weights array does not contain at least one non-zero value (applies when length is non zero)
Does not change the internal state of the statistic.
Throws
MathIllegalArgumentException
if either array is null.- Specified by:
evaluate
in interfaceWeightedEvaluation
- Parameters:
values
- the input arrayweights
- the weights array- Returns:
- the weighted variance of the values
- Throws:
MathIllegalArgumentException
- if the parameters are not valid- Since:
- 2.1
-
evaluate
public double evaluate(double[] values, double mean, int begin, int length) throws MathIllegalArgumentException
Returns the variance of the entries in the specified portion of the input array, using the precomputed mean value. ReturnsDouble.NaN
if the designated subarray is empty.See
Variance
for details on the computing algorithm.The formula used assumes that the supplied mean value is the arithmetic mean of the sample data, not a known population parameter. This method is supplied only to save computation when the mean has already been computed.
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
MathIllegalArgumentException
if the array is null.Does not change the internal state of the statistic.
- Parameters:
values
- the input arraymean
- the precomputed mean valuebegin
- index of the first array element to includelength
- the number of elements to include- Returns:
- the variance of the values or Double.NaN if length = 0
- Throws:
MathIllegalArgumentException
- if the array is null or the array index parameters are not valid
-
evaluate
public double evaluate(double[] values, double mean) throws MathIllegalArgumentException
Returns the variance of the entries in the input array, using the precomputed mean value. ReturnsDouble.NaN
if the array is empty.See
Variance
for details on the computing algorithm.If
isBiasCorrected
istrue
the formula used assumes that the supplied mean value is the arithmetic mean of the sample data, not a known population parameter. If the mean is a known population parameter, or if the "population" version of the variance is desired, setisBiasCorrected
tofalse
before invoking this method.Returns 0 for a single-value (i.e. length = 1) sample.
Throws
MathIllegalArgumentException
if the array is null.Does not change the internal state of the statistic.
- Parameters:
values
- the input arraymean
- the precomputed mean value- Returns:
- the variance of the values or Double.NaN if the array is empty
- Throws:
MathIllegalArgumentException
- if the array is null
-
evaluate
public double evaluate(double[] values, double[] weights, double mean, int begin, int length) throws MathIllegalArgumentException
Returns the weighted variance of the entries in the specified portion of the input array, using the precomputed weighted mean value. ReturnsDouble.NaN
if the designated subarray is empty.Uses the formula
Σ(weights[i]*(values[i] - mean)2)/(Σ(weights[i]) - 1)
The formula used assumes that the supplied mean value is the weighted arithmetic mean of the sample data, not a known population parameter. This method is supplied only to save computation when the mean has already been computed.
This formula will not return the same result as the unweighted variance when all weights are equal, unless all weights are equal to 1. The formula assumes that weights are to be treated as "expansion values," as will be the case if for example the weights represent frequency counts. To normalize weights so that the denominator in the variance computation equals the length of the input vector minus one, use
evaluate(values, MathArrays.normalizeArray(weights, values.length), mean);
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
MathIllegalArgumentException
if any of the following are true:- the values array is null
- the weights array is null
- the weights array does not have the same length as the values array
- the weights array contains one or more infinite values
- the weights array contains one or more NaN values
- the weights array contains negative values
- the weights array does not contain at least one non-zero value (applies when length is non zero)
- the start and length arguments do not determine a valid array
Does not change the internal state of the statistic.
- Parameters:
values
- the input arrayweights
- the weights arraymean
- the precomputed weighted mean valuebegin
- index of the first array element to includelength
- the number of elements to include- Returns:
- the variance of the values or Double.NaN if length = 0
- Throws:
MathIllegalArgumentException
- if the parameters are not valid- Since:
- 2.1
-
evaluate
public double evaluate(double[] values, double[] weights, double mean) throws MathIllegalArgumentException
Returns the weighted variance of the values in the input array, using the precomputed weighted mean value.
Uses the formula
Σ(weights[i]*(values[i] - mean)2)/(Σ(weights[i]) - 1)
The formula used assumes that the supplied mean value is the weighted arithmetic mean of the sample data, not a known population parameter. This method is supplied only to save computation when the mean has already been computed.
This formula will not return the same result as the unweighted variance when all weights are equal, unless all weights are equal to 1. The formula assumes that weights are to be treated as "expansion values," as will be the case if for example the weights represent frequency counts. To normalize weights so that the denominator in the variance computation equals the length of the input vector minus one, use
evaluate(values, MathArrays.normalizeArray(weights, values.length), mean);
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
MathIllegalArgumentException
if any of the following are true:- the values array is null
- the weights array is null
- the weights array does not have the same length as the values array
- the weights array contains one or more infinite values
- the weights array contains one or more NaN values
- the weights array contains negative values
- the weights array does not contain at least one non-zero value (applies when length is non zero)
Does not change the internal state of the statistic.
- Parameters:
values
- the input arrayweights
- the weights arraymean
- the precomputed weighted mean value- Returns:
- the variance of the values or Double.NaN if length = 0
- Throws:
MathIllegalArgumentException
- if the parameters are not valid- Since:
- 2.1
-
isBiasCorrected
public boolean isBiasCorrected()
- Returns:
- Returns the isBiasCorrected.
-
setBiasCorrected
public void setBiasCorrected(boolean biasCorrected)
- Parameters:
biasCorrected
- The isBiasCorrected to set.
-
copy
public Variance copy()
Returns a copy of the statistic with the same internal state.- Specified by:
copy
in interfaceStorelessUnivariateStatistic
- Specified by:
copy
in interfaceUnivariateStatistic
- Specified by:
copy
in classAbstractStorelessUnivariateStatistic
- Returns:
- a copy of the statistic
-
copy
public static void copy(Variance source, Variance dest) throws NullArgumentException
Copies source to dest.Neither source nor dest can be null.
- Parameters:
source
- Variance to copydest
- Variance to copy to- Throws:
NullArgumentException
- if either source or dest is null
-
-