Module: Distribution::Binomial::Ruby_

Defined in:
lib/distribution/binomial/ruby.rb

Class Method Summary collapse

Class Method Details

.cdf(k, n, pr) ⇒ Object

TODO: Use exact_regularized_beta for small values and regularized_beta for bigger ones.


14
15
16
17
# File 'lib/distribution/binomial/ruby.rb', line 14

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

.exact_cdf(k, n, pr) ⇒ Object


19
20
21
22
23
# File 'lib/distribution/binomial/ruby.rb', line 19

def exact_cdf(k, n, pr)
  out = (0..k).inject(0) { |ac, i| ac + pdf(i, n, pr) }
  out = 1 if out > 1.0
  out
end

.pdf(k, n, pr) ⇒ Object Also known as: exact_pdf


5
6
7
8
# File 'lib/distribution/binomial/ruby.rb', line 5

def pdf(k, n, pr)
  fail 'k>n' if k > n
  Math.binomial_coefficient(n, k) * (pr**k) * (1 - pr)**(n - k)
end

.quantile(prob, n, pr) ⇒ Object Also known as: p_value


25
26
27
28
29
30
31
# File 'lib/distribution/binomial/ruby.rb', line 25

def quantile(prob, n, pr)
  ac = 0
  (0..n).each do |i|
    ac += pdf(i, n, pr)
    return i if prob <= ac
  end
end