Class SynchronizedDescriptiveStatistics
- java.lang.Object
-
- org.apache.commons.math4.legacy.stat.descriptive.DescriptiveStatistics
-
- org.apache.commons.math4.legacy.stat.descriptive.SynchronizedDescriptiveStatistics
-
- All Implemented Interfaces:
StatisticalSummary
public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics
Implementation ofDescriptiveStatistics
that is safe to use in a multithreaded environment. Multiple threads can safely operate on a single instance without causing runtime exceptions due to race conditions. In effect, this implementation makes modification and access methods atomic operations for a single instance. That is to say, as one thread is computing a statistic from the instance, no other thread can modify the instance nor compute another statistic.- Since:
- 1.2
-
-
Field Summary
-
Fields inherited from class org.apache.commons.math4.legacy.stat.descriptive.DescriptiveStatistics
INFINITE_WINDOW
-
-
Constructor Summary
Constructors Constructor Description SynchronizedDescriptiveStatistics()
Construct an instance with infinite window.SynchronizedDescriptiveStatistics(int window)
Construct an instance with finite window.SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original)
A copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addValue(double v)
Adds the value to the dataset.double
apply(UnivariateStatistic stat)
Apply the given statistic to the data associated with this set of statistics.void
clear()
Resets all statistics and storage.SynchronizedDescriptiveStatistics
copy()
Returns a copy of this SynchronizedDescriptiveStatistics instance with the same internal state.static void
copy(SynchronizedDescriptiveStatistics source, SynchronizedDescriptiveStatistics dest)
Copies source to dest.double
getElement(int index)
Returns the element at the specified index.long
getN()
Returns the number of available values.double
getQuadraticMean()
Returns the quadratic mean, a.k.a.double
getStandardDeviation()
Returns the standard deviation of the available values.double[]
getValues()
Returns the current set of values in an array of double primitives.int
getWindowSize()
Returns the maximum number of values that can be stored in the dataset, or INFINITE_WINDOW (-1) if there is no limit.void
setWindowSize(int windowSize)
WindowSize controls the number of values that contribute to the reported statistics.String
toString()
Generates a text report displaying univariate statistics from values that have been added.-
Methods inherited from class org.apache.commons.math4.legacy.stat.descriptive.DescriptiveStatistics
copy, getGeometricMean, getGeometricMeanImpl, getKurtosis, getKurtosisImpl, getMax, getMaxImpl, getMean, getMeanImpl, getMin, getMinImpl, getPercentile, getPercentileImpl, getPopulationVariance, getSkewness, getSkewnessImpl, getSortedValues, getSum, getSumImpl, getSumsq, getSumsqImpl, getVariance, getVarianceImpl, removeMostRecentValue, replaceMostRecentValue, setGeometricMeanImpl, setKurtosisImpl, setMaxImpl, setMeanImpl, setMinImpl, setPercentileImpl, setSkewnessImpl, setSumImpl, setSumsqImpl, setVarianceImpl
-
-
-
-
Constructor Detail
-
SynchronizedDescriptiveStatistics
public SynchronizedDescriptiveStatistics()
Construct an instance with infinite window.
-
SynchronizedDescriptiveStatistics
public SynchronizedDescriptiveStatistics(int window) throws MathIllegalArgumentException
Construct an instance with finite window.- Parameters:
window
- the finite window size.- Throws:
MathIllegalArgumentException
- if window size is less than 1 but not equal toDescriptiveStatistics.INFINITE_WINDOW
-
SynchronizedDescriptiveStatistics
public SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original) throws NullArgumentException
A copy constructor. Creates a deep-copy of theoriginal
.- Parameters:
original
- theSynchronizedDescriptiveStatistics
instance to copy- Throws:
NullArgumentException
- if original is null
-
-
Method Detail
-
addValue
public void addValue(double v)
Adds the value to the dataset. If the dataset is at the maximum size (i.e., the number of stored elements equals the currently configured windowSize), the first (oldest) element in the dataset is discarded to make room for the new value.- Overrides:
addValue
in classDescriptiveStatistics
- Parameters:
v
- the value to be added
-
apply
public double apply(UnivariateStatistic stat)
Apply the given statistic to the data associated with this set of statistics.- Overrides:
apply
in classDescriptiveStatistics
- Parameters:
stat
- the statistic to apply- Returns:
- the computed value of the statistic.
-
clear
public void clear()
Resets all statistics and storage.- Overrides:
clear
in classDescriptiveStatistics
-
getElement
public double getElement(int index)
Returns the element at the specified index.- Overrides:
getElement
in classDescriptiveStatistics
- Parameters:
index
- The Index of the element- Returns:
- return the element at the specified index
-
getN
public long getN()
Returns the number of available values.- Specified by:
getN
in interfaceStatisticalSummary
- Overrides:
getN
in classDescriptiveStatistics
- Returns:
- The number of available values
-
getStandardDeviation
public double getStandardDeviation()
Returns the standard deviation of the available values.- Specified by:
getStandardDeviation
in interfaceStatisticalSummary
- Overrides:
getStandardDeviation
in classDescriptiveStatistics
- Returns:
- The standard deviation, Double.NaN if no values have been added or 0.0 for a single value set.
-
getQuadraticMean
public double getQuadraticMean()
Returns the quadratic mean, a.k.a. root-mean-square of the available values- Overrides:
getQuadraticMean
in classDescriptiveStatistics
- Returns:
- The quadratic mean or
Double.NaN
if no values have been added.
-
getValues
public double[] getValues()
Returns the current set of values in an array of double primitives. The order of addition is preserved. The returned array is a fresh copy of the underlying data -- i.e., it is not a reference to the stored data.- Overrides:
getValues
in classDescriptiveStatistics
- Returns:
- returns the current set of numbers in the order in which they were added to this set
-
getWindowSize
public int getWindowSize()
Returns the maximum number of values that can be stored in the dataset, or INFINITE_WINDOW (-1) if there is no limit.- Overrides:
getWindowSize
in classDescriptiveStatistics
- Returns:
- The current window size or -1 if its Infinite.
-
setWindowSize
public void setWindowSize(int windowSize) throws MathIllegalArgumentException
WindowSize controls the number of values that contribute to the reported statistics. For example, if windowSize is set to 3 and the values {1,2,3,4,5} have been added in that order then the available values are {3,4,5} and all reported statistics will be based on these values. IfwindowSize
is decreased as a result of this call and there are more than the new value of elements in the current dataset, values from the front of the array are discarded to reduce the dataset towindowSize
elements.- Overrides:
setWindowSize
in classDescriptiveStatistics
- Parameters:
windowSize
- sets the size of the window.- Throws:
MathIllegalArgumentException
- if window size is less than 1 but not equal toDescriptiveStatistics.INFINITE_WINDOW
-
toString
public String toString()
Generates a text report displaying univariate statistics from values that have been added. Each statistic is displayed on a separate line.- Overrides:
toString
in classDescriptiveStatistics
- Returns:
- String with line feeds displaying statistics
-
copy
public SynchronizedDescriptiveStatistics copy()
Returns a copy of this SynchronizedDescriptiveStatistics instance with the same internal state.- Overrides:
copy
in classDescriptiveStatistics
- Returns:
- a copy of this
-
copy
public static void copy(SynchronizedDescriptiveStatistics source, SynchronizedDescriptiveStatistics dest) throws NullArgumentException
Copies source to dest.Neither source nor dest can be null.
Acquires synchronization lock on source, then dest before copying.
- Parameters:
source
- SynchronizedDescriptiveStatistics to copydest
- SynchronizedDescriptiveStatistics to copy to- Throws:
NullArgumentException
- if either source or dest is null
-
-