Class: Distrb::Normal::BoxMullerTransform

Inherits:
Object
  • Object
show all
Defined in:
lib/distrb/normal/box_muller_transform.rb

Overview

Box-Muller transform to sample from Normal distribution See: en.wikipedia.org/wiki/Box%E2%80%93Muller_transform

Instance Method Summary collapse

Constructor Details

#initializeBoxMullerTransform

Returns a new instance of BoxMullerTransform.



8
9
10
# File 'lib/distrb/normal/box_muller_transform.rb', line 8

def initialize
  @uniform = Distrb::Uniform.new
end

Instance Method Details

#sampleObject



12
13
14
15
16
# File 'lib/distrb/normal/box_muller_transform.rb', line 12

def sample
  x = @uniform.sample
  y = @uniform.sample
  Math.sqrt(-2 * Math.log(x)) * Math.cos(2 * Math::PI * y)
end