An example using javascript, for comparison:
https://jsfiddle.net/drzaus/Lxsa4rpz/
function calcNormalAvg(list) {
// sum(list) / len(list)
return list.reduce(function(a, b) { return a + b; }) / list.length;
}
function calcRunningAvg(previousAverage, currentNumber, index) {
// [ avg' * (n-1) + x ] / n
return ( previousAverage * (index - 1) + currentNumber ) / index;
}
(function(){_x000D_
// populate base list_x000D_
var list = [];_x000D_
function getSeedNumber() { return Math.random()*100; }_x000D_
for(var i = 0; i < 50; i++) list.push( getSeedNumber() );_x000D_
_x000D_
// our calculation functions, for comparison_x000D_
function calcNormalAvg(list) {_x000D_
// sum(list) / len(list)_x000D_
return list.reduce(function(a, b) { return a + b; }) / list.length;_x000D_
}_x000D_
function calcRunningAvg(previousAverage, currentNumber, index) {_x000D_
// [ avg' * (n-1) + x ] / n_x000D_
return ( previousAverage * (index - 1) + currentNumber ) / index;_x000D_
}_x000D_
function calcMovingAvg(accumulator, new_value, alpha) {_x000D_
return (alpha * new_value) + (1.0 - alpha) * accumulator;_x000D_
}_x000D_
_x000D_
// start our baseline_x000D_
var baseAvg = calcNormalAvg(list);_x000D_
var runningAvg = baseAvg, movingAvg = baseAvg;_x000D_
console.log('base avg: %d', baseAvg);_x000D_
_x000D_
var okay = true;_x000D_
_x000D_
// table of output, cleaner console view_x000D_
var results = [];_x000D_
_x000D_
// add 10 more numbers to the list and compare calculations_x000D_
for(var n = list.length, i = 0; i < 10; i++, n++) {_x000D_
var newNumber = getSeedNumber();_x000D_
_x000D_
runningAvg = calcRunningAvg(runningAvg, newNumber, n+1);_x000D_
movingAvg = calcMovingAvg(movingAvg, newNumber, 1/(n+1));_x000D_
_x000D_
list.push(newNumber);_x000D_
baseAvg = calcNormalAvg(list);_x000D_
_x000D_
// assert and inspect_x000D_
console.log('added [%d] to list at pos %d, running avg = %d vs. regular avg = %d (%s), vs. moving avg = %d (%s)'_x000D_
, newNumber, list.length, runningAvg, baseAvg, runningAvg == baseAvg, movingAvg, movingAvg == baseAvg_x000D_
)_x000D_
results.push( {x: newNumber, n:list.length, regular: baseAvg, running: runningAvg, moving: movingAvg, eqRun: baseAvg == runningAvg, eqMov: baseAvg == movingAvg } );_x000D_
_x000D_
if(runningAvg != baseAvg) console.warn('Fail!');_x000D_
okay = okay && (runningAvg == baseAvg); _x000D_
}_x000D_
_x000D_
console.log('Everything matched for running avg? %s', okay);_x000D_
if(console.table) console.table(results);_x000D_
})();
_x000D_