PureBytes Links
Trading Reference Links
|
Paul, yours is a new sd measurement and I can't judge its value
but it's not the regular one.
Look at bars 1 and 2 in your code assuming period =2:
Array1: C1 array2: C2
Mean1: (C0+C1)/2 mean2: (C1+C2)/2
Diff1: C1-(C0+C1)/2 diff2: C2-(C1+C2)/2
Diffsq1: (C1-(C0+C1)/2)^2 diffsq2: (C2-(C1+C2)/2)^2
Varsq1: --
varsq2: ( (C1-(C0+C1)/2)^2 + (C2-(C1+C2)/2)^2 ) / 2
Compare your varsq2 to what it should be:
( (C1-(C0+C1)/2)^2 + (C2-(C1+C2)/2)^2 ) / 2 (your varsq2)
( (C1-(C1+C2)/2)^2 + (C2-(C1+C2)/2)^2 ) / 2 (what it should be)
something's wrong on the left side.
I had quite some work finding the code because (when thinking in
grid terms) to find VR one has to jump "diagonally" from VAR
cell to
VAR cell, specifically (using notation as in my code without array):
VR(n)[i] = VAR(i-n+2)[i]
so
VR(2)[1] = VAR(1)[1]
VR(2)[2] = VAR(2)[2]
VR(2)[3] = VAR(3)[3]
and
VR(3)[2] = VAR(1)[2]
VR(3)[3] = VAR(2)[3]
VR(3)[4] = VAR(3)[4]
I could find no other way but pls do let me know if indeed this
looping appears unnecessary.
-treliff
--- In amibroker@xxxxxxxxxxxxxxx, "Paul Chivers" <pchivers@xxxx>
wrote:
> 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@xxxx>
> 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/
|