Perl Statistics ::描述性和修剪标准差

时间:2011-01-27 19:52:03

标签: perl statistics

Statistics::Descriptive模块提供Full类,允许您计算修剪后的平均值。有没有办法从中调整标准偏差?它已经对数据进行排序以获得修剪后的平均值,因此很难完成。

1 个答案:

答案 0 :(得分:3)

如果您的数据都是正数或全部为负数,那么您可以保留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