Class: Rust::RandomVariable
Constant Summary
collapse
- EPSILON =
1e-7
- ENGLISH_ALPHABET =
RandomVariable.new({
"a" => 0.08167,
"b" => 0.01492,
"c" => 0.02782,
"d" => 0.04253,
"e" => 0.12703,
"f" => 0.02228,
"g" => 0.02015,
"h" => 0.06094,
"i" => 0.06966,
"j" => 0.00153,
"k" => 0.00772,
"l" => 0.04025,
"m" => 0.02406,
"n" => 0.06749,
"o" => 0.07507,
"p" => 0.01929,
"q" => 0.00095,
"r" => 0.05987,
"s" => 0.06327,
"t" => 0.09056,
"u" => 0.02758,
"v" => 0.00978,
"w" => 0.02360,
"x" => 0.00150,
"y" => 0.01974,
"z" => 0.00074
})
- DICE =
UniformRandomVariable.new([1, 2, 3, 4, 5, 6])
- COIN =
UniformRandomVariable.new(["h", "t"])
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#<, #<=, #==, #>, #>=, #between, #expected, #ml, #so_that
Constructor Details
#initialize(values = {0 => 1.0}, exact = false) ⇒ RandomVariable
Returns a new instance of RandomVariable.
92
93
94
95
96
97
98
99
100
|
# File 'lib/rust/stats/probabilities.rb', line 92
def initialize(values = {0 => 1.0}, exact = false)
@values = values
@exact = exact
raise "All the probabilities should be in the range [0, 1]" unless @values.values.all? { |v| v.between? 0, 1 }
raise "The cumulative probability must be exactly 1 (#{@values.values.sum} instead)" unless @values.values.sum.between? 1-EPSILON, 1+EPSILON
approx!
end
|
Instance Attribute Details
Returns the value of attribute values.
90
91
92
|
# File 'lib/rust/stats/probabilities.rb', line 90
def values
@values
end
|
Class Method Details
.complete(hash, key = 0) ⇒ Object
183
184
185
186
|
# File 'lib/rust/stats/probabilities.rb', line 183
def self.complete(hash, key=0)
hash[key] = 1 - hash.values.sum
return RandomVariable.new(hash)
end
|
Instance Method Details
120
121
122
123
124
125
126
127
128
|
# File 'lib/rust/stats/probabilities.rb', line 120
def *(times)
if times.is_a? Integer
return rep(times)
elsif times.is_a? RandomVariable
return mul(times)
else
raise "The argument must be an Integer or a RandomVariable"
end
end
|
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/rust/stats/probabilities.rb', line 106
def +(other)
new_hash = {}
@values.each do |my_key, my_value|
other.values.each do |other_key, other_value|
sum_key = my_key + other_key
new_hash[sum_key] = new_hash[sum_key].to_f + (my_value * other_value)
end
end
return RandomVariable.new(new_hash, @exact)
end
|
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/rust/stats/probabilities.rb', line 157
def approx!
return if @exact
to_delete = []
@values.each do |v, probability|
to_delete.push v if probability <= EPSILON
end
to_delete.each do |v|
probability = @values.delete v
nearest = @values.keys.min_by { |k| k.distance v }
@values[nearest] += probability
end
end
|
153
154
155
|
# File 'lib/rust/stats/probabilities.rb', line 153
def exact!
@exact = true
end
|
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/rust/stats/probabilities.rb', line 172
def
v = rand
cumulative = 0
@values.each do |key, prob|
cumulative += prob
return key if cumulative >= v
end
end
|
#mul(other) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/rust/stats/probabilities.rb', line 130
def mul(other)
new_hash = {}
@values.each do |my_key, my_value|
other.values.each do |other_key, other_value|
mul_key = my_key * other_key
new_hash[mul_key] = new_hash[mul_key].to_f + (my_value * other_value)
end
end
return RandomVariable.new(new_hash, @exact)
end
|
#probability(v) ⇒ Object
102
103
104
|
# File 'lib/rust/stats/probabilities.rb', line 102
def probability(v)
return @values[v].to_f
end
|
#rep(times) ⇒ Object
144
145
146
147
148
149
150
151
|
# File 'lib/rust/stats/probabilities.rb', line 144
def rep(times)
rv = self
(times-1).times do
rv += self
end
return rv
end
|