class RV::Exponential

Exponential random variate generator with specified rate or mean. One and only one of rate or mean should be specified.

Arguments
  • rate -> the rate of occurrences per unit time (default: nil).

  • mean -> the expected value of the distribution (default: nil).

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

Attributes

mean[R]
rate[R]

Public Class Methods

new(rate: nil, mean: nil, rng: U_GENERATOR) click to toggle source
# File lib/random_variates.rb, line 133
def initialize(rate: nil, mean: nil, rng: U_GENERATOR)
  raise 'Rate must be positive.' if rate && rate <= 0
  raise 'Mean must be positive.' if mean && mean <= 0
  unless rate.nil? ^ mean.nil?
    raise 'Supply one and only one of mean or rate'
  end

  if rate.nil?
    @mean = mean
    @rate = 1.0 / mean
  else
    @mean = 1.0 / rate
    @rate = rate
  end
  @rng = rng
end

Public Instance Methods

mean=(mean) click to toggle source
# File lib/random_variates.rb, line 161
def mean=(mean)
  raise 'Mean must be a number.' unless mean.is_a? Numeric
  raise 'Mean must be positive.' if mean <= 0
  @mean = mean
  @rate = 1.0 / mean
end
next() click to toggle source
# File lib/random_variates.rb, line 150
def next
  -@mean * Math.log(@rng.next)
end
rate=(rate) click to toggle source
# File lib/random_variates.rb, line 154
def rate=(rate)
  raise 'Rate must be a number.' unless rate.is_a? Numeric
  raise 'Rate must be positive.' if rate <= 0
  @mean = 1.0 / rate
  @rate = rate
end