Sha256: 2fde961b64813bffb9561decf0ef5b48777e10f14d33062dcb918cb3464d7bf9

Contents?: true

Size: 798 Bytes

Versions: 2

Compression:

Stored size: 798 Bytes

Contents

module NumberCrusher
  def BootstrappedConfidenceInterval(numbers = nil, **options)
    function = BootstrappedConfidenceInterval.new(**options)
    numbers ? function.call(numbers) : function
  end

  class BootstrappedConfidenceInterval
    PRECISION = 4
    UNIT = 10**PRECISION
    def initialize(function:, confidence: 0.95, samples: 1000)
      @function = function
      @samples = samples

      conf = (confidence * UNIT).to_i
      lower_quant = (UNIT - conf) / 2
      @lower_quantile = lower_quant / UNIT.to_f
      @upper_quantile = (UNIT - lower_quant) / UNIT.to_f
    end

    def call(numbers)
      boot = Bootstrapping(numbers, function: @function, samples: @samples)
      [Quantile(boot, quant: @lower_quantile),
       Quantile(boot, quant: @upper_quantile)]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
number_crusher-0.1.2 lib/number_crusher/bootstrapped_confidence_interval.rb
number_crusher-0.1.1 lib/number_crusher/bootstrapped_confidence_interval.rb