File: ruby.rb

package info (click to toggle)
ruby-distribution 0.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 572 kB
  • ctags: 370
  • sloc: ruby: 4,270; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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