Question

If I have a data feed of numbers for a variable, how can I calculate Mean and SD for this variable on the fly i.e update it every time a new input value becomes available.

I am looking for a solution in Java environment.

regards

Was it helpful?

Solution

For mean: You would keep two variables: 1. A runnting total 2. A running count of items seen so far. Mean is simply running total divided by running count.

For standard deviation: See John D. Cook's Accurately computing running variance. I have used it in the past in SQL using window function and found it very useful

OTHER TIPS

You can use the SummaryStatistics class in Commons Math library to do this.

SummaryStatistics stats = new SummaryStatistics();
stats.addValue(1.0);
stats.addValue(2.0);
stats.addValue(3.5);
stats.addValue(8.0);

System.out.println("Mean: " + stats.getMean() + ", SD: " + stats.getStandardDeviation());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top