class Digiproc::Probability::TheoreticalBinomialDistribution

Allows you to solve problems whose probabilities are dictated by a binomial distribution Does not simulate a problem, but calculates probabilities based off of the binomial distribution equation See examples/binomial_distribution/dice.rb

Attributes

k[RW]
n[RW]
p[RW]

Public Class Methods

new(n: ,k: nil , p: ) click to toggle source

Initialize with n, k, and p

n
Integer

Number of trials

k
Integer

(optional at initialization) Number of successes

p
Numeric

Probability of success

# File lib/probability/binomial_distribution.rb, line 15
def initialize(n: ,k: nil , p: )
    @n = n
    @k = k
    @p = p
end

Public Instance Methods

coeff() click to toggle source

Will run `coefficient` method using `self.k`

bd.coeff # => 252.0
# File lib/probability/binomial_distribution.rb, line 49
def coeff
    coefficient(self.k)
end
coefficient(k = self.k) click to toggle source

Returns the binomial equation coefficient for a given number of wins, ie returns n! / (k! * (n - k)!) Can take an argument of `k`, which defaults to `self.k` Returns a BigDecimal

bd.coefficient(4) # => 210.0
# File lib/probability/binomial_distribution.rb, line 77
def coefficient(k = self.k)
    n_fact = BigDecimal(Digiproc::Functions.fact(self.n))
    k_fact = BigDecimal(Digiproc::Functions.fact(k))
    n_fact / (k_fact * BigDecimal((Digiproc::Functions.fact(self.n - k))))
end
expect() click to toggle source

Returns the Expected Value of a binomial distributed random variable (n * p)

bd = Digiproc::Probability::TheoreticalBinomialDistribution.new(n:10, p: 0.5)
bd.expect # => 5

ie in above example, expect 5 successes (heads) in 10 flips of a coin

# File lib/probability/binomial_distribution.rb, line 26
def expect
    self.n * self.p
end
prob() click to toggle source

Will run `probability` method using `self.k`

bd.k = 5
bd.prob # => 0.24609375
# File lib/probability/binomial_distribution.rb, line 42
def prob
    self.probability(self.k)
end
probability(*k) click to toggle source

Calculates the probability of `k` number of `wins` `k` can be an array or a range of numbers, a variable number of args, or a single integer (see example referenced above) If k is more than one value, the probabilities will be summed together For example:

bd.probability(5) # => 0.24609375
bd.probability(0..5) # => 0.623046875
bd.probability(1,2,5,6) # => 0.5048828125
bd.probability([0,1,2,3,4,5]) # => 0.623046875
# File lib/probability/binomial_distribution.rb, line 62
def probability(*k)
    sum = 0
    karr = k.map{ |val| val.respond_to?(:to_a) ? val.to_a : val}
    karr.flatten!
    karr.each do |k_val| 
        sum += self.coefficient(k_val) * ((self.p) ** (k_val)) * (1 - self.p) ** (n - k_val)
    end
    sum.to_f
end
var() click to toggle source

Return the Variance of the binomial distributed random variable (n * p * (1 - p)) Using above example:

bd.var # => 2.5
# File lib/probability/binomial_distribution.rb, line 34
def var
    self.n * self.p * ( 1 - self.p )
end