Class: RV::VonMises

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

Overview

von Mises generator.

This von Mises distribution generator is based on the VML algorithm by

  1. Barabesis: “Generating von Mises variates by the Ratio-of-Uniforms Method”

Statistica Applicata Vol. 7, #4, 1995 sa-ijas.stat.unipd.it/sites/sa-ijas.stat.unipd.it/files/417-426.pdf

Arguments
  • kappa -> concentration coefficient (kappa ≥ 0).

  • 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(kappa:, rng: U_GENERATOR) ⇒ VonMises

Returns a new instance of VonMises.



380
381
382
383
384
385
# File 'lib/random_variates.rb', line 380

def initialize(kappa:, rng: U_GENERATOR)
  raise 'kappa must be positive.' if kappa < 0
  @kappa = kappa
  @rng = rng
  @s = (kappa > 1.3 ? 1.0 / Math.sqrt(kappa) : Math::PI * Math.exp(-kappa))
end

Instance Attribute Details

#kappaObject (readonly)

Returns the value of attribute kappa.



378
379
380
# File 'lib/random_variates.rb', line 378

def kappa
  @kappa
end

Instance Method Details

#nextObject



387
388
389
390
391
392
393
394
395
# File 'lib/random_variates.rb', line 387

def next
  while true
    r1 = @rng.rand
    theta = @s * (2.0 * @rng.rand - 1.0) / r1
    next if theta.abs > Math::PI
    return theta if (0.25 * @kappa * theta * theta < 1.0 - r1) ||
                    (0.5 * @kappa * (Math.cos(theta) - 1.0) >= Math.log(r1))
  end
end