Class: RandomVariateGenerator::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/random_variate_generator/random.rb

Class Method Summary collapse

Class Method Details

.bernoulli(params) ⇒ Object



42
43
44
# File 'lib/random_variate_generator/random.rb', line 42

def self.bernoulli(params)
  (rand() < params[:probability_of_success])? 1 : 0
end

.binomial(params) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/random_variate_generator/random.rb', line 46

def self.binomial(params)
  counter = 0
  params[:number_of_trials].times{ 
    counter += bernoulli(:probability_of_success => params[:probability_of_success]) 
  } 
  counter
end

.empirical(params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/random_variate_generator/random.rb', line 79

def self.empirical(params) 
  probs = params[:probabilities]
  cda = []
  acum = 0
  probs.each{|x|
    acum += x
    cda << acum
  }
  cda << 1 
  cda.each{|x| x=1 if x > 1}
  u = rand
  index = 0
  index += 1 while u > cda[index]
  index
end

.exponential(params) ⇒ Object



5
6
7
8
9
# File 'lib/random_variate_generator/random.rb', line 5

def self.exponential(params)    
  u = rand()
  u = rand() while u <= 1e-7
  -Math.log(u)/params[:lambda]
end

.geometric(params) ⇒ Object



63
64
65
66
# File 'lib/random_variate_generator/random.rb', line 63

def self.geometric(params) 
  negative_binomial(:number_of_success => 1,
                    :probability_of_success => params[:probability_of_success])
end

.negative_binomial(params) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/random_variate_generator/random.rb', line 54

def self.negative_binomial(params)
  counter = 0
  success_counter = 0
  while success_counter < params[:number_of_success]
    counter += 1
    success_counter += bernoulli(:probability_of_success => params[:probability_of_success])
  end           
  counter
end

.normal(params) ⇒ Object



11
12
13
14
15
16
# File 'lib/random_variate_generator/random.rb', line 11

def self.normal(params)
  x2pi = rand() * Math::PI
  g2rad = Math.sqrt(-2.0 * Math.log(1.0 - rand()))
  z = Math.cos(x2pi) * g2rad
  params[:mu] + z*params[:sigma]
end

.poisson(params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/random_variate_generator/random.rb', line 68

def self.poisson(params) 
  cummulative_time = 0
  cummulative_time += exponential(:lambda => params[:lambda])
  counter = 0
  while cummulative_time <= 1.0
    counter += 1
    cummulative_time += exponential(:lambda => params[:lambda])
  end   
  counter
end

.triangular(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/random_variate_generator/random.rb', line 22

def self.triangular(params)
  params[:min] *= 1.0
  params[:mode] *= 1.0
  params[:max] *= 1.0
  u = rand()
  c = 0.5
  (params[:mode] - params[:min]) / (params[:max] - params[:min]) if params[:mode]
  if u > c
    u = 1.0 - u
    c = 1.0 - c
    params[:min], params[:max] = params[:max], params[:min]
  end
  params[:min] + (params[:max] - params[:min]) * (u * c) ** 0.5
end

.uniform(params) ⇒ Object



18
19
20
# File 'lib/random_variate_generator/random.rb', line 18

def self.uniform(params)
  params[:min] + (params[:max]-params[:min]) * rand()
end

.uniform_discrete(params) ⇒ Object



37
38
39
40
# File 'lib/random_variate_generator/random.rb', line 37

def self.uniform_discrete(params)
  uniform(:min => params[:min],
          :max => (params[:max]+1)).to_i
end