Sha256: 1b1407cd00ae9ce9c0adbdcb8500741be8ce43765a83275a2e01598ae74e3453
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
module Distribution module Poisson module Ruby_ class << self # Return a Proc object which returns a random number drawn # from the poisson distribution with lambda. # # == Arguments # * +lambda_val+ - mean of the poisson distribution # * +seed+ - seed, an integer value to set the initial state # # == Algorithm # * Donald Knuth # def rng(lambda_val = 1, seed = nil) seed = Random.new_seed if seed.nil? r = Random.new(seed).rand x = 0 l = Math.exp(-lambda_val) s = l while r > s x += 1 l *= lambda_val / x.to_f s += l end x end def pdf(k, l) (l**k * Math.exp(-l)).quo(Math.factorial(k)) end def cdf(k, l) Math.exp(-l) * (0..k).inject(0) { |ac, i| ac + (l**i).quo(Math.factorial(i)) } end def quantile(prob, l) ac = 0 (0..100).each do |i| ac += pdf(i, l) return i if prob <= ac end end alias_method :p_value, :quantile end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
distribution-0.8.0 | lib/distribution/poisson/ruby.rb |