With n number of trials and p probability of successes, I need to find the probability of having less than or equal to x successes
I have built a function to find x number of successes.
I have tried to use this function with MAP
def bin_dist(n, p, x):
if x > n:
raise ValueError('the number of success cannot be greater than the number of trials')
else:
def fact2(x):
if x < 0:
return ValueError("number of successes cannot be less than 1")
else:
fact = 1
for i in range(1, x + 1):
fact = fact * i
return fact
def combination2(n, x):
return(fact2(n)/fact2(n-x)*fact2(x))
print("The probability of x successes, followed by the total number of arrangements/possible combinations used to find x:")
print(combination2(n, x) * (p)**(x) * ((1-p)**(n-x)))
def bin_cdf(n, p, x):
li = range(0, x)
print map(bin_dist, li)
return map(bin_dist, li)
bin_cdf(3, 0.5, 2)
Unfortunately, I either receive an invalid syntax error or simply a MAP object
File "<ipython-input-1-e333d90fddea>" , line 21
print map (bin_dist, li)
^
SyntaxError : invalid syntax
I have also been informed the function used to return p of x successes, bin_dist, is missing two positional arguments when converting MAP to a list
----> 3 print ( list ( map ( bin_dist , li ) ) )
4 return ( list ( map ( bin_dist , li ) ) )
5
TypeError : bin_dist () missing 2 required positional arguments: 'p' and 'x'
I have also tried iterating from 0 to x and finding the sum of all values, but seem to be finding dubious results
def bin_cdf(n, p, x):
if x > n:
raise ValueError('the number of success cannot be greater than the number of trials')
else:
def fact2(x):
if x < 0:
return ValueError("number of successes cannot be less than 1")
else:
fact = 1
for i in range(1, x + 1):
fact = fact * i
return fact
def combination2(n, x):
return(fact2(n)/(fact2(n-x)*fact2(x)))
bin_dist = (combination2(n, x) * (p)**(x) * ((1-p)**(n-x)))
li = range(0, x)
bin_cdf = sum([element * bin_dist for element in li])
print(bin_cdf)
return bin_cdf
bin_cdf(3, 0.5, 3)
Out: 0.375
The stack overflow community appears, at least in my humble view, the brightest on the internet. Any advice much recommended