@@ -47,7 +47,7 @@ import matplotlib.pyplot as plt
|
47 | 47 | plt.rcParams["figure.figsize"] = (11, 5) #set default figure size |
48 | 48 | import numpy as np |
49 | 49 | from numpy import exp |
50 | | -from scipy.special import factorial |
| 50 | +from scipy.special import factorial, gammaln |
51 | 51 | import pandas as pd |
52 | 52 | from mpl_toolkits.mplot3d import Axes3D |
53 | 53 | import statsmodels.api as sm |
|
334 | 334 | = & |
335 | 335 | \sum_{i=1}^{n} y_i \log{\mu_i} - |
336 | 336 | \sum_{i=1}^{n} \mu_i - |
337 | | - \sum_{i=1}^{n} \log y! |
| 337 | + \sum_{i=1}^{n} \log y_i! |
338 | 338 | \end{split} |
339 | 339 | $$ |
340 | 340 | |
@@ -344,7 +344,7 @@ $$
|
344 | 344 | \underset{\beta}{\max} \Big( |
345 | 345 | \sum_{i=1}^{n} y_i \log{\mu_i} - |
346 | 346 | \sum_{i=1}^{n} \mu_i - |
347 | | -\sum_{i=1}^{n} \log y! \Big) |
| 347 | +\sum_{i=1}^{n} \log y_i! \Big) |
348 | 348 | $$ |
349 | 349 | |
350 | 350 | However, no analytical solution exists to the above problem -- to find the MLE |
@@ -458,7 +458,7 @@ class PoissonRegression:
|
458 | 458 | def logL(self): |
459 | 459 | y = self.y |
460 | 460 | μ = self.μ() |
461 | | - return np.sum(y * np.log(μ) - μ - np.log(factorial(y))) |
| 461 | + return np.sum(y * np.log(μ) - μ - gammaln(y + 1)) |
462 | 462 | |
463 | 463 | def G(self): |
464 | 464 | y = self.y |
@@ -868,17 +868,20 @@ class ProbitRegression:
|
868 | 868 | return norm.pdf(self.X @ self.β.T) |
869 | 869 | |
870 | 870 | def logL(self): |
| 871 | + y = self.y |
871 | 872 | μ = self.μ() |
872 | | - return np.sum(y * np.log(μ) + (1 - y) * np.log(1 - μ)) |
| 873 | + return y @ np.log(μ) + (1 - y) @ np.log(1 - μ) |
873 | 874 | |
874 | 875 | def G(self): |
| 876 | + X = self.X |
| 877 | + y = self.y |
875 | 878 | μ = self.μ() |
876 | 879 | ϕ = self.ϕ() |
877 | | - return np.sum((X.T * y * ϕ / μ - X.T * (1 - y) * ϕ / (1 - μ)), |
878 | | - axis=1) |
| 880 | + return X.T @ (y * ϕ / μ - (1 - y) * ϕ / (1 - μ)) |
879 | 881 | |
880 | 882 | def H(self): |
881 | 883 | X = self.X |
| 884 | + y = self.y |
882 | 885 | β = self.β |
883 | 886 | μ = self.μ() |
884 | 887 | ϕ = self.ϕ() |
|