Class: Gamma

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

Overview

Gamma generator based on Marsaglia and Tsang method Algorithm 4.33

Produces gamma RVs with expected value alpha * beta.

Arguments
  • alpha -> the shape parameter (alpha > 0; default: 1).

  • beta -> the rate parameter (beta > 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(alpha: 1.0, beta: 1.0, rng: U_GENERATOR) ⇒ Gamma

Returns a new instance of Gamma.



246
247
248
249
250
251
252
253
254
255
# File 'lib/random_variates.rb', line 246

def initialize(alpha: 1.0, beta: 1.0, rng: U_GENERATOR)
  raise 'Alpha and beta must be positive.' if alpha <= 0 || beta <= 0

  @alpha = alpha
  @beta = beta
  std_normal = Gaussian.new(mean: 0.0, sd: 1.0, rng: rng)
  @generator = Enumerator.new do |yielder|
    loop { yielder << __gen__(alpha, beta, std_normal, rng) }
  end
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



244
245
246
# File 'lib/random_variates.rb', line 244

def alpha
  @alpha
end

#betaObject (readonly)

Returns the value of attribute beta.



244
245
246
# File 'lib/random_variates.rb', line 244

def beta
  @beta
end