使用bc或其他标准实用程序对任意数量的数字进行标准偏差 [英] standard deviation of an arbitrary number of numbers using bc or other standard utilities

查看:91
本文介绍了使用bc或其他标准实用程序对任意数量的数字进行标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些技巧可以让人们使用bc(或其他一些标准实用程序)返回任意数量数字的标准偏差?为了方便起见,假设数字以以下方式存储在Bash变量中:

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

推荐答案

使用:

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

使用:

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

这篇关于使用bc或其他标准实用程序对任意数量的数字进行标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆