Task[edit]
Write a stateful function, class, generator or co-routine that takes a series of floating point numbers, one at a time, and returns the running standard deviation of the series.

The task implementation should use the most natural programming style of those listed for the function in the implementation language; the task must state which is being used.

Do not apply Bessel's correction; the returned standard deviation should always be computed as if the sample seen so far is the entire population.



Test case

Use this to compute the standard deviation of this demonstration set, 



{
2
,
4
,
4
,
4
,
5
,
5
,
7
,
9
}


{\displaystyle \{2,4,4,4,5,5,7,9\}}

, which is 



2


{\displaystyle 2}

.



Related tasks

 Random numbers


Tasks for calculating statistical measures



in one go
moving (sliding window)
moving (cumulative)

Mean
Arithmetic

Statistics/Basic
Averages/Arithmetic mean
Averages/Pythagorean means


Averages/Simple moving average




Geometric

Averages/Pythagorean means




Harmonic

Averages/Pythagorean means




Quadratic

Averages/Root mean square




Circular

Averages/Mean angle
Averages/Mean time of day




Median

Averages/Median




Mode

Averages/Mode




Standard deviation

Statistics/Basic



Cumulative standard deviation




INCLUDE "H6:REALMATH.ACT"
 
REAL sum,sum2
INT count
 
PROC Calc(REAL POINTER x,sd)
  REAL tmp1,tmp2,tmp3
 
  RealAdd(sum,x,tmp1)       ;tmp1=sum+x
  RealAssign(tmp1,sum)      ;sum=sum+x
  RealMult(x,x,tmp1)        ;tmp1=x*x
  RealAdd(sum2,tmp1,tmp2)   ;tmp2=sum2+x*x
  RealAssign(tmp2,sum2)     ;sum2=sum2+x*x
  count==+1
  IF count=0 THEN
    IntToReal(0,sd)         ;sd=0
  ELSE
    IntToReal(count,tmp1)
    RealMult(sum,sum,tmp2)  ;tmp2=sum*sum
    RealDiv(tmp2,tmp1,tmp3) ;tmp3=sum*sum/count
    RealDiv(tmp3,tmp1,tmp2) ;tmp2=sum*sum/count/count
    RealDiv(sum2,tmp1,tmp3) ;tmp3=sum2/count
    RealSub(tmp3,tmp2,tmp1) ;tmp1=sum2/count-sum*sum/count/count
    Sqrt(tmp1,sd)           ;sd=sqrt(sum2/count-sum*sum/count/count)
  FI
RETURN
 
PROC Main()
  INT ARRAY values=[2 4 4 4 5 5 7 9]
  INT i
  REAL x,sd
 
  Put(125) PutE() ;clear screen
  MathInit()
  IntToReal(0,sum)
  IntToReal(0,sum2)
  count=0
  FOR i=0 TO 7
  DO
    IntToReal(values(i),x)
    Calc(x,sd)
    Print("x=") PrintR(x)
    Print(" sum=") PrintR(sum)
    Print(" sd=") PrintRE(sd)
  OD
RETURN