# Assignment 3

Lorenzo Biasi and Michael Aichmüller



In [1]:

import numpy as np
from scipy.special import binom
import matplotlib.pylab as plt
from scipy.misc import factorial as fact
%matplotlib inline

def binomial(p, n, k):
return binom(n, k) * p ** k * (1 - p) ** (n-k)



### Exercise 1.

#### a.

$\Omega$ will be all the possible combinations we have for 150 object two have two diffent values. For example (0, 0, ..., 0), (1, 0, ..., 0), (0, 1, ..., 0), ... (1, 1, ..., 0), ... (1, 1, ..., 1). This sample space has size of $2^{150}$. The random variable $X(\omega)$ will be the number of defective objects there are in the sample $\omega$. We can also define $Y(\omega) = 150 - X(\omega)$, that will be counting the number of checked items.

#### b.

The binomial distribution is the distribution that gives the probability of the number of "succeses" in a sequence of random and indipendent boolean values. This is the case for counting the number of broken object in a group of 150 and the probability of being broken of 4%.

#### c.

For computing the probability that at most 4 objects are broken we need to sum the probabilities that $k$ objects are broken with $k \in [0, 4]$. $P(<5) = \sum_{k=0}^{4} P(X=k) = \sum_{k=0}^{4} {4\choose k}p^k(1-p)^{4-k}$ The probability is 28 %



In [2]:

p  = 4. / 100
np.sum(binomial(p, 150, np.arange(5)))




Out[2]:

0.27959262672536833



#### b.

The same of before just that this time $k \in [5, 9]$. The probability is 64%



In [3]:

np.sum(binomial(p, 150, np.arange(5, 10)))




Out[3]:

0.64069735753998025




In [4]:

plt.bar(np.arange(20), binomial(p, 150, np.arange(20)))
plt.bar(np.arange(5), binomial(p, 150, np.arange(5)))
plt.bar(np.arange(5, 10), binomial(p, 150, np.arange(5,10)))
plt.xlabel('# defectives')
plt.ylabel('P(X=k)')




Out[4]:

<matplotlib.text.Text at 0x7f3f4caa95c0>



### Exercise 2.

For computing how big $q$ needs to be we can compute the probability $p^*$ that nobody has the same birthday in a group of $q$ and compute $1 - p^*$. The first two people will not have the same birthday with probability of $364/365$, the probability that the third will also have a different birthday will be $364/365 * 363 / 365$. this will go on until the last person. One can make the computation and finds that the minimum for having over 50% of probability that at least two people have the same birthday is 23 with p = 50.73%.



In [5]:

def not_same_birthday(q):
return np.prod((365 - np.arange(q))/ 365)

q = 45
p = np.empty(q - 1)
for i in range(1, q):
p[i - 1] = 1 - not_same_birthday(i)
plt.plot(np.arange(1, q), p)
plt.plot(23, 1 - not_same_birthday(23), 'r+', label='23 people')
plt.grid()
plt.ylabel('Probability')
plt.xlabel('q')
plt.legend()
1 - not_same_birthday(23)




Out[5]:

0.50729723432398566



### Exercise 3.

#### a.

Let's define $\Omega$ as all the possible combination we can have with 3 throws of a 6-faced dice. $\Omega$ will be then:



In [6]:

import itertools
x = [1, 2, 3, 4, 5, 6]
omega = set([p for p in itertools.product(x, repeat=3)])
print(r'Omega has', len(omega), 'elements and they are:')
print(omega)




Omega has 216 elements and they are:
{(4, 2, 2), (1, 4, 4), (2, 2, 4), (5, 5, 1), (5, 2, 1), (1, 4, 2), (5, 5, 3), (3, 1, 6), (5, 2, 3), (5, 5, 5), (3, 1, 4), (2, 6, 5), (3, 2, 2), (4, 1, 5), (3, 1, 2), (2, 6, 3), (6, 5, 5), (2, 5, 3), (4, 4, 2), (1, 2, 2), (6, 6, 3), (2, 6, 1), (6, 5, 3), (3, 2, 6), (2, 5, 1), (4, 6, 1), (4, 1, 1), (1, 2, 4), (6, 6, 1), (5, 3, 2), (1, 5, 5), (6, 5, 1), (3, 2, 4), (4, 6, 3), (4, 1, 3), (1, 2, 6), (2, 5, 5), (4, 6, 5), (1, 3, 5), (6, 3, 1), (4, 3, 6), (6, 6, 5), (5, 3, 6), (1, 5, 1), (3, 4, 5), (2, 3, 4), (1, 3, 3), (6, 3, 3), (4, 3, 4), (5, 6, 2), (5, 3, 4), (1, 5, 3), (2, 3, 6), (1, 3, 1), (6, 3, 5), (4, 3, 2), (5, 6, 4), (6, 4, 4), (3, 3, 1), (5, 1, 5), (3, 4, 1), (6, 2, 6), (5, 6, 6), (6, 4, 6), (3, 3, 3), (3, 4, 3), (2, 3, 2), (6, 2, 4), (6, 1, 2), (3, 3, 5), (2, 4, 2), (5, 1, 1), (3, 6, 6), (6, 2, 2), (5, 4, 3), (1, 6, 4), (6, 4, 2), (5, 1, 3), (2, 4, 4), (3, 6, 4), (4, 5, 2), (6, 1, 6), (5, 4, 1), (1, 6, 6), (2, 4, 6), (3, 6, 2), (2, 1, 5), (4, 5, 4), (4, 2, 5), (6, 1, 4), (3, 5, 2), (2, 2, 3), (4, 5, 6), (2, 1, 3), (5, 4, 5), (1, 6, 2), (2, 2, 1), (4, 4, 5), (2, 1, 1), (4, 2, 1), (1, 1, 2), (5, 2, 4), (3, 5, 6), (4, 4, 3), (4, 2, 3), (1, 1, 4), (5, 2, 6), (3, 5, 4), (2, 2, 5), (4, 4, 1), (1, 1, 6), (1, 4, 5), (5, 2, 2), (1, 4, 3), (5, 5, 2), (3, 1, 5), (2, 6, 6), (6, 5, 6), (1, 4, 1), (4, 1, 4), (5, 5, 4), (3, 1, 3), (2, 6, 4), (6, 5, 4), (3, 2, 3), (2, 5, 2), (4, 1, 6), (1, 2, 1), (5, 5, 6), (3, 1, 1), (2, 6, 2), (6, 5, 2), (3, 2, 1), (1, 2, 3), (6, 6, 2), (5, 3, 3), (2, 5, 6), (4, 1, 2), (1, 2, 5), (5, 3, 1), (1, 5, 4), (3, 2, 5), (2, 5, 4), (4, 6, 2), (1, 3, 6), (6, 6, 6), (1, 5, 6), (3, 4, 4), (4, 6, 4), (1, 3, 4), (6, 3, 2), (5, 6, 1), (6, 6, 4), (5, 3, 5), (3, 4, 6), (2, 3, 5), (4, 6, 6), (1, 3, 2), (6, 3, 4), (4, 3, 5), (5, 6, 3), (6, 4, 5), (1, 5, 2), (6, 3, 6), (4, 3, 3), (5, 6, 5), (5, 1, 4), (2, 4, 1), (3, 4, 2), (2, 3, 1), (6, 2, 5), (4, 3, 1), (6, 4, 1), (3, 3, 2), (2, 4, 3), (5, 1, 6), (2, 3, 3), (6, 2, 3), (6, 1, 3), (5, 4, 2), (6, 4, 3), (3, 3, 4), (2, 4, 5), (4, 5, 1), (2, 1, 6), (6, 2, 1), (6, 1, 1), (1, 6, 5), (3, 3, 6), (5, 1, 2), (3, 6, 5), (2, 1, 4), (4, 5, 3), (5, 4, 6), (3, 5, 3), (3, 6, 3), (2, 1, 2), (4, 5, 5), (4, 2, 4), (1, 1, 1), (6, 1, 5), (5, 4, 4), (1, 6, 1), (3, 5, 1), (2, 2, 2), (4, 4, 6), (3, 6, 1), (4, 2, 6), (1, 1, 3), (1, 6, 3), (4, 4, 4), (1, 1, 5), (5, 2, 5), (1, 4, 6), (3, 5, 5), (2, 2, 6)}



X would be -30 when the sample $\omega$ has no 6s, 50 when has one, 75 when it has two, and 100 when it has three. The probability distribution of such variable would be the binomial with $p = 1 / 6$, $n=3$ and $k$ the number of 6s.

So:

$P_X(X = -30) = {3\choose 0}(1 / 6)^0(1-1/6)^{3-0}$

$P_X(X = 50) = {3\choose 1}(1 / 6)^1(1-1/6)^{3-1}$

$P_X(X = 75) = {3\choose 2}(1 / 6)^2(1-1/6)^{3-2}$

$P_X(X = 100) = {3\choose 3}(1 / 6)^3(1-1/6)^{3-3}$

#### b.

I would be part of this competition, in fact if calculate the mean of $X$ as suggested we obtain $\approx$ 5.67(€).



In [7]:

g = binomial(1 / 6, 3, np.arange(4)) * np.array([-30, 50, 75, 100])
np.sum(g)




---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-ca5408d1eb9b> in <module>()
----> 1 np.sum(g)

NameError: name 'g' is not defined




In [ ]:

plt.bar(np.arange(4), g)
plt.plot([-.5, 3.5], np.ones(2) * np.sum(g), 'r')