Class: ViralSeq::Math::RandomGaussian

Inherits:
Object
  • Object
show all
Defined in:
lib/viral_seq/math.rb

Overview

Generate values from the standard normal distribution with given mean and standard deviation

Instance Method Summary collapse

Constructor Details

#initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) ⇒ RandomGaussian

generate RandomGaussian instance with given mean and standard deviation

Parameters:

  • mean (Float) (defaults to: 0.0)

    mean value.

  • sd (Float) (defaults to: 1.0)

    standard deviation value.



17
18
19
20
# File 'lib/viral_seq/math.rb', line 17

def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand })
  @mean, @sd, @rng = mean, sd, rng
  @compute_next_pair = false
end

Instance Method Details

#randFloat

generate a random number that falls in the pre-defined gaussian distribution

Examples:

generate 10 random number that falls in the a gaussian distribution with mean at 0 and standard deviation at 1.0

a = RandomGaussian.new
numbers = []
10.times {numbers << a.rand.round(5)}
numbers
=> [-1.83457, 1.24439, -0.30109, 0.13977, 0.61556, 1.3548, 1.72878, 2.46171, 0.97031, -0.29496]

Returns:

  • (Float)


31
32
33
34
35
36
37
38
39
40
# File 'lib/viral_seq/math.rb', line 31

def rand
  if (@compute_next_pair = !@compute_next_pair)
    theta = 2 * ::Math::PI * @rng.call
    scale = @sd * ::Math.sqrt(-2 * Math.log(1 - @rng.call))
    @g1 = @mean + scale * ::Math.sin(theta)
    @g0 = @mean + scale * ::Math.cos(theta)
  else
    @g1
  end
end