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

Attributes included from RV_Generator

#generator

Instance Method Summary collapse

Methods included from RV_Generator

#each, #next

Constructor Details

#initialize(kappa:, rng: U_GENERATOR) ⇒ VonMises



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/random_variates.rb', line 338

def initialize(kappa:, rng: U_GENERATOR)
  raise 'kappa must be positive.' if kappa < 0
  s = (kappa > 1.3 ? 1.0 / Math.sqrt(kappa) : Math::PI * Math.exp(-kappa))
  @generator = Enumerator.new do |yielder|
    loop do
      r1 = rng.next
      theta = s * (2.0 * rng.next - 1.0) / r1
      next if (theta.abs > Math::PI)
      yielder << theta if (0.25 * kappa * theta * theta < 1.0 - r1) ||
                    (0.5 * kappa * (Math.cos(theta) - 1.0) >= Math.log(r1))
    end
  end
end

Instance Attribute Details

#kappaObject (readonly)

Returns the value of attribute kappa.



336
337
338
# File 'lib/random_variates.rb', line 336

def kappa
  @kappa
end