gpt4 book ai didi

Perl Statistics::Descriptive and a trimmed standard deviation

转载 作者:行者123 更新时间:2023-12-02 00:35:24 26 4
gpt4 key购买 nike

Statistics::Descriptive 模块提供了 Full 类,可让您计算截尾平均值。有没有办法从中得到修剪的标准偏差?它已经对数据进行排序以获得修剪均值,因此困难的部分已经完成。

最佳答案

如果您的数据全部为正数或全部为负数,那么您可以只保留 2 个 $stat 对象,一个用于普通值,一个用于平方值,并组合 trimmed_mean 结果以生成修剪后的 stddev:

my $stat_x = Statistics::Descriptive::Full->new();
my $stat_xx = Statistics::Descriptive::Full->new();
sub add_x {
$stat_x->add_data( @_ );
$stat_xx->add_data( map {$_*$_} @_ );
}
sub trimmed_stddev {
my $lower = shift;
my $upper = @_ ? shift : $lower;

my $trm_x = $stat_x->trimmed_mean( $lower, $upper );
my $trm_xx = $stat_xx->trimmed_mean( $lower, $upper );

my $nn = int( $stat_x->count() * ( 1.0 - $lower - $upper ) );

return ($nn <= 1 ? 0.0 : sqrt(($trm_xx - $trm_x * $trm_x ) * $nn / ($nn -1)) );
}

# add some data
add_x( 1, 5, 12, 29, 10, 11, 2 );

print trimmed_stddev(0.0);
print trimmed_stddev(0.05);
print trimmed_stddev(0.2);
# output
# 9.45163125250522
# 9.58570364061576
# 4.44222166638871

关于Perl Statistics::Descriptive and a trimmed standard deviation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com