Class: Bernoulli::Distribution::Hypergeometric

Inherits:
Object
  • Object
show all
Includes:
Bernoulli::Distribution
Defined in:
lib/bernoulli/distribution/hypergeometric.rb

Overview

Implements hypergeometrically distributed random variable See en.wikipedia.org/wiki/Hypergeometric_distribution for more information

Instance Method Summary collapse

Methods included from Bernoulli::Distribution

#[], #probability_range, #standard_deviation

Constructor Details

#initialize(bn, m, n) ⇒ Hypergeometric

Returns a new instance of Hypergeometric.

Parameters:

  • bn (Integer)

    the size of the population (big n) (> 0)

  • m (Integer)

    the number of successes (>= 0, <= bn)

  • n (Integer)

    the number of draws (>= 1, <= bn)



17
18
19
20
21
22
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 17

def initialize(bn, m, n)
  if bn < 1 or m < 0 or m > bn or n < 1 or n > bn
    raise 'Expecting bn > 1, 0 < m < bn, 0 < n < bn'
  end
  @bn, @m, @n = bn.to_i, m.to_i, n.to_i
end

Instance Method Details

#excessObject



48
49
50
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 48

def excess
  ((@bn - 1) * @bn * @bn * (@bn * (@bn + 1) - 6 * @m * (@bn - @m) - 6 * @n * (@bn - @n)) + 6 * @n * @m * (@bn - @m) * (@bn - @n) * (5 * @bn - 6)).to_f / (@n * @m * (@bn - @m) * (@bn - @n) * (@bn - 2) * (@bn - 3))
end

#expected_valueObject Also known as: ev



34
35
36
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 34

def expected_value
  @n * (@m.to_f / @bn)
end

#probability(k) ⇒ Float

Returns the probability of ‘k` successes.

Parameters:

  • k (Integer)

    number of successes (>= 0)

Returns:

  • (Float)

    the probability of ‘k` successes



30
31
32
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 30

def probability(k)
  (Math.binomial(@m, k) * Math.binomial(@bn - @m, @n - k)).to_f / Math.binomial(@bn, @n)
end

#skewnessObject



44
45
46
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 44

def skewness
  ((@bn - 2 * @m) * Math.sqrt(@bn - 1) * (@bn - 2 * @n)) / (Math.sqrt(@n * @m * (@bn - @m) * (@bn - @n)) * (@bn - 2))
end

#to_sObject



24
25
26
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 24

def to_s
  "#<Distribution::Hypergeometric @bn=#@bn, @m=#@m, @n=#@n>"
end

#varianceObject Also known as: v



39
40
41
# File 'lib/bernoulli/distribution/hypergeometric.rb', line 39

def variance
  (@n * @m * (@bn - @m) *(@bn - @n)).to_f / (@bn * @bn * (@bn - 1))
end