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

public Class Mean

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.stat.descriptive.AbstractStorelessUnivariateStatistic, .WeightedEvaluation, org.apache.commons.math3.stat.descriptive.summary.Sum, org.apache.commons.math3.util.MathUtils

Computes the arithmetic mean of a set of values. Uses the definitional formula:

mean = sum(x_i) / n

where n is the number of observations.

When increment(double) is used to add data incrementally from a stream of (unstored) values, the value of the statistic that getResult() returns is computed using the following recursive updating algorithm:

  1. Initialize m = the first value
  2. For each additional value, update using
    m = m + (new value - m) / (number of observations)

If evaluate(double[]) is used to compute the mean of an array of stored values, a two-pass, corrected algorithm is used, starting with the definitional formula computed using the array of stored values and then correcting this by adding the mean deviation of the data values from the arithmetic mean. See, e.g. "Comparison of Several Algorithms for Computing Sample Means and Variances," Robert F. Ling, Journal of the American Statistical Association, Vol. 69, No. 348 (Dec., 1974), pp. 859-866.

Returns Double.NaN if the dataset is empty. Note that Double.NaN may also be returned if the input includes NaN and / or infinite values.

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

Determines whether or not this statistic can be incremented or cleared.

protected FirstMoment
moment

First moment on which this statistic is based.

private static final long
serialVersionUID

Serializable version identifier

Constructor Summary

AccessConstructor and Description
public
Mean()

Constructs a Mean.

public
Mean(final FirstMoment
the moment
m1
)

Constructs a Mean with an External Moment.

public
Mean(Mean
the Mean instance to copy
original
)

Copy constructor, creates a new Mean identical to the original

Method Summary

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

Copies source to dest.

public double

Returns:

the mean 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 arithmetic mean of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.

public double

Returns:

the mean 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 arithmetic mean of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.

public double

Returns:

the mean of the values or Double.NaN if length = 0
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 arithmetic mean of the entries in the input array.

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.

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

Field Detail

incMomentback to summary
protected boolean incMoment

Determines whether or not this statistic can be incremented or cleared.

Statistics based on (constructed from) external moments cannot be incremented or cleared.

momentback to summary
protected FirstMoment moment

First moment on which this statistic is based.

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier

Constructor Detail

Meanback to summary
public Mean()

Constructs a Mean.

Meanback to summary
public Mean(final FirstMoment m1)

Constructs a Mean with an External Moment.

Parameters
m1:FirstMoment

the moment

Meanback to summary
public Mean(Mean original) throws NullArgumentException

Copy constructor, creates a new Mean identical to the original

Parameters
original:Mean

the Mean 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 Mean 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:Mean

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

a copy of the statistic

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

Copies source to dest.

Neither source nor dest can be null.

Parameters
source:Mean

Mean to copy

dest:Mean

Mean to copy to

Exceptions
NullArgumentException:
if either source or dest 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 arithmetic mean of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.

Throws IllegalArgumentException if the array is null.

See Mean for details on the computing algorithm.

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 mean 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 arithmetic mean of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.

Throws IllegalArgumentException if either array is null.

See Mean for details on the computing algorithm. The two-pass algorithm described above is used here, with weights applied in computing both the original estimate and the correction factor.

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

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 mean 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 arithmetic mean of the entries in the input array.

Throws MathIllegalArgumentException if either array is null.

See Mean for details on the computing algorithm. The two-pass algorithm described above is used here, with weights applied in computing both the original estimate and the correction factor.

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

Parameters
values:double[]

the input array

weights:double[]

the weights array

Returns:double

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

Note that when Mean(FirstMoment) is used to create a Mean, this method does nothing. In that case, the FirstMoment should be incremented directly.

Parameters
d:double

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

the new value.

Annotations
@Override