Class: ERV::LogNormDistribution

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

Constant Summary

Constants inherited from Distribution

Distribution::DEFAULT_SEED

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LogNormDistribution

Returns a new instance of LogNormDistribution.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/erv/lognorm_distribution.rb', line 5

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

  raise ArgumentError, 'mu is required' unless opts[:mu]
  raise ArgumentError, 'sigma is required' unless opts[:sigma]
  raise ArgumentError, 'sigma must be greater than zero' unless opts[:sigma].to_f > 0.0

  @mu = opts[:mu].to_f
  @sigma = opts[:sigma].to_f

  # From Handbook of Monte Carlo Methods [KROESE11], section 4.2.10, pag 121
  @gaussian_dist = GaussianDistribution.new(mean: @mu, sd: @sigma)
end

Instance Method Details

#meanObject



24
25
26
# File 'lib/erv/lognorm_distribution.rb', line 24

def mean
  Math.exp(@mu + 0.5 * @sigma**2)
end

#sampleObject



19
20
21
22
# File 'lib/erv/lognorm_distribution.rb', line 19

def sample
  # just sample from @gaussian_dist and exponentiate the result
  Math.exp(@gaussian_dist.sample) # just sample from @gaussian_dist and exponentiate the res    Math.exp(@gaussian_dist.sample)))
end

#varianceObject



28
29
30
# File 'lib/erv/lognorm_distribution.rb', line 28

def variance
  Math.exp(2 * @mu + @sigma**2) * (Math.exp(@sigma**2) - 1)
end