Class: RV::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

Instance Method Summary collapse

Methods included from RV_Generator

#each

Constructor Details

#initialize(alpha: 1.0, beta: 1.0, rng: U_GENERATOR) ⇒ Gamma

Returns a new instance of Gamma.



284
285
286
287
288
289
290
291
# File 'lib/random_variates.rb', line 284

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
  @rng = rng
  @std_normal = Normal.new(rng: rng)
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



282
283
284
# File 'lib/random_variates.rb', line 282

def alpha
  @alpha
end

#betaObject (readonly)

Returns the value of attribute beta.



282
283
284
# File 'lib/random_variates.rb', line 282

def beta
  @beta
end

Instance Method Details

#nextObject



293
294
295
# File 'lib/random_variates.rb', line 293

def next
  __gen__(@alpha, @beta)
end