Java Utililty Methods Variance

List of utility methods to do Variance

Description

The list of methods to do Variance are organized into topic(s).

Method

doublevariance(double[] v)
Computes the (bias-corrected sample) variance.
if (v.length > 1) {
    final double m = mean(v);
    double ans = 0.0;
    for (int i = 0; i < v.length; i++)
        ans += (v[i] - m) * (v[i] - m);
    return ans / (v.length - 1);
} else
    throw new IllegalArgumentException("Array length must be of 2 or greater.");
...
doublevariance(double[] vals)
|vals| is const.
if (vals.length == 0) {
    return Double.NaN;
double mean = mean(vals);
double[] deviations = new double[vals.length];
for (int i = 0; i < vals.length; i++) {
    deviations[i] = Math.pow(vals[i] - mean, 2);
return mean(deviations);
doublevariance(double[] values)
Computes the sample variance of the elements of the vector.
if (values.length < 2) {
    return 0;
double mean = mean(values);
double var = 0;
for (double d : values) {
    var += (d - mean) * (d - mean);
return var / (values.length - 1);
doublevariance(double[] values, boolean isUnbiased)
variance
double variance = Double.NaN;
double mean = arithmeticMean(values);
int size = values.length;
if (size > 0) {
    double sum = 0.0;
    for (int i = 0; i < size; i++) {
        sum += Math.pow((values[i] - mean), 2.0);
    if (isUnbiased) {
        variance = sum / (size - 1);
    } else {
        variance = sum / size;
return variance;
doublevariance(double[] vector)
Computes the variance for an array of doubles.
double sum = 0, sumSquared = 0;
if (vector.length <= 1) {
    return 0;
for (int i = 0; i < vector.length; i++) {
    sum += vector[i];
    sumSquared += (vector[i] * vector[i]);
return (sumSquared - (sum * sum / (double) vector.length)) / (double) (vector.length - 1);
doublevariance(double[] vector)
variance
return sumOfMeanDifferencesOnePoint(vector) / vector.length;
doublevariance(double[] vector)
variance
double sum = 0, sumSquared = 0;
if (vector.length <= 1) {
    return 0;
for (int i = 0; i < vector.length; i++) {
    sum += vector[i];
    sumSquared += (vector[i] * vector[i]);
double result = (sumSquared - (sum * sum / (double) vector.length)) / (double) (vector.length - 1);
if (result < 0) {
    return 0;
} else {
    return result;
doublevariance(final double[] values)
Computes the (bias-adjusted) variance of the values in the input array.
final int length = values.length;
final double mean = mean(values);
double var = Double.NaN;
if (length == 1) {
    var = 0.0;
} else if (length > 1) {
    double accum = 0.0;
    double dev = 0.0;
...
doublevariance(final double[] values, final double mean)
variance
final double[] squares = pow(shift(values, -mean), 2);
return sum(squares) / (squares.length - 1);
floatvariance(final int[] scores)
variance
if (scores.length == 0) {
    return 0f;
final float mean = mean(scores);
float variance = 0f;
for (int s : scores) {
    float deviation = s - mean;
    variance += (deviation * deviation);
...