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




******** Standard deviation of a population
STDDEV   CSECT
         USING  STDDEV,R13
SAVEAREA B      STM-SAVEAREA(R15)
         DC     17F'0'
         DC     CL8'STDDEV'
STM      STM    R14,R12,12(R13)
         ST     R13,4(R15)
         ST     R15,8(R13)
         LR     R13,R15
         SR     R8,R8           s=0
         SR     R9,R9           ss=0
         SR     R4,R4           i=0
         LA     R6,1
         LH     R7,N
LOOPI    BXH    R4,R6,ENDLOOPI
         LR     R1,R4           i
         BCTR   R1,0
         SLA    R1,1
         LH     R5,T(R1)
         ST     R5,WW           ww=t(i)
         MH     R5,=H'1000'     w=ww*1000
         AR     R8,R5           s=s+w
         LR     R15,R5
         MR     R14,R5          w*w
         AR     R9,R15          ss=ss+w*w
         LR     R14,R8          s
         SRDA   R14,32
         DR     R14,R4          /i
         ST     R15,AVG         avg=s/i
         LR     R14,R9          ss
         SRDA   R14,32
         DR     R14,R4          ss/i
         LR     R2,R15          ss/i
         LR     R15,R8          s
         MR     R14,R8          s*s
         LR     R3,R15
         LR     R15,R4          i
         MR     R14,R4          i*i
         LR     R1,R15
         LA     R14,0
         LR     R15,R3
         DR     R14,R1          (s*s)/(i*i)
         SR     R2,R15
         LR     R10,R2          std=ss/i-(s*s)/(i*i)
         LR     R11,R10         std
         SRA    R11,1           x=std/2
         LR     R12,R10         px=std
LOOPWHIL EQU    *
         CR     R12,R11         while px<>=x
         BE     ENDWHILE
         LR     R12,R11         px=x
         LR     R15,R10         std
         LA     R14,0
         DR     R14,R12         /px
         LR     R1,R12          px
         AR     R1,R15          px+std/px
         SRA    R1,1            /2
         LR     R11,R1          x=(px+std/px)/2
         B      LOOPWHIL
ENDWHILE EQU    *
         LR     R10,R11
         CVD    R4,P8           i
         MVC    C17,MASK17
         ED     C17,P8
         MVC    BUF+2(1),C17+15
         L      R1,WW
         CVD    R1,P8
         MVC    C17,MASK17
         ED     C17,P8
         MVC    BUF+10(1),C17+15
         L      R1,AVG
         CVD    R1,P8
         MVC    C18,MASK18
         ED     C18,P8
         MVC    BUF+17(5),C18+12
         CVD    R10,P8          std
         MVC    C18,MASK18
         ED     C18,P8
         MVC    BUF+31(5),C18+12
         WTO    MF=(E,WTOMSG)		  
         B      LOOPI
ENDLOOPI EQU    *
         L      R13,4(0,R13)
         LM     R14,R12,12(R13)
         XR     R15,R15
         BR     R14
         DS     0D
N        DC     H'8'
T        DC     H'2',H'4',H'4',H'4',H'5',H'5',H'7',H'9'
WW       DS     F
AVG      DS     F
P8       DS     PL8
MASK17   DC     C' ',13X'20',X'2120',C'-'              
MASK18   DC     C' ',10X'20',X'2120',C'.',3X'20',C'-' 
C17      DS     CL17
C18      DS     CL18
WTOMSG   DS     0F
         DC     H'80',XL2'0000'
BUF      DC     CL80'N=1  ITEM=1  AVG=1.234  STDDEV=1.234 '
         YREGS  
         END    STDDEV