Class: ERV::ExponentialDistribution
- Inherits:
-
Distribution
- Object
- Distribution
- ERV::ExponentialDistribution
- Defined in:
- lib/erv/exponential_distribution.rb
Constant Summary
Constants inherited from Distribution
Instance Attribute Summary collapse
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#variance ⇒ Object
readonly
Returns the value of attribute variance.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ ExponentialDistribution
constructor
A new instance of ExponentialDistribution.
- #sample ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ ExponentialDistribution
Returns a new instance of ExponentialDistribution.
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
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
7 8 9 |
# File 'lib/erv/exponential_distribution.rb', line 7 def mean @mean end |
#variance ⇒ Object (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
#sample ⇒ Object
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 |