Bernoulli implements the four most common discrete probability distributions in an object oriented manner and in pure Ruby.
- The binomial distribution
- The geometric distribution
- The hypergeometric distribution
- The Poisson distribution
Use RubyGems to install bernoulli
$ gem install bernoulli
The classes in bernoulli
represent random variables:
Bernoulli::Distribution::<DistributionName>
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:
binomdist
geomdist
hgeomdist
poissondist
So 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:
probability
probability_range
expected_value
variance
standard_deviation
skewness
excess
Again, shortcuts include (but this time included automatically):
ev
forexpected_value
v
forvariance
sd
forstandard_deviation
[]
, which takes a number or a range and then executesprobability
orprobability_range
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:
The method 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.
The method sample_value
returns the number of successes in a random binomial expriment that was executed using sample
. sv
is a shortcut for sample_value
.
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 contact me about anything I could/should add or to contribute in any way to this simple library.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request