Class: ERV::ExponentialDistribution

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ExponentialDistribution

Returns a new instance of ExponentialDistribution.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
# File 'lib/erv/exponential_distribution.rb', line 10

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

  @rate = opts[:rate].try(:to_f)
  raise ArgumentError unless @rate and @rate > 0.0

  @mean = 1 / @rate
  @variance = @mean ** 2
end

Instance Attribute Details

#meanObject (readonly)

Returns the value of attribute mean.



8
9
10
# File 'lib/erv/exponential_distribution.rb', line 8

def mean
  @mean
end

#varianceObject (readonly)

Returns the value of attribute variance.



8
9
10
# File 'lib/erv/exponential_distribution.rb', line 8

def variance
  @variance
end

Instance Method Details

#sampleObject



20
21
22
23
24
25
26
# File 'lib/erv/exponential_distribution.rb', line 20

def sample
  # starting from a random variable X ~ U(0,1), which is provided by the
  # RNG, we can obtain a random variable Y ~ Exp(\lambda), with mean = 1 /
  # \lambda, through the transformation: Y = - (1 / \lambda) ln X. see
  # [KROESE11], section 4.2.3.
  - @mean * Math.log(@rng.rand)
end