Module: Distribution::Hypergeometric::Ruby_

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

Class Method Summary collapse

Class Method Details

.bc(n, k) ⇒ Object


8
9
10
# File 'lib/distribution/hypergeometric/ruby.rb', line 8

def bc(n, k)
  Math.binomial_coefficient(n, k)
end

.cdf(k, m, n, total) ⇒ Object Also known as: exact_cdf

Cumulative distribution function. The probability of obtain, from a sample of size +n+, +k+ or less elements in a population size +total+ with +m+ interesting elements.

Slow, but secure


39
40
41
42
43
44
45
# File 'lib/distribution/hypergeometric/ruby.rb', line 39

def cdf(k, m, n, total)
  fail(ArgumentError, 'k>m') if k > m
  fail(ArgumentError, 'k>n') if k > n
  # Store the den
  den = bc(total, n)
  (0..k).collect { |ki| pdf_with_den(ki, m, n, total, den) }.inject { |sum, v| sum + v }
end

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

Hypergeometric probability density function

Probability p(+k+, +m+, +n+, +total+) of drawing sets of size +m+ and +n+ with an intersection of size +k+ from a total pool of size +total+, without replacement.

==References


20
21
22
23
24
25
# File 'lib/distribution/hypergeometric/ruby.rb', line 20

def pdf(k, m, n, total)
  min_m_n = m < n ? m : n
  max_t = [0, m + n - total].max
  return 0 if k > min_m_n || k < max_t
  (bc(m, k) * bc(total - m, n - k)).quo(bc(total, n))
end

.pdf_with_den(k, m, n, total, den) ⇒ Object


29
30
31
# File 'lib/distribution/hypergeometric/ruby.rb', line 29

def pdf_with_den(k, m, n, total, den)
  (bc(m, k) * bc(total - m, n - k)).quo(den)
end

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

p-value:


50
51
52
53
54
55
56
57
58
# File 'lib/distribution/hypergeometric/ruby.rb', line 50

def quantile(pr, m, n, total)
  ac = 0
  den = bc(total, n)

  (0..total).each do |i|
    ac += pdf_with_den(i, m, n, total, den)
    return i if ac >= pr
  end
end