Class: ERV::ExponentialDistribution
- Inherits:
-
Distribution
- Object
- Distribution
- ERV::ExponentialDistribution
- Defined in:
- lib/erv/exponential_distribution.rb
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.
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
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
8 9 10 |
# File 'lib/erv/exponential_distribution.rb', line 8 def mean @mean end |
#variance ⇒ Object (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
#sample ⇒ Object
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 |