@Test public void testVarianceSingleValue() { assertTrue(Double.isNaN(SampleStats.variance(singleValue, true))); }
public static double variance(Double[] values, boolean ignoreNaN) { if (values.length < 2) { return Double.NaN; } double mNew, mOld = 0.0d, s = 0.0d; int n = 0; for (int i = 0; i < values.length; i++) { if (Double.isNaN(values[i])) { if (!ignoreNaN) { return Double.NaN; } } else { n++; if (n == 1) { mNew = mOld = values[i]; } else { mNew = mOld + (values[i] - mOld) / n; s = s + (values[i] - mOld) * (values[i] - mNew); mOld = mNew; } } } if (n > 1) { return s / (n - 1); } else if (n == 1) { return 0.0d; } else { return Double.NaN; } }
SampleStats { public static double variance(Double[] values, boolean ignoreNaN) { if (values.length < 2) { return Double.NaN; } double mNew, mOld = 0.0d, s = 0.0d; int n = 0; for (int i = 0; i < values.length; i++) { if (Double.isNaN(values[i])) { if (!ignoreNaN) { return Double.NaN; } } else { n++; if (n == 1) { mNew = mOld = values[i]; } else { mNew = mOld + (values[i] - mOld) / n; s = s + (values[i] - mOld) * (values[i] - mNew); mOld = mNew; } } } if (n > 1) { return s / (n - 1); } else if (n == 1) { return 0.0d; } else { return Double.NaN; } } }
SampleStats { public static double variance(Double[] values, boolean ignoreNaN) { if (values.length < 2) { return Double.NaN; } double mNew, mOld = 0.0d, s = 0.0d; int n = 0; for (int i = 0; i < values.length; i++) { if (Double.isNaN(values[i])) { if (!ignoreNaN) { return Double.NaN; } } else { n++; if (n == 1) { mNew = mOld = values[i]; } else { mNew = mOld + (values[i] - mOld) / n; s = s + (values[i] - mOld) * (values[i] - mNew); mOld = mNew; } } } if (n > 1) { return s / (n - 1); } else if (n == 1) { return 0.0d; } else { return Double.NaN; } }  }
SampleStats { public static double variance(Double[] values, boolean ignoreNaN) { if (values.length < 2) { return Double.NaN; } double mNew, mOld = 0.0d, s = 0.0d; int n = 0; for (int i = 0; i < values.length; i++) { if (Double.isNaN(values[i])) { if (!ignoreNaN) { return Double.NaN; } } else { n++; if (n == 1) { mNew = mOld = values[i]; } else { mNew = mOld + (values[i] - mOld) / n; s = s + (values[i] - mOld) * (values[i] - mNew); mOld = mNew; } } } if (n > 1) { return s / (n - 1); } else if (n == 1) { return 0.0d; } else { return Double.NaN; } }  static double max(Double[] values, boolean ignoreNaN); static double mean(Double[] values, boolean ignoreNaN); static double min(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double median(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double mode(Double[] values, boolean ignoreNaN); static double range(Double[] values, boolean ignoreNaN); static double variance(Double[] values, boolean ignoreNaN); static double sdev(Double[] values, boolean ignoreNaN); static double sum(Double[] values, boolean ignoreNaN); }
SampleStats { public static double variance(Double[] values, boolean ignoreNaN) { if (values.length < 2) { return Double.NaN; } double mNew, mOld = 0.0d, s = 0.0d; int n = 0; for (int i = 0; i < values.length; i++) { if (Double.isNaN(values[i])) { if (!ignoreNaN) { return Double.NaN; } } else { n++; if (n == 1) { mNew = mOld = values[i]; } else { mNew = mOld + (values[i] - mOld) / n; s = s + (values[i] - mOld) * (values[i] - mNew); mOld = mNew; } } } if (n > 1) { return s / (n - 1); } else if (n == 1) { return 0.0d; } else { return Double.NaN; } }  static double max(Double[] values, boolean ignoreNaN); static double mean(Double[] values, boolean ignoreNaN); static double min(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double median(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double mode(Double[] values, boolean ignoreNaN); static double range(Double[] values, boolean ignoreNaN); static double variance(Double[] values, boolean ignoreNaN); static double sdev(Double[] values, boolean ignoreNaN); static double sum(Double[] values, boolean ignoreNaN);  }