jmbejara / comp-econ-sp19

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

HW 3 Productivity-Monte-Carlo #25

Closed erineidschun closed 5 years ago

erineidschun commented 5 years ago

I keep getting an error when using the fsolve function for Q1.

My code:

from scipy.optimize import fsolve

funct = lambda l_it: (1.0/0.3) * (gamma_i + rho * L_omega_it + 0.3 * k_it + 0.5 * sigma2epsilon - w_it + np.log(0.7)) - l_it

fsolve(funct, 4.0)

Error points to fsolve: TypeError: can't multiply sequence by non-int of type 'float'

I am not sure what I am doing wrong here? I checked and all the variables in funct are recognized as floats.

richard-archer commented 5 years ago

Also stuck on Q1. My approach:

def foc(l_i_t):
    return (.7 * np.exp(gamma_i + rho*w_i_tminus1 + .7*l_i_t + .3 * k_i_t + .5 * sigma2epsilon) - np.exp(l_i_t + w_i_t))

l_star = scipy.optimize.fsolve(foc,np.ndarray([1]))[0]

(also tried l_star = scipy.optimize.fsolve(foc,8)[0])

but the estimates I'm getting are unstable - every time I run the code, even with the same initial guess, I'm getting different answers. Is this an issue with the way I've set up the call of scipy.optimize.fsolve?

jmbejara commented 5 years ago

Hi @erineidschun . Your error means that your variables is not a number, but a sequence. That is, it's maybe a list, tuple, numpy.ndarray, etc.

Another issue, is that your first-order condition is not right. The equation that you have coded is the analytical solution for labor. The first-order condition is the one that starts looks like this:

[ℓ𝑖𝑑:].7exp(𝛾𝑖+πœŒπœ”π‘–,π‘‘βˆ’1+.7ℓ𝑖𝑑+.3π‘˜π‘–π‘‘+12𝜎2πœ€)βˆ’exp(ℓ𝑖𝑑+𝑀𝑖𝑑)=0.

jmbejara commented 5 years ago

@richard-archer . This is a good point to bring up. Because of the shape of the FOC, the default algorithm for fsolve has trouble when the initial value is too low. We can maybe see this by exploring the shape of the FOC. Here's some code to do it:

import ipywidgets

def plot_foc(gamma_i = 1.0, L_omega_it = -1.0, k_it = 1.0, w_it = -1.):
    '''
    Produce a plot of the foc and the zero line.
    '''
    f = lambda l: .7 * np.exp(gamma_i + rho * L_omega_it + .7 * l + .3 * k_it + .5 * (sigma2epsilon)) - np.exp(l + w_it)
    vf = np.vectorize(f)
    labor = np.linspace(-2,10,100)
    plt.plot(labor, vf(labor))
    plt.plot(labor, 0*labor)
    plt.ylim([-1,3])
    plt.xlabel('labor')
    plt.ylabel('foc(labor) = 0');

ipywidgets.interact(plot_foc);

It produces something that looks like this:

image

My suggestion is to just use a very large value for the initial labor value. I use 50.0.

richard-archer commented 5 years ago

Thank you! That solves my issue with unstable identification/inability to identify.

erineidschun commented 5 years ago

Ah I see what I did wrong now, thank you!

erineidschun commented 5 years ago

On another note, I am having a problem with the fixed effects model in Q8:

df2 = df.dropna() reg_fixedeff = linearmodels.PanelOLS.from_formula("y ~ a + l + k + w + EntityEffects + TimeEffects", data=df2) reg_fitted = reg_fixedeff.fit() reg_fitted

my regression summary indicates that l and k are 100% accurately estimated (had the exact coefficients), so I think I may have done something wrong in gathering df, where df = create_DF(all_data) <--- your given code.

jmbejara commented 5 years ago

Ok. Don't include a. This is meant to be unobserved. Check the rest of your regressors too. I have limited connectivity, so I can't check this out for sure.