Class: Poisson

Inherits:
Object
  • Object
show all
Includes:
RV_Generator
Defined in:
lib/random_variates.rb

Overview

Poisson generator.

Arguments
  • rate -> expected number per unit time/distance (rate > 0; default: 1).

  • rng -> the (Enumerable) source of U(0, 1)‘s (default: U_GENERATOR)

Instance Attribute Summary collapse

Attributes included from RV_Generator

#generator

Instance Method Summary collapse

Methods included from RV_Generator

#each, #next

Constructor Details

#initialize(rate: 1.0, rng: U_GENERATOR) ⇒ Poisson

Returns a new instance of Poisson.



364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/random_variates.rb', line 364

def initialize(rate: 1.0, rng: U_GENERATOR)
  raise 'rate must be positive.' if rate <= 0

  @rate = rate
  threshold = Math.exp(-rate)
  @generator = Enumerator.new do |yielder|
    loop do
      count = 0
      product = 1.0
      count += 1 until (product *= rng.next) < threshold
      yielder << count
    end
  end
end

Instance Attribute Details

#rateObject (readonly)

Returns the value of attribute rate.



362
363
364
# File 'lib/random_variates.rb', line 362

def rate
  @rate
end