jmbejara / comp-econ-sp19

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

error with pdf in Q11 #44

Closed rhuselid closed 5 years ago

rhuselid commented 5 years ago
def get_e(params, df):
    b0, b1, b2, b3, sigma = params
    e = df['sick'] - (b0 + b1*df['age'] + b2*df['children'] + b3*df['avgtemp_winter'])
    # e is an array
    return e

def pdf_residuals(params, e):
    b0, b1, b2, b3, sigma = params
    likelihoods = (1 / (2 * np.pi * sigma)) * np.exp(-e**2 / (2 * sigma**2))
    return likelihoods

def neg_log_likes(params, df):
    e = get_e(params, df)
    likes = pdf_residuals(params, e)
    log_likes = np.log(likes)
    neg_sum = -(log_likes.sum())
    return neg_sum

params_init = np.array([1,1,1,1,1])
results = minimize(neg_log_likes, params_init, args=(df))
results.x

I am running the code above for q11 and I am getting stuck on an error where the residuals look correct, but when they are put through the pdf, then become all 0 (which throws an error). Yet if I run:

k eknd

I get real values.

Any ideas for why this may be?

jmbejara commented 5 years ago

It looks like you're missing a square root and a square in the PDF. I'm not sure if that's the cause, though.

rhuselid commented 5 years ago

I added the sqrt term I forgot, but not sure about square term? I think I am matching the pdf in the lecture notes?

nlndslkn kknk
jmbejara commented 5 years ago

It looks like there is a slight typo in PDF in the notes. I've just fixed it now. You can find the appropriate PDF here: https://en.wikipedia.org/wiki/Normal_distribution

It should be sigma**2 if you put the term inside the square root.

rhuselid commented 5 years ago

Thanks! This and setting the initial params guess to be closer to the real coefficients solved my problem. Basically, the likelihood of getting those residuals was so close to zero that it was being stored as a 0 in memory, which threw an error later on.