不安装数学模块的正态分布计算的累积密度函数
Cumulative Density Function of a normal distribution calculation without installing a Math module
我想用 Perl 计算正态分布的累积密度函数。我正在使用
Math::Gauss
来自 CPAN 的模块可以毫无问题地计算 CDF。
ttt.pl
#!/usr/bin/perl
use strict;
use warnings;
use Math::Gauss ':all';
my $x = 0.1;
my $mean = 0;
my $std = 0.1;
my $output = cdf($x, $mean, $std);
print $output;
但是,当我 运行 代码在不同的服务器中时,我遇到了模块安装问题,如下所示:
Can't locate Math/Gauss.pm in @INC (@INC contains:
/etc/perl
/usr/local/lib/perl/5.14.2
/usr/local/share/perl/5.14.2
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.14
/usr/share/perl/5.14
/usr/local/lib/site_perl
. ) at ./ttt.pl line 5.
BEGIN failed--compilation aborted at ./ttt.pl line 5.
我没有root权限所以需要在本地安装,但是我觉得CDF计算不是很大的计算。 (CDF方程很简单。)
所以,如果我知道不用任何安装就可以在 Perl 中计算 CDF 的方法,那就太好了。或者有没有办法在我的代码中包含 Math/Gauss.pm
以便我可以在不安装的情况下使用它?
下面是基于Math::Gauss.
的模块
# Copyright (C) 2011 by Philipp K. Janert
# No rights reserved by Eric L. Brine
#
# March 13th, 2017 - Eric Brine - Trimmed into light version of the module.
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself, either Perl version 5.10.1 or,
# at your option, any later version of Perl 5 you may have available.
package Math::GaussLite;
use strict;
use warnings;
use Carp;
use Exporter qw( import );
our @EXPORT_OK = qw( cdf );
my $SQRT2PI = 2.506628274631;
sub pdf {
my ( $x, $m, $s ) = ( 0, 0, 1 );
$x = shift if @_;
$m = shift if @_;
$s = shift if @_;
if( $s <= 0 ) {
croak( "Can't evaluate Math::Gauss:pdf for $s=$s not strictly positive" );
}
my $z = ($x-$m)/$s;
return exp(-0.5*$z*$z)/($SQRT2PI*$s);
}
sub cdf {
my ( $x, $m, $s ) = ( 0, 0, 1 );
$x = shift if @_;
$m = shift if @_;
$s = shift if @_;
# Abramowitz & Stegun, 26.2.17
# absolute error less than 7.5e-8 for all x
if( $s <= 0 ) {
croak( "Can't evaluate Math::Gauss:cdf for $s=$s not strictly positive" );
}
my $z = ($x-$m)/$s;
my $t = 1.0/(1.0 + 0.2316419*abs($z));
my $y = $t*(0.319381530
+ $t*(-0.356563782
+ $t*(1.781477937
+ $t*(-1.821255978
+ $t*1.330274429 ))));
if( $z > 0 ) {
return 1.0 - pdf( $z )*$y;
} else {
return pdf( $z )*$y;
}
}
1;
(老实说,因为这真的只是一个公式,版权并没有提供太多保护。)
我想用 Perl 计算正态分布的累积密度函数。我正在使用
Math::Gauss
来自 CPAN 的模块可以毫无问题地计算 CDF。
ttt.pl
#!/usr/bin/perl
use strict;
use warnings;
use Math::Gauss ':all';
my $x = 0.1;
my $mean = 0;
my $std = 0.1;
my $output = cdf($x, $mean, $std);
print $output;
但是,当我 运行 代码在不同的服务器中时,我遇到了模块安装问题,如下所示:
Can't locate Math/Gauss.pm in @INC (@INC contains:
/etc/perl
/usr/local/lib/perl/5.14.2
/usr/local/share/perl/5.14.2
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.14
/usr/share/perl/5.14
/usr/local/lib/site_perl
. ) at ./ttt.pl line 5.
BEGIN failed--compilation aborted at ./ttt.pl line 5.
我没有root权限所以需要在本地安装,但是我觉得CDF计算不是很大的计算。 (CDF方程很简单。)
所以,如果我知道不用任何安装就可以在 Perl 中计算 CDF 的方法,那就太好了。或者有没有办法在我的代码中包含 Math/Gauss.pm
以便我可以在不安装的情况下使用它?
下面是基于Math::Gauss.
的模块# Copyright (C) 2011 by Philipp K. Janert
# No rights reserved by Eric L. Brine
#
# March 13th, 2017 - Eric Brine - Trimmed into light version of the module.
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself, either Perl version 5.10.1 or,
# at your option, any later version of Perl 5 you may have available.
package Math::GaussLite;
use strict;
use warnings;
use Carp;
use Exporter qw( import );
our @EXPORT_OK = qw( cdf );
my $SQRT2PI = 2.506628274631;
sub pdf {
my ( $x, $m, $s ) = ( 0, 0, 1 );
$x = shift if @_;
$m = shift if @_;
$s = shift if @_;
if( $s <= 0 ) {
croak( "Can't evaluate Math::Gauss:pdf for $s=$s not strictly positive" );
}
my $z = ($x-$m)/$s;
return exp(-0.5*$z*$z)/($SQRT2PI*$s);
}
sub cdf {
my ( $x, $m, $s ) = ( 0, 0, 1 );
$x = shift if @_;
$m = shift if @_;
$s = shift if @_;
# Abramowitz & Stegun, 26.2.17
# absolute error less than 7.5e-8 for all x
if( $s <= 0 ) {
croak( "Can't evaluate Math::Gauss:cdf for $s=$s not strictly positive" );
}
my $z = ($x-$m)/$s;
my $t = 1.0/(1.0 + 0.2316419*abs($z));
my $y = $t*(0.319381530
+ $t*(-0.356563782
+ $t*(1.781477937
+ $t*(-1.821255978
+ $t*1.330274429 ))));
if( $z > 0 ) {
return 1.0 - pdf( $z )*$y;
} else {
return pdf( $z )*$y;
}
}
1;
(老实说,因为这真的只是一个公式,版权并没有提供太多保护。)