-
Notifications
You must be signed in to change notification settings - Fork 36
/
fepois_.py
692 lines (603 loc) · 22.1 KB
/
fepois_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
import warnings
from importlib import import_module
from typing import Optional, Protocol, Union
import numpy as np
import pandas as pd
from pyfixest.errors import (
NonConvergenceError,
NotImplementedError,
)
from pyfixest.estimation.demean_ import demean
from pyfixest.estimation.feols_ import Feols
from pyfixest.estimation.FormulaParser import FixestFormula
from pyfixest.utils.dev_utils import DataFrameType, _to_integer
class Fepois(Feols):
"""
Estimate a Poisson regression model.
Non user-facing class to estimate a Poisson regression model via Iterated
Weighted Least Squares (IWLS).
Inherits from the Feols class. Users should not directly instantiate this class,
but rather use the [fepois()](/reference/estimation.fepois.qmd) function.
Note that no demeaning is performed in this class: demeaning is performed in the
[FixestMulti](/reference/estimation.fixest_multi.qmd) class (to allow for caching
of demeaned variables for multiple estimation).
The method implements the algorithm from Stata's `ppmlhdfe` module.
Attributes
----------
Y : np.ndarray
Dependent variable, a two-dimensional numpy array.
X : np.ndarray
Independent variables, a two-dimensional numpy array.
fe : np.ndarray
Fixed effects, a two-dimensional numpy array or None.
weights : np.ndarray
Weights, a one-dimensional numpy array or None.
coefnames : list[str]
Names of the coefficients in the design matrix X.
drop_singletons : bool
Whether to drop singleton fixed effects.
collin_tol : float
Tolerance level for the detection of collinearity.
maxiter : Optional[int], default=25
Maximum number of iterations for the IRLS algorithm.
tol : Optional[float], default=1e-08
Tolerance level for the convergence of the IRLS algorithm.
solver: str, default is 'np.linalg.solve'
Solver to use for the estimation. Alternative is 'np.linalg.lstsq'.
fixef_tol: float, default = 1e-08.
Tolerance level for the convergence of the demeaning algorithm.
solver:
weights_name : Optional[str]
Name of the weights variable.
weights_type : Optional[str]
Type of weights variable.
_data: pd.DataFrame
The data frame used in the estimation. None if arguments `lean = True` or
`store_data = False`.
"""
def __init__(
self,
FixestFormula: FixestFormula,
data: pd.DataFrame,
ssc_dict: dict[str, Union[str, bool]],
drop_singletons: bool,
drop_intercept: bool,
weights: Optional[str],
weights_type: Optional[str],
collin_tol: float,
fixef_tol: float,
lookup_demeaned_data: dict[str, pd.DataFrame],
tol: float,
maxiter: int,
solver: str = "np.linalg.solve",
store_data: bool = True,
copy_data: bool = True,
lean: bool = False,
sample_split_var: Optional[str] = None,
sample_split_value: Optional[Union[str, int]] = None,
separation_check: Optional[list[str]] = None,
):
super().__init__(
FixestFormula,
data,
ssc_dict,
drop_singletons,
drop_intercept,
weights,
weights_type,
collin_tol,
fixef_tol,
lookup_demeaned_data,
solver,
store_data,
copy_data,
lean,
sample_split_var,
sample_split_value,
)
# input checks
_fepois_input_checks(drop_singletons, tol, maxiter)
self.maxiter = maxiter
self.tol = tol
self._method = "fepois"
self.convergence = False
self.separation_check = separation_check
self._support_crv3_inference = True
self._support_iid_inference = True
self._supports_cluster_causal_variance = False
self._Y_hat_response = np.array([])
self.deviance = None
self._Xbeta = np.array([])
def prepare_model_matrix(self):
"Prepare model inputs for estimation."
super().prepare_model_matrix()
# check if Y is a weakly positive integer
self._Y = _to_integer(self._Y)
# check that self._Y is a weakly positive integer
if np.any(self._Y < 0):
raise ValueError(
"The dependent variable must be a weakly positive integer."
)
# check for separation
na_separation: list[int] = []
if (
self._fe is not None
and self.separation_check is not None
and self.separation_check # not an empty list
):
na_separation = _check_for_separation(
Y=self._Y,
X=self._X,
fe=self._fe,
fml=self._fml,
data=self._data,
methods=self.separation_check,
)
if na_separation:
self._Y.drop(na_separation, axis=0, inplace=True)
self._X.drop(na_separation, axis=0, inplace=True)
self._fe.drop(na_separation, axis=0, inplace=True)
self._data.drop(na_separation, axis=0, inplace=True)
self._N = self._Y.shape[0]
self.na_index = np.concatenate([self.na_index, np.array(na_separation)])
self.n_separation_na = len(na_separation)
def to_array(self):
"Turn estimation DataFrames to np arrays."
self._Y, self._X, self._Z = (
self._Y.to_numpy(),
self._X.to_numpy(),
self._X.to_numpy(),
)
if self._fe is not None:
self._fe = self._fe.to_numpy()
if self._fe.ndim == 1:
self._fe = self._fe.reshape((self._N, 1))
def get_fit(self) -> None:
"""
Fit a Poisson Regression Model via Iterated Weighted Least Squares (IWLS).
Returns
-------
None
Attributes
----------
beta_hat : np.ndarray
Estimated coefficients.
Y_hat : np.ndarray
Estimated dependent variable.
u_hat : np.ndarray
Estimated residuals.
weights : np.ndarray
Weights (from the last iteration of the IRLS algorithm).
X : np.ndarray
Demeaned independent variables (from the last iteration of the IRLS
algorithm).
Z : np.ndarray
Demeaned independent variables (from the last iteration of the IRLS
algorithm).
Y : np.ndarray
Demeaned dependent variable (from the last iteration of the IRLS
algorithm).
"""
_Y = self._Y
_X = self._X
_fe = self._fe
_N = self._N
_convergence = self.convergence # False
_maxiter = self.maxiter
_tol = self.tol
_fixef_tol = self._fixef_tol
_solver = self._solver
def compute_deviance(_Y: np.ndarray, mu: np.ndarray):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
deviance = (
2 * np.sum(np.where(_Y == 0, 0, _Y * np.log(_Y / mu)) - (_Y - mu))
).flatten()
return deviance
stop_iterating = False
crit = 1
for i in range(_maxiter):
if stop_iterating:
_convergence = True
break
if i == _maxiter:
raise NonConvergenceError(
f"""
The IRLS algorithm did not converge with {_maxiter}
iterations. Try to increase the maximum number of iterations.
"""
)
if i == 0:
_mean = np.mean(_Y)
mu = (_Y + _mean) / 2
eta = np.log(mu)
Z = eta + _Y / mu - 1
reg_Z = Z.copy()
last = compute_deviance(_Y, mu)
else:
# update w and Z
Z = eta + _Y / mu - 1 # eq (8)
reg_Z = Z.copy() # eq (9)
# tighten HDFE tolerance - currently not possible with PyHDFE
# if crit < 10 * inner_tol:
# inner_tol = inner_tol / 10
# Step 1: weighted demeaning
ZX = np.concatenate([reg_Z, _X], axis=1)
if _fe is not None:
# ZX_resid = algorithm.residualize(ZX, mu)
ZX_resid, success = demean(
x=ZX, flist=_fe, weights=mu.flatten(), tol=_fixef_tol
)
if success is False:
raise ValueError("Demeaning failed after 100_000 iterations.")
else:
ZX_resid = ZX
Z_resid = ZX_resid[:, 0].reshape((_N, 1)) # z_resid
X_resid = ZX_resid[:, 1:] # x_resid
# Step 2: estimate WLS
WX = np.sqrt(mu) * X_resid
WZ = np.sqrt(mu) * Z_resid
XWX = WX.transpose() @ WX
XWZ = WX.transpose() @ WZ
delta_new = self.solve_ols(XWX, XWZ, _solver).reshape(
(-1, 1)
) # eq (10), delta_new -> reg_z
resid = Z_resid - X_resid @ delta_new
mu_old = mu.copy()
# more updating
eta = Z - resid
mu = np.exp(eta)
# same criterion as fixest
# https://github.com/lrberge/fixest/blob/6b852fa277b947cea0bad8630986225ddb2d6f1b/R/ESTIMATION_FUNS.R#L2746
deviance = compute_deviance(_Y, mu)
crit = np.abs(deviance - last) / (0.1 + np.abs(last))
last = deviance.copy()
stop_iterating = crit < _tol
self._beta_hat = delta_new.flatten()
self._Y_hat_response = mu
self._Y_hat_link = eta
# (Y - self._Y_hat)
# needed for the calculation of the vcov
# updat for inference
self._weights = mu_old
self._irls_weights = mu
# if only one dim
if self._weights.ndim == 1:
self._weights = self._weights.reshape((self._N, 1))
self._u_hat = (WZ - WX @ delta_new).flatten()
self._u_hat_working = resid
self._u_hat_response = self._Y - np.exp(eta)
self._Y = WZ
self._X = WX
self._Z = self._X
self.deviance = deviance
self._tZX = np.transpose(self._Z) @ self._X
self._tZXinv = np.linalg.inv(self._tZX)
self._Xbeta = eta
self._scores = self._u_hat[:, None] * self._X
self._hessian = XWX
if _convergence:
self._convergence = True
def resid(self, type: str = "response") -> np.ndarray:
"""
Return residuals from regression model.
Parameters
----------
type : str, optional
The type of residuals to be computed.
Can be either "response" (default) or "working".
Returns
-------
np.ndarray
A flat array with the residuals of the regression model.
"""
if type == "response":
return self._u_hat_response.flatten()
elif type == "working":
return self._u_hat_working.flatten()
else:
raise ValueError("type must be one of 'response' or 'working'.")
def predict(
self,
newdata: Optional[DataFrameType] = None,
atol: float = 1e-6,
btol: float = 1e-6,
type: str = "link",
) -> np.ndarray:
"""
Return predicted values from regression model.
Return a flat np.array with predicted values of the regression model.
If new fixed effect levels are introduced in `newdata`, predicted values
for such observations
will be set to NaN.
Parameters
----------
newdata : Union[None, pd.DataFrame], optional
A pd.DataFrame with the new data, to be used for prediction.
If None (default), uses the data used for fitting the model.
atol : Float, default 1e-6
Stopping tolerance for scipy.sparse.linalg.lsqr().
See https://docs.scipy.org/doc/
scipy/reference/generated/scipy.sparse.linalg.lsqr.html
btol : Float, default 1e-6
Another stopping tolerance for scipy.sparse.linalg.lsqr().
See https://docs.scipy.org/doc/
scipy/reference/generated/scipy.sparse.linalg.lsqr.html
type : str, optional
The type of prediction to be computed.
Can be either "response" (default) or "link".
If type="response", the output is at the level of the response variable,
i.e., it is the expected predictor E(Y|X).
If "link", the output is at the level of the explanatory variables,
i.e., the linear predictor X @ beta.
atol : Float, default 1e-6
Stopping tolerance for scipy.sparse.linalg.lsqr().
See https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.lsqr.html
btol : Float, default 1e-6
Another stopping tolerance for scipy.sparse.linalg.lsqr().
See https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.lsqr.html
Returns
-------
np.ndarray
A flat array with the predicted values of the regression model.
"""
_Xbeta = self._Xbeta.flatten()
_has_fixef = self._has_fixef
if _has_fixef:
raise NotImplementedError(
"Prediction with fixed effects is not yet implemented for Poisson regression."
)
if newdata is not None:
raise NotImplementedError(
"Prediction with function argument `newdata` is not yet implemented for Poisson regression."
)
# y_hat = super().predict(newdata=newdata, type=type, atol=atol, btol=btol)
if type == "link":
return _Xbeta # np.exp(_Xbeta)
elif type == "response":
return np.exp(_Xbeta)
else:
raise ValueError("type must be one of 'response' or 'link'.")
def _check_for_separation(
fml: str,
data: pd.DataFrame,
Y: pd.DataFrame,
X: pd.DataFrame,
fe: pd.DataFrame,
methods: Optional[list[str]] = None,
) -> list[int]:
"""
Check for separation.
Check for separation of Poisson Regression. For details, see the ppmlhdfe
documentation on separation checks.
Parameters
----------
fml : str
The formula used for estimation.
data : pd.DataFrame
The data used for estimation.
Y : pd.DataFrame
Dependent variable.
X : pd.DataFrame
Independent variables.
fe : pd.DataFrame
Fixed effects.
methods: list[str], optional
Methods used to check for separation. One of fixed effects ("fe") or
iterative rectifier ("ir"). Executes all methods by default.
Returns
-------
list
List of indices of observations that are removed due to separation.
"""
valid_methods: dict[str, _SeparationMethod] = {
"fe": _check_for_separation_fe,
"ir": _check_for_separation_ir,
}
if methods is None:
methods = list(valid_methods)
invalid_methods = [method for method in methods if method not in valid_methods]
if invalid_methods:
raise ValueError(
f"Invalid separation method. Expecting {list(valid_methods)}. Received {invalid_methods}"
)
separation_na: set[int] = set()
for method in methods:
separation_na = separation_na.union(
valid_methods[method](fml=fml, data=data, Y=Y, X=X, fe=fe)
)
if separation_na:
warnings.warn(
f"{str(len(separation_na))} observations removed because of separation."
)
return list(separation_na)
class _SeparationMethod(Protocol):
def __call__(
self,
fml: str,
data: pd.DataFrame,
Y: pd.DataFrame,
X: pd.DataFrame,
fe: pd.DataFrame,
) -> set[int]:
"""
Check for separation.
Parameters
----------
fml : str
The formula used for estimation.
data : pd.DataFrame
The data used for estimation.
Y : pd.DataFrame
Dependent variable.
X : pd.DataFrame
Independent variables.
fe : pd.DataFrame
Fixed effects.
Returns
-------
set
Set of indices of separated observations.
"""
...
def _check_for_separation_fe(
fml: str, data: pd.DataFrame, Y: pd.DataFrame, X: pd.DataFrame, fe: pd.DataFrame
) -> set[int]:
"""
Check for separation using the "fe" check.
Parameters
----------
fml : str
The formula used for estimation.
data : pd.DataFrame
The data used for estimation.
Y : pd.DataFrame
Dependent variable.
X : pd.DataFrame
Independent variables.
fe : pd.DataFrame
Fixed effects.
Returns
-------
set
Set of indices of separated observations.
"""
separation_na: set[int] = set()
if fe is not None and not (Y > 0).all(axis=0).all():
Y_help = (Y > 0).astype(int).squeeze()
# loop over all elements of fe
for x in fe.columns:
ctab = pd.crosstab(Y_help, fe[x])
null_column = ctab.xs(0)
# sep_candidate if
# fixed effect level has only observations with Y > 0
sep_candidate = (np.sum(ctab > 0, axis=0).values == 1) & (
null_column > 0
).to_numpy().flatten()
# droplist: list of levels to drop
droplist = ctab.xs(0)[sep_candidate].index.tolist()
# dropset: list of indices to drop
if len(droplist) > 0:
fe_in_droplist = fe[x].isin(droplist)
dropset = set(fe[x][fe_in_droplist].index)
separation_na = separation_na.union(dropset)
return separation_na
def _check_for_separation_ir(
fml: str,
data: pd.DataFrame,
Y: pd.DataFrame,
X: pd.DataFrame,
fe: pd.DataFrame,
tol: float = 1e-4,
maxiter: int = 100,
) -> set[int]:
"""
Check for separation using the "iterative rectifier" algorithm
proposed by Correia et al. (2021). For details see http://arxiv.org/abs/1903.01633.
Parameters
----------
fml : str
The formula used for estimation.
data : pd.DataFrame
The data used for estimation.
Y : pd.DataFrame
Dependent variable.
X : pd.DataFrame
Independent variables.
fe : pd.DataFrame
Fixed effects.
tol : float
Tolerance to detect separated observation. Defaults to 1e-4.
maxiter : int
Maximum number of iterations. Defaults to 100.
Returns
-------
set
Set of indices of separated observations.
"""
# lazy load to avoid circular import
fixest_module = import_module("pyfixest.estimation")
feols = getattr(fixest_module, "feols")
# initialize
separation_na: set[int] = set()
tmp_suffix = "_separationTmp"
# build formula
name_dependent, rest = fml.split("~")
name_dependent_separation = "U"
if name_dependent_separation in data.columns:
name_dependent_separation += tmp_suffix
fml_separation = f"{name_dependent_separation} ~ {rest}"
dependent: pd.Series = data[name_dependent]
is_interior = dependent > 0
if is_interior.all():
# no boundary sample, can exit
return separation_na
# initialize variables
tmp: pd.DataFrame = pd.DataFrame(index=data.index)
tmp["U"] = (dependent == 0).astype(float).rename("U")
# weights
N0 = (dependent > 0).sum()
K = N0 / tol**2
tmp["omega"] = pd.Series(
np.where(dependent > 0, K, 1), name="omega", index=data.index
)
# combine data
# TODO: avoid create new object?
tmp = data.join(tmp, how="left", validate="one_to_one", rsuffix=tmp_suffix)
# TODO: need to ensure that join doesn't create duplicated columns
# assert not tmp.columns.duplicated().any()
iteration = 0
has_converged = False
while iteration < maxiter:
iteration += 1
# regress U on X
# TODO: check acceleration in ppmlhdfe's implementation: https://github.com/sergiocorreia/ppmlhdfe/blob/master/src/ppmlhdfe_separation_relu.mata#L135
fitted = feols(fml_separation, data=tmp, weights="omega")
tmp["Uhat"] = pd.Series(fitted.predict(), index=fitted._data.index, name="Uhat")
Uhat = tmp["Uhat"]
# update when within tolerance of zero
# need to be more strict below zero to avoid false positives
within_zero = (Uhat > -0.1 * tol) & (Uhat < tol)
Uhat.where(~(is_interior | within_zero.fillna(True)), 0, inplace=True)
if (Uhat >= 0).all():
# all separated observations have been identified
has_converged = True
break
tmp.loc[~is_interior, "U"] = np.fmax(
Uhat[~is_interior], 0
) # rectified linear unit (ReLU)
if has_converged:
separation_na = set(dependent[Uhat > 0].index)
else:
warnings.warn(
"iterative rectivier separation check: maximum number of iterations reached before convergence"
)
return separation_na
def _fepois_input_checks(drop_singletons: bool, tol: float, maxiter: int):
"""
Perform input checks for Fepois constructor arguments.
Parameters
----------
drop_singletons : bool
Whether to drop singleton fixed effects.
tol : float
Tolerance level for convergence check.
maxiter : int
Maximum number of iterations.
Returns
-------
None
"""
# drop singletons must be logical
if not isinstance(drop_singletons, bool):
raise TypeError("drop_singletons must be logical.")
# tol must be numeric and between 0 and 1
if not isinstance(tol, (int, float)):
raise TypeError("tol must be numeric.")
if tol <= 0 or tol >= 1:
raise AssertionError("tol must be between 0 and 1.")
# maxiter must be integer and greater than 0
if not isinstance(maxiter, int):
raise TypeError("maxiter must be integer.")
if maxiter <= 0:
raise AssertionError("maxiter must be greater than 0.")