Stats class, binomial distribution Python
A little background:
For my statistics class at the local community college, the teacher has us plug numbers into Excel to get answers, because well, we don't need to learn to do the formula.
I may be kinda a stickler about knowing the formula, while it's great Excel has a way to just plug in the numbers, I need to see the math. I need to see and understand what's going on.
I'll come back for more testing on this, and more explanation. The link below for khan academy will do a far superior job at explaining the math than I can.
Links
I don't know about you all, but when I looked at the SciPy and Numpy links, I had a hard time following. Also, the formulas and methods I found return an array or an object. I just need the result of that big hairy formula. I must not be googling correctly. If anyone can link me to the method in either one of these that just returns the results of that binomial distribution formula, it'd be much appreciated. *Also, then I can test mine against one written by mathaletes.*
SciPy:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html
Numpy:
https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.random.binomial.html
Khan Academy Link:
Here is a good video explaining binomial distribution:
https://www.khanacademy.org/math/statistics-probability/random-variables-stats-library/binomial-random-variables/v/binomial-distribution
What we need
This bit is explained in the Khan Academy video with all the fancy math pieces:
We want the combitorial * (the percent chance of event happening to the power of how many times we're looking for it to happen) * ( the percent chance of other event happening the other times event doesn't happen)
Psuedo Code
So lets write our psuedo code:
Since I'll be using this in class, I want a command line prompt that will let me enter my numbers from the problems given in class in a way I can understand.
So we'll need :
pk = probability of event happening
pki = possibility of event not happening(inverse)
n = number of events we wish to account for ( How many times is event going to happen?)
t = total number of events outcomes ( coin toss has 2 possible event outcomes)
We'll make this in a separate function under the hood => (total possible events/outcomes) = tp
I'll have the prompt ask for these numbers and then provide my answer. rounded to probably the thousandth, because the teacher likes to round to the hundreth or higher.
function total_events = t^n
function combitorial =
the factorial bit: comb = t! / n!(t -n)!
possibility = comb/ total_events
function deviation = combitorial * (pk)^n * (pki)^(t - n)
It looks big and hairy, and it is in my opinion. But if it wasn't so hairy, I wouldn't be shaving a yak right now.
Gimme the code
--start code block--
--end code block--
For my statistics class at the local community college, the teacher has us plug numbers into Excel to get answers, because well, we don't need to learn to do the formula.
I may be kinda a stickler about knowing the formula, while it's great Excel has a way to just plug in the numbers, I need to see the math. I need to see and understand what's going on.
I'll come back for more testing on this, and more explanation. The link below for khan academy will do a far superior job at explaining the math than I can.
Links
I don't know about you all, but when I looked at the SciPy and Numpy links, I had a hard time following. Also, the formulas and methods I found return an array or an object. I just need the result of that big hairy formula. I must not be googling correctly. If anyone can link me to the method in either one of these that just returns the results of that binomial distribution formula, it'd be much appreciated. *Also, then I can test mine against one written by mathaletes.*
SciPy:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html
Numpy:
https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.random.binomial.html
Khan Academy Link:
Here is a good video explaining binomial distribution:
https://www.khanacademy.org/math/statistics-probability/random-variables-stats-library/binomial-random-variables/v/binomial-distribution
What we need
This bit is explained in the Khan Academy video with all the fancy math pieces:
We want the combitorial * (the percent chance of event happening to the power of how many times we're looking for it to happen) * ( the percent chance of other event happening the other times event doesn't happen)
Psuedo Code
So lets write our psuedo code:
Since I'll be using this in class, I want a command line prompt that will let me enter my numbers from the problems given in class in a way I can understand.
So we'll need :
pk = probability of event happening
pki = possibility of event not happening(inverse)
n = number of events we wish to account for ( How many times is event going to happen?)
t = total number of events outcomes ( coin toss has 2 possible event outcomes)
We'll make this in a separate function under the hood => (total possible events/outcomes) = tp
I'll have the prompt ask for these numbers and then provide my answer. rounded to probably the thousandth, because the teacher likes to round to the hundreth or higher.
function total_events = t^n
function combitorial =
the factorial bit: comb = t! / n!(t -n)!
possibility = comb/ total_events
function deviation = combitorial * (pk)^n * (pki)^(t - n)
It looks big and hairy, and it is in my opinion. But if it wasn't so hairy, I wouldn't be shaving a yak right now.
Gimme the code
--start code block--
# variables=>
# pk = probablity of event happening
# pki = probability of event not happening(inverse)
# n = number of events we wish to account for ( How many times is event going to happen?)
# t = total number of events outcomes ( coin toss has 2 possible event outcomes)
# We'll make this in a separate function under the hood => (total possible events/outcomes) = tp
import math
import numpy as np
class BinomDist(object):
def __init__(self, total_outcomes, number_of_events, probability_of, inverse_probability_of):
self.t = total_outcomes #how many times we flip the coin
self.n = number_of_events
self.pk = probability_of
self.pki = inverse_probability_of
self.tk = None
self.comb = None
def test_init(self):
self.find_tk()
self.find_factorial_comb()
attributes = [self.t, self.n, self.pk, self.pki, self.tk, self.comb]
names = ['total_outcomes=t', 'number_of_events=n', 'probability_of=pk', 'inverse_probability_of=pki', 'total_possible=tk', 'combitoral=comb']
i = 0
for item in attributes:
name = names[i]
try:
float(item)
except:
print("ERROR with: ", name)
print("Was unable to convert attribute to a float.")
i += 1
print("Initiate values test complete")
def find_tk(self):
tk = self.t ** self.n
self.tk = tk
print("BinomDist tk set to: ", self.tk)
def find_factorial_comb(self):
t_fact = math.factorial(self.t)
n_fact = math.factorial(self.n)
t = self.t
n = self.n
comb = t_fact / (n_fact * math.factorial(t-n))
print("BinomDist comb set to: ", comb)
self.comb = comb
def find_binomial_distribution(self):
self.find_tk()
self.find_factorial_comb()
# the hairy bits
f_of_x = self.comb * (self.pk ** self.n) * (self.pki**(self.t-self.n))
print("the function of x = ", f_of_x)
def test1():
# attributes: BinomDist(total outcomes, event seeked, probability of event, inverse of probablility of event)
new = BinomDist(5, 2, 0.5, 0.5)
new.test_init()
def test2():
new = BinomDist(5, 2, 0.5, 0.5)
#new.test_init()
answer = new.find_binomial_distribution()
return answer
def test3():
""" Test against the Excel formula """
# six sided die, result 3
six_die = BinomDist(6, 3, .166, .833)
# Something that we repeat 10 times and want 5 successful results.
# probability of that result is 0.1 or 10% so the inverse is .9 or 90%
new = BinomDist(10, 5, .1, .9)
mine = new.find_binomial_distribution()
test3()
Comments
Post a Comment