Class: ERV::GaussianDistribution
- Inherits:
-
Distribution
- Object
- Distribution
- ERV::GaussianDistribution
- Defined in:
- lib/erv/gaussian_distribution.rb
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ GaussianDistribution
constructor
A new instance of GaussianDistribution.
- #mean ⇒ Object
- #sample ⇒ Object
- #variance ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ GaussianDistribution
Returns a new instance of GaussianDistribution.
7 8 9 10 11 12 13 |
# File 'lib/erv/gaussian_distribution.rb', line 7 def initialize(opts={}) super(opts) raise ArgumentError unless opts[:mean] and opts[:sd] @mu = opts[:mean].to_f @sigma = opts[:sd].to_f end |
Instance Method Details
#mean ⇒ Object
27 28 29 |
# File 'lib/erv/gaussian_distribution.rb', line 27 def mean @mu end |
#sample ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/erv/gaussian_distribution.rb', line 15 def sample # Use Box-Muller algorithm (see [KROESE11], section 4.2.11, algorithm # 4.47) to obtain x ~ N(0,1). u_1 = @rng.rand u_2 = @rng.rand x = Math.sqrt(-2.0 * Math.log(u_1)) * Math.cos(2.0 * Math::PI * u_2) # Use location-scale transformation to obtain a N(\mu, \sigma^2) # distribution. See [KROESE11], section 3.1.2.2. @mu + @sigma * x end |
#variance ⇒ Object
31 32 33 |
# File 'lib/erv/gaussian_distribution.rb', line 31 def variance @sigma ** 2 end |