Top Description Fields Constructors Methods
org.apache.commons.math3.stat.descriptive.moment

public Class Variance

extends AbstractStorelessUnivariateStatistic
implements Serializable, WeightedEvaluation
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.stat.descriptive.WeightedEvaluation, java.io.Serializable
Imports
java.io.Serializable, org.apache.commons.math3.exception.MathIllegalArgumentException, .NullArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.stat.descriptive.WeightedEvaluation, .AbstractStorelessUnivariateStatistic, org.apache.commons.math3.util.MathUtils

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 and n 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.

Note that adding values using increment or incrementAll and then executing getResult will sometimes give a different, less accurate, result than executing evaluate 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 the evaluate and getResult methods. To compute population variances, set this property to false.

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

Modifier and TypeField and Description
protected boolean
incMoment

Whether or not increment(double) should increment the internal second moment.

private boolean
isBiasCorrected

Whether or not bias correction is applied when computing the value of the statistic.

protected SecondMoment
moment

SecondMoment is used in incremental calculation of Variance

private static final long
serialVersionUID

Serializable version identifier

Constructor Summary

AccessConstructor and Description
public
Variance()

Constructs a Variance with default (true) isBiasCorrected property.

public
Variance(final SecondMoment
the SecondMoment (Third or Fourth moments work here as well.)
m2
)

Constructs a Variance based on an external second moment.

public
Variance(boolean
setting for bias correction - true means bias will be corrected and is equivalent to using the argumentless constructor
isBiasCorrected
)

Constructs a Variance with the specified isBiasCorrected property

public
Variance(boolean
setting for bias correction - true means bias will be corrected
isBiasCorrected
,
SecondMoment
the SecondMoment (Third or Fourth moments work here as well.)
m2
)

Constructs a Variance with the specified isBiasCorrected property and the supplied external second moment.

public
Variance(Variance
the Variance instance to copy
original
)

Copy constructor, creates a new Variance identical to the original

Method Summary

Modifier and TypeMethod and Description
public void
public Variance
public static void
copy(Variance
Variance to copy
source
,
Variance
Variance to copy to
dest
)

Copies source to dest.

public double

Returns:

the variance of the values or Double.NaN if length = 0
evaluate
(final double[]
the input array
values
)

Overrides org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.evaluate.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.evaluate.

Returns the variance of the entries in the input array, or Double.NaN if the array is empty.

public double

Returns:

the variance of the values or Double.NaN if length = 0
evaluate
(final double[]
the input array
values
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
length
)

Overrides org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.evaluate.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.evaluate.

Returns the variance of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.

public double

Returns:

the weighted variance of the values or Double.NaN if length = 0
evaluate
(final double[]
the input array
values
,
final double[]
the weights array
weights
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
length
)

Implements org.apache.commons.math3.stat.descriptive.WeightedEvaluation.evaluate.

Returns the weighted variance of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.

public double

Returns:

the weighted variance of the values
evaluate
(final double[]
the input array
values
,
final double[]
the weights array
weights
)

Implements org.apache.commons.math3.stat.descriptive.WeightedEvaluation.evaluate.

Returns the weighted variance of the entries in the the input array.

public double

Returns:

the variance of the values or Double.NaN if length = 0
evaluate
(final double[]
the input array
values
,
final double
the precomputed mean value
mean
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
length
)

Returns the variance of the entries in the specified portion of the input array, using the precomputed mean value.

public double

Returns:

the variance of the values or Double.NaN if the array is empty
evaluate
(final double[]
the input array
values
,
final double
the precomputed mean value
mean
)

Returns the variance of the entries in the input array, using the precomputed mean value.

public double

Returns:

the variance of the values or Double.NaN if length = 0
evaluate
(final double[]
the input array
values
,
final double[]
the weights array
weights
,
final double
the precomputed weighted mean value
mean
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
length
)

Returns the weighted variance of the entries in the specified portion of the input array, using the precomputed weighted mean value.

public double

Returns:

the variance of the values or Double.NaN if length = 0
evaluate
(final double[]
the input array
values
,
final double[]
the weights array
weights
,
final double
the precomputed weighted mean value
mean
)

Returns the weighted variance of the values in the input array, using the precomputed weighted mean value.

public long
getN()

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.getN.

Returns the number of values that have been added.

public double
public void
increment(final double
the new value.
d
)

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.increment.

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.increment.

Updates the internal state of the statistic to reflect the addition of the new value.

public boolean

Returns:

Returns the isBiasCorrected.
isBiasCorrected
()

public void
setBiasCorrected(boolean
The isBiasCorrected to set.
biasCorrected
)

Inherited from org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic:
equalshashCodeincrementAllincrementAll

Field Detail

incMomentback to summary
protected boolean incMoment

Whether or not increment(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.

isBiasCorrectedback to summary
private boolean isBiasCorrected

Whether or not bias correction is applied when computing the value of the statistic. True means that bias is corrected. See Variance for details on the formula.

momentback to summary
protected SecondMoment moment

SecondMoment is used in incremental calculation of Variance

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier

Constructor Detail

Varianceback to summary
public Variance()

Constructs a Variance with default (true) isBiasCorrected property.

Varianceback to summary
public Variance(final 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; whereas m2.increment(value) increments both m2 and the Variance instance constructed from it.

Parameters
m2:SecondMoment

the SecondMoment (Third or Fourth moments work here as well.)

Varianceback to summary
public Variance(boolean isBiasCorrected)

Constructs a Variance with the specified isBiasCorrected property

Parameters
isBiasCorrected:boolean

setting for bias correction - true means bias will be corrected and is equivalent to using the argumentless constructor

Varianceback to summary
public Variance(boolean isBiasCorrected, SecondMoment m2)

Constructs a Variance with the specified isBiasCorrected property and the supplied external second moment.

Parameters
isBiasCorrected:boolean

setting for bias correction - true means bias will be corrected

m2:SecondMoment

the SecondMoment (Third or Fourth moments work here as well.)

Varianceback to summary
public Variance(Variance original) throws NullArgumentException

Copy constructor, creates a new Variance identical to the original

Parameters
original:Variance

the Variance instance to copy

Exceptions
NullArgumentException:
if original is null

Method Detail

clearback to summary
public void clear()

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.clear.

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.clear.

Doc from org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.clear.

Clears the internal state of the Statistic

Annotations
@Override
copyback to summary
public Variance copy()

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.copy.

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.copy, org.apache.commons.math3.stat.descriptive.UnivariateStatistic.copy.

Doc from org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.copy.

Returns a copy of the statistic with the same internal state.

Returns:Variance

Doc from org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.copy.

a copy of the statistic

Annotations
@Override
copyback to summary
public static void copy(Variance source, Variance dest) throws NullArgumentException

Copies source to dest.

Neither source nor dest can be null.

Parameters
source:Variance

Variance to copy

dest:Variance

Variance to copy to

Exceptions
NullArgumentException:
if either source or dest is null
evaluateback to summary
public double evaluate(final double[] values) throws MathIllegalArgumentException

Overrides org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.evaluate.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.evaluate.

Returns the variance of the entries in the input array, or Double.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.

Parameters
values:double[]

the input array

Returns:double

the variance of the values or Double.NaN if length = 0

Annotations
@Override
Exceptions
MathIllegalArgumentException:
if the array is null
evaluateback to summary
public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException

Overrides org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.evaluate.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.evaluate.

Returns the variance of the entries in the specified portion of the input array, or Double.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.

Parameters
values:double[]

the input array

begin:int

index of the first array element to include

length:int

the number of elements to include

Returns:double

the variance of the values or Double.NaN if length = 0

Annotations
@Override
Exceptions
MathIllegalArgumentException:
if the array is null or the array index parameters are not valid
evaluateback to summary
public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException

Implements org.apache.commons.math3.stat.descriptive.WeightedEvaluation.evaluate.

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

  Σ(weights[i]*(values[i] - weightedMean)2)/(Σ(weights[i]) - 1)
where weightedMean is the weighted mean

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 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.

Parameters
values:double[]

the input array

weights:double[]

the weights array

begin:int

index of the first array element to include

length:int

the number of elements to include

Returns:double

the weighted variance of the values or Double.NaN if length = 0

Exceptions
MathIllegalArgumentException:
if the parameters are not valid
Since
2.1
evaluateback to summary
public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException

Implements org.apache.commons.math3.stat.descriptive.WeightedEvaluation.evaluate.

Returns the weighted variance of the entries in the the input array.

Uses the formula

  Σ(weights[i]*(values[i] - weightedMean)2)/(Σ(weights[i]) - 1)
where weightedMean is the weighted mean

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

Does not change the internal state of the statistic.

Throws MathIllegalArgumentException if either array is null.

Parameters
values:double[]

the input array

weights:double[]

the weights array

Returns:double

the weighted variance of the values

Exceptions
MathIllegalArgumentException:
if the parameters are not valid
Since
2.1
evaluateback to summary
public double evaluate(final double[] values, final double mean, final int begin, final int length) throws MathIllegalArgumentException

Returns the variance of the entries in the specified portion of the input array, using the precomputed mean value. Returns Double.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:double[]

the input array

mean:double

the precomputed mean value

begin:int

index of the first array element to include

length:int

the number of elements to include

Returns:double

the variance of the values or Double.NaN if length = 0

Exceptions
MathIllegalArgumentException:
if the array is null or the array index parameters are not valid
evaluateback to summary
public double evaluate(final double[] values, final double mean) throws MathIllegalArgumentException

Returns the variance of the entries in the input array, using the precomputed mean value. Returns Double.NaN if the array is empty.

See Variance for details on the computing algorithm.

If isBiasCorrected is true 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, set isBiasCorrected to false 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:double[]

the input array

mean:double

the precomputed mean value

Returns:double

the variance of the values or Double.NaN if the array is empty

Exceptions
MathIllegalArgumentException:
if the array is null
evaluateback to summary
public double evaluate(final double[] values, final double[] weights, final double mean, final int begin, final 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. Returns Double.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 start and length arguments do not determine a valid array

Does not change the internal state of the statistic.

Parameters
values:double[]

the input array

weights:double[]

the weights array

mean:double

the precomputed weighted mean value

begin:int

index of the first array element to include

length:int

the number of elements to include

Returns:double

the variance of the values or Double.NaN if length = 0

Exceptions
MathIllegalArgumentException:
if the parameters are not valid
Since
2.1
evaluateback to summary
public double evaluate(final double[] values, final double[] weights, final 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

Does not change the internal state of the statistic.

Parameters
values:double[]

the input array

weights:double[]

the weights array

mean:double

the precomputed weighted mean value

Returns:double

the variance of the values or Double.NaN if length = 0

Exceptions
MathIllegalArgumentException:
if the parameters are not valid
Since
2.1
getNback to summary
public long getN()

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.getN.

Doc from org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.getN.

Returns the number of values that have been added.

Returns:long

the number of values.

getResultback to summary
public double getResult()

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.getResult.

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.getResult.

Doc from org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.getResult.

Returns the current value of the Statistic.

Returns:double

Doc from org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.getResult.

value of the statistic, Double.NaN if it has been cleared or just instantiated.

Annotations
@Override
incrementback to summary
public void increment(final double d)

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.increment.

Implements org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.increment.

Doc from org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic.increment.

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 executing getResult, since evaluate leverages the fact that is has the full list of values together to execute a two-pass algorithm. See Variance.

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.

Parameters
d:double

Doc from org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic.increment.

the new value.

Annotations
@Override
isBiasCorrectedback to summary
public boolean isBiasCorrected()
Returns:boolean

Returns the isBiasCorrected.

setBiasCorrectedback to summary
public void setBiasCorrected(boolean biasCorrected)
Parameters
biasCorrected:boolean

The isBiasCorrected to set.