DoubleStatistics.java [src/java/util] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.DoubleSummaryStatistics;

/**
 *
 * @author sidereus
 */
// from https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
public class DoubleStatistics extends DoubleSummaryStatistics {

  private double delta;
  private double m = 0.0d;
  private double m3 = 0.0d;
  private double m2 = 0.0d;

  @Override
  public void accept(double value) {
    super.accept(value);

    delta = value - m;
    double delta_n = delta / getCount();
    double term1 = delta * delta_n * (getCount() - 1);
    m = m + delta_n;
    m3 = m3 + term1 * delta_n * (getCount() - 2) - (3 * delta_n * m2);
    m2 = m2 + term1;

  }

  public double getStandardDeviation() {
    return Math.sqrt(m2 / getCount());
  }

  public double getSkewness() {
    double num = Math.sqrt(getCount()) * m3;
    double p = 3.0 / 2.0;
    double den = Math.pow(m2, p);
    return num / den;
  }

}