class MonteCarlo::ExperimentResults

Class containing the results of a {MonteCarlo::Experiment}

Public Class Methods

new() click to toggle source
# File lib/monte_carlo/experiment_results.rb, line 6
def initialize
  @results = []
end

Public Instance Methods

<<(result) click to toggle source

Add a result to the list

@param result [MonteCarlo::Result]

# File lib/monte_carlo/experiment_results.rb, line 22
def << result
  @results << result
  reset_memoizers
end
each() { |result| ... } click to toggle source

Returns an enumerable of the results or runs the given block

@param block [optional, Block] the block to run on each of the results

# File lib/monte_carlo/experiment_results.rb, line 13
def each(&block)
  @results.each do |result|
    block_given? ? block.call(result) : yield(result)
  end
end
frequency_distribution() click to toggle source

Calculates the frequency distibution of the results

@return [Hash]

# File lib/monte_carlo/experiment_results.rb, line 47
def frequency_distribution
  @frequency_distribution ||= @results.group_by(&:value).inject({}) do |frequencies, (value, results)|
    frequencies[value] = results.size
    frequencies
  end
end
frequency_of(value) click to toggle source

Calculates the frequency of the given value in the results

@param value the value for which to get the frequency @return [Fixnum]

# File lib/monte_carlo/experiment_results.rb, line 66
def frequency_of(value)
  frequency_distribution.fetch(value, 0)
end
probability_distribution() click to toggle source

Calculates the probabilty distribution of the results

@return [Hash]

# File lib/monte_carlo/experiment_results.rb, line 37
def probability_distribution
  @probability_distribution ||= @results.group_by(&:value).inject({}) do |probabilites, (value, results)|
    probabilites[value] = results.size.to_f / self.size
    probabilites
  end
end
probability_of(value) click to toggle source

Calcuates the probablity of the given value in the results

@param value the value for which to get the probability @return [Float]

# File lib/monte_carlo/experiment_results.rb, line 58
def probability_of(value)
  probability_distribution.fetch(value, 0)
end
size() click to toggle source

The number of results

@return [Fixnum]

# File lib/monte_carlo/experiment_results.rb, line 30
def size
  @results.size
end

Private Instance Methods

reset_memoizers() click to toggle source
# File lib/monte_carlo/experiment_results.rb, line 72
def reset_memoizers
  @probability_distribution = nil
  @frequency_distribution = nil
end