Class: ERV::GaussianDistribution

Inherits:
Distribution show all
Defined in:
lib/erv/gaussian_distribution.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GaussianDistribution

Returns a new instance of GaussianDistribution.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/erv/gaussian_distribution.rb', line 7

def initialize(opts={})
  super(opts)

  raise ArgumentError unless opts[:mean] and opts[:sd]
  @mu    = opts[:mean].to_f
  @sigma = opts[:sd].to_f
end

Instance Method Details

#meanObject



27
28
29
# File 'lib/erv/gaussian_distribution.rb', line 27

def mean
  @mu
end

#sampleObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/erv/gaussian_distribution.rb', line 15

def sample
  # Use Box-Muller algorithm (see [KROESE11], section 4.2.11, algorithm
  # 4.47) to obtain x ~ N(0,1).
  u_1 = @rng.rand
  u_2 = @rng.rand
  x = Math.sqrt(-2.0 * Math.log(u_1)) * Math.cos(2.0 * Math::PI * u_2)

  # Use location-scale transformation to obtain a N(\mu, \sigma^2)
  # distribution. See [KROESE11], section 3.1.2.2.
  @mu + @sigma * x
end

#varianceObject



31
32
33
# File 'lib/erv/gaussian_distribution.rb', line 31

def variance
  @sigma ** 2
end