Solved

# Rolling Standard Deviations on Fortran from the arrays

Posted on 2011-10-28

I want to calculate the rolling 20 day standard deviation on Fortran. I was able to write the following code, but I think I am not adding the sum squares correctly or I am not calculating the SD in the correct fashion that I need to include the averge somewhere. How can change this code so it will work.

SUM(1)=0

SUMSQ(1)=0

Do I4=1,20

SUM(1)=SUM(1)+X(I4)

SUMSQ(1)=SUMSQ(1)+X(I4)**2

Enddo

Do I4=21,M1+19

SUM(I4-19)=SUM(I4-20)+X(I4)-X(I4-20)

SUMSQ(I4-19)=SUMSQ(I4-20)+X(I4)**2 -X(I4-20)**2

End Do

DO I4=1,M1

AVG(I4) =SUM(I4)/20

SD(I4)=SQRT((SUMSQ(I4))/19)

Write(15,*) SD(I4),AVG(I4),SUMSQ(I4)

END DO