<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Assignment 3
Lorenzo Biasi and Michael Aichmüller
Step1:  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 %
Step2:  b.
The same of before just that this time $k \in [5, 9]$. The probability is 64%
Step3:  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%.
Step4:  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
Step5:  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
<ASSISTANT_TASK:>
Python Code:
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)
Explanation: Assignment 3
Lorenzo Biasi and Michael Aichmüller
End of explanation
p  = 4. / 100
np.sum(binomial(p, 150, np.arange(5)))
Explanation: 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 %
End of explanation
np.sum(binomial(p, 150, np.arange(5, 10)))
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)')
Explanation: b.
The same of before just that this time $k \in [5, 9]$. The probability is 64%
End of explanation
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)
Explanation: 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%.
End of explanation
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)
Explanation: 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:
End of explanation
g = binomial(1 / 6, 3, np.arange(4)) * np.array([-30, 50, 75, 100])
np.sum(g)
plt.bar(np.arange(4), g)
plt.plot([-.5, 3.5], np.ones(2) * np.sum(g), 'r')
Explanation: 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(€).
End of explanation
<END_TASK>