I'm a newbie to thinkscript. As such I decided to write my own little piece of code to calculate the std of a set of data. To do this I used the sum function. However, when I compared the results to the inbuilt std formula there was a difference between my calculation and that of the inbuilt function. When I looked at the code for the inbuilt function (provided in the thinkscript help pages) I noticed that it was using the fold command rather than the sum command.
I must admit I am totally lost as to why my code doesn't give the same answer. I have posted it below. Have I just made a silly booboo or is there something fundamentally wrong with my understanding (or both?). (I was expecting the output of my code to be a straight line with yy=0.)
script getstd_diff {
input y = close;
input length = 50;
def av_y= Average(y,length);
def mystd = Sqrt( Sum(Sqr(y-av_y),length)/length );
def inbuilt = StDev(close,length);
plot difff=mystd-inbuilt;
}
declare lower;
input length = 50;
plot yy=getstd_diff(close,length);
I must admit I am totally lost as to why my code doesn't give the same answer. I have posted it below. Have I just made a silly booboo or is there something fundamentally wrong with my understanding (or both?). (I was expecting the output of my code to be a straight line with yy=0.)
script getstd_diff {
input y = close;
input length = 50;
def av_y= Average(y,length);
def mystd = Sqrt( Sum(Sqr(y-av_y),length)/length );
def inbuilt = StDev(close,length);
plot difff=mystd-inbuilt;
}
declare lower;
input length = 50;
plot yy=getstd_diff(close,length);