Class: ERV::ExponentialDistribution

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

Constant Summary

Constants inherited from Distribution

Distribution::DEFAULT_SEED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ExponentialDistribution

Returns a new instance of ExponentialDistribution.

Raises:

  • (ArgumentError)


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

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

  @rate = opts[:rate]&.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.



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

def mean
  @mean
end

#varianceObject (readonly)

Returns the value of attribute variance.



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

def variance
  @variance
end

Instance Method Details

#sampleObject



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

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