Module: Mjai::ConfidenceInterval

Defined in:
lib/mjai/confidence_interval.rb

Class Method Summary collapse

Class Method Details

.calculate(samples, params = {}) ⇒ Object

Uses bootstrap resampling.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mjai/confidence_interval.rb', line 8

def calculate(samples, params = {})
  params = {:min => 0.0, :max => 1.0, :conf_level => 0.95}.merge(params)
  num_tries = 1000
  averages = []
  num_tries.times() do
    sum = 0.0
    (samples.size + 2).times() do
      idx = rand(samples.size + 2)
      case idx
        when samples.size
          sum += params[:min]
        when samples.size + 1
          sum += params[:max]
        else
          sum += samples[idx]
      end
    end
    averages.push(sum / (samples.size + 2))
  end
  averages.sort!()
  margin = (1.0 - params[:conf_level]) / 2
  return [
    averages[(num_tries * margin).to_i()],
    averages[(num_tries * (1.0 - margin)).to_i()],
  ]
end