March 2016
Intermediate to advanced
73 pages
1h 33m
English
functionfixedWindow(size){this.name='window';this.ready=false;this.points=[];this.total=0;this.sos=0;this.push=function(newValue){if(this.points.length==size){varremoved=this.points.shift();this.total-=removed;this.sos-=removed*removed;}this.total+=newValue;this.sos+=newValue*newValue;this.points.push(newValue);this.ready=(this.points.length==size);}this.mean=function(){if(this.points.length==0){return0;}returnthis.total/this.points.length;}this.stddev=function(){varmean=this.mean();returnMath.sqrt(this.sos/this.points.length-mean*mean);}}varwindow=newfixedWindow(5);window.push(1);window.push(5);window.push(9);console.log(window);console.log(window.mean());console.log(window.stddev()*3);
functionmovingAverage(alpha){this.name='ewma';this.ready=true;functionma(){this.value=NaN;this.push=function(newValue){if(isNaN(this.value)){this.value=newValue;ready=true;return;}this.value=alpha*newValue+(1-alpha)*this.value;};}this.MA=newma(alpha);this.sosMA=newma(alpha);this.push=function(newValue){this.MA.push(newValue);this.sosMA.push(newValue*newValue);};this.mean=function(){returnthis.MA.
Read now
Unlock full access