Test Failed
Push — master ( 851a09...4c58a5 )
by Lenore
02:03
created

exercises02.bin()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 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