Class: Exponential
- Inherits:
-
Object
- Object
- Exponential
- Includes:
- RV_Generator
- Defined in:
- lib/random_variates.rb
Overview
Exponential random variate generator with specified rate or mean. One and only one of rate or mean should be specified.
- Arguments
-
rate-> the rate of occurrences per unit time (default:nil). -
mean-> the expected value of the distribution (default:nil). -
rng-> the (Enumerable) source of U(0, 1)‘s (default: U_GENERATOR)
-
Instance Attribute Summary collapse
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#rate ⇒ Object
readonly
Returns the value of attribute rate.
Attributes included from RV_Generator
Instance Method Summary collapse
-
#initialize(rate: nil, mean: nil, rng: U_GENERATOR) ⇒ Exponential
constructor
A new instance of Exponential.
Methods included from RV_Generator
Constructor Details
#initialize(rate: nil, mean: nil, rng: U_GENERATOR) ⇒ Exponential
Returns a new instance of Exponential.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/random_variates.rb', line 136 def initialize(rate: nil, mean: nil, rng: U_GENERATOR) raise 'Rate must be positive.' if rate && rate <= 0 raise 'Mean must be positive.' if mean && mean <= 0 raise 'Supply one and only one of mean or rate' unless rate.nil? ^ mean.nil? if rate.nil? @mean = mean @rate = 1.0 / mean else @mean = 1.0 / rate @rate = rate end @generator = Enumerator.new do |yielder| loop { yielder << (-@mean * Math.log(rng.next)) } end end |
Instance Attribute Details
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
134 135 136 |
# File 'lib/random_variates.rb', line 134 def mean @mean end |
#rate ⇒ Object (readonly)
Returns the value of attribute rate.
134 135 136 |
# File 'lib/random_variates.rb', line 134 def rate @rate end |