Class: ERV::GeometricDistribution
- Inherits:
-
Distribution
- Object
- Distribution
- ERV::GeometricDistribution
- Defined in:
- lib/erv/geometric_distribution.rb
Overview
We adopt the following version of the geometric distribution:
Pr(X = k) = (1 - p) ^ {k-1} * p
Instance Attribute Summary collapse
-
#mean ⇒ Object
Returns the value of attribute mean.
-
#variance ⇒ Object
Returns the value of attribute variance.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ GeometricDistribution
constructor
A new instance of GeometricDistribution.
- #sample ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ GeometricDistribution
Returns a new instance of GeometricDistribution.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/erv/geometric_distribution.rb', line 14 def initialize(opts={}) super(opts) raise ArgumentError unless opts[:probability_of_success] @p = opts[:probability_of_success].to_f @mean = 1.0 / @p @variance = (1.0 - @p) / (@p ** 2) @ln_1_minus_p = Math::log(1 - @p) end |
Instance Attribute Details
#mean ⇒ Object
Returns the value of attribute mean.
12 13 14 |
# File 'lib/erv/geometric_distribution.rb', line 12 def mean @mean end |
#variance ⇒ Object
Returns the value of attribute variance.
12 13 14 |
# File 'lib/erv/geometric_distribution.rb', line 12 def variance @variance end |
Instance Method Details
#sample ⇒ Object
26 27 28 29 |
# File 'lib/erv/geometric_distribution.rb', line 26 def sample u = @rng.rand (Math::log(u) / @ln_1_minus_p).ceil end |