Cumulative standard deviation
Using a closure:
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;Using a state variable (remember that <(x-<x>)²> = <x²> - <x>²):
sub stddev($x) {
sqrt
( .[2] += $x²) / ++.[0]
- ((.[1] += $x ) / .[0])²
given state @;
}
say .&stddev for <2 4 4 4 5 5 7 9>;Output:
Last updated
Was this helpful?