# Day 4 - Binomial and geometric distributions - Blog
Data Science for Political and Social Phenomena
Author: Guillaume Redoulès

# Day 4 - Binomial and geometric distributions

## Binomial distribution

### Problem 1

The ratio of boys to girls for babies born in Russia is \(r=\frac{N_b}{N_g}=1.09\). If there is 1 child born per birth, what proportion of Russian families with exactly 6 children will have at least 3 boys?

### Mathematical explanation

Let's first compute the probability of having a boy :

$$p_b=\frac{N_b}{N_b+N_g}$$

where: * \(N_b\) is the number of boys * \(N_g\) is the number of girls * \(r=\frac{N_b}{N_g}\)

$$p_b=\frac{1}{1+\frac{1}{r}}$$
$$p_b=\frac{r}{r+1}$$

    r = 1.09
    p_b=r/(r+1)

    print(f"The probability of having a boy is p={p_b:3f}")

    The probability of having a boy is p=0.521531

The probability of getting 3 boys in 6 children is given by :

$$b(x=3, n=6, p=p_b)$$

In order to compute the proportion of Russian families with exactly 6 children will have at 3 least boys we need to compute the cumulative probability distribution

$$b(x\geq 3, n=6, p=p_b) = \sum_{i=3}^{6} b(x\geq i, n=6, p=p_b)$$

### Let's code it !

    import math

    def bi_dist(x, n, p):
        b = (math.factorial(n)/(math.factorial(x)*math.factorial(n-x)))*(p**x)*((1-p)**(n-x))
        return(b)

    b, p, n = 0, p_b, 6
    for i in range(3,7):
        b += bi_dist(i, n, p)
    print(f"probability of getting at least 3 boys in a family with exactly 6 children : {b:.3f}")

    probability of getting at least 3 boys in a family with exactly 6 children : 0.696

### Problem 2

A manufacturer of metal pistons finds that, 12% on average, of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of 10 pistons will contain: * No more than 2 rejects? * At least 2 rejects?

### Mathematical explanation

On average 12% of the pistons are rejected, this means that a piston has a probability of \(p_{rejected}=0.12\) to be rejected.

The probability of getting less than 2 faulty pistons in a batch is :

$$p(rejet<2) = b(x\leq 2, n= 10, p=p_{rejected})$$
$$p(rejet<2) = \sum_{i=0}^{2} b(x\leq i, n=10, p=p_{rejected})$$

    b, p, n = 0, 12/100, 10
    for i in range(0,3):
        b += bi_dist(i, n, p)
    print(f"The probability of getting less than 2 faulty pistons in a batch is : {b:.3f}")

    The probability of getting less than 2 faulty pistons in a batch is : 0.891

The probability that a batch of 10 pistons will contain at least 2 rejects :

$$p(rejet<2) = b(x\geq 2, n= 10, p=p_{rejected})$$
$$p(rejet<2) = \sum_{i=2}^{10} b(x\geq i, n=10, p=p_{rejected})$$

    b, p, n = 0, 12/100, 10
    for i in range(2,11):
        b += bi_dist(i, n, p)
    print(f"The probability of getting at least 2 faulty pistons in a batch is : {b:.3f}")

    The probability of getting at least 2 faulty pistons in a batch is : 0.342

## Geometric distribution

### Problem 1

The probability that a machine produces a defective product is \(\frac{1}{3}\). What is the probability that the first defect is found during the fith inspection?

### Mathematical explanation

In this case, we will use a geometric distribution to evaluate the probability : * \(n=5\) * \(p=\frac{1}{3}\)

Hence, the probability that the first defect is found during the fith inspection is \(g(n=5,p=1/3)\)

    print(f"The probability that the first defect is found during the fith inspection is {round(((1-p)**(n-1)) * p, 3)}")

    The probability that the first defect is found during the fith inspection is 0.038

### Problem 2

The probability that a machine produces a defective product is \(\frac{1}{3}\). What is the probability that the first defect is found during the first 5 inspections?

### Mathematical explanation

In this problem, we need to compute the cumulative distribution function

$$p(x \leq5) = \sum_{i=1}^{5} g(n=i,p=1/3)$$

    p_x5 = 0
    p=1/3
    n=5
    for i in range(1,n+1):
        p_x5+=(1-p)**(i-1) * p

    print(f"The probability that the first defect is found during the first 5 inspection is {round(p_x5, 3)}")

    The probability that the first defect is found during the first 5 inspection is 0.868

### Find an error or bug? Have a suggestion?

Everything on this site is avaliable on GitHub. Head on over and submit an issue. You can also message me directly by email.