java.lang.Object
org.bzdev.math.stats.BasicStatsMV
org.bzdev.math.stats.BasicStatsMV.Sample
- Enclosing class:
- BasicStatsMV
Class to compute a sample variance.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.math.stats.BasicStatsMV
BasicStatsMV.Population, BasicStatsMV.Sample
-
Constructor Summary
ConstructorsConstructorDescriptionSample
(double[][] arrays, int n) Constructor given an array containing the initial dataset used to compute a variance.Sample
(double[] means, double[] variances, long m) Constructor given means, variances, and the data-set size (the number for each variable)Sample
(int n) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected double
The elements of the variances array that is computed will be scaled by a correction factor.static final org.bzdev.math.stats.BasicStatsMV.Sample.Identity
identity
(int n) Get an instance of Sample that serves as an identity forStream.reduce(Object,BiFunction,BinaryOperator)
.Methods inherited from class org.bzdev.math.stats.BasicStatsMV
add, addAll, getMeans, getMeans, getSDevs, getSDevs, getVariances, getVariances, length, size
-
Constructor Details
-
Sample
public Sample(int n) Constructor. The methodBasicStatsMV.add(double[])
must be called repeatedly to provide the dataset used to compute the variance and mean array.- Parameters:
n
- the number of variables
-
Sample
public Sample(double[] means, double[] variances, long m) Constructor given means, variances, and the data-set size (the number for each variable)- Parameters:
means
- an array of length n containing the mean values for n variablesvariances
- an array of length n containing the variances for n variablesm
- the data-set size- Throws:
IllegalArgumentException
- the array lengths differ
-
Sample
Constructor given an array containing the initial dataset used to compute a variance. This dataset can be extended using theBasicStatsMV.add(double[])
method.- Parameters:
arrays
- an array, each component of which is an array containing the values for n variablesn
- the number of variables- Throws:
IllegalArgumentException
- the argument arrays was too short
-
-
Method Details
-
identity
public static final org.bzdev.math.stats.BasicStatsMV.Sample.Identity identity(int n) Get an instance of Sample that serves as an identity forStream.reduce(Object,BiFunction,BinaryOperator)
. An identity implementation will not store any data, but will return an appropriate instance of BasicStats that does store the data added, but with no history of previously added values.If rv is a random variable that returns a stream of double values, the following example shows a typical use of this method:
The method mapToObj is needed because the reduce method is a method ofBasicStatsMV stats = rvs.parallelStream(1000000) .mapToObj(Double::valueOf) .reduce(BasicStats.Sample.identity(10), BasicStats::add, BasicStats::addAll);
Stream
but notDoubleStream
. When used with a parallel stream, the mapToObj method;'s argument will typically be a lambda expression that peforms significantly more compuation: otherwise the cost of threading can result in the parallel version performing worse than the sequential version.- Parameters:
n
- the length of each array that can be entered- Returns:
- an instance of BasicStat that can be used as an idenity in a "reduce" method.
-
getCorrection
protected double getCorrection()Description copied from class:BasicStatsMV
The elements of the variances array that is computed will be scaled by a correction factor. This factor should be 1.0 when the dataset represents a total population. For a sample of a population, the factor is m/(m-1) where m is the sample size (the number of factors.- Specified by:
getCorrection
in classBasicStatsMV
- Returns:
- the correction factor; Double.NaN if the factor cannot be computed due to too few entries
-