Module: Simulation

Included in:
Monte::Commands::Carlo
Defined in:
lib/monte/simulation.rb

Overview

The code to run n monte carlo simulations args is a hash that must contain the following: :backlog :split_factor :runs :low :high

Constant Summary collapse

PERCENTILES =
[0.05, 0.15, 0.3, 0.5, 0.7, 0.85, 0.95].freeze

Instance Method Summary collapse

Instance Method Details

#percentiles(options) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/monte/simulation.rb', line 13

def percentiles(options)
  results = run_simulations(options).sort
  PERCENTILES.map do |percentile|
    index = options[:runs] * (percentile - 1)
    results[index]
  end
end

#run_simulations(options) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/monte/simulation.rb', line 21

def run_simulations(options)
  estimated_backlog = options[:backlog] * options[:split]
  Array.new(options[:runs]) do |_|
    options[:start] + simulate(
      estimated_backlog,
      options[:throughput]
    ) * 7
  end
end

#simulate(backlog, throughput, result = 0) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/monte/simulation.rb', line 31

def simulate(backlog, throughput, result = 0)
  return result if backlog <= 0

  simulate(
    backlog - throughput.sample,
    throughput,
    result + 1
  )
end