jmbejara / comp-econ-sp19

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

Setting up problem (Q1) #54

Closed rhuselid closed 5 years ago

rhuselid commented 5 years ago

I am getting incorrect returns from my optimizer, which suggests to me that I am setting up the problem in a wrong way. Moreover, when I try to minimize the negative log likelihoods I get an infinite loop, but when I minimize the regular log likelihoods I converge to an incorrect answer.

My guess is that the part of the problem I am thinking about incorrectly is in the following chunk of code. Is that the case?

def log_like(theta, df):
    lam = theta[0:4]
    beta = theta[4:8]
    alpha = theta[8:13]
    gamma = theta[13:19]

    prob_df = pd.DataFrame()
    for N in range(0, 6): #0,1,2,3,4,5
        expectation = s(lam, df)*v(alpha, beta, df, N) - f(gamma, df, N)
        epsilon = df.TIRE - expectation
        prob_col = norm.pdf(epsilon)
        for i, prob in enumerate(prob_col):
            if prob == 0:
                prob_col[i] = 1e-08   # make a small number
        prob_df[N] = prob_col

    log_sum = 0
    for row_index, real_n in enumerate(df.TIRE):
        if real_n <= 5:
            i = real_n
        else:
            i = 5
        prob = prob_df.loc[row_index,i]
        #print(prob)
        log_sum += np.log(prob)
    #print(prob_df)
    return log_sum

Thanks!

jmbejara commented 5 years ago

It looks like there might be a couple errors. However, there is definitely one here:

        epsilon = df.TIRE - expectation
        prob_col = norm.pdf(epsilon)

This is the wrong definition of epsilon. We don't actually observe a single value for epsilon---rather a range of possible values for epsilon. This is explained in this section of the notes:

image

Also, note that Phi is supposed to be the cumulative distribution function of a standard normal. This is a typo and I have just fixed it in the GitHub repo.

Hope that helps!