# Day 5 - Poisson and Normal distributions - Blog
Data Science for Political and Social Phenomena
Author: Guillaume Redoulès

# Day 5 - Poisson and Normal distributions

## Poisson Distribution

### Problem 1

A random variable, \(X\), follows Poisson distribution with mean of 2.5. Find the probability with which the random variable \(X\) is equal to 5.

### Mathematical explanation

In this case, the answer is straightforward, we just need to compute the value of the Poisson distribution of mean 2.5 at 5:

$$P(\lambda = 2.5, x=5)=\frac{\lambda^ke^{-\lambda}}{k!}$$
$$P(\lambda = 2.5, x=5)=\frac{2.5^5e^{-2.5}}{5!}$$

    def factorial(k):
        return 1 if k == 1 else k * factorial(k-1)

    from math import exp

    def poisson(l,k):
        return (l**k * exp(-l)) / factorial(k)

    l = 2.5
    k = 5

    print(f'Probability that a random variable X following a Poisson distribution of mean {l} equals {k} : {round(poisson(l,k),3)}')

    Probability that a random variable X following a Poisson distribution of mean 2.5 equals 5 : 0.067

### Problem 2

The manager of a industrial plant is planning to buy a machine of either type \(A\) or type \(B\). For each day’s operation:

- The number of repairs, \(X\), that machine \(A\) needs is a Poisson random variable with mean 0.88. The daily cost of operating \(A\) is \(C_A=160+40X^2\).
- The number of repairs, \(Y\), that machine \(B\) needs is a Poisson random variable with mean 1.55. The daily cost of operating \(B\) is \(C_B=128+40Y^2\).

Assume that the repairs take a negligible amount of time and the machines are maintained nightly to ensure that they operate like new at the start of each day. What is the expected daily cost for each machine.

### Mathematical explanation

The cost for each machine follows a law that is the square of a Poisson distribution.

$$C_Z = a + b*Z^2$$

Since the expectation is a linear operator :

$$E[C_Z] = aE[1] + bE[Z^2]$$

Knowing that \(Z\) follows a Poisson distribution of mean \(\lambda\) we have :

$$E[C_Z] = a+ b(\lambda + \lambda^2)$$

    averageX = 0.88
    averageY = 1.55

    CostX = 160 + 40*(averageX + averageX**2)
    CostY = 128 + 40*(averageY + averageY**2)

    print(f'Expected cost to run machine A : {round(CostX, 3)}')
    print(f'Expected cost to run machine A : {round(CostY, 3)}')

    Expected cost to run machine A : 226.176
    Expected cost to run machine A : 286.1

## Normal Distribution

### Problem 1

In a certain plant, the time taken to assemble a car is a random variable, \(X\), having a normal distribution with a mean of 20 hours and a standard deviation of 2 hours. What is the probability that a car can be assembled at this plant in:

Less than 19.5 hours? Between 20 and 22 hours?

### Mathematical explanation

\(X\) is a real-valued random variable following a normal distribution : the probability of assembly the car in less than 19.5 hours is the cumulative distribution function of X evaluated at 19.5:

$$P(X\leq 19.5)=F_X(19.5)$$

For a normal distribution, the cumulative distribution function is :

$$\Phi(x)=\frac{1}{2}\left(1+erf\left(\frac{x-\mu}{\sigma\sqrt{2}}\right)\right)$$

    import math

    def cumulative(x,mean,sd):
        return 0.5*(1+math.erf((x-mean)/(sd*math.sqrt(2))))

    mean = 20
    sd = 2
    print(f'Probability that the car is built in less than 19.5 hours : {round(cumulative(19.5,mean,sd),3)}')

    Probability that the car is built in less than 19.5 hours : 0.401

Similarly, the probability that a car is built between 20 and 22hours can be computed thanks to the cumulative density function:

$$P(20\leq x\leq 22) = F_X(22)-F_X(20)$$

    print(f'Probability that the car is built between 20 and 22 hours : {round(cumulative(22,mean,sd)-cumulative(20,mean,sd),3)}')

    Probability that the car is built between 20 and 22 hours : 0.341

### Problem 2

The final grades for a Physics exam taken by a large group of students have a mean of \(\mu=70\) and a standard deviation of \(\sigma=10\). If we can approximate the distribution of these grades by a normal distribution, what percentage of the students: * Scored higher than 80 (i.e., have a \(grade \gt 80\)))? * Passed the test (i.e., have a \(grade \gt 60\))? * Failed the test (i.e., have a \(grade \lt 60\))?

### Mathematical explanation

Here again, we need to appy the cumulative density function to get the probabilities :

Probability that they scored higher than 80 :

$$P(X\gt80) = 1- P(X\lt80)$$
$$P(X\gt80) = 1- F_X(80)$$

    mean = 70
    sd = 10
    print(f'Probability that the the student scored higher than 80 : {round(1- cumulative(80,mean,sd),3)}')

    Probability that the the student scored higher than 80 : 0.159

Probability that they passed the test :

$$P(X\gt60) = 1- P(X\lt60)$$
$$P(X\gt80) = 1- F_X(60)$$

    print(f'Probability that the the student passed the test : {round(1- cumulative(60,mean,sd),3)}')

    Probability that the the student passed the test : 0.841

Probability that they failed :

$$P(X\lt60) = F_X(60)$$

    print(f'Probability that the student failed the test: {round(cumulative(60,mean,sd),3)}')

    Probability that the student failed the test: 0.159

### 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.