Sha256: d497227129dbde313deb5d0f1270adcc0fe2ee6ba4d731e6aa5870aef9f5ffe6

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'fathom'))
class Fathom::MonteCarloSet

  class << self
    def define_key(key)
      define_method(key.to_sym) do
        self.samples[key.to_sym]
      end
    end
  end
  
  attr_reader :value_description, :samples_taken, :samples
  
  def initialize(value_description)
    @value_description = value_description
    @samples = {}
  end
  
  def process(n=10_000)
    @samples_taken = n
    @samples_taken.times do
      result = value_description.process
      store(result)
    end
    assert_sample_vectors
  end
  
  def reset!
    @samples = {}
    @keys_asserted = nil
  end
  
  protected
  
    def assert_sample_vectors
      vectors = @samples.inject({}) do |h, o|
        key, array = o.first, o.last
        h[key] = GSL::Vector.ary_to_gv(array)
        h
      end
      @samples = vectors
    end
    
    def store(result)
      result = assert_result_hash(result)
      assert_keys(result)
      result.each do |key, value|
        @samples[key.to_sym] << value
      end
    end
    
    def assert_result_hash(result)
      result.is_a?(Hash) ? result : {:result => result}
    end
    
    # Assumes the same value description for all samples taken
    def assert_keys(result)
      return true if @keys_asserted
      result.keys.each do |key|
        assert_key(key)
      end
      @keys_asserted = true
    end
    
    def assert_key(key)
      self.class.define_key(key)
      @samples[key.to_sym] ||= []
    end
    
end

if __FILE__ == $0
  include Fathom
  # TODO: Is there anything you want to do to run this file on its own?
  # MonteCarloSet.new
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fathom-0.2.1 lib/fathom/monte_carlo_set.rb
fathom-0.2.0 lib/fathom/monte_carlo_set.rb
fathom-0.1.0 lib/fathom/monte_carlo_set.rb