# Cumulative distribution functions?

Discussion in 'Perl Misc' started by PJ, Nov 14, 2004.

1. ### PJGuest

I am going trough a book that uses that NORMDIST function in Excel. I
would like to repoduce the results in perl. Does anyone have a
function or module that is the same as Excel's NORMDIST? Actually, I
only need the cumulative distribution function (in Excel's NORMDIST
function, the 4th parameter is set to TRUE).

As an example, normdist(42, 40, 1.5, TRUE) equals 0.908789.

I am using perl on a FreeBSD machine - so I don't have access to Excel
via perl.

I appreciate any help,

-Garrett.

p.s. This is Excel's documentation of the function:

NORMDIST

Returns the normal distribution for the specified mean and standard
deviation. This function has a very wide range of applications in
statistics, including hypothesis testing.

Syntax

NORMDIST(x,mean,standard_dev,cumulative)

X is the value for which you want the distribution.

Mean is the arithmetic mean of the distribution.

Standard_dev is the standard deviation of the distribution.

Cumulative is a logical value that determines the form of the
function. If cumulative is TRUE, NORMDIST returns the cumulative
distribution function; if FALSE, it returns the probability mass
function.

PJ, Nov 14, 2004

2. ### A. Sinan UnurGuest

(PJ) wrote in
news::

> I am going trough a book that uses that NORMDIST function in Excel. I
> would like to repoduce the results in perl. Does anyone have a
> function or module that is the same as Excel's NORMDIST? Actually, I
> only need the cumulative distribution function (in Excel's NORMDIST
> function, the 4th parameter is set to TRUE).

http://search.cpan.org/~mikek/Statistics-Distributions-
1.02/Distributions.pm

A. Sinan Unur, Nov 14, 2004

3. ### PJGuest

CPAN, of course, is the first place I checked. Most of the time, it is

The module you mention, Statistics:istributions does not have a
corresponding function. Math::CDF is also lacking.

q/NORMDIST (x, mean, standard_dev, cumulative) returns the area under
the normal distribution up to point x where you have to define the
mean and SD of the population./

Now, there have been arguments posted on why not to use NORMDIST, but
I simply need to reproduce the examples in the book.

q/Microsoft chose to use a 6 polynomial approximation to the NORMSDIST
function (rather than the infinite series), and it is the worst of the
"DIST" functions. I have a nice graph that shows the error. So avoid
depending on these 3 "DIST" functions when sigma is more than 4./

Fortunately, I found Peter Acklam's Statistics:istrib::Normal module
(requires his Math::SpecFun::Erf). Why they are not posted on CPAN, I
don't know, but you can find them at
http://home.online.no/~pjacklam/perl/modules/index.html.

The following clode produces the same output as the NORMDIST example
given in MS Excel Help.

#!/usr/local/bin/perl -w

use strict;
use Statistics:istrib::Normal;

my \$dist = Statistics:istrib::Normal->new(mu => 40, sigma=> 1.5);
print \$dist->ltp(42) . "\n";

"A. Sinan Unur" <> wrote in message news:<Xns95A1925FD08Fasu1cornelledu@132.236.56.8>...
> (PJ) wrote in
> news::
>
> > I am going trough a book that uses that NORMDIST function in Excel. I
> > would like to repoduce the results in perl. Does anyone have a
> > function or module that is the same as Excel's NORMDIST? Actually, I
> > only need the cumulative distribution function (in Excel's NORMDIST
> > function, the 4th parameter is set to TRUE).

>
>
> http://search.cpan.org/~mikek/Statistics-Distributions-
> 1.02/Distributions.pm

PJ, Nov 14, 2004
4. ### A. Sinan UnurGuest

(PJ) wrote in
news::

> CPAN, of course, is the first place I checked. Most of the time, it is
>
> The module you mention, Statistics:istributions does not have a
> corresponding function.

Don't mess with distributions if you do not know statistics.

use strict;
use warnings;

use Statistics:istributions;

my (\$mean, \$stdev) = (40, 1.5);
my \$z = (42 - \$mean)/\$stdev;
my \$p = 1 - Statistics:istributions::uprob(\$z);
print "\$p\n";

D:\Home> perl st.pl
0.908789

To recall:

(PJ) wrote in
news::

> As an example, normdist(42, 40, 1.5, TRUE) equals 0.908789.

I am glad you found something that does what you want. But think before you
speak. Or, at least read the documentation for the module you are using
which shows how to get what you want from the module.

I am also going to suggest that you read the posting guidelines for this
group. Top-posting and full-quoting are not really appreciated here.

Sinan.

A. Sinan Unur, Nov 14, 2004