Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evans' maximum likelihood linear regression code (almost real code) #16

Open
rickecon opened this issue Jan 29, 2020 · 0 comments
Open

Comments

@rickecon
Copy link
Contributor

Here is some pseudo code.

# Linear regression by Max Likelihood
import numpy as np
import scipy.stats as sts
import scipy.optimize as opt

y, x1, x2 = data

def log_lik(y, x1, x2, beta_0,
            beta_1, beta_2, sigma):
    epsilon = y - beta_0 - beta_1 * x1 - beta_2 * x2
    pdf_vals = sts.norm.pdf(epsilon, loc=0.0, scale=sigma)
    log_lik_func = np.log(pdf_vals).sum()
    
    return log_lik_func

def crit_lr(params, *args):
    beta_0, beta_1, beta_2, sigma = params
    y, x1, x2 = args
    neg_log_lik = -log_lik(y, x1, x2, beta_0,
                           beta_1, beta_2, sigma)
    
    return neg_log_lik

beta_0_init = ?
beta_1_init = ?
beta_2_init = ?
sigma_init = ?
params_init = np.array([beta_0_init, beta_1_init,
                        beta_2_init, sigma_init])
args_lr = (y, x1, x2)
results = opt.minimize(crit_lr, params_init, args=(args_lr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant