PureBytes Links
Trading Reference Links
|
Firstly, stats is not my strong point, so there's probably an obvious error
in my calculations.
Assuming SD = root(mean(squares(diffs)));
Avoiding the loops - and calculating each step separately for clarity - I
get :
// set array and periods
array = C ;
per = 12 ;
mean = MA(array,12);
diff = (array-mean);
diffsq = diff*diff;
Varsq = Sum(diffsq,per)/(per-1); // or "/per" as per your preference
sd = sqrt(Varsq);
GraphXSpace = 3;
Plot(sd, "sd", colorLightBlue);
Plot(StDev(array,per),"StDev",colorBlue);
Unfortunately the two plots are different, although roughly the same shape.
(But at least it doesn't "result in an AB freeze").
Where to now? Perhaps treliff can spot my obvious mistake?
Paul
----- Original Message -----
From: "treliff" <treliff@xxxxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Tuesday, 23 March 2004 12:45 PM
Subject: [amibroker] code for standard deviation with variable periods
Graham, Dimitris, Paul, Ed and others, thanks to you experts I got
the looping stuff under control, at least sufficiently
for the code below of a standard deviation measurement with variable
periods. (Had been some previous discussion about in the forum but I
did not find a code.)
This code works (AFL StDev is added as a visual check) but this is
sooo-slooow, on my pc more than a few hundred bars loaded results in
an AB freeze. Certainly in view of my plan to combine this one with
other loops in a more complex code, I wonder if these kind of
executions are in fact feasible or perhaps I made a giant detour
somewhere in this code.
If you'd find the time to take a look and give some comments
I'd appreciate it. Thanks in advance.
-treliff
/* code start */
/*
MEAN is mean of data from bar n-1 till current bar.
VAR is variance of data from bar n-1 till current bar.
VR is variance of data of last n bars.
SD = sqrt(VR) is standard deviation of last n bars.
*/
// set array and periods
array = C ;
per = 12 ;
function MEAN(input,n)
{
result[n-1] = input[n-1] ;
for (i=n;i<BarCount;i++)
{
result[i] = ((i-n+1)/(i-n+2))*result[i-1] + (1/(i-n+2))*input[i] ;
}
return result;
}
function VAR(input,n)
{
result[n-1] = 0 ;
for (i=n;i<BarCount;i++)
{
myvar = MEAN(input,n);
result[i] = ((i-n+1)/(i-n+2))*result[i-1] + (1/(i-n+1))*(input[i]-
myvar[i])^2 ;
}
return result;
}
function VR(input,n)
{
for (i=n-1;i<BarCount;i++)
{
myvar = VAR(input,i-n+2);
result[i] = myvar[i] ;
}
return result;
}
function SD(input,n)
{
return sqrt(VR(input,n));
}
GraphXSpace = 3;
Plot(SD(array,per),"SD",colorBlack);
Plot(StDev(array,per),"StDev",colorBlue);
/* code end */
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|