Is there some trick that would allow one to use bc (or some other standard utility) to return the standard deviation of an arbitrary number of numbers? For convenience, let's say that the numbers are stored in a Bash variable in the following way:
myNumbers="0.556
1.456
45.111
7.812
5.001"
So, the answer I'm looking for would be in a form such as the following:
standardDeviation="$(echo "${myNumbers}" | <insert magic here>)"
Using awk:
standardDeviation=$(
echo "$myNumbers" |
awk '{sum+=$1; sumsq+=$1*$1}END{print sqrt(sumsq/NR - (sum/NR)**2)}'
)
echo $standardDeviation
Using perl :
#!/usr/bin/env perl
use strict; use warnings;
use Math::NumberCruncher;
my @data = qw/
0.556
1.456
45.111
7.812
5.001
/;
print Math::NumberCruncher::StandardDeviation(\@data);
16.7631
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With