As mentioned in my earlier post original C++ rosettacode sucks.
Here is my example for a simple moving variance and standard deviation. One has to add only a few lines of code:
in add()
...
sum2 = sum2 - oldValue*oldValue + value*value;
...
double var() const {
return sum2/size - avg()*avg();
}
double std() const {
return std::sqrt(var());
}
Data1 Period 3 Added 1 avg: 1 var: 0 std: 0 Added 2 avg: 1.5 var: 0.25 std: 0.5 Added 3 avg: 2 var: 0.666667 std: 0.816497 Added 4 avg: 3 var: 0.666667 std: 0.816497 Added 5 avg: 4 var: 0.666667 std: 0.816497 Added 5 avg: 4.66667 var: 0.222222 std: 0.471405 Added 4 avg: 4.66667 var: 0.222222 std: 0.471405 Added 3 avg: 4 var: 0.666667 std: 0.816497 Added 2 avg: 3 var: 0.666667 std: 0.816497 Added 1 avg: 2 var: 0.666667 std: 0.816497 Data2 Period 8 Added 2 avg: 2 var: 0 std: 0 Added 4 avg: 3 var: 1 std: 1 Added 4 avg: 3.33333 var: 0.888889 std: 0.942809 Added 4 avg: 3.5 var: 0.75 std: 0.866025 Added 5 avg: 3.8 var: 0.96 std: 0.979796 Added 5 avg: 4 var: 1 std: 1 Added 7 avg: 4.42857 var: 1.95918 std: 1.39971 Added 9 avg: 5 var: 4 std: 2
ToDo: add Skewness and Kurtosis