Skip to content

Commit abc7292

Browse files
authored
[mle] Replace np.sum with @, fix typos, and use gammaln for stable Poisson log-likelihood (#473)
* Replace np.sum with @ for vector inner products, fix typos, and improve numerical stability using scipy.special.gammaln * Add missing import of gammaln from scipy.special * Add self. to instance variable references.md
1 parent 36c56c7 commit abc7292

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lectures/mle.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import matplotlib.pyplot as plt
4747
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
4848
import numpy as np
4949
from numpy import exp
50-
from scipy.special import factorial
50+
from scipy.special import factorial, gammaln
5151
import pandas as pd
5252
from mpl_toolkits.mplot3d import Axes3D
5353
import statsmodels.api as sm
@@ -334,7 +334,7 @@ $$
334334
= &
335335
\sum_{i=1}^{n} y_i \log{\mu_i} -
336336
\sum_{i=1}^{n} \mu_i -
337-
\sum_{i=1}^{n} \log y!
337+
\sum_{i=1}^{n} \log y_i!
338338
\end{split}
339339
$$
340340

@@ -344,7 +344,7 @@ $$
344344
\underset{\beta}{\max} \Big(
345345
\sum_{i=1}^{n} y_i \log{\mu_i} -
346346
\sum_{i=1}^{n} \mu_i -
347-
\sum_{i=1}^{n} \log y! \Big)
347+
\sum_{i=1}^{n} \log y_i! \Big)
348348
$$
349349

350350
However, no analytical solution exists to the above problem -- to find the MLE
@@ -458,7 +458,7 @@ class PoissonRegression:
458458
def logL(self):
459459
y = self.y
460460
μ = self.μ()
461-
return np.sum(y * np.log(μ) - μ - np.log(factorial(y)))
461+
return np.sum(y * np.log(μ) - μ - gammaln(y + 1))
462462
463463
def G(self):
464464
y = self.y
@@ -868,17 +868,20 @@ class ProbitRegression:
868868
return norm.pdf(self.X @ self.β.T)
869869
870870
def logL(self):
871+
y = self.y
871872
μ = self.μ()
872-
return np.sum(y * np.log(μ) + (1 - y) * np.log(1 - μ))
873+
return y @ np.log(μ) + (1 - y) @ np.log(1 - μ)
873874
874875
def G(self):
876+
X = self.X
877+
y = self.y
875878
μ = self.μ()
876879
ϕ = self.ϕ()
877-
return np.sum((X.T * y * ϕ / μ - X.T * (1 - y) * ϕ / (1 - μ)),
878-
axis=1)
880+
return X.T @ (y * ϕ / μ - (1 - y) * ϕ / (1 - μ))
879881
880882
def H(self):
881883
X = self.X
884+
y = self.y
882885
β = self.β
883886
μ = self.μ()
884887
ϕ = self.ϕ()

0 commit comments

Comments
 (0)