jmbejara / comp-econ-sp19

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2019)
48 stars 26 forks source link

Monte Carlo Demo/Monte Carlo Hw Q1 #17

Closed erineidschun closed 5 years ago

erineidschun commented 5 years ago

I still do not understand the generating process for this chunk from monte_carlo_demo:

def simulate_data_ex1(N=50, seed=65594):
    if seed == None:
        pass
    else:
        np.random.seed(seed)
    cov = np.array([[2,1],
                    [1,2]])
    mean = np.array([0, 0])
    X = scipy.stats.multivariate_normal.rvs(mean=mean, cov=cov, size=N)
    epsilon = np.random.randn(N,1)
    beta0, beta1, beta2 = 0, 1, 1
    df = pd.DataFrame(X, columns=['x1', 'x2'])
    df['epsilon'] = epsilon
    df['y'] = beta0 + beta1 * df.x1 + beta2 * df.x2 + df.epsilon
    df = df[['y', 'x1', 'x2', 'epsilon']]
    return df

Why do we need a covariance matrix and a mean array? Why are they set to these particular numbers? In the homework, we are asked to generate data in a way similar to this, and I am not sure what to do other than to remove the x2 and beta2 terms from the code above. How are we supposed to account for the 𝑥1=𝛾0+𝛾1𝑧1+𝑢 equation in the code?

jmbejara commented 5 years ago

The need the covariance matrix and mean array because we need to give the computer a specific distribution to generate data from. The exercise in class was to generate "synthetic data" from a known distribution. Then, we run our estimation procedure on that synthetic data and test how our estimator performs. These particular numbers were chosen arbitrarily. I just wanted numbers that would allow me to demonstrate omitted variable bias.

In the HW, I describe a probability distribution in which an instrument exists that will allow us to correct for this endogeneity. You'll need to write code to generate synthetic data from that probability distribution. This means that you'll need to generate data for epsilon, u, and z_1. You'll use that data to construct the data for x_1 and y.

Hope this answers your question! Lemme know if I can elaborate on anything further.

erineidschun commented 5 years ago

Not sure I understand how to construct the covariance matrix, as I am not sure the "order" to put the rows and columns in in the matrix in order to correctly place the covariance values given.

Currently I have this. I'm not sure if I got the covariance matrix right.

cov = np.array([[1,0, 0],
                [0,1,0.5],
                [0, 0.5, 1]])
mean = np.array([0, 0, 0])
beta0, beta1 , gamma0, gamma1 = 1, 1, 1, 1

Z = scipy.stats.multivariate_normal.rvs(mean=mean, cov=cov, size=N)
df = pd.DataFrame(Z, columns=['epsilon', 'u', 'z1'])
df['x1'] = gamma0 + gamma1 * df.z1 + df.u
df['y'] = beta0 + beta1 * df.x1 + df.epsilon
return df
jmbejara commented 5 years ago

The order doesn't really matter as long as you're consistent with it. The random number generator will give you an N x 3 matrix of random variables. Each row is a separate draw. The order of the variables in that row is the order that you chose when you defined the mean vector and the variance-covariance matrix.

Judging from the ordering of you columns columns=['epsilon', 'u', 'z1'], it looks like you might need to adjust your variance-covariance matrix. We should have covariance between $\epsilon$ and $u$ as 1/2.