-1

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

Khurram Majeed
  • 2,291
  • 8
  • 37
  • 59
  • 1
    See: http://stackoverflow.com/questions/1930454/what-is-a-good-solution-for-calculating-an-average-where-the-sum-of-all-values-e and http://stackoverflow.com/questions/895929/how-do-i-determine-the-standard-deviation-stddev-of-a-set-of-values/897463 – qqilihq Feb 20 '14 at 16:10

2 Answers2

1

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

0

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());
prunge
  • 22,460
  • 3
  • 73
  • 80