I saw this algorithm in an answer to this [question](https://stackoverflow.com/questions/11978667/online-algorithm-for-calculating-standard-deviation).

Does this correctly calculate standard deviation? Can someone walk me through why this works mathematically? Preferably working back from this formula:

![enter image description here](https://i.stack.imgur.com/T9kXX.png)

```
public class Statistics {

    private int n;
    private double sum;
    private double sumsq;

    public void reset() {
        this.n = 0;
        this.sum = 0.0;
        this.sumsq = 0.0;
    }

    public synchronized void addValue(double x) {
        ++this.n;
        this.sum += x;
        this.sumsq += x*x;
    }

    public synchronized double calculateMean() {
        double mean = 0.0;
        if (this.n > 0) {
            mean = this.sum/this.n;
        }
        return mean;
    }

    public synchronized double calculateStandardDeviation() {
        double deviation = 0.0;
        if (this.n > 1) {
            deviation = Math.sqrt((this.sumsq - this.sum*this.sum/this.n)/(this.n-1));
        }
        return deviation;
    }
}

```