Bernoulli implements the four most common discrete probability distributions in an object oriented manner and in pure Ruby.
Use RubyGems to install
$ gem install bernoulli
The classes in
bernoulli reprsent random variables:
If, for example you wanted to have a geometrically distributed random variable with parameter 0.43, you would do:
y = Bernoulli::Distribution::Geometric.new(0.43)
Also if you
require 'bernoulli/shorthand' too, you will get top level shortcut functions to create new instances of all distributions:
y = geomdist(0.43) would be equivalent to the code shown above.
After this you call methods on
y. The methods avalible to all distributions are:
Again, shortcuts include (but this time included automatically):
, which takes a number or a range and then executes
require 'bernoulli' require 'bernoulli/shorthand' x = binomdist(180, 1.0/6) # => #<Distribution::Binomial @n=180, @p=0.16666666666666666> x.ev # => 30.0 x.v # => 25.0 x[25..36] # => 0.7665588897840108
Last, but not least, there are two methods exclusive to the binomial distribution:
sample returns a random array of length
n, where each entry is 1 with a probability of
p. This is esentially modelling a binomial experiment.
sample_value returns the number of successes in a random binomial expriment that was executed using
sv is a shortcut for
bernoulli is a small project. After writing the same code for some project and then losing it two or three times I decided to do it one time and well, so I can just call in the code from here next time.
Feel free to cantact me about anything I could/should add or to contribute in any way to this simple library.
git checkout -b my-new-feature)
git commit -am 'added some feature')
git push origin my-new-feature)