문제

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

도움이 되었습니까?

해결책

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

다른 팁

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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top