Class: Rust::RandomVariableSlice

Inherits:
Object
  • Object
show all
Defined in:
lib/rust/stats/probabilities.rb

Direct Known Subclasses

RandomVariable

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ RandomVariableSlice

Returns a new instance of RandomVariableSlice.

Raises:

  • (TypeError)


36
37
38
39
40
# File 'lib/rust/stats/probabilities.rb', line 36

def initialize(values)
    raise TypeError, "Expected Hash" unless values.is_a?(Hash)
    
    @values = values
end

Instance Method Details

#<(n) ⇒ Object



66
67
68
# File 'lib/rust/stats/probabilities.rb', line 66

def <(n)
    self.so_that { |k| k < n}
end

#<=(n) ⇒ Object



70
71
72
# File 'lib/rust/stats/probabilities.rb', line 70

def <=(n)
    self.so_that { |k| k <= n}
end

#==(n) ⇒ Object



74
75
76
# File 'lib/rust/stats/probabilities.rb', line 74

def ==(n)
    self.so_that { |k| k == n}
end

#>(n) ⇒ Object



58
59
60
# File 'lib/rust/stats/probabilities.rb', line 58

def >(n)
    self.so_that { |k| k > n}
end

#>=(n) ⇒ Object



62
63
64
# File 'lib/rust/stats/probabilities.rb', line 62

def >=(n)
    self.so_that { |k| k >= n}
end

#between(a, b) ⇒ Object



82
83
84
# File 'lib/rust/stats/probabilities.rb', line 82

def between(a, b)
    RandomVariableSlice.new(@values.select { |k, v| k.between? a, b })
end

#expectedObject



54
55
56
# File 'lib/rust/stats/probabilities.rb', line 54

def expected
    @values.map { |k, v| k*v }.sum
end

#mlObject



50
51
52
# File 'lib/rust/stats/probabilities.rb', line 50

def ml
    @values.max_by { |k, v| v }[0]
end

#probability(v = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rust/stats/probabilities.rb', line 42

def probability(v=nil)
    unless v
        return @values.values.sum
    else
        return @values[v]
    end
end

#so_thatObject



78
79
80
# File 'lib/rust/stats/probabilities.rb', line 78

def so_that
    RandomVariableSlice.new(@values.select { |k, v| yield(k) })
end