class RV::VonMises

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)

Attributes

kappa[R]

Public Class Methods

new(kappa:, rng: U_GENERATOR) click to toggle source
# File lib/random_variates.rb, line 376
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

Public Instance Methods

next() click to toggle source
# File lib/random_variates.rb, line 383
def next
  loop do
    r1 = @rng.next
    theta = @s * (2.0 * @rng.next - 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