practice – Problem 2
Copyright By PowCoder代写 加微信 powcoder
import numpy as np
from scipy.stats import poisson, uniform
## record your uni here
Problem Statement¶
Suppose the number of accidents on a highway on a given day follows a Poisson with distribution with parameter $\lambda$, depending on weather conditions.
85\% of days have good conditions
10\% of days have rain
5\% of days have snow
If a day has good conditions, the number of accidents is expected to be 0.8. If a day has rain, the number of accident is expected to be 2, and if it is snowing, we expect to have 4 accidents.
Part I (25 points)¶
Write a function accidents(n) that generates a sample of size from the distribution of daily accidents.
Using $10,000$ simulations, calculate the probability that we have (strictly) greater than $4$ accidents in a day, and a $95\%$ confidence for this probability.
For the confidence interval, feel free to use the normal critical value $z_{.025}$ instead of $t_{.025, 9,999}$
np.random.seed(42)
n = 10_000
def accidents(n):
U = uniform.rvs(size=n)
X = np.zeros(n)
for i in range(n):
if U[i] <= 0.85:
elif U[i] <= 0.95:
X[i] = poisson.rvs(lam)
X = accidents(n)
# probability x > 4
mu_p = np.mean(Y)
sig = np.std(Y)
ci_low = mu_p – 1.96 * sig / np.sqrt(n)
ci_up = mu_p + 1.96 * sig / np.sqrt(n)
print(mu_p, ci_low, ci_up)
0.0265 0.02335191111307193 0.02964808888692807
Part II (10 points)¶
If there are 3 accidents in a day, what is the probability it was snowing?
Hint: modify the function accidents to also return the type of day it was.
np.random.seed(42)
def accidents(n):
U = uniform.rvs(size=n)
X = np.zeros(n)
lam = np.zeros(n)
for i in range(n):
if U[i] <= 0.85:
lam[i] = 0.8
elif U[i] <= 0.95:
lam[i] = 2
X[i] = poisson.rvs(lam[i])
return X, lam
X, lam = accidents(n)
loc = X == 3
p_3 = np.mean(lam[loc] == 4)
print(p_3)
0.1348122866894198
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com