Skip to content

[mle] Replace np.sum with @, fix typos, and use gammaln for stable Poisson log-likelihood #473

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lectures/mle.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
import numpy as np
from numpy import exp
from scipy.special import factorial
from scipy.special import factorial, gammaln
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import statsmodels.api as sm
Expand Down Expand Up @@ -334,7 +334,7 @@ $$
= &
\sum_{i=1}^{n} y_i \log{\mu_i} -
\sum_{i=1}^{n} \mu_i -
\sum_{i=1}^{n} \log y!
\sum_{i=1}^{n} \log y_i!
\end{split}
$$

Expand All @@ -344,7 +344,7 @@ $$
\underset{\beta}{\max} \Big(
\sum_{i=1}^{n} y_i \log{\mu_i} -
\sum_{i=1}^{n} \mu_i -
\sum_{i=1}^{n} \log y! \Big)
\sum_{i=1}^{n} \log y_i! \Big)
$$

However, no analytical solution exists to the above problem -- to find the MLE
Expand Down Expand Up @@ -458,7 +458,7 @@ class PoissonRegression:
def logL(self):
y = self.y
μ = self.μ()
return np.sum(y * np.log(μ) - μ - np.log(factorial(y)))
return np.sum(y * np.log(μ) - μ - gammaln(y + 1))

def G(self):
y = self.y
Expand Down Expand Up @@ -868,17 +868,20 @@ class ProbitRegression:
return norm.pdf(self.X @ self.β.T)

def logL(self):
y = self.y
μ = self.μ()
return np.sum(y * np.log(μ) + (1 - y) * np.log(1 - μ))
return y @ np.log(μ) + (1 - y) @ np.log(1 - μ)

def G(self):
X = self.X
y = self.y
μ = self.μ()
ϕ = self.ϕ()
return np.sum((X.T * y * ϕ / μ - X.T * (1 - y) * ϕ / (1 - μ)),
axis=1)
return X.T @ (y * ϕ / μ - (1 - y) * ϕ / (1 - μ))

def H(self):
X = self.X
y = self.y
β = self.β
μ = self.μ()
ϕ = self.ϕ()
Expand Down
Loading