Class: ERV::GeometricDistribution

Inherits:
Distribution show all
Defined in:
lib/erv/geometric_distribution.rb

Instance Method Summary collapse

Methods inherited from Distribution

#sample

Constructor Details

#initialize(opts) ⇒ GeometricDistribution

Returns a new instance of GeometricDistribution.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/erv/geometric_distribution.rb', line 10

def initialize(opts)
  super(opts)

  raise ArgumentError unless opts[:probability_of_success]
  p = opts[:probability_of_success].to_f

  if RUBY_PLATFORM == 'java'
    # create distribution
    d = JGeometricDistribution.new(@rng, p)
    # setup sampling function
    @func = Proc.new { d.sample }
  else
    # setup sampling function
    #
    # WARNING: I HAVEN'T TRIED THIS CODE!!!
    # Note that GSL implements the shifted version of the geometric
    # distribution, so we might need to change the result (removing 1?)
    @func = Proc.new { @rng.geometric(p) }
  end
end