Monte Carlo Simulation and Resampling Methods
Massive Data Institute, Georgetown University
February 16, 2022
The goal of MDI workshops
MDI Workshop Spring
beta
, chisquare
, dirichlet
, logistic
, multinomial
, multivariate_normal
, negative_binomial
, poisson
, standard_t
, etc.rpois
, rmvnorm
, rnbinom
, rbeta
, rchisq
, rexp
, rgamma
, rlogis
, rstab
, rt
, rgeom
, rhyper
, rwilcox
, rweibull
, etc.Suppose g(x) is difficult to express or manipulate but for which we could generate samples on an arbitrary support of interest:
A common quantity of interest is
A substitute for analytically calculating this is to randomly generate
The idea is to replace analytical integration with summartion from a large number of simulated values, rejecting (ignoring) values outside of
By the law of large numbers,
Although
Normal PDF over a specific region, suppose we want:
In Python:
\begin{equation*} \int_{-\infty}^{-1.96}\mathcal{N}(x;0,1); \int_{1.96}^{\infty}\mathcal{N}(x;0,1) \end{equation*}
n_sim = 5000; n_sample = 2000
alpha = 1.71; beta = 5.6 #truth
est_alpha = []; se_alpha = []; est_beta = []; se_beta = []
for i in range(n_sim):
eps = np.random.normal(0, 1, n_sample)
x1 = np.random.uniform(0, 1, n_sample)
y = (alpha + beta * x1 + eps)
X = sm.add_constant(x1.reshape((-1, 1)))
model = sm.OLS(y.reshape((-1, 1)), X)
ols_result = model.fit()
coefs = ols_result.params
ses = ols_result.bse
est_alpha.append(coefs[0]); est_beta.append(coefs[1])
se_alpha.append(ses[0]); se_beta.append(ses[1])
def coverage(beta, se, truth, df, level=0.95,):
conf_level = level + (1 - level)/2
lower = np.array(beta) - qt.ppf(conf_level, df = df)*np.array(se)
upper = np.array(beta) + qt.ppf(conf_level, df = df)*np.array(se)
covered = np.where((truth >= lower) & (truth <= upper), 1, 0)
cover_prob = np.mean(covered)
conf_int = np.column_stack((lower, upper))
return cover_prob
n_sim = 100; n_sample = 2000; alpha = 1.71; beta = 5.6
est_alpha = []; se_alpha = []; est_beta = []; se_beta = []
for i in range(n_sim):
eps = np.random.normal(0, 1, n_sample)
x1 = np.random.uniform(0, 1, n_sample)
y = (alpha + beta * x1 + eps)
X = sm.add_constant(x1.reshape((-1, 1)))
model = sm.OLS(y.reshape((-1, 1)), X)
ols_result = model.fit()
coefs = ols_result.params; ses = ols_result.bse
est_alpha.append(coefs[0]); est_beta.append(coefs[1])
se_alpha.append(ses[0]); se_beta.append(ses[1])
coverage(est_beta, se_beta, truth = beta, df = n_sample-1-1)
0.95
Model | Variable | MAE | MSE | Coverage |
---|---|---|---|---|
Homoskedastic | Alpha | 0.0353149 | 0.0019719 | 0.9554 |
Homoskedastic | Beta | 0.0618423 | 0.0059748 | 0.9524 |
Heteroskedastic | Alpha | 0.0651124 | 0.0067536 | 0.7170 |
Heteroskedastic | Beta | 0.1737809 | 0.0473037 | 0.5226 |
\begin{align*}
Pr(Y = 1|X) &= logit^{-1}(\mathbf{X} \beta)\\
&= \frac{exp(\mathbf{X} \beta)}{1 + exp(\mathbf{X} \beta)}
\end{align*}
n_sim = 100; n_sample = 2000
alpha = 1.71; beta = 5.6 #truth
est_alpha = []; se_alpha = []; est_beta = []; se_beta = []
for i in range(n_sim):
x1 = np.random.uniform(0, 1, n_sample)
y = np.random.binomial(n = 1, p = inv_logit(alpha + beta*x1), size = n_sample)
X = sm.add_constant(x1.reshape((-1, 1)))
model = sm.Logit(y.reshape((-1, 1)), X)
logit_result = model.fit()
coefs = logit_result.params; ses = logit_result.bse
est_alpha.append(coefs[0]); est_beta.append(coefs[1])
se_alpha.append(ses[0]); se_beta.append(ses[1])
est_alpha.append(coefs[0]); est_beta.append(coefs[1])
se_alpha.append(ses[0]); se_beta.append(ses[1])
\begin{equation*} E(Y|X) = exp(\mathbf{X} \beta) \end{equation*}
Instrument variable model
Two Stage Least Squares (2SLS)
Number | Treatment | Outcome |
---|---|---|
1 | Treated | 4 |
2 | Control | 7 |
3 | Treated | 8 |
4 | Control | 3 |
5 | Treated | 7 |
6 | Control | 3 |
7 | Control | 2 |
8 | Treated | 5 |
Number | Treatment | Outcome | Random 1 | Random 2 | Random 3 | Random 100 | |
---|---|---|---|---|---|---|---|
1 | Treated | 4 | Control | Treated | Treated | ... | Control |
2 | Control | 7 | Control | Treated | Treated | ... | Control |
3 | Treated | 8 | Treated | Control | Control | ... | Treated |
4 | Control | 3 | Treated | Control | Control | ... | Treated |
5 | Treated | 7 | Control | Control | Control | ... | Control |
6 | Control | 3 | Control | Treated | Treated | ... | Treated |
7 | Control | 2 | Treated | Treated | Control | ... | Control |
8 | Treated | 5 | Treated | Control | Treated | ... | Treated |
ATE | 2.25 | -0.75 | -1.75 | -0.25 | -0.25 |
Le Bao · MDI Workshop · https://baole.io/