Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Hypergeometric test

by jeteve (Pilgrim)
on May 02, 2005 at 13:40 UTC ( [id://453227]=perlquestion: print w/replies, xml ) Need Help??

jeteve has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I'm seeking for a CPAN module to make an hypergeometric test. Does someone knows such a thing ? Thx all !!

Replies are listed 'Best First'.
Re: Hypergeometric test
by monkfan (Curate) on May 02, 2005 at 15:35 UTC
    Hi,
    Here is some modification from Wolf Book (pp.596 ff). The notation is based on this. It also uses Math::Pari. For related topic you may find this node helpful.
    #!/usr/bin/perl -w use strict; use Data::Dumper; use Math::Pari qw/binomial pari2num/; my $val = hypergeometric(5,3,53,5); print "Hyper = $val\n"; sub hypergeometric { my ($M,$p,$F,$n) = @_; return unless $n>0 && $n == int($n) && $p > 0 && $p == int($p) && $M > 0 && $M <= $n+$p; return 0 unless $p <= $M && $p == int($p); return pari2num((binomial($M,$p) * binomial($F-$M, $n-$p) / binomial($F,$n))); }
    That gives:
    Hyper = 0.00393074501208321
    Regards,
    Edward
Re: Hypergeometric test
by Fletch (Bishop) on May 02, 2005 at 13:52 UTC

    Erm, more context might be helpful. Do you perhaps mean something to do with an hypergeometric distribution? If so nothing springs readily to mind from CPAN, but if you get anything working the author of Statistics::Distributions might be interested in a patch.

Re: Hypergeometric test
by tlm (Prior) on May 02, 2005 at 14:23 UTC

    Are you looking for the Fisher's Exact Test perchance? If so, below is a home-grown implementation for the 2x2 one-tailed case. Requires Math::Pari, but you can eliminate this dependency by writing your own factorial function, and changing $max_arr to something like 170 (or whatever your architecture can handle without overflow).

    The code is not very well documented. Sorry. You will have to read some source code to understand what it's doing. Feedback/comments welcome.

    Update 3: This code is OBSOLETE. See the new improved version here.

    the lowliest monk

Re: Hypergeometric test
by ghenry (Vicar) on May 02, 2005 at 13:52 UTC

    Stupid question, but did you try Searching CPAN?

    I found a few references to hypergeometric.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://453227]
Approved by ghenry
Front-paged by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-28 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found