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
aandb. -
#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.
-
#plot ⇒ Object
Creates a bar plot of the distribution.
-
#probability(v = nil) ⇒ Object
Gets the probability of a value
v. -
#sd ⇒ Object
Returns the standard deviation for this slice.
-
#so_that ⇒ Object
Returns a slice with the values for which the given block returns true.
-
#variance ⇒ Object
Returns the variance for this slice.
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.
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 lower than or equal to n.
126 127 128 |
# File 'lib/rust/stats/probabilities.rb', line 126 def <=(n) self.so_that { |k| k <= n } end |
#==(n) ⇒ Object
Returns a slice with the value n.
133 134 135 |
# File 'lib/rust/stats/probabilities.rb', line 133 def ==(n) self.so_that { |k| k == n } end |
#>(n) ⇒ Object
Returns a slice with the values that are greater 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 greater 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 |
#between(a, b) ⇒ Object
Returns a slice with the values between a and b.
140 141 142 |
# File 'lib/rust/stats/probabilities.rb', line 140 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 |
#plot ⇒ Object
Creates a bar plot of the distribution
154 155 156 |
# File 'lib/rust/stats/probabilities.rb', line 154 def plot Rust::Plots::BarPlot.new(@values.sort_by { |k, v| k }.to_h) 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 |
#sd ⇒ Object
Returns the standard deviation for this slice.
98 99 100 |
# File 'lib/rust/stats/probabilities.rb', line 98 def sd Math.sqrt(self.variance) end |
#so_that ⇒ Object
Returns a slice with the values for which the given block returns true.
147 148 149 |
# File 'lib/rust/stats/probabilities.rb', line 147 def so_that RandomVariableSlice.new(@values.select { |k, v| yield(k) }) end |
#variance ⇒ Object
Returns the variance for this slice.
91 92 93 |
# File 'lib/rust/stats/probabilities.rb', line 91 def variance @values.map { |k, v| k**2 * v }.sum - (self.expected ** 2) end |