Module: Math

Defined in:
lib/binomial_distribution.rb

Defined Under Namespace

Classes: SuperiorityError

Class Method Summary collapse

Class Method Details

.binomial_distribution(*unamed, tries: nil, success: nil, probability: nil) ⇒ Object

Note:

if no named parameters are used, then it will try to use the unamed parameters (tries, success, probability)



16
17
18
19
# File 'lib/binomial_distribution.rb', line 16

def self.binomial_distribution(*unamed, tries: nil, success: nil, probability: nil)
  unamed.reverse! # pop works on a stack
  Binomial.new(tries: tries || unamed.pop, probability: probability || unamed.pop).distribute(success || unamed.pop)
end

.coef_binomial(n, k) ⇒ Object



10
11
12
13
# File 'lib/binomial_distribution.rb', line 10

def self.coef_binomial(n, k)
  return 0 if n < 0 or k < 0 or n < k
  return factorial(n) / (factorial(k) * factorial(n - k))
end

.factorial(n) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
# File 'lib/binomial_distribution.rb', line 3

def self.factorial(n)
  raise ArgumentError, 'The argument muse be a natural Fixnum N' if not n.is_a? Fixnum
  raise Math::DomainError, 'The argument must be a natural (out of domain -- factorial)' if n < 0
  return 1 if n.zero?
  return (1..n).inject(:*)
end