splaisan
splaisan

Reputation: 923

random binomial distributed dataset in perl

I try to do in perl what I succeedd in R but is difficult to combine with my downstream needs.

in R I did the following

library("MASS")
d <- rnegbin(100000, mu = 250, theta = 2)
hist(d, breaks=1000, xlim=c(0,1000))

producing the nice graph I need with a peak round 180-200 and a tail to the right.

enter image description here

Could someone help me code the perl equivalent using Math::Random

I tried this but do not get the right shape

use Math::Random qw(random_negative_binomial);

# random_negative_binomial($n, $ne, $p)
# When called in an array context, returns an array of $n outcomes 
# generated from the negative binomial distribution with number of 
# events $ne and probability of an event in each trial $p. 
# When called in a scalar context, generates and returns only one 
# such outcome as a scalar, regardless of the value of $n.
# Argument restrictions: $ne is rounded using int(), the result must be positive. 
# $p must be between 0 and 1 exclusive.

# I tried different variable values but never got the right shape
my @dist = random_negative_binomial($n, $ne, $p);

what values do I need to mimic the R results? I need the same range of values on X and the same general shape

Thanks for any help, I did not find illustrated examples of that package

Stephane

Upvotes: 3

Views: 325

Answers (1)

Jorgen
Jorgen

Reputation: 195

I don't know much about statistics, but since nobody else comes forward: I would use the Perl Data Language PDL (which I use for other things) and fetch the PDL::Stats::Distr module. You can find an example that looks somewhat similar to yours here http://pdl-stats.sourceforge.net/Distr.htm. The module includes pmf_binomial (mass function) and mme_binomial (distribution). You will also need the PGPLOT module.

You will need some random data:

    $data = pdl  1..100000;    ## generate linear 1 - 100000
    $data = $data->random;     ## make them random between 0..1

Upvotes: 1

Related Questions