Class: Digiproc::Probability::TheoreticalBinomialDistribution
- Inherits:
-
Object
- Object
- Digiproc::Probability::TheoreticalBinomialDistribution
- Defined in:
- lib/probability/binomial_distribution.rb
Overview
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
Instance Attribute Summary collapse
-
#k ⇒ Object
Returns the value of attribute k.
-
#n ⇒ Object
Returns the value of attribute n.
-
#p ⇒ Object
Returns the value of attribute p.
Instance Method Summary collapse
-
#coeff ⇒ Object
Will run ‘coefficient` method using `self.k` bd.coeff # => 252.0.
-
#coefficient(k = self.k) ⇒ Object
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.
-
#expect ⇒ Object
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.
-
#initialize(n:, k: nil, p:) ⇒ TheoreticalBinomialDistribution
constructor
Initialize with n, k, and p n:: [Integer] Number of trials k:: [Integer] (optional at initialization) Number of successes p:: [Numeric] Probability of success.
-
#prob ⇒ Object
Will run ‘probability` method using `self.k` bd.k = 5 bd.prob # => 0.24609375.
-
#probability(*k) ⇒ Object
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.623046875.
-
#var ⇒ Object
Return the Variance of the binomial distributed random variable (n * p * (1 - p)) Using above example: bd.var # => 2.5.
Constructor Details
#initialize(n:, k: nil, p:) ⇒ TheoreticalBinomialDistribution
Initialize with n, k, and p
- n
- Integer
-
Number of trials
- k
- Integer
-
(optional at initialization) Number of successes
- p
- Numeric
-
Probability of success
15 16 17 18 19 |
# File 'lib/probability/binomial_distribution.rb', line 15 def initialize(n: ,k: nil , p: ) @n = n @k = k @p = p end |
Instance Attribute Details
#k ⇒ Object
Returns the value of attribute k.
8 9 10 |
# File 'lib/probability/binomial_distribution.rb', line 8 def k @k end |
#n ⇒ Object
Returns the value of attribute n.
8 9 10 |
# File 'lib/probability/binomial_distribution.rb', line 8 def n @n end |
#p ⇒ Object
Returns the value of attribute p.
8 9 10 |
# File 'lib/probability/binomial_distribution.rb', line 8 def p @p end |
Instance Method Details
#coeff ⇒ Object
Will run ‘coefficient` method using `self.k` bd.coeff # => 252.0
49 50 51 |
# File 'lib/probability/binomial_distribution.rb', line 49 def coeff coefficient(self.k) end |
#coefficient(k = self.k) ⇒ Object
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
77 78 79 80 81 |
# 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 ⇒ Object
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
26 27 28 |
# File 'lib/probability/binomial_distribution.rb', line 26 def expect self.n * self.p end |
#prob ⇒ Object
Will run ‘probability` method using `self.k` bd.k = 5 bd.prob # => 0.24609375
42 43 44 |
# File 'lib/probability/binomial_distribution.rb', line 42 def prob self.probability(self.k) end |
#probability(*k) ⇒ Object
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.623046875
62 63 64 65 66 67 68 69 70 |
# 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 ⇒ Object
Return the Variance of the binomial distributed random variable (n * p * (1 - p)) Using above example: bd.var # => 2.5
34 35 36 |
# File 'lib/probability/binomial_distribution.rb', line 34 def var self.n * self.p * ( 1 - self.p ) end |