Module: Bernoulli::Distribution
- Included in:
- Binomial, Geometric, Hypergeometric, Poisson
- Defined in:
- lib/bernoulli/distribution.rb,
lib/bernoulli/distribution/poisson.rb,
lib/bernoulli/distribution/binomial.rb,
lib/bernoulli/distribution/geometric.rb,
lib/bernoulli/distribution/hypergeometric.rb
Overview
Top level Module for all the probability distributions Also provides convinience methods and ‘#standard_deviation`, which is calculated the same way independent of the distribution
Defined Under Namespace
Classes: Binomial, Geometric, Hypergeometric, Poisson
Instance Method Summary collapse
-
#[](k) ⇒ Object
A shortcut to the ‘#probability` and `#probability_range` methods.
-
#probability_range(r) ⇒ Object
Calculate the sum of the probabilities across a range.
-
#standard_deviation ⇒ Object
(also: #sd)
The standard deviation of a random variable is the square root of its variance.
Instance Method Details
#[](k) ⇒ Object
A shortcut to the ‘#probability` and `#probability_range` methods
22 23 24 25 26 27 28 29 30 |
# File 'lib/bernoulli/distribution.rb', line 22 def [](k) if k.is_a? Integer probability(k).to_f elsif k.is_a? Range probability_range(k).to_f else raise TypeError, 'Expecting Integer or Range - "[]"' end end |
#probability_range(r) ⇒ Object
Calculate the sum of the probabilities across a range
15 16 17 |
# File 'lib/bernoulli/distribution.rb', line 15 def probability_range(r) r.inject(0) { |s, k| s + probability(k) } end |
#standard_deviation ⇒ Object Also known as: sd
The standard deviation of a random variable is the square root of its variance.
34 35 36 |
# File 'lib/bernoulli/distribution.rb', line 34 def standard_deviation Math.sqrt(variance) end |