Sha256: 7e7d2ec325013e00df8f19bbacd9f597c4e95135f88be2d8e8890c1cf6bc97f1

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'fathom'))
class Fathom::Distributions::DiscreteGaussian
  extend Fathom::Distributions::SharedMethods
  class << self
    def rng
      @rng ||= GSL::Rng.alloc(GSL::Rng::MT19937_1999, Kernel.rand(100_000))
    end
    
    def rand(sd)
      (rng.gaussian(sd) / size).floor + 1
    end
    
    def inverse_cdf(opts={})
      mean = opts[:mean]
      sd = opts[:sd]
      sd ||= opts[:std]
      sd ||= opts[:standard_deviation]
      lower = opts.fetch(:lower, true)
      lower = false if opts[:upper]
      confidence_interval = opts.fetch(:confidence_interval, 0.05)
      value = lower ? GSL::Cdf.gaussian_Pinv(confidence_interval, sd) : GSL::Cdf.gaussian_Qinv(confidence_interval, sd)
      value + mean
    end
    alias :lower_bound :inverse_cdf

    def upper_bound(opts={})
      inverse_cdf(opts.merge(:lower => false))
    end

    def interval_values(opts={})
      confidence_interval = opts.fetch(:confidence_interval, 0.9)
      bound = (1 - confidence_interval) / 2.0
      [lower_bound(opts.merge(:confidence_interval => bound)), upper_bound(opts.merge(:confidence_interval => bound))]
    end
    
    # If only I had the background to explain what this is....
    # I want to know how many standard deviations are expressed by the confidence interval
    # I can then divide  the range by this number to get the standard deviation
    def standard_deviations_under(confidence_interval)
      GSL::Cdf.gaussian_Qinv((1 - confidence_interval) / 2) * 2
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fathom-0.3.7 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.3.6 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.3.4 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.3.3 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.3.2 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.3.1 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.3.0 lib/fathom/distributions/discrete_gaussian.rb
fathom-0.2.3 lib/fathom/distributions/discrete_gaussian.rb