Skip to content

Instantly share code, notes, and snippets.

@luckythandel
Created January 23, 2022 09:18
Show Gist options
  • Save luckythandel/476d15257fed1a2540cd988e2d60741e to your computer and use it in GitHub Desktop.
Save luckythandel/476d15257fed1a2540cd988e2d60741e to your computer and use it in GitHub Desktop.
binomial distribution in python. finds Theoretical frequency , mean, p, q, total(f, fx), p(r), N*p(r)
import math
def mean(xf, f):
return xf/f
def mul(arr1, arr2):
mul_arr = []
for i in range(0, len(arr1)):
mul_arr.append(arr1[i]*arr2[i])
return mul_arr
def probability(n, mean):
return mean/n
def p_of_r(n, p, q, t_f):
result = []
max_n = n
min_n = 0
for i in range(n, -1, -1):
result.append(t_f*(math.comb(n, min_n)*(p**min_n)*(q**max_n)))
max_n -=1
min_n +=1
return result
def total(arr):
total = 0
for i in range(0, len(arr)):
total+=arr[i]
return total
def main():
print("@LuckyThandel")
n = int(input("Enter the `n`: "))
x_arr = []
f_arr = []
#print("[+] Enter all x values: ")
for i in range(0, n+1):
x_arr.append(i)
print("[+] Enter all f values: ")
for i in range(0, n+1):
f_arr.append(int(input("Value: ")))
total_f_arr = total(f_arr)
mul_fx_arr = mul(x_arr, f_arr)
total_fx_arr = total(mul_fx_arr)
print("Total_f", total_f_arr)
print("Total_fx", total_fx_arr)
mean_x = mean(total_fx_arr, total_f_arr)
print("mean:", mean_x)
p = probability(n, mean_x)
q = 1 - p
print("P:", p)
print("Q:", q)
np_r = p_of_r(n, p, q, total_f_arr)
print("N*p(r):", np_r)
theor_fre = [round(digit) for digit in np_r]
print("Therortical Frequency:", theor_fre)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment