Skip to content

Commit 754b86d

Browse files
authored
removes alias in fepois (#1072)
* removes alias in fepois * pre-commit
1 parent 89f5a97 commit 754b86d

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

pyfixest/estimation/fepois_.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -227,75 +227,65 @@ def get_fit(self) -> None:
227227
Demeaned dependent variable (from the last iteration of the IRLS
228228
algorithm).
229229
"""
230-
_Y = self._Y
231-
_X = self._X
232-
_fe = self._fe
233-
_N = self._N
234-
_convergence = self.convergence # False
235-
_maxiter = self.maxiter
236-
_tol = self.tol
237-
_fixef_tol = self._fixef_tol
238-
_fixef_maxiter = self._fixef_maxiter
239-
_solver = self._solver
240-
241-
def compute_deviance(_Y: np.ndarray, mu: np.ndarray):
230+
231+
def compute_deviance(Y: np.ndarray, mu: np.ndarray):
242232
with warnings.catch_warnings():
243233
warnings.simplefilter("ignore")
244234
deviance = (
245-
2 * np.sum(np.where(_Y == 0, 0, _Y * np.log(_Y / mu)) - (_Y - mu))
235+
2 * np.sum(np.where(Y == 0, 0, Y * np.log(Y / mu)) - (Y - mu))
246236
).flatten()
247237
return deviance
248238

249239
stop_iterating = False
250240
crit = 1
251241

252-
for i in range(_maxiter):
242+
for i in range(self.maxiter):
253243
if stop_iterating:
254-
_convergence = True
244+
self.convergence = True
255245
break
256-
if i == _maxiter:
246+
if i == self.maxiter:
257247
raise NonConvergenceError(
258248
f"""
259-
The IRLS algorithm did not converge with {_maxiter}
249+
The IRLS algorithm did not converge with {self.maxiter}
260250
iterations. Try to increase the maximum number of iterations.
261251
"""
262252
)
263253

264254
if i == 0:
265-
_mean = np.mean(_Y)
266-
mu = (_Y + _mean) / 2
255+
_mean = np.mean(self._Y)
256+
mu = (self._Y + _mean) / 2
267257
eta = np.log(mu)
268-
Z = eta + _Y / mu - 1
258+
Z = eta + self._Y / mu - 1
269259
reg_Z = Z.copy()
270-
last = compute_deviance(_Y, mu)
260+
last = compute_deviance(self._Y, mu)
271261

272262
else:
273263
# update w and Z
274-
Z = eta + _Y / mu - 1 # eq (8)
264+
Z = eta + self._Y / mu - 1 # eq (8)
275265
reg_Z = Z.copy() # eq (9)
276266

277267
# tighten HDFE tolerance - currently not possible with PyHDFE
278268
# if crit < 10 * inner_tol:
279269
# inner_tol = inner_tol / 10
280270

281271
# Step 1: weighted demeaning
282-
ZX = np.concatenate([reg_Z, _X], axis=1)
272+
ZX = np.concatenate([reg_Z, self._X], axis=1)
283273

284-
if _fe is not None:
274+
if self._fe is not None:
285275
# ZX_resid = algorithm.residualize(ZX, mu)
286276
ZX_resid, success = demean(
287277
x=ZX,
288-
flist=_fe.astype(np.uintp),
278+
flist=self._fe.astype(np.uintp),
289279
weights=mu.flatten(),
290-
tol=_fixef_tol,
291-
maxiter=_fixef_maxiter,
280+
tol=self._fixef_tol,
281+
maxiter=self._fixef_maxiter,
292282
)
293283
if success is False:
294284
raise ValueError("Demeaning failed after 100_000 iterations.")
295285
else:
296286
ZX_resid = ZX
297287

298-
Z_resid = ZX_resid[:, 0].reshape((_N, 1)) # z_resid
288+
Z_resid = ZX_resid[:, 0].reshape((self._N, 1)) # z_resid
299289
X_resid = ZX_resid[:, 1:] # x_resid
300290

301291
# Step 2: estimate WLS
@@ -305,7 +295,7 @@ def compute_deviance(_Y: np.ndarray, mu: np.ndarray):
305295
XWX = WX.transpose() @ WX
306296
XWZ = WX.transpose() @ WZ
307297

308-
delta_new = solve_ols(XWX, XWZ, _solver).reshape(
298+
delta_new = solve_ols(XWX, XWZ, self._solver).reshape(
309299
(-1, 1)
310300
) # eq (10), delta_new -> reg_z
311301
resid = Z_resid - X_resid @ delta_new
@@ -317,11 +307,11 @@ def compute_deviance(_Y: np.ndarray, mu: np.ndarray):
317307

318308
# same criterion as fixest
319309
# https://github.com/lrberge/fixest/blob/6b852fa277b947cea0bad8630986225ddb2d6f1b/R/ESTIMATION_FUNS.R#L2746
320-
deviance = compute_deviance(_Y, mu)
310+
deviance = compute_deviance(self._Y, mu)
321311
crit = np.abs(deviance - last) / (0.1 + np.abs(last))
322312
last = deviance.copy()
323313

324-
stop_iterating = crit < _tol
314+
stop_iterating = crit < self.tol
325315

326316
self._beta_hat = delta_new.flatten()
327317
self._Y_hat_response = mu.flatten()
@@ -352,7 +342,7 @@ def compute_deviance(_Y: np.ndarray, mu: np.ndarray):
352342
self._scores = self._u_hat[:, None] * self._X
353343
self._hessian = XWX
354344

355-
if _convergence:
345+
if self.convergence:
356346
self._convergence = True
357347

358348
def resid(self, type: str = "response") -> np.ndarray:

0 commit comments

Comments
 (0)