Skip to content

Instantly share code, notes, and snippets.

@tyler
Created March 23, 2010 22:54
Show Gist options
  • Save tyler/341783 to your computer and use it in GitHub Desktop.
Save tyler/341783 to your computer and use it in GitHub Desktop.
Binomial Distribution
def factorial(n)
(2..n).inject(1) { |o,k| o * k }
end
def binomial_coefficient(n,k)
factorial(n) / (factorial(n - k) * factorial(k))
end
def binomial_distribution(p,n)
(1..n).inject(0) do |total,k|
total + binomial_coefficient(n,k) * ((p ** k) * ((1 - p) ** (n - k)))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment