Class: RandomDistribution::LogNormalSequence

Inherits:
Sequence show all
Defined in:
lib/redshift/util/random.rb

Overview

Based on newran02:

Real VariLogNormal::Next(Real mean, Real sd)
{
   // should have special version of log for small sd/mean
   Real n_var = log(1 + square(sd / mean));
   return mean * exp(N.Next() * sqrt(n_var) - 0.5 * n_var);
}

Constant Summary

Constants included from Math

Math::Infinity

Instance Attribute Summary collapse

Attributes inherited from Sequence

#generator

Instance Method Summary collapse

Methods inherited from Sequence

random_pool_seed, random_seed, serial_count

Constructor Details

#initialize(opt = {}) ⇒ LogNormalSequence

Returns a new instance of LogNormalSequence.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/redshift/util/random.rb', line 171

def initialize opt = {}
  @gaussian_seq = GaussianSequence.new(
    :mean   => 0,
    :stdev  => 1,
    :seed   => opt[:seed],
    :generator => opt[:generator]
  )
  
  super :generator => @gaussian_seq

  @mean = Float(opt[:mean] || 1)
  @stdev = Float(opt[:stdev] || 1)
  
  n_var = log(1 + (stdev / mean)**2)
  @sqrt_n_var = sqrt(n_var)
  @half_n_var = 0.5 * n_var
end

Instance Attribute Details

#meanObject (readonly)

Returns the value of attribute mean.



169
170
171
# File 'lib/redshift/util/random.rb', line 169

def mean
  @mean
end

#stdevObject (readonly)

Returns the value of attribute stdev.



169
170
171
# File 'lib/redshift/util/random.rb', line 169

def stdev
  @stdev
end

Instance Method Details

#nextObject



189
190
191
# File 'lib/redshift/util/random.rb', line 189

def next
  mean * exp(super() * @sqrt_n_var - @half_n_var)
end