public class Stats {

    private double _sum, _sumsq;
    private int _n;

    /**
     * Create a new instance.
     */

    public Stats() {
	_sum = 0;
	_sumsq = 0;
	_n = 0;
    }

    /**
     * Provide the next value in the sequence.
     */

    public void datum(double v) {
	_sum += v;
	_sumsq += v * v;
	_n += 1;
    }

    /**
     * Compute the arithmetic mean of the sequence.
     */

    public double mean() {
	return( _sum / _n );
    }

    /**
     * Compute the variance of the sequence assuming that the sequence
     * represents a sample of the population.
     */

    public double sampleVariance() {
	return( (_sumsq - (_sum * _sum) / _n) / (_n - 1) );
    }

    /**
     * Compute the variance of the sequence assuming that the sequence
     * represents the entire population.
     */

    public double populationVariance() {
	return( (_sumsq - (_sum * _sum) / _n) / _n );
    }

    /**
     * Compute the variance of the sequence assuming that the sequence
     * represents the entire population.
     */

    public double populationStdev() {
	return( Math.sqrt( populationVariance() ) );
    }

    /**
     * Compute the variance of the sequence assuming that the sequence
     * represents a sample of the population.
     */

    public double sampleStdev() {
	return( Math.sqrt( sampleVariance() ) );
    }

    /**
     * Return the number of elements in the sequence.
     */

    public int n() {
	return( _n );
    }

}

