Module: Sylfy::Mathf

Defined in:
lib/sylfy/mathf.rb

Overview

additional Mathematic function

Class Method Summary collapse

Class Method Details

.hypergeometric(k, nn, kk, n) ⇒ Object

hypergeometric function

Parameters:

  • nn (Int)

    the population size

  • kk (Int)

    the number of success states in the population

  • n (Int)

    the number of draws

  • k (Int)

    the number of successes



34
35
36
37
# File 'lib/sylfy/mathf.rb', line 34

def self.hypergeometric(k, nn, kk, n)
    log_h = lncombinatorial(kk, k) + lncombinatorial(nn-kk, n-k) - lncombinatorial(nn, n)
    return (Math::E ** log_h)
end

.lncombinatorial(n, r) ⇒ Object

calculate log combinatorial

Parameters:

  • n (Int)

    the population size

  • r (Int)

    the number of draws



25
26
27
# File 'lib/sylfy/mathf.rb', line 25

def self.lncombinatorial(n, r)
    return lnfactorial(n) - lnfactorial(r) - lnfactorial(n-r)
end

.lnfactorial(n) ⇒ Object

calculate log factorial

Parameters:

  • n (Int)


18
19
20
# File 'lib/sylfy/mathf.rb', line 18

def self.lnfactorial(n)
    return GSL::Sf::lngamma(n+1)
end