[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to calculate STDev?



PureBytes Links

Trading Reference Links

>But I don't think you can do that with the SD calculation.  You
>could add in the SumX2 term, but the square(SumX)/Ntrades term
>presents a problem.  You might know Ntrades ahead of time --
>let's say you always want to look at the SD of the last 20
>samples -- but how do you add in one piece of the square(SumX)
>term, without knowing the sum of all X's?  square(SumX)/Ntrades
>is eqivalent to SumX*Avg, so you could subtract the SumX term,
>but you don't know Avg until the end of the sample period.  I may
>be missing something, but I don't think there's any way to
>calculate the SD accurately, in a sample-by-sample sum-it-up-as-
>you-go approach, given TS's precision limitations.  I think you
>have to do it after-the-fact, the way TS's functions do.

You can greatly improve the accuracy by subtracting a constant from all
values since:

    StdDev(Price, Length) = StdDev(Price - A, Length)

where A is any constant.

Thus, to take the standard deviation of the SP (about 880) could calculate:

    StdDev(Price - 880, Length)

using your formula.

I liked your derivation. I use that formula all the time but I
cheated and got it from the "Help" for the Excel STDEV function...

The code is listed below and the ELA is attached.

Bob Fulks

-----

{ *******************************************************************

    Function      : StandardDevFC

    Last Edit     : 5/10/2002

    Provided By   : Bob Fulks

    Description   : This is a faster version of the StandardDev
       function. It uses the formula from the Excel help file for
       STDEV with three additions:
          > Is keeps a running sum to avoid the look-back loop
          > It offsets each value of Price by a constant to
            eliminate the need to take the difference of two
            large values.
          > It does an exact calculation every 100 bars to
            reduce round-off error accumulation and reset
            the offset

            © 2002 Bob Fulks, All rights reserved.

********************************************************************}

Inputs:
    Price(numericseries),
    Length(numericsimple),
    DataType(numericsimple); { pass in 1 for population, 2 for sample }

Variables:
    Divisor(iff(DataType = 1, Length * Length, Length * (Length - 1))),
    SumSqr(0),
    Sum(0),
    Offset(0),
    j(0),
    Init(TRUE);

StandardDevFC = 0;

if Divisor > 0 then begin
    if Init or Mod(CurrentBar, 100) = 0 then begin
       Init = FALSE;
       Sum = 0;
       SumSqr = 0;
       Offset = Price;
       for j = 0 to Length - 1 begin
          Sum = Sum + Price[j] - Offset;
          SumSqr = SumSqr + Square(Price[j] - Offset);
       end;
    end else begin
       Sum = Sum + Price - Price[Length];
       SumSqr = SumSqr + Square(Price - Offset) -
           Square(Price[Length] - Offset);
    end;
    Value1 = Length * SumSqr - Square(Sum);
    if Value1 > 0
       then StandardDevFC = SquareRoot(Value1/ Divisor)
       else Print("StandardDevFC Error", Date:8:0, Time:5:0,
               CurrentBar:6:0, Value1:8:2);
end;



Attachment:
%STDEV_FC.ELA
Description: application/applefile
Attachment:

Attachment: Description: "Description: Binary data"

Attachment: Description: "STDEV_FC.ELA"