How to resolve the algorithm Cumulative standard deviation step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cumulative standard deviation step by step in the OCaml programming language
Table of Contents
Problem Statement
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.
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}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cumulative standard deviation step by step in the OCaml programming language
Source code in the ocaml programming language
let sqr x = x *. x
let stddev l =
let n, sx, sx2 =
List.fold_left
(fun (n, sx, sx2) x -> succ n, sx +. x, sx2 +. sqr x)
(0, 0., 0.) l
in
sqrt ((sx2 -. sqr sx /. float n) /. float n)
let _ =
let l = [ 2.;4.;4.;4.;5.;5.;7.;9. ] in
Printf.printf "List: ";
List.iter (Printf.printf "%g ") l;
Printf.printf "\nStandard deviation: %g\n" (stddev l)
You may also check:How to resolve the algorithm Flow-control structures step by step in the Ring programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Oz programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the LSL programming language
You may also check:How to resolve the algorithm Faces from a mesh step by step in the Python programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Ruby programming language