How to resolve the algorithm Cumulative standard deviation step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cumulative standard deviation step by step in the Raku 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 Raku programming language

Source code in the raku programming language

sub sd (@a) {
    my $mean = @a R/ [+] @a;
    sqrt @a R/ [+] map (* - $mean)², @a;
}
 
sub sdaccum {
    my @a;
    return { push @a, $^x; sd @a; };
}
 
my &f = sdaccum;
say f $_ for 2, 4, 4, 4, 5, 5, 7, 9;


sub stddev($x) {
    sqrt
        ( .[2] += $x²) / ++.[0]
      - ((.[1] += $x ) /   .[0])²
    given state @;
}

say .&stddev for <2 4 4 4 5 5 7 9>;


  

You may also check:How to resolve the algorithm Formatted numeric output step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Smith numbers step by step in the Ada programming language
You may also check:How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Fortran programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the PHP programming language