Class: Rust::RandomVariableSlice
- Defined in:
- lib/rust/stats/probabilities.rb
Overview
Represents a slice of a random variable, for which no check is made in terms of cumulative probability.
Direct Known Subclasses
Instance Method Summary collapse
-
#<(n) ⇒ Object
Returns a slice with the values that are lower than
n
. -
#<=(n) ⇒ Object
Returns a slice with the values that are lower than or equal to
n
. -
#==(n) ⇒ Object
Returns a slice with the value
n
. -
#>(n) ⇒ Object
Returns a slice with the values that are greater than
n
. -
#>=(n) ⇒ Object
Returns a slice with the values that are greater than or equal to
n
. -
#between(a, b) ⇒ Object
Returns a slice with the values between
a
andb
. -
#expected ⇒ Object
Returns the expected value for this slice.
-
#initialize(values) ⇒ RandomVariableSlice
constructor
Creates a new slice of random variable.
-
#ml ⇒ Object
Returns the value with the maximum probability.
-
#probability(v = nil) ⇒ Object
Gets the probability of a value
v
. -
#so_that ⇒ Object
Returns a slice with the values for which the given block returns true.
Constructor Details
#initialize(values) ⇒ RandomVariableSlice
Creates a new slice of random variable. values
is a hash of values associated with their probabilities.
56 57 58 59 60 |
# File 'lib/rust/stats/probabilities.rb', line 56 def initialize(values) raise TypeError, "Expected Hash" unless values.is_a?(Hash) @values = values end |
Instance Method Details
#<(n) ⇒ Object
Returns a slice with the values that are lower than n
.
105 106 107 |
# File 'lib/rust/stats/probabilities.rb', line 105 def <(n) self.so_that { |k| k < n } end |
#<=(n) ⇒ Object
Returns a slice with the values that are lower than or equal to n
.
112 113 114 |
# File 'lib/rust/stats/probabilities.rb', line 112 def <=(n) self.so_that { |k| k <= n } end |
#==(n) ⇒ Object
Returns a slice with the value n
.
119 120 121 |
# File 'lib/rust/stats/probabilities.rb', line 119 def ==(n) self.so_that { |k| k == n } end |
#>(n) ⇒ Object
Returns a slice with the values that are greater than n
.
91 92 93 |
# File 'lib/rust/stats/probabilities.rb', line 91 def >(n) self.so_that { |k| k > n } end |
#>=(n) ⇒ Object
Returns a slice with the values that are greater than or equal to n
.
98 99 100 |
# File 'lib/rust/stats/probabilities.rb', line 98 def >=(n) self.so_that { |k| k >= n } end |
#between(a, b) ⇒ Object
Returns a slice with the values between a
and b
.
126 127 128 |
# File 'lib/rust/stats/probabilities.rb', line 126 def between(a, b) self.so_that { |k| k.between(a, b) } end |
#expected ⇒ Object
Returns the expected value for this slice.
84 85 86 |
# File 'lib/rust/stats/probabilities.rb', line 84 def expected @values.map { |k, v| k*v }.sum end |
#ml ⇒ Object
Returns the value with the maximum probability.
77 78 79 |
# File 'lib/rust/stats/probabilities.rb', line 77 def ml @values.max_by { |k, v| v }[0] end |
#probability(v = nil) ⇒ Object
Gets the probability of a value v
. If v
is not specified, returns the cumulative probability of the whole slice.
66 67 68 69 70 71 72 |
# File 'lib/rust/stats/probabilities.rb', line 66 def probability(v=nil) unless v return @values.values.sum else return @values[v] end end |
#so_that ⇒ Object
Returns a slice with the values for which the given block returns true.
133 134 135 |
# File 'lib/rust/stats/probabilities.rb', line 133 def so_that RandomVariableSlice.new(@values.select { |k, v| yield(k) }) end |