本文整理了Java中org.apache.commons.math3.stat.descriptive.moment.Variance
类的一些代码示例,展示了Variance
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Variance
类的具体详情如下:
包路径:org.apache.commons.math3.stat.descriptive.moment.Variance
类名称:Variance
[英]Computes the variance of the available values. By default, the unbiased "sample variance" definitional formula is used:
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.
[中]计算可用值的方差。默认情况下,使用无偏“样本方差”定义公式:
方差=和((x_i-均值)^2)/(n-1)
其中mean是平均值,n
是样本观察数。
定义公式不具有良好的数值特性,因此此实现不使用定义公式计算统计量。
*getResult
方法使用基于West算法的更新公式计算方差,如Chan, T. F. and J. G. Lewis 1979, Communications of the ACM, vol. 22 no. 9, pp. 526-531.中所述
*evaluate
方法利用内存中有完整的值数组这一事实来执行两次传递算法。具体而言,这些方法使用了Chan,Golub,Levesque的“修正二次通过算法”,计算样本方差的算法,美国统计学家,第37卷,第3期(1983)第242-247页。
请注意,使用increment
或incrementAll
添加值,然后执行getResult
有时会得到与使用完整值数组执行evaluate
不同的、不太准确的结果。前一种方法只应在值的完整数组不可用时使用。
“总体方差”(总和((x_i-平均值)^2)/n)也可以使用此统计数据进行计算。isBiasCorrected
属性确定evaluate
和getResult
方法返回的是“总体”还是“样本”值。要计算总体方差,请将此属性设置为false.
请注意,此实现是不同步的。如果多个线程同时访问该类的一个实例,并且至少有一个线程调用increment()
或clear()
方法,则必须在外部对其进行同步。
代码示例来源:origin: org.apache.commons/commons-math3
/** {@inheritDoc} */
@Override
public double score(final List<? extends Cluster<T>> clusters) {
double varianceSum = 0.0;
for (final Cluster<T> cluster : clusters) {
if (!cluster.getPoints().isEmpty()) {
final Clusterable center = centroidOf(cluster);
// compute the distance variance of the current cluster
final Variance stat = new Variance();
for (final T point : cluster.getPoints()) {
stat.increment(distance(point, center));
}
varianceSum += stat.getResult();
}
}
return varianceSum;
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
* population variance</a> of the entries in the input array, or
* <code>Double.NaN</code> if the array is empty.
* <p>
* See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for
* details on the formula and computing algorithm.</p>
* <p>
* Returns 0 for a single-value (i.e. length = 1) sample.</p>
* <p>
* Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
*
* @param values the input array
* @return the population variance of the values or Double.NaN if the array is empty
* @throws MathIllegalArgumentException if the array is null
*/
public static double populationVariance(final double[] values)
throws MathIllegalArgumentException {
return new Variance(false).evaluate(values);
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
* population variance</a> of the values that have been added.
*
* <p>Double.NaN is returned if no values have been added.</p>
*
* @return the population variance
*/
public double getPopulationVariance() {
Variance populationVariance = new Variance(secondMoment);
populationVariance.setBiasCorrected(false);
return populationVariance.getResult();
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* {@inheritDoc}
*/
@Override
public Variance copy() {
Variance result = new Variance();
// No try-catch or advertised exception because parameters are guaranteed non-null
copy(this, result);
return result;
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Constructs a StandardDeviation. Sets the underlying {@link Variance}
* instance's <code>isBiasCorrected</code> property to true.
*/
public StandardDeviation() {
variance = new Variance();
}
代码示例来源:origin: org.apache.commons/commons-math3
Variance variance = new Variance();
variance.incrementAll(values, begin, length);
double mean = variance.moment.m1;
double stdDev = FastMath.sqrt(variance.getResult());
代码示例来源:origin: org.apache.commons/commons-math3
if (test(values, begin, length)) {
clear();
if (length == 1) {
var = 0.0;
Mean mean = new Mean();
double m = mean.evaluate(values, begin, length);
var = evaluate(values, m, begin, length);
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Returns the variance of the entries in the input array, or
* <code>Double.NaN</code> if the array is empty.
*
* <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
* the denominator). Use {@link #populationVariance(double[])} for the non-bias-corrected
* population variance.</p>
* <p>
* See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for
* details on the computing algorithm.</p>
* <p>
* Returns 0 for a single-value (i.e. length = 1) sample.</p>
* <p>
* Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
*
* @param values the input array
* @return the variance of the values or Double.NaN if the array is empty
* @throws MathIllegalArgumentException if the array is null
*/
public static double variance(final double[] values) throws MathIllegalArgumentException {
return VARIANCE.evaluate(values);
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* {@inheritDoc}
*/
@Override
public double getResult() {
return FastMath.sqrt(variance.getResult());
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* {@inheritDoc}
*/
@Override
public void increment(final double d) {
variance.increment(d);
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Copy constructor, creates a new {@code Variance} identical
* to the {@code original}
*
* @param original the {@code Variance} instance to copy
* @throws NullArgumentException if original is null
*/
public Variance(Variance original) throws NullArgumentException {
copy(original, this);
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* @param isBiasCorrected The isBiasCorrected to set.
*/
public void setBiasCorrected(boolean isBiasCorrected) {
variance.setBiasCorrected(isBiasCorrected);
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* {@inheritDoc}
*/
@Override
public void clear() {
variance.clear();
}
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Constructs a StandardDeviation from an external second moment.
*
* @param m2 the external moment
*/
public StandardDeviation(final SecondMoment m2) {
variance = new Variance(m2);
}
代码示例来源:origin: geogebra/geogebra
Variance variance = new Variance();
variance.incrementAll(values, begin, length);
double mean = variance.moment.m1;
double stdDev = Math.sqrt(variance.getResult());
代码示例来源:origin: org.apache.commons/commons-math3
dest.varianceImpl = new Variance(dest.secondMoment);
} else {
dest.varianceImpl = source.varianceImpl.copy();
dest.variance = (Variance) dest.varianceImpl;
} else {
Variance.copy(source.variance, dest.variance);
代码示例来源:origin: org.apache.commons/commons-math3
if (test(values, weights,begin, length)) {
clear();
if (length == 1) {
var = 0.0;
Mean mean = new Mean();
double m = mean.evaluate(values, weights, begin, length);
var = evaluate(values, weights, m, begin, length);
代码示例来源:origin: org.apache.commons/commons-math3
/**
* Returns the variance of the entries in the specified portion of
* the input array, or <code>Double.NaN</code> if the designated subarray
* is empty.
*
* <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
* the denominator). Use {@link #populationVariance(double[], int, int)} for the non-bias-corrected
* population variance.</p>
* <p>
* See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for
* details on the computing algorithm.</p>
* <p>
* Returns 0 for a single-value (i.e. length = 1) sample.</p>
* <p>
* Throws <code>MathIllegalArgumentException</code> if the array is null or the
* array index parameters are not valid.</p>
*
* @param values the input array
* @param begin index of the first array element to include
* @param length the number of elements to include
* @return the variance of the values or Double.NaN if length = 0
* @throws MathIllegalArgumentException if the array is null or the array index
* parameters are not valid
*/
public static double variance(final double[] values, final int begin,
final int length) throws MathIllegalArgumentException {
return VARIANCE.evaluate(values, begin, length);
}
代码示例来源:origin: meyerjp3/psychometrics
/**
* Observed variance of the estimate.
*
* @return observed variance.
*/
public double observedVariance(){
return var.getResult();
}
代码示例来源:origin: geogebra/geogebra
/**
* {@inheritDoc}
*/
@Override
public void increment(final double d) {
variance.increment(d);
}
内容来源于网络,如有侵权,请联系作者删除!