Class: Rust::RandomVariableSlice

Inherits:
Object
  • Object
show all
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

RandomVariable

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ RandomVariableSlice

Creates a new slice of random variable. values is a hash of values associated with their probabilities.

Raises:

  • (TypeError)


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

#expectedObject

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

#mlObject

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_thatObject

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