Class: Rust::RandomVariable
- Inherits:
-
RandomVariableSlice
- Object
- RandomVariableSlice
- Rust::RandomVariable
- Defined in:
- lib/rust/stats/probabilities.rb
Overview
Represents a random variable. The cumulative probability of the values must equal 1.
Direct Known Subclasses
Constant Summary collapse
- EPSILON =
1e-7
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
-
.complete(hash, key = 0) ⇒ Object
Creates a random variable by partially specifying the values through
hash.
Instance Method Summary collapse
-
#*(arg) ⇒ Object
Based on the type of
arg, either mul (product with another random variable) or rep (repeated sum) is called. -
#+(other) ⇒ Object
Returns a new random variable which represents the sum of this and the
otherrandom variable. -
#approx! ⇒ Object
If this variable is not exact, the values with probability lower than EPSLION are removed.
-
#exact! ⇒ Object
Makes sure that the operations yield all the values, even the most unlikely ones.
-
#extract ⇒ Object
Returns a random value, according to the data distribution.
-
#initialize(values = {0 => 1.0}, exact = false) ⇒ RandomVariable
constructor
Creates a new random variable.
-
#mul(other) ⇒ Object
Returns a new random variable which represents the product of this and the
otherrandom variable. -
#probability(v) ⇒ Object
Returns the probability of value
v. -
#rep(times) ⇒ Object
Returns a new random variable which represents the sum of this random variable with itself
ntimes.
Methods inherited from RandomVariableSlice
#<, #<=, #==, #>, #>=, #between, #expected, #ml, #plot, #sd, #so_that, #variance
Constructor Details
#initialize(values = {0 => 1.0}, exact = false) ⇒ RandomVariable
Creates a new random variable. values is a hash of values associated with their probabilities. exact indicates whether this variable, when combined with others, should force to keep all the values, even the most unlikely ones. If this is false (default), the most improbable values (lower than EPSILON) are removed for efficiency reasons.
173 174 175 176 177 178 179 180 181 |
# File 'lib/rust/stats/probabilities.rb', line 173 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
#values ⇒ Object (readonly)
Returns the value of attribute values.
165 166 167 |
# File 'lib/rust/stats/probabilities.rb', line 165 def values @values end |
Class Method Details
.complete(hash, key = 0) ⇒ Object
Creates a random variable by partially specifying the values through hash. The remaining probability is attributed to key (0, by default).
292 293 294 295 |
# File 'lib/rust/stats/probabilities.rb', line 292 def self.complete(hash, key=0) hash[key] = 1 - hash.values.sum return RandomVariable.new(hash) end |
Instance Method Details
#*(arg) ⇒ Object
Based on the type of arg, either mul (product with another random variable) or rep (repeated sum) is called.
210 211 212 213 214 215 216 217 218 |
# File 'lib/rust/stats/probabilities.rb', line 210 def *(arg) if arg.is_a? Integer return rep(arg) elsif arg.is_a? RandomVariable return mul(arg) else raise "The argument must be an Integer or a RandomVariable" end end |
#+(other) ⇒ Object
Returns a new random variable which represents the sum of this and the other random variable.
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rust/stats/probabilities.rb', line 193 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 |
#approx! ⇒ Object
If this variable is not exact, the values with probability lower than EPSLION are removed.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/rust/stats/probabilities.rb', line 259 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._rust_prob_distance v } @values[nearest] += probability end end |
#exact! ⇒ Object
Makes sure that the operations yield all the values, even the most unlikely ones.
252 253 254 |
# File 'lib/rust/stats/probabilities.rb', line 252 def exact! @exact = true end |
#extract ⇒ Object
Returns a random value, according to the data distribution.
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/rust/stats/probabilities.rb', line 277 def extract v = rand cumulative = 0 @values.sort_by { |k, v| k }.each do |key, prob| cumulative += prob return key if cumulative >= v end end |
#mul(other) ⇒ Object
Returns a new random variable which represents the product of this and the other random variable.
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rust/stats/probabilities.rb', line 223 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
Returns the probability of value v.
186 187 188 |
# File 'lib/rust/stats/probabilities.rb', line 186 def probability(v) return @values[v].to_f end |
#rep(times) ⇒ Object
Returns a new random variable which represents the sum of this random variable with itself n times.
240 241 242 243 244 245 246 247 |
# File 'lib/rust/stats/probabilities.rb', line 240 def rep(times) rv = self (times-1).times do rv += self end return rv end |