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




IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
  select input-file assign to "input.txt"
    organization is line sequential.
data division.
file section.
fd input-file.
  01  inp-record.
    03  inp-fld  pic 9(03).
working-storage section.
01  filler pic 9(01)   value 0.
  88 no-more-input     value 1.
01  ws-tb-data.
  03  ws-tb-size         pic 9(03).
  03  ws-tb-table.
    05  ws-tb-fld     pic s9(05)v9999 comp-3 occurs 0 to 100 times 
        depending on ws-tb-size.
01 ws-stddev       pic s9(05)v9999 comp-3.
PROCEDURE DIVISION.
  move 0 to ws-tb-size 
  open  input input-file
    read input-file
    at end
      set no-more-input to true
    end-read
    perform
      test after
    until no-more-input
      add 1 to ws-tb-size
      move inp-fld to ws-tb-fld (ws-tb-size)
      call 'stddev' using  by reference ws-tb-data 
         ws-stddev
      display  'inp=' inp-fld ' stddev=' ws-stddev
      read input-file at end set no-more-input to true end-read
    end-perform
  close input-file
  stop run.
end program run-stddev.
IDENTIFICATION DIVISION.
PROGRAM-ID. stddev.
data division.
working-storage section.
01 ws-tbx             pic s9(03) comp.
01 ws-tb-work.
  03  ws-sum          pic s9(05)v9999 comp-3 value +0.
  03  ws-sumsq        pic s9(05)v9999 comp-3 value +0.
  03  ws-avg          pic s9(05)v9999 comp-3 value +0.
linkage section.
01  ws-tb-data.
  03  ws-tb-size         pic 9(03).
  03  ws-tb-table.
    05  ws-tb-fld     pic s9(05)v9999 comp-3 occurs 0 to 100 times 
        depending on ws-tb-size.
01  ws-stddev       pic s9(05)v9999 comp-3.
PROCEDURE DIVISION using  ws-tb-data  ws-stddev.
    compute ws-sum = 0
    perform test before varying ws-tbx from 1 by +1 until ws-tbx > ws-tb-size
        compute ws-sum = ws-sum + ws-tb-fld (ws-tbx) 
    end-perform
    compute ws-avg rounded = ws-sum / ws-tb-size
    compute ws-sumsq = 0
    perform test before varying ws-tbx from 1 by +1 until ws-tbx > ws-tb-size
        compute ws-sumsq = ws-sumsq
        + (ws-tb-fld (ws-tbx) - ws-avg) ** 2.0
    end-perform
    compute ws-stddev = ( ws-sumsq / ws-tb-size) ** 0.5 
    goback.
end program stddev.
 