Class: Rubythinking::Distributions::Binomial
- Inherits:
-
Object
- Object
- Rubythinking::Distributions::Binomial
- Defined in:
- lib/rubythinking/distributions/binomial.rb
Class Method Summary collapse
- .density(value:, size:, success:) ⇒ Object
- .factorial(n) ⇒ Object
- .likelihood(w, l, p) ⇒ Object
- .samples(n, size:, success:) ⇒ Object
Class Method Details
.density(value:, size:, success:) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rubythinking/distributions/binomial.rb', line 8 def density(value:, size:, success:) # Use variable names often used in math # textbooks and wikipedia to have code # close to the formula k = value n = size p = success (factorial(n).to_f / (factorial(k) * factorial(n - k))).to_f * (p**k) * ((1-p)**(n-k)) end |
.factorial(n) ⇒ Object
19 20 21 22 |
# File 'lib/rubythinking/distributions/binomial.rb', line 19 def factorial(n) return 1 if n < 1 n.to_i.downto(1).inject(:*) end |
.likelihood(w, l, p) ⇒ Object
25 26 27 |
# File 'lib/rubythinking/distributions/binomial.rb', line 25 def likelihood(w, l, p) density(value: w, size: (w+l), success: p) end |
.samples(n, size:, success:) ⇒ Object
3 4 5 6 |
# File 'lib/rubythinking/distributions/binomial.rb', line 3 def samples(n, size:, success:) dist = Croupier::Distributions::Binomial.new(size: size, success: success) samples = dist.to_enum.take(n) end |