class Markov::Statistics::RandomEvent
Public Class Methods
new(outcome_counts = {})
click to toggle source
# File lib/markov/statistics/random_event.rb, line 4 def initialize(outcome_counts = {}) @outcome_counts = outcome_counts end
Public Instance Methods
add_outcome(outcome, count)
click to toggle source
# File lib/markov/statistics/random_event.rb, line 8 def add_outcome(outcome, count) @outcome_counts[outcome] = count end
normalized_outcome_probabilities()
click to toggle source
# File lib/markov/statistics/random_event.rb, line 12 def normalized_outcome_probabilities total_outcome_counts = @outcome_counts.values.reduce(:+).to_f @outcome_counts.map { |outcome, count| [outcome, count / total_outcome_counts] }.to_h end
predict!()
click to toggle source
# File lib/markov/statistics/random_event.rb, line 17 def predict! roll = rand selected = nil normalized_outcome_probabilities.inject(0.0) do |acc, (outcome, probability)| if (acc += probability) > roll selected = outcome break end acc end selected end