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.
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.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 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.
| Modifier and Type | Field and Description |
|---|---|
| protected boolean | incMoment
Whether or not |
| 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 |
| Access | Constructor and Description |
|---|---|
| public | |
| 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 |
| 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 |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Implements abstract org. Implements org. Clears the internal state of the Statistic |
| public Variance | copy()
Implements abstract org. Implements org. Returns a copy of the statistic with the same internal state. |
| public static void | |
| public double | Returns: the variance of the values or Double.NaN if length = 0the input array values)Overrides org. Implements org. Returns the variance of the entries in the input array, or
|
| public double | Returns: the variance of the values or Double.NaN if length = 0the 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. Implements org. Returns the variance of the entries in the specified portion of
the input array, or |
| public double | Returns: the weighted variance of the values or Double.NaN if length = 0the 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. Returns the weighted variance of the entries in the specified portion of
the input array, or |
| public double | Returns: the weighted variance of the valuesthe input array values, final double[] the weights array weights)Implements org. 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 = 0the 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 emptythe 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 = 0the 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 = 0the 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. Returns the number of values that have been added. |
| public double | getResult()
Implements abstract org. Implements org. Returns the current value of the Statistic. |
| public void | increment(final double
the new value. d)Implements abstract org. Implements org. Updates the internal state of the statistic to reflect the addition of the new value. |
| public boolean | |
| public void |
| incMoment | back to summary |
|---|---|
| protected boolean incMoment Whether or not | |
| isBiasCorrected | back 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
| |
| moment | back to summary |
|---|---|
| protected SecondMoment moment SecondMoment is used in incremental calculation of Variance | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serializable version identifier | |
| Variance | back to summary |
|---|---|
| public Variance() Constructs a Variance with default (true) | |
| Variance | back 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.,
| |
| Variance | back to summary |
|---|---|
| public Variance(boolean isBiasCorrected) Constructs a Variance with the specified
| |
| Variance | back to summary |
|---|---|
| public Variance(boolean isBiasCorrected, SecondMoment m2) Constructs a Variance with the specified
| |
| Variance | back to summary |
|---|---|
| public Variance(Variance original) throws NullArgumentException Copy constructor, creates a new
| |
| clear | back to summary |
|---|---|
| public void clear() Implements abstract org. Implements org. Doc from org. Clears the internal state of the Statistic
| |
| copy | back to summary |
|---|---|
| public Variance copy() Implements abstract org. Implements org. Doc from org. Returns a copy of the statistic with the same internal state.
| |
| copy | back to summary |
|---|---|
| public static void copy(Variance source, Variance dest) throws NullArgumentException Copies source to dest. Neither source nor dest can be null.
| |
| evaluate | back to summary |
|---|---|
| public double evaluate(final double[] values) throws MathIllegalArgumentException Overrides org. Implements org. Returns the variance of the entries in the input array, or
See Returns 0 for a single-value (i.e. length = 1) sample.
Throws Does not change the internal state of the statistic.
| |
| evaluate | back to summary |
|---|---|
| public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException Overrides org. Implements org. Returns the variance of the entries in the specified portion of
the input array, or
See Returns 0 for a single-value (i.e. length = 1) sample. Does not change the internal state of the statistic.
Throws
| |
| evaluate | back to summary |
|---|---|
| public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException Implements org. Returns the weighted variance of the entries in the specified portion of
the input array, or 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
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
Does not change the internal state of the statistic.
Throws
| |
| evaluate | back to summary |
|---|---|
| public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException Implements org. 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
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
Does not change the internal state of the statistic.
Throws
| |
| evaluate | back 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
See 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 Does not change the internal state of the statistic.
| |
| evaluate | back 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
See
If Returns 0 for a single-value (i.e. length = 1) sample.
Throws Does not change the internal state of the statistic.
| |
| evaluate | back 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
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
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
Does not change the internal state of the statistic.
| |
| evaluate | back 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
Returns 0 for a single-value (i.e. length = 1) sample.
Throws
Does not change the internal state of the statistic.
| |
| getN | back to summary |
|---|---|
| public long getN() Implements org. Doc from org. Returns the number of values that have been added.
| |
| getResult | back to summary |
|---|---|
| public double getResult() Implements abstract org. Implements org. Doc from org. Returns the current value of the Statistic.
| |
| increment | back to summary |
|---|---|
| public void increment(final double d) Implements abstract org. Implements org. Doc from org. 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
Note also that when
| |
| isBiasCorrected | back to summary |
|---|---|
public boolean isBiasCorrected()
| |
| setBiasCorrected | back to summary |
|---|---|
public void setBiasCorrected(boolean biasCorrected)
| |