Search code examples
bashvariablesstandard-deviation

standard deviation of an arbitrary number of numbers using bc or other standard utilities


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>)"

Solution

  • Using :

    standardDeviation=$(
        echo "$myNumbers" |
            awk '{sum+=$1; sumsq+=$1*$1}END{print sqrt(sumsq/NR - (sum/NR)**2)}'
    )
    echo $standardDeviation
    

    Using :

    #!/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);
    

    Output

    16.7631