Sha256: 7e9e87276f77705d9be1d14effc07eaf24b95031323b9f5ad294144a804528b7

Contents?: true

Size: 780 Bytes

Versions: 3

Compression:

Stored size: 780 Bytes

Contents

module Distribution
  module Binomial
    module Ruby_
      class << self
        def pdf(k,n,pr)
          raise "k>n" if k>n
	  Math.binomial_coefficient(n,k)*(pr**k)*(1-pr)**(n-k)          
        end
        # TODO: Use exact_regularized_beta for 
        # small values and regularized_beta for bigger ones.
        def cdf(k,n,pr)
          #(0..x.floor).inject(0) {|ac,i| ac+pdf(i,n,pr)}
          Math.regularized_beta(1-pr,n - k,k+1)
        end
        def exact_cdf(k,n,pr)
          (0..k).inject(0) {|ac,i| ac+pdf(i,n,pr)}
        end
        def p_value(prob,n,pr)
          ac=0
          (0..n).each do |i|
            ac+=pdf(i,n,pr)
            return i if prob<=ac
          end
        end
        
        alias :exact_pdf :pdf 
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
distribution-0.7.1 lib/distribution/binomial/ruby.rb
distribution-0.7.0 lib/distribution/binomial/ruby.rb
distribution-0.6.0 lib/distribution/binomial/ruby.rb