| Total Complexity | 6 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | #!/usr/bin/env python3 |
||
| 2 | """ |
||
| 3 | Matmod exercise 02 |
||
| 4 | |||
| 5 | ported to python by https://github.com/SpaceLenore |
||
| 6 | """ |
||
| 7 | |||
| 8 | import math |
||
| 9 | |||
| 10 | def truncate(f, n): |
||
| 11 | return math.floor(f * 10 ** n) / 10 ** n |
||
| 12 | |||
| 13 | def factorial(n): |
||
| 14 | """ Calculate the factorial of the integer n """ |
||
| 15 | val = 1 |
||
| 16 | for value in range(1, n + 1): |
||
| 17 | val = val * value |
||
| 18 | return val |
||
| 19 | |||
| 20 | def bin(trials, population, probability): |
||
| 21 | """ Calculate the probability binomial distribution Pr(X = x) """ |
||
| 22 | # x = trials |
||
| 23 | # n = population |
||
| 24 | # p = probability |
||
| 25 | bern = ((1 - probability)**(population-trials))*probability**trials |
||
| 26 | bincof = factorial(population) / (factorial(trials) * factorial(population - trials)) |
||
| 27 | binpro = bern * bincof |
||
| 28 | return round(binpro, 3) |
||
| 29 | |||
| 30 | def cumulative_bin(trials, population, probability): |
||
| 31 | """ Calculate the probability binomial distribution Pr(X <= x) """ |
||
| 32 | # x = trials |
||
| 33 | # n = population |
||
| 34 | # p = probability |
||
| 35 | sumtotal = 0; |
||
| 36 | for xval in range(trials + 1): |
||
| 37 | sumtotal += bin(xval, population, probability) |
||
| 38 | return round(sumtotal, 3) |
||
| 39 |