# Probability distributions library in Ruby

**URL:** <https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872>\
**Category:** Ruby\
**Created:** [April 12, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872 "2007-04-12T23:56:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [April 12, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/1 "2007-04-12T23:56:19Z")

</div>

Hello,

I would extract some numbers according to a given  
probability distribution (at least, Normal, Exponential,  
Poisson and Bernoulli).

Do you know if a similar library already exists in Ruby? I  
only found pseudo-random number generator.

TIA

Libra

---

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [April 13, 2007, 12:35am UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/2 "2007-04-13T00:35:22Z")

</div>

If you don’t find any library, you can always write your own using the  
technique outlined in section 1.3 of this paper.  
[http://eeyore.ucdavis.edu/stat141/RNG.pdf](http://eeyore.ucdavis.edu/stat141/RNG.pdf)

Basically given the CDF F of some distribution (say, Poisson), you can  
generate random numbers from the Poisson distribution by doing:  
u = rand # uniformely generated random number  
y = Finv(u)

-Szymon

---

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [April 13, 2007, 2:36am UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/3 "2007-04-13T02:36:30Z")

</div>

On 13 Apr 2007, at 06:55, Libra wrote:

> Libra  
> If you are comfortable with R, you can use RSRuby (http://  
> web.kuicr.kyoto-u.ac.jp/~alexg/rsruby/) to generate numbers from any  
> its distribution functions. For example, extracting 10 numbers from a  
> normal distribution with mean 0:

[alexg@powerbook]/Users/alexg(1): irb -rubygems  
irb(main):001:0\> require ‘rsruby’  
=\> true  
irb(main):002:0\> r = RSRuby.instance  
=\> #RSRuby:0x1c1510  
irb(main):003:0\> r.rnorm(10)  
=\> [-0.418488870801272, 3.8442920287686, -2.23915973195997,  
-1.1852964455658, 0.292187794718865, -0.0538355720349994,  
-0.682675725085902, -0.179593328583021, 0.617238096715829,  
-0.353627801112474]

The default R stats package has the following distributions built in  
and you can use any R library to add more (I couldn’t see Bernoulli  
in this list, but maybe it has an alternative name?):

Beta(stats) The Beta Distribution  
Binomial(stats) The Binomial Distribution  
Cauchy(stats) The Cauchy Distribution  
Chisquare(stats) The (non-central) Chi-Squared Distribution  
Exponential(stats) The Exponential Distribution  
FDist(stats) The F Distribution  
GammaDist(stats) The Gamma Distribution  
Geometric(stats) The Geometric Distribution  
Hypergeometric(stats) The Hypergeometric Distribution  
Logistic(stats) The Logistic Distribution  
Lognormal(stats) The Log Normal Distribution  
Multinomial(stats) The Multinomial Distribution  
NegBinomial(stats) The Negative Binomial Distribution  
Normal(stats) The Normal Distribution  
Poisson(stats) The Poisson Distribution  
SignRank(stats) Distribution of the Wilcoxon Signed Rank  
Statistic  
TDist(stats) The Student t Distribution  
Tukey(stats) The Studentized Range Distribution  
Uniform(stats) The Uniform Distribution  
Weibull(stats) The Weibull Distribution  
Wilcoxon(stats) Distribution of the Wilcoxon Rank Sum Statistic  
ecdf(stats) Empirical Cumulative Distribution Function  
survreg.distributions(survival)  
Parametric Survival Distributions

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [April 15, 2007, 2:16pm UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/4 "2007-04-15T14:16:19Z")

</div>

Libra wrote:

> Libra  
> R is great! maybe too great for your requirements  
> rb-gsl is easy

for example this will print you a nice gauss-shape graph  
from rb-gsl-1.8.3/samples/histogram/gauss.rb

> sigma, mean, height, = h.fit\_gaussian
> 
> x = GSL::Vector.linspace(-MAX, MAX, 100)  
> y = height\*Ran::gaussian\_pdf(x-mean, sigma)  
> GSL::graph(h, [x, y], “-T X -C -g 3”)

NICE!

cauchy exponential power poisson etc. also available  
ruby - making maths easy

---

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [April 13, 2007, 2:50am UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/5 "2007-04-13T02:50:30Z")

</div>

On 13 Apr 2007, at 09:35, Alex G. wrote:

> > TIA
> > 
> > Libra
> 
> If you are comfortable with R, you can use RSRuby (http://  
> web.kuicr.kyoto-u.ac.jp/~alexg/rsruby/) to generate numbers from  
> any its distribution functions.

A quick bit of googling found the missing Bernoulli distribution  
function:

[http://rss.acs.unt.edu/Rdoc/library/Rlab/html/Bernoulli.html](http://rss.acs.unt.edu/Rdoc/library/Rlab/html/Bernoulli.html)

It seems to be part of a CRAN package called Rlab:

[http://cran.r-project.org/src/contrib/Descriptions/Rlab.html](http://cran.r-project.org/src/contrib/Descriptions/Rlab.html)

You should be able to install and then access this library through  
RSRuby to get your complete set of functions.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [August 25, 2007, 6:06am UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/6 "2007-08-25T06:06:26Z")

</div>

Roger P. wrote:

> u = rand
> 
> > Libra wrote:
> > 
> > > GSL::graph(h, [x, y], “-T X -C -g 3”)  
> > > NICE!
> > 
> > cauchy exponential power poisson etc. also available  
> > ruby - making maths easy

There is also RSRuby, a way of calling the R libraries, including their  
probability distribution routines, from Ruby. I don’t know anything  
about the ones in GSL, but I know the algorithms in R are first-rate,  
and I wouldn’t code my own no matter _how_ bored I got. 🙂

---

<div class="post-metadata">

**Author:** ![Libra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed655f/32.png) [@Libra](https://www.ruby-forum.com/u/Libra)\
**Post date:** [August 23, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/probability-distributions-library-in-ruby/91872/7 "2007-08-23T23:55:59Z")

</div>

From wikipedia : algorithm for generating pseudo poissons:

def poisson(lambda)

# init

l = Math.exp(-lambda)  
k = 0  
p = 1  
while p \>= l  
k += 1  
u = rand  
p = p \*u  
end  
return k -1  
end

only returns ints, but hey, they center around lambda  
gl.  
-Roger

maw wrote:

> Libra wrote:
> 
> > Libra  
> > R is great! maybe too great for your requirements  
> > rb-gsl is easy
> 
> for example this will print you a nice gauss-shape graph  
> from rb-gsl-1.8.3/samples/histogram/gauss.rb
> 
> > sigma, mean, height, = h.fit\_gaussian
> > 
> > x = GSL::Vector.linspace(-MAX, MAX, 100)  
> > y = height\*Ran::gaussian\_pdf(x-mean, sigma)  
> > GSL::graph(h, [x, y], “-T X -C -g 3”)
> 
> NICE!
> 
> cauchy exponential power poisson etc. also available  
> ruby - making maths easy
