Hints for Lab 3: Classical Inference I: Maximum Likelihood Estimation#
For the class on Monday, January 22nd
See also
Go back to Lab 3
A. Coin Flips#
Hints for the implementation of Question 1
From class we obtained the expression for \(\sigma_p\) in terms of \(N\) and \(K\):
\[ \sigma_p = \sqrt{\frac{K(N-K)}{N^3}} \]We can then rewrite the inequality as \(\hat{p} - 2 \sigma_p > 0.5 \). Plug in the corresponding expressions for \(\hat{p}\) and \(\sigma_p\) such that the left-hand side of the inequality only depends on \(N\) and \(K\).
Here’s a (not efficient, but easy to understand) way to find the minimal value of \(K\) for the inequality to hold.
import numpy as np N = 100 for K in range(1, N+1): if ... > 0.5: # TODO: replace ... with the expression of \hat{p} - 2 \sigma_p but in terms of N and K print(K) break
Hints for the implementation of Question 2
To simulate the number of heads for 100 coin flips, you can simply run
import numpy as np n_flips = 100 # number of coin flips p_heads = 0.5 # heads probability n_heads = np.random.default_rng().binomial(n_flips, p_heads) print(n_heads)
Note that each time you run the code above,
n_heads
has a different value.The above would be one “simulation”. Repeat this simulation 10,000 time and collect the
n_heads
from each simulation.You can then use
np.percentile
to calculate the percentile corresponding to \(2\sigma\) among the 10,000n_heads
values.
B. Estimator Properties#
Hints for “How to verify” 1
Here’s a snippet to get you started with the code structure:
import numpy as np a = ... # TODO: set the true value of a b = ... # TODO: set the true value of b N = ... # TODO: set the number of x's x = np.random.default_rng().uniform(a, b, size=N) a_mle = np.min(x) # this would be \hat{a} b_mle = np.max(x) # this would be \hat{b} # TODO: repeat the above with different values of N and see how a_mle and b_mle change as N increases
Hints for “How to verify” 2
Here’s a snippet to get you started with the code structure:
a = ... # TODO: set the true value of a b = ... # TODO: set the true value of b N = ... # TODO: set the number of x's K = ... # TODO: set the number of simulations a_mle_avg = 0 b_mle_avg = 0 for _ in range(K): x = np.random.default_rng().uniform(a, b, size=N) a_mle_avg += np.min(x) b_mle_avg += np.max(x) a_mle_avg /= K # this would be $E(\hat{a})$ b_mle_avg /= K # this would be $E(\hat{b})$ # TODO: repeat the above with different values of K and see how a_mle_avg and b_mle_avg change as K increases