Class: McForecast::Simulation

Inherits:
Object
  • Object
show all
Defined in:
lib/mc_forecast/simulation.rb

Instance Method Summary collapse

Instance Method Details

#run(init_state: nil, trials: 1_000, steps: 1, quantiles: [0.025, 0.16, 0.84, 0.975], ranges: []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mc_forecast/simulation.rb', line 5

def run(init_state: nil, trials: 1_000, steps: 1, quantiles: [0.025, 0.16, 0.84, 0.975], ranges: [])
  events = {}
  (0..trials - 1).each do |trial|
    state = DeepDup.deep_dup(init_state)
    (0..steps - 1).each do |step|
      state, e = yield state, step, trial
      e.each_pair do |k, v|
        # We explicitly store all the events, because we expect the block
        # to be relatively expensive this will not be so bad.
        # could replace with a quantile estimator like CKMS later
        events[k] ||= Array.new(steps) { Array.new(trials) }
        events[k][step][trial] = v
      end
    end
  end
  analyze(events, quantiles, ranges)
end