Class: ERV::GeometricDistribution

Inherits:
Distribution show all
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

See: en.wikipedia.org/wiki/Geometric_distribution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GeometricDistribution

Returns a new instance of GeometricDistribution.

Raises:

  • (ArgumentError)


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

#meanObject

Returns the value of attribute mean.



12
13
14
# File 'lib/erv/geometric_distribution.rb', line 12

def mean
  @mean
end

#varianceObject

Returns the value of attribute variance.



12
13
14
# File 'lib/erv/geometric_distribution.rb', line 12

def variance
  @variance
end

Instance Method Details

#sampleObject



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