-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinomial.py
46 lines (37 loc) · 1.63 KB
/
binomial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import math
from combination import CombinationCalc
class BinomialCalc(object):
def binomial_calc(self, success_prob, trials, successes):
result = []
comb = CombinationCalc()
if success_prob < 0 or trials < 0 or successes < 0:
print 'None of the numbers can be negative.'
return
elif successes > trials:
print 'The number of success cannot be larger than the number of trials.'
return
else:
try:
#X = number of successes
equal = (comb.calc_combination(trials, successes)) * math.pow(success_prob,successes) * math.pow(1 - success_prob, trials - successes)
result.append(equal)
#X < number of success
less = 0
for i in range(int(successes)):
less += (comb.calc_combination(trials, i)) * math.pow(success_prob, i) * math.pow(1 - success_prob, trials - i)
result.append(less)
#X <= number of successes
result.append(less + equal)
#X > number of successes
result.append(1 - (less + equal))
#X >= number of sucesses
result.append(equal + (1 - (less + equal)))
#round all numbers to fourth decimal place
for i in range(len(result)):
result[i] = round(result[i], 4)
except:
print 'An error occurred. Enter only numbers for the parameters. Binomial'
return
return result
# bin = BinomialCalc()
# print bin.binomial_calc(0.5, 5.0, 3)