Sha256: 68bc2c3e3dc9ba0cfe230ff9738a922068ea6cd45d536d79a3bdb09b9b6a4e80

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module MonteCarlo
  class Experiment

    DEFAULT_TIMES = 10000

    attr_accessor :times, :sample_method, :computation, :before_each, :after_each

    def self.run(times = DEFAULT_TIMES, &block)
      MonteCarlo::Experiment.new(times, &block).run
    end

    def initialize(times = DEFAULT_TIMES, &block)
      @times = times
      @sample_method = block
    end

    def run
      if @sample_method.nil?
        raise MonteCarlo::Errors::NoSampleMethodError, 'A sample method for this experiment is not defined'
      end

      results = MonteCarlo::ExperimentResults.new

      @times.times do |index|
        begin
          @before_each.call() unless @before_each.nil?
          results << run_sample(index)
        ensure
          @after_each.call() unless @after_each.nil?
        end
      end

      results
    end

    private

    def run_sample(index)
      result = MonteCarlo::Result.new(index)
      result.sample_value = @sample_method.call()
      if @computation.nil?
        result.value = result.sample_value
      else
        result.value = @computation.call(result.sample_value)
      end

      result
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
monte_carlo-0.0.2 lib/monte_carlo/experiment.rb