public final class Mean
extends java.lang.Object
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:
m =
the first valuem = 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.
increment()
or
clear()
method, it must be synchronized externally.Modifier and Type | Field and Description |
---|---|
protected boolean |
incMoment
Determines whether or not this statistic can be incremented or cleared.
|
protected com.kontakt.sdk.android.ble.math.FirstMoment |
moment
First moment on which this statistic is based.
|
Constructor and Description |
---|
Mean()
Constructs a Mean.
|
Mean(com.kontakt.sdk.android.ble.math.FirstMoment m1)
Constructs a Mean with an External Moment.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the internal state of the Statistic
|
static boolean |
equals(double x,
double y,
int maxUlps)
Returns true if both arguments are equal or within the range of allowed
error (inclusive).
|
boolean |
equals(java.lang.Object object)
Returns true iff
object is an
AbstractStorelessUnivariateStatistic returning the same
values as this for getResult() and getN() |
double |
evaluate()
Returns the result of evaluating the statistic over the stored data.
|
double |
evaluate(double[] values)
This default implementation calls
clear() , then invokes
increment(double) in a loop over the the input array, and then uses
getResult() to compute the return value. |
double |
evaluate(double[] values,
int begin,
int length)
Returns the arithmetic mean of the entries in the specified portion of
the input array, or
Double.NaN if the designated subarray
is empty. |
double[] |
getData()
Get a copy of the stored data array.
|
protected double[] |
getDataRef()
Get a reference to the stored data array.
|
long |
getN() |
double |
getResult()
Returns the current value of the Statistic.
|
int |
hashCode()
Returns hash code based on getResult() and getN()
|
void |
increment(double d)
Updates the internal state of the statistic to reflect the addition of the new value.
|
void |
incrementAll(double[] values)
This default implementation just calls
increment(double) in a loop over
the input array. |
void |
incrementAll(double[] values,
int begin,
int length)
This default implementation just calls
increment(double) in a loop over
the specified portion of the input array. |
void |
setData(double[] values)
Set the data array.
|
void |
setData(double[] values,
int begin,
int length)
Set the data array.
|
protected boolean |
test(double[] values,
double[] weights,
int begin,
int length)
This method is used by
evaluate(double[], double[], int, int) methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero. |
protected boolean |
test(double[] values,
double[] weights,
int begin,
int length,
boolean allowEmpty)
This method is used by
evaluate(double[], double[], int, int) methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero. |
protected boolean |
test(double[] values,
int begin,
int length)
This method is used by
evaluate(double[], int, int) methods
to verify that the input parameters designate a subarray of positive length. |
protected boolean |
test(double[] values,
int begin,
int length,
boolean allowEmpty)
This method is used by
evaluate(double[], int, int) methods
to verify that the input parameters designate a subarray of positive length. |
static boolean |
verifyValues(double[] values,
double[] weights,
int begin,
int length,
boolean allowEmpty)
This method is used
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero.
|
static boolean |
verifyValues(double[] values,
int begin,
int length,
boolean allowEmpty) |
protected com.kontakt.sdk.android.ble.math.FirstMoment moment
protected boolean incMoment
Statistics based on (constructed from) external moments cannot be incremented or cleared.
public Mean()
public Mean(com.kontakt.sdk.android.ble.math.FirstMoment m1)
m1
- the momentpublic void increment(double d)
d
- the new value.public void clear()
public double getResult()
Double.NaN
if it
has been cleared or just instantiated.public long getN()
public double evaluate(double[] values, int begin, int length)
Double.NaN
if the designated subarray
is empty.
Throws IllegalArgumentException
if the array is null.
See Mean
for details on the computing algorithm.
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includejava.lang.NullPointerException
- if the array is nulljava.lang.IllegalStateException
- if the array index
parameters are not validpublic double evaluate(double[] values)
clear()
, then invokes
increment(double)
in a loop over the the input array, and then uses
getResult()
to compute the return value.
Note that this implementation changes the internal state of the
statistic. Its side effects are the same as invoking clear()
and
then incrementAll(double[])
.
Implementations may override this method with a more efficient and possibly more accurate implementation that works directly with the input array.
If the array is null, a MathIllegalArgumentException is thrown.
values
- input arrayjava.lang.IllegalStateException
- if values is nullpublic void incrementAll(double[] values)
increment(double)
in a loop over
the input array.
Throws IllegalArgumentException if the input values array is null.
values
- values to addjava.lang.NullPointerException
- if values is nullpublic void incrementAll(double[] values, int begin, int length)
increment(double)
in a loop over
the specified portion of the input array.
Throws IllegalArgumentException if the input values array is null.
values
- array holding values to addbegin
- index of the first array element to addlength
- number of array elements to addjava.lang.NullPointerException
- if values is nullpublic boolean equals(java.lang.Object object)
object
is an
AbstractStorelessUnivariateStatistic
returning the same
values as this for getResult()
and getN()
equals
in class java.lang.Object
object
- object to test equality against.public int hashCode()
hashCode
in class java.lang.Object
public static boolean equals(double x, double y, int maxUlps)
Two float numbers are considered equal if there are (maxUlps - 1)
(or fewer) floating point numbers between them, i.e. two adjacent
floating point numbers are considered equal.
Adapted from Bruce Dawson
x
- first valuey
- second valuemaxUlps
- is the number of floating point
values between
and
.public void setData(double[] values)
The stored value is a copy of the parameter array, not the array itself.
values
- data array to store (may be null to remove stored data)evaluate()
public double[] getData()
protected double[] getDataRef()
public void setData(double[] values, int begin, int length)
values
- data array to storebegin
- the index of the first element to includelength
- the number of elements to include
are not validevaluate()
public double evaluate()
The stored array is the one which was set by previous calls to setData(double[])
.
java.lang.IllegalStateException
- if the stored data array is nullprotected boolean test(double[] values, int begin, int length)
evaluate(double[], int, int)
methods
to verify that the input parameters designate a subarray of positive length.
true
iff the parameters designate a subarray of
positive lengthMathIllegalArgumentException
if the array is null or
or the indices are invalidfalse
length
is 0.
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeprotected boolean test(double[] values, int begin, int length, boolean allowEmpty)
evaluate(double[], int, int)
methods
to verify that the input parameters designate a subarray of positive length.
true
iff the parameters designate a subarray of
non-negative lengthIllegalArgumentException
if the array is null or
or the indices are invalidfalse
length
is 0 unless allowEmpty
is true
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeallowEmpty
- if true
then zero length arrays are allowedjava.lang.NullPointerException
- or IllegalStateException
if the indices are invalid or the array is nullprotected boolean test(double[] values, double[] weights, int begin, int length)
evaluate(double[], double[], int, int)
methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero.
true
iff the parameters designate a subarray of
positive length and the weights array contains legitimate values.IllegalArgumentException
if any of the following are true:
false
length
is 0.
values
- the input arrayweights
- the weights arraybegin
- index of the first array element to includelength
- the number of elements to includejava.lang.NullPointerException
- or java.lang.IllegalStateException if
the indices are invalid or the array is nullpublic static boolean verifyValues(double[] values, double[] weights, int begin, int length, boolean allowEmpty)
true
iff the parameters designate a subarray of
non-negative length and the weights array contains legitimate values.MathIllegalArgumentException
if any of the following are true:
false
length
is 0 unless allowEmpty
is true
.
values
- the input array.weights
- the weights array.begin
- index of the first array element to include.length
- the number of elements to include.allowEmpty
- if true
than allow zero length arrays to pass.true
if the parameters are valid.java.lang.NullPointerException
- if either of the arrays are nulljava.lang.IllegalStateException
- if the array indices are not valid,
the weights array contains NaN, infinite or negative elements, or there
are no positive weights.protected boolean test(double[] values, double[] weights, int begin, int length, boolean allowEmpty)
evaluate(double[], double[], int, int)
methods
to verify that the begin and length parameters designate a subarray of positive length
and the weights are all non-negative, non-NaN, finite, and not all zero.
true
iff the parameters designate a subarray of
non-negative length and the weights array contains legitimate values.MathIllegalArgumentException
if any of the following are true:
false
length
is 0 unless allowEmpty
is true
.
values
- the input array.weights
- the weights array.begin
- index of the first array element to include.length
- the number of elements to include.allowEmpty
- if true
than allow zero length arrays to pass.true
if the parameters are valid.java.lang.NullPointerException
- if either of the arrays are nulljava.lang.IllegalStateException
- if the array indices are not valid,
the weights array contains NaN, infinite or negative elements, or there
are no positive weights.public static boolean verifyValues(double[] values, int begin, int length, boolean allowEmpty)