"""`StreamingGLM`: Poisson, logit and probit regression with high-dimensional
fixed effects, by iteratively reweighted least squares (IRLS).
Each IRLS step is a weighted least-squares fit of a working response on the
covariates and the fixed effects -- the regression `StreamingHDFE` solves --
with weights that change from step to step. (Those steps, which do not depend
on the family, are `core/irls.py`'s `_IRLSMixin`; the family's row pass, the
loop and the fit statistics are here.) The rows, their sort and the cell
structure do not change, so pass 0 runs once, and pass 1 once for the
structure only (which cells identify the other effects, their levels, the
connected components). A step is then:
row pass the linear predictor of every row, from the current
coefficients: eta = x'beta + a[group] + sum_d fe_d[level] +
offset; from it the IRLS weight W and the working response z;
per cell, sum W and W [z, x], all in one multithreaded sweep
(`kernels_glm._nb_glm_rows`); and the within-cell
cross-products, with BLAS
solve the reduced system for z and the covariates, from the cell sums,
starting from the previous step's solution
update X~'WX~ and X~'Wz~ assembled from the cells; beta; the other
effects; and each fe[0] group's effect, from its cells
The row pass for a step's coefficients also gives their deviance, so a step
costs one read of the rows and one solve. Nothing row-sized is written: the
linear predictor is rebuilt from the coefficients, and the files rewritten
each step are the cells' weight and sums. The fe[0] effects are held in
memory, one float per group (two or three sets at once).
Which cell a row belongs to is found by reading the rows in order: pass 0
sorts each fe[0] group's rows by the other dimensions' codes, so a cell is one
run of rows, and a group with more than one run is one of pass 1's
identifying groups. Cells are numbered in the order they are read, which is
the order pass 1 stored them in.
The iteration follows pyfixest's (`fepois`, `feglm`): the same starting
values, convergence when the relative change in deviance,
|dev - dev_old| / (0.1 + |dev|), falls below `iwls_tol`, and step-halving when
a step raises the deviance. Inference is that of the final weighted
least-squares step, as in pyfixest: bread (X~'WX~)^-1, scores W e x~ with e
the working residual, and pyfixest's small-sample factors, with the iid vcov
the bread alone (no dispersion is estimated).
With no fixed effect or one, Newton's method comes first (`_newton_start`).
Each Newton step is one read of the rows through the fused pass of
`core.newton`, which forms the log-likelihood, the gradient and the
information in one sweep. The one fixed effect is profiled out exactly, and
the products run over each row's nonzeros when the design is mostly zeros,
as a design of indicators is. The first read copies the rows into a block
store (0/1 columns as one byte each), which the later reads use. Newton's
method stops on the decrement: below 1e-8 (or `iwls_tol`, if smaller), or
below a thousandth of `iwls_tol` relative to the deviance. IRLS then starts
from its estimates, and its first step, which converges, supplies
everything else unchanged: the collinearity check, the inference and the
result files. A step that raises the log-likelihood is halved, as in IRLS.
With ``device="cuda"`` the Newton steps run on the device (`core.gpu.newton_rows`),
the rows uploaded once if they fit. Not with `bias_correction`.
Before anything is written, fixed-effect levels whose outcome is always zero
(Poisson) or never varies (logit, probit) are dropped: their effect would be
infinite. For Poisson one pass over the dimensions finds them all, since
dropping zero rows leaves every other level's positive rows in place. For
logit and probit it can take more: dropping a firm whose outcome is all ones
can leave a worker with only zeros, so the passes repeat until one drops
nothing. See `separation_check`.
"""
from __future__ import annotations
import time
from typing import Any, Sequence
from types import SimpleNamespace
import numba
import numpy as np
import polars as pl
from scipy import special
from .core._columns import PREFIX, vcol
from .core.typing import Variables
from .core.families import get_family
from .core.irls import _OFFSET, _IRLSMixin, _State
from .core.kernels_glm import FAMILY_CODES, _nb_glm_rows, _nb_glm_rows_plain
from .core.passes import _drop_levels
from .core.report import warn
from .core.store import BlockStore
from .core.inference import _criteria
from .core.utils import SINGLE_BLAS_MAX_COLS as _SINGLE_BLAS_MAX_COLS
from .core.utils import _norm_vars, single_blas
from .estimator import StreamingHDFE
LO, HI = f"{PREFIX}lo", f"{PREFIX}hi" # per-level outcome range, in the separation check
# blocks of groups the fused row pass reduces its sums over: fixed, so that the
# deviance does not depend on the number of threads
_ROW_BLOCKS = 256
_MAX_HALVINGS = 30
# the relative change in deviance that rounding alone can make at the maximum
# (the deviance is a sum over every row): a step changing it by less is
# accepted, and the fit has converged, whatever iwls_tol asks for
_DEV_ROUNDING = 1e-12
# without batch_rows, chunks of about this many MB of the design (at most
# 2,000,000 rows, at least 100,000): with BLAS on one thread, small chunks
# cost no time, and memory follows the chunk
_CHUNK_MB = 64
[docs]
class StreamingGLM(_IRLSMixin, StreamingHDFE):
"""Low-level GLM interface: column names or Polars expressions instead of
a formula (see `fepois_stream` and `feglm_stream` for formulas). One
dependent variable, any number of models of it.
Takes `StreamingHDFE`'s parameters, and these. ``assembly`` does not apply
(the cross-products come from each step's row pass), and varying slopes,
IV and CRV3 are not available. With ``solver="auto"`` the first step
builds S and later steps use ``"stream_cg"``.
Parameters
----------
y : str or tuple
The dependent variable: a column name, or a ``(name, polars.Expr)``
pair.
x : str, tuple, list or None
Covariates, as in `StreamingHDFE`.
fe : sequence of str
Fixed-effect dimensions, as in `StreamingHDFE`, without varying
slopes.
family : {"poisson", "logit", "probit"}, default "poisson"
Poisson (log link), logit or probit.
offset : str or polars.Expr, optional
Column name or Polars expression added to the linear predictor with a
coefficient of one (e.g. log exposure).
iwls_tol : float, default 1e-8
IRLS convergence tolerance on the relative change in deviance
(pyfixest's default). Below what rounding resolves (about 1e-12), the
iteration stops once a step no longer lowers the deviance.
iwls_maxiter : int, default 25
The most IRLS steps (pyfixest's default).
separation_check : bool, default True
Drop fixed-effect levels whose outcome is always zero (Poisson) or
never varies (logit, probit), for logit and probit repeating until
none is left. Covariates that separate the outcome are not detected.
bias_correction : bool or dict, optional
Correct the coefficients for the incidental-parameter bias of the
fixed effects (logit and probit, one or two fixed effects), by the
analytic correction of Fernandez-Val and Weidner (2016) as R's
alpaca computes it (`biasCorr`): ``True`` for strictly exogenous
regressors, which needs no roles for the fixed effects (the two
terms have the same form); ``{"time": "year", "lags": L}`` for
predetermined regressors (a lagged outcome, ...), with two fixed
effects, the unit and the period ``time``: the unit's term then also
sums the scores' products up to ``L`` periods apart, so each unit
and period must have at most one row, and the period column must be
numeric or temporal. The fixed effects are then re-estimated with
the corrected coefficients held, and the variances, residuals and
fixed effects are those of that fit; the fit statistics (deviance,
log-likelihood, AIC) remain the maximum-likelihood fit's. Its
coefficients and the bias are in ``diagnostics["bias_correction"]``.
The correction is O(1/T) in the observations per level, and
unreliable for very short panels (a warning below five). Not for
Poisson, whose coefficients have no such bias with one or two fixed
effects and strictly exogenous regressors.
within_pseudo_r2 : bool, default False
Also fit the model with the fixed effects and no covariates, for
the within pseudo R-squared (`HDFEResult.pseudo_r2_within`) when it
is not free: with two or more fixed effects, or an offset. With one
fixed effect and no offset that model is the groups' means, and it
is always reported.
**options
`StreamingHDFE`'s other parameters.
Examples
--------
>>> import polars as pl
>>> fit = StreamingGLM("visits", ["x"], ["worker_id", "firm_id", "year"],
... family="poisson", offset=pl.col("log_exposure"),
... workdir="scratch").fit("data/*.parquet")
"""
[docs]
def __init__(self, y: Variables, x: Variables | None, fe: Sequence[str],
family: str = "poisson", offset: str | pl.Expr | None = None,
iwls_tol: float = 1e-8, iwls_maxiter: int = 25,
separation_check: bool = True, within_pseudo_r2: bool = False,
bias_correction: bool | dict | None = None, **options: Any) -> None:
ys = _norm_vars(y)
if len(ys) != 1:
raise ValueError("StreamingGLM takes one dependent variable; "
"fepois_stream/feglm_stream fit one estimator per outcome")
options.pop("assembly", None)
super().__init__(list(ys.items()), x, fe, **options)
if self.slopes:
raise ValueError("varying slopes are not available for GLMs")
self.yname = next(iter(ys))
for model in self.models:
if model.get("iv"):
raise ValueError(f"{model['fml']}: IV (2SLS) is not available for GLMs")
if model["y"] != self.yname:
raise ValueError(f"{model['fml']}: every model must have {self.yname!r} "
"as its dependent variable")
if not 0 < iwls_tol < 1:
raise ValueError("iwls_tol must be between 0 and 1")
if int(iwls_maxiter) < 1:
raise ValueError("iwls_maxiter must be at least 1")
self.family = get_family(family)
self.iwls_tol, self.iwls_maxiter = float(iwls_tol), int(iwls_maxiter)
self.separation_check = bool(separation_check)
self.within_pseudo_r2 = bool(within_pseudo_r2)
self.bias = self._bias_spec(bias_correction)
self._bias_acc = None
self._fixed_beta = None
self.assembly = "rows" # the cross-products come from each step
self.offset_name = None
if offset is not None:
expr = pl.col(offset) if isinstance(offset, str) else offset
self.var_names.append(_OFFSET)
self.var_exprs[_OFFSET] = expr.cast(pl.Float64)
self.vidx[_OFFSET] = self.m
self.m += 1
self.offset_name = offset if isinstance(offset, str) else "<expr>"
self._p2 = None
self._raw_w = None
self._bufs = {}
self._dev_rows = None # device copy of the rows, no-FE fits on the GPU
self._align_batches = False # the GLM's chunks follow its own rule
if options.get("batch_rows") is None:
self.batch_rows = min(2_000_000,
max(100_000, int(_CHUNK_MB * 1e6 / (8 * (self.m + 4)))))
def _bias_spec(self, spec):
"""`bias_correction=`, checked: None, or the dimensions to correct,
and with lags the unit, the period and L (the unit then streamed, so
that each chunk holds whole units, their rows in period order)."""
if spec is None or spec is False:
return None
if spec is True:
spec = {}
if not isinstance(spec, dict) or set(spec) - {"time", "lags"}:
raise ValueError("bias_correction is True, or {'time': <period>, 'lags': L} for "
"predetermined regressors")
if self.family.name == "poisson":
raise ValueError(
"bias_correction is for logit and probit: Poisson's coefficients have no "
"incidental-parameter bias with one or two fixed effects and strictly "
"exogenous regressors, and the three-way (gravity) correction of Weidner "
"and Zylkin (2021) is not implemented")
dims = list(self.fe_user)
if len(dims) not in (1, 2):
raise ValueError("bias_correction is for models with one or two fixed effects "
f"(this one has {len(dims)})")
lags, time = int(spec.get("lags", 0)), spec.get("time")
if lags < 0:
raise ValueError("bias_correction: lags must be a nonnegative integer")
if time is not None:
time = "^".join(c.strip() for c in str(time).split("^"))
if time not in dims:
raise ValueError(f"bias_correction: the period {time!r} is not one of the "
f"fixed effects {dims}")
unit = None
if lags:
if time is None or len(dims) != 2:
raise ValueError("bias_correction with lags needs two fixed effects, the unit "
"and the period, and the period named: "
"{'time': 'year', 'lags': L}")
if len(self.fe_cols[time]) != 1:
raise ValueError(f"bias_correction: the period {time!r} must be one column")
unit = next(d for d in dims if d != time)
if self.stream is not None and self.stream != unit:
raise ValueError(f"bias_correction with lags reads the rows unit by unit: "
f"stream={unit!r}, or leave stream unset")
self.stream = unit
return {"dims": dims, "time": time, "lags": lags, "unit": unit}
def _bias_correct(self, tag, model, fit, res, ctx, acc):
"""The bias-corrected coefficients from pass 2's sums, as alpaca's
`biasCorr`: beta + H^-1 (sum over the corrected dimensions of the
levels' (sum x~ z) / (sum w), halved, plus the lagged term), with H
= X~' W X~; then the fixed effects re-estimated with them held, and
the pass 2 of that fit."""
names = list(res.coefnames)
if not names:
return res
delta = np.linalg.solve(self._last_bread, acc.totals())
beta = res.beta + delta
info = acc.report(self)
self._log("bias correction: re-estimating the fixed effects with the corrected "
"coefficients held")
start = _State(beta=beta.copy(), fe=fit.final.fe, a_id=fit.final.a_id,
a_s=fit.final.a_s)
self._fixed_beta = beta
try:
with self._quiet(True):
try:
fit_c = self._irls(dict(model, x=fit.names), start=start)
except RuntimeError:
# from the first fit's effects, which are far from these
# coefficients' when the correction is large (very short
# panels), the first steps can overshoot beyond what
# step-halving recovers: start the effects from zero
self._log("bias correction: the re-estimation failed from the first "
"fit's fixed effects; again from zero")
fit_c = self._irls(dict(model, x=fit.names), start=_State(
beta=beta.copy(), fe=np.zeros_like(start.fe),
a_id=np.zeros_like(start.a_id), a_s=np.zeros_like(start.a_s)))
finally:
self._fixed_beta = None
out = self._inference(tag, model, fit_c, ctx, beta=beta)
out.diagnostics["bias_correction"] = {
"method": "Fernandez-Val and Weidner (2016), as alpaca::biasCorr",
"dims": list(self.bias["dims"]), "time": self.bias["time"],
"lags": self.bias["lags"],
"beta_uncorrected": dict(zip(names, map(float, res.beta))),
"se_uncorrected": dict(zip(names, map(float, res.se))),
"bias": dict(zip(names, map(float, -delta))),
"refit": {"iterations": fit_c.iterations, "converged": fit_c.converged},
**info}
return out
# ------------------------------------------------------------ sample
def _restriction_design(self):
return [self.yname]
def _restrict_sample(self, lf):
"""Check the outcome, and drop the separated fixed-effect levels."""
if self.bias is not None and self.bias["lags"]:
t = self.bias["time"]
dt = lf.collect_schema()[self.fe_cols[t][0]]
if not (dt.is_numeric() or dt.is_temporal()):
raise ValueError(f"bias_correction: the period {t!r} must be numeric or a "
f"date or time, to order each unit's rows (it is {dt})")
y = pl.col(vcol(self.vidx[self.yname]))
chk = (lf.select(n=pl.len(), lo=y.min(), hi=y.max(),
other=((y != 0) & (y != 1)).sum(), zeros=(y == 0).sum())
.collect(engine="streaming"))
n = chk["n"].item()
self.n_before_separation = n
self._separation_tables = {}
self.separation = {"checked": self.separation_check and bool(self.fe_user),
"observations": 0, "levels": {}, "rounds": 0}
if n == 0:
return None # pass 0 reports it
self.family.check(chk["lo"].item(), chk["hi"].item(), chk["other"].item())
if not self.separation["checked"]:
return None
binary = self.family.name != "poisson"
if not binary and chk["zeros"].item() == 0:
return None
separated = (pl.col(LO) == pl.col(HI)) if binary else (pl.col(HI) == 0)
cols = list(dict.fromkeys(c for d in self.fe_user for c in self.fe_cols[d]))
cur = lf.select([pl.col(c) for c in cols] + [y])
found = {}
while True:
changed = False
for d in self.fe_user:
on = self.fe_cols[d]
bad = (cur.group_by(on).agg(y.min().alias(LO), y.max().alias(HI))
.filter(separated).select(on).collect(engine="streaming"))
if bad.height:
found.setdefault(d, []).append(bad)
cur = cur.join(bad.lazy(), on=on, how="anti")
changed = True
# rounds counts the passes over the dimensions that dropped something
self.separation["rounds"] += changed
# levels of the only dimension cannot separate each other, and
# dropping rows whose outcome is zero separates nothing new
if not changed or len(self.fe_user) == 1 or not binary:
break
if not found:
return None
tables = {d: pl.concat(v) for d, v in found.items()}
self._separation_tables = tables
self.separation["levels"] = {d: t.height for d, t in tables.items()}
return lambda frame: _drop_levels(frame, tables, self.fe_cols)
# ------------------------------------------------------------ driver
def _after_pass0(self):
# analytic weights rescaled to average 1 over the estimation sample, as
# Stata does for maximum likelihood: the fit is the same, but the iid
# vcov, the log-likelihood and the bias correction would otherwise
# depend on the weights' scale (frequency weights are counts)
if self.weights is not None and self.weights_type != "fweights":
self._wscale = self.n_obs / self.wsum
dropped = self.n_before_separation - self.n_obs
self.separation["observations"] = dropped
if dropped:
warn(f"{dropped:,} observations removed because of separation ("
+ ", ".join(f"{v:,} levels of {d}"
for d, v in self.separation["levels"].items()) + ")",
self.logger)
def _fit_passes(self, source, keys, extra, default, fe_dof, t0):
if any(k.startswith("CRV3:") for k in keys):
raise ValueError("CRV3 standard errors are not available for GLMs; use CRV1")
if getattr(self, "boot_req", None):
raise ValueError("the wild cluster bootstrap (bootcluster=) is for linear "
"models; it is not available for GLMs, as in pyfixest")
self._run_passes(source, keys, extra)
if self.bias is not None and self.bias["lags"] and self.n_cells < self.n_obs:
raise ValueError(
f"bias_correction with lags needs at most one row per unit and period; "
f"{self.n_obs - self.n_cells:,} rows repeat a ({self.bias['unit']}, "
f"{self.bias['time']}) pair")
nested = {t: self._nested_dims(t) for t in self.clusters}
ctx = {"k_fe": self._k_fe(fe_dof), "fe_dof": fe_dof, "nested": nested, "default": default, "t0": t0}
self._layout()
self._track_disk()
return [self._fit_model(f"m{mi:03d}", model, ctx)
for mi, model in enumerate(self.models)]
def _fit_model(self, tag, model, ctx):
start = self._newton_start(model)
self._log(f"IRLS ({self.family.name}): {model['fml']}")
fit = self._irls(model, start=start)
if start is not None:
fit.newton = self._newton_info
if self.bias is not None:
self._bias_acc = _BiasSums(self)
try:
res = self._inference(tag, model, fit, ctx)
acc = self._bias_acc
finally:
self._bias_acc = None
if acc is not None:
res = self._bias_correct(tag, model, fit, res, ctx, acc)
return self._fit_statistics(res, model, fit)
# ------------------------------------------------------------ Newton
# Whether a model with no fixed effect or one is fitted by Newton's method
# on the fused passes of `core.newton` before IRLS takes its final step.
newton = True
def _newton_eligible(self):
return (self.newton and not self.o_fe and not self.slopes
and getattr(self, "_fixed_beta", None) is None and self.bias is None
and (not self.fe or self.G_id == 0))
def _newton_start(self, model):
"""Newton's method for `model` on the stored rows, the one fixed
effect (if any) profiled out exactly: each step one read of the rows
with the fused pass of `core.newton` (the log-likelihood, gradient and
arrow-shaped information, from each row's nonzeros when the design is
sparse), and a halving of the step when the log-likelihood falls.
Returns the coefficients as IRLS's state, from which IRLS takes the
final step (and with it the collinearity check, the cell sums of the
variance and the convergence check on the deviance), or None when the
model is not one this applies to."""
if not self._newton_eligible():
return None
from .core.kernels_newton import newton_pass_rows, part_starts
from .core.newton import NNZ_SHARE, NewtonMoments, newton_direction
t0 = time.time()
fam = FAMILY_CODES[self.family.name]
yj = self.vidx[self.yname]
xj = [self.vidx[x] for x in model["x"]]
k = len(xj)
G = self.G_s if self.fe else 1
cols = self._columns(yj, xj)
nt = numba.get_num_threads()
state = {"nnz": None, "store": None, "binary": np.ones(k, bool), "nz": 0, "size": 0}
def from_rows():
"""The rows from the row files (the first read)."""
g0 = 0
for ch, starts, codes in self._row_chunks(cols):
y, X, w, off = self._chunk_data(ch, yj, xj)
n = len(y)
if self.fe:
ng = len(starts) - 1
f = np.repeat(np.arange(g0, g0 + ng, dtype=np.int64), np.diff(starts))
g0 += ng
else:
f = np.zeros(n, np.int64)
yield np.ascontiguousarray(X), y, w, off, f
def chunks():
"""The rows: from the row files the first time, which also notes
the design's 0/1 columns and its share of nonzeros; then from a
copy in a block store, which keeps those columns as one byte each
and the rest as plain arrays (much less to read and nothing to
assemble per pass than the row files' Parquet columns)."""
if state["store"] is not None:
for b in state["store"].iterate(["X", "y", "w", "off", "f"]):
yield b["X"], b["y"], b.get("w"), b.get("off"), b["f"]
return
for X, y, w, off, f in from_rows():
state["binary"] &= np.all((X == 0) | (X == 1), axis=0)
state["nz"] += int(np.count_nonzero(X))
state["size"] += X.size
yield X, y, w, off, f
state["nnz"] = bool(state["size"]) and state["nz"] < NNZ_SHARE * state["size"]
store = BlockStore(self.workdir / "newton_blocks", in_memory=False,
binary=state["binary"] if state["binary"].any() else None)
for X, y, w, off, f in from_rows():
store.add({"X": X, "y": y, "w": w, "off": off, "f": f})
state["store"] = store
empty = np.zeros(0)
colpos = np.arange(k, dtype=np.int64)
inc = np.ones((1, 1), np.bool_)
passes = [0]
on_device = self.device == "cuda"
if on_device:
from .core.gpu import cuda_backend, group_indicator, newton_rows
be = cuda_backend()
dev = {"chunks": None}
def dev_chunks():
"""The rows on the device: uploaded from the block copy on the
first pass and kept there if they fit in the device's free
memory with room to spare, else uploaded again each pass."""
if dev["chunks"] is not None:
yield from dev["chunks"]
return
budget = 0.6 * getattr(be, "free_bytes", lambda: float("inf"))()
kept, used, keep = [], 0, True
for X, y, w, off, f in chunks():
d = (be.xp.asarray(X), be.xp.asarray(y),
None if w is None else be.xp.asarray(w),
None if off is None else be.xp.asarray(off),
f if self.fe else None,
group_indicator(be, f, G) if self.fe else None)
used += sum(a.nbytes for a in d[:4] if a is not None)
if keep and used > budget:
keep, kept = False, []
warn("device='cuda': the rows do not fit in the device's free "
"memory, so every Newton step uploads them again", self.logger)
if keep:
kept.append(d)
yield d
if keep:
dev["chunks"] = kept
def dpass(b, a, info):
ll, gb, ga = 0.0, np.zeros(k), np.zeros(G)
H = np.zeros((k, k)) if info else None
D = np.zeros(G) if info else None
C = np.zeros((G, k)) if info else None
for X, y, w, off, f, F in dev_chunks():
out = newton_rows(be, self.family.name, X, y, w, off, f, F, b, a, info, G)
ll += out[0]
gb += out[1]
ga += out[2]
if info:
H += out[3]
D += out[4]
C += out[5]
if not info:
return ll, None
return ll, NewtonMoments(
loglik=np.array([[ll]]), gradient=gb[None], effects_gradient=ga[None],
information=((H + H.T) / 2)[None], effects_information=D[None],
cross=C[None], skipped=np.zeros((1, 1), np.int64), columns=colpos)
def npass(b, a, info):
if on_device:
passes[0] += 1
return dpass(b, a, info)
passes[0] += 1
B = np.ascontiguousarray(b[None, :], dtype=np.float64)
A = np.ascontiguousarray(a[None, :], dtype=np.float64)
c = k if info else 0
ll = np.zeros((1, 1))
sk = np.zeros((1, 1))
gb = np.zeros((1, k))
H = np.zeros((1, c, c))
ga = np.zeros((1, G))
D = np.zeros((1, G if info else 0))
C = np.zeros((1, G if info else 0, c))
grouped = bool(self.fe)
# each thread's copies of the sums, zeroed once per pass and added
# up after it (not per chunk: at a wide information's c x c that
# would cost as much as the rows)
gshape = (0, 1, 0) if grouped else (nt, 1, G)
parts = (np.zeros((nt, 1, 1)), np.zeros((nt, 1, 1)), np.zeros((nt, 1, k)),
np.zeros((nt, 1, c, c)), np.zeros(gshape),
np.zeros(gshape if info else (0, 1, 0)),
np.zeros((gshape[0], 1, gshape[2], c) if info else (0, 1, 0, c)))
for X, y, w, off, f in chunks():
n = len(y)
P = max(1, min(nt, n // 256 or 1))
starts = (part_starts(f, P) if grouped
else np.linspace(0, n, P + 1).astype(np.int64))
P = len(starts) - 1
ll_t, sk_t, gb_t, H_t, ga_t, D_t, C_t = (t[:P] if len(t) else t
for t in parts)
with numba.parallel_chunksize(1):
newton_pass_rows(fam, X, y, empty if w is None else w,
empty if off is None else off, f, np.zeros(n, np.int64),
B, A, inc, colpos, info, state["nnz"], grouped, starts,
ll_t, sk_t, gb_t, H_t, ga_t, D_t, C_t, ga, D, C)
ll_t, sk_t, gb_t, H_t, ga_t, D_t, C_t = parts
ll += ll_t.sum(axis=0)
sk += sk_t.sum(axis=0)
gb += gb_t.sum(axis=0)
H += H_t.sum(axis=0)
if not grouped:
ga += ga_t.sum(axis=0)
if info:
D += D_t.sum(axis=0)
C += C_t.sum(axis=0)
if not info:
return float(ll.sum()), None
Hs = np.triu(H) + np.transpose(np.triu(H, 1), (0, 2, 1))
return float(ll.sum()), NewtonMoments(
loglik=ll, gradient=gb, effects_gradient=ga, information=Hs,
effects_information=D, cross=C, skipped=sk.astype(np.int64),
columns=colpos)
# the start: each group's (or, without a fixed effect, the intercept's)
# value at the outcome's mean, the slopes at zero
sy, sw = np.zeros(G), np.zeros(G)
for X, y, w, off, f in chunks():
wt = np.ones(len(y)) if w is None else w
sy += np.bincount(f, wt * y, G)
sw += np.bincount(f, wt, G)
mean = np.clip(sy / np.where(sw > 0, sw, 1.0), 1e-10, None)
if self.family.name == "poisson":
a0 = np.log(mean)
else:
mean = np.clip(mean, 1e-10, 1 - 1e-10)
a0 = (np.log(mean / (1 - mean)) if self.family.name == "logit"
else special.ndtri(mean))
b = np.zeros(k)
if self.fe:
a = a0
else:
a = np.zeros(1)
if "Intercept" in model["x"] and self.offset_name is None:
b[model["x"].index("Intercept")] = a0[0]
ll, mom = npass(b, a, True)
# the collinearity check IRLS makes on its first step, on the
# information at the start: the variables it drops are held at zero
Cb = mom.profiled(0)[0] if self.fe else mom.information[0]
hs = float(mom.effects_information[0].sum()) if self.fe else None
if self.fe:
hx = mom.cross[0].sum(axis=0)
raw = np.diag(mom.information[0]) - hx * hx / hs if hs > 0 else None
else:
raw = None
free = ~self._collinear(Cb, raw=raw) if k else np.zeros(0, bool)
if k and not free.any():
state["store"].delete()
return None # IRLS says what is wrong
sel = np.flatnonzero(free)
# Stop when the decrement (about twice the log-likelihood the next step
# would add, and so the deviance it would remove) is below 1e-8, or
# iwls_tol if smaller, or relative to the deviance below a thousandth of
# iwls_tol (IRLS's own test on the relative change in deviance). IRLS's
# final step follows, and then changes the deviance by far less than
# iwls_tol, so it is one step.
# (With a quasi-separated covariate, whose coefficient grows without
# bound, the decrement falls only linearly, and an absolute tolerance
# alone would spend every iteration.)
tol_abs = min(1e-8, self.iwls_tol)
tol_rel = 1e-3 * self.iwls_tol
it, halvings, decrement, converged = 0, 0, np.inf, False
for it in range(1, self.iwls_maxiter + 1):
if self.fe:
Cb, g = mom.profiled(0)
else:
Cb, g = mom.information[0], mom.gradient[0]
db = np.zeros(k)
db[sel], decrement = newton_direction(Cb[np.ix_(sel, sel)], g[sel])
if decrement < max(tol_abs, tol_rel * (0.1 + 2.0 * abs(ll))):
converged = True
break
da = mom.effects_step(db, 0) if self.fe else np.zeros(1)
t = 1.0
for _ in range(_MAX_HALVINGS):
ll_new, mom_new = npass(b + t * db, a + t * da, True)
if np.isfinite(ll_new) and ll_new >= ll - 1e-12 * (1.0 + abs(ll)):
break
t /= 2
halvings += 1
else:
break
b, a, ll, mom = b + t * db, a + t * da, ll_new, mom_new
self._log(f" Newton {it}: log-likelihood {ll:.12g}, decrement {decrement:.2e}"
+ (f", step {t:g}" if t < 1 else ""))
state["store"].delete()
self._newton_info = {"iterations": it, "converged": converged, "passes": passes[0] + 1,
"step_halvings": halvings, "decrement": float(decrement),
"nonzeros_only": bool(state["nnz"]) and not on_device,
"device": be.name if on_device else None,
"seconds": round(time.time() - t0, 2)}
if self.fe:
return _State(beta=b, fe=np.zeros(0), a_id=np.zeros(self.G_id), a_s=a)
return _State(beta=b, fe=np.zeros(0), a_id=np.zeros(0), a_s=np.zeros(0))
def _inference(self, tag, model, fit, ctx, beta=None):
"""Pass 2 on the final weighted least-squares step of `fit`: the
variances, the residual and fixed-effect files (`_estimate`); with
`beta`, at those coefficients."""
m, (_, L) = self.m, self._offsets()
cols = [self.vidx[self.yname]] + fit.xj
A = np.zeros((m, m))
A[np.ix_(cols, cols)] = fit.A
gamma = np.zeros((L, m))
gamma[:, cols] = fit.gamma
self._raw_w = np.zeros(m)
self._raw_w[cols] = fit.raw
self._p2 = SimpleNamespace(prev=fit.prev, final=fit.final, yname=model["y"])
try:
with single_blas(len(cols) <= _SINGLE_BLAS_MAX_COLS):
return self._estimate(tag, dict(model, x=fit.names), A, gamma,
dict(ctx, info=fit.info), beta=beta)
finally:
self._p2 = self._raw_w = None
def _fit_statistics(self, res, model, fit):
"""The family's statistics, of the maximum-likelihood fit `fit`."""
ll0 = self.family.loglik_null(fit.sy, fit.sw, fit.slg)
res.family = self.family.name
res.deviance, res.loglik = float(fit.dev), float(fit.ll)
res.loglik_null = float(ll0)
res.pseudo_r2 = float(1 - fit.ll / ll0) if ll0 else np.nan
aic, bic = _criteria(fit.ll, len(fit.names) + res.k_fe, res.n_obs)
res.aic, res.bic = float(aic), float(bic)
if self.fe:
ll_fe = fit.ll_fe0
if ll_fe is None and self.within_pseudo_r2:
self._log(f"IRLS ({self.family.name}), fixed effects only: {model['fml']}")
with self._quiet(True):
ll_fe = self._irls(dict(model, x=[])).ll
if ll_fe is not None:
res.loglik_fe_null = float(ll_fe)
res.pseudo_r2_within = float(1 - fit.ll / ll_fe) if ll_fe else np.nan
res.rss = res.r2 = res.adj_r2 = res.r2_within = res.adj_r2_within = res.rmse = np.nan
res.collin_vars = fit.dropped + res.collin_vars
res.diagnostics["irls"] = {"iterations": fit.iterations, "converged": fit.converged,
"step_halvings": fit.halvings,
"deviance": [float(d) for d in fit.path],
"seconds": fit.seconds}
if getattr(fit, "newton", None) is not None:
res.diagnostics["newton"] = dict(fit.newton)
res.diagnostics["separation"] = dict(self.separation)
if self.offset_name is not None:
res.diagnostics["offset"] = self.offset_name
return res
# ------------------------------------------------------------ IRLS
def _irls(self, model, start=None):
"""IRLS for `model`, from `start` (coefficients) or the family's
starting values."""
yj = self.vidx[self.yname]
xj = [self.vidx[x] for x in model["x"]]
names = list(model["x"])
t0 = time.time()
binary = self.family.name != "poisson"
# Two sets of cell weights: the current step's (`slot`) and the
# candidate's, which become current when the step is accepted. The
# cell sums, k + 1 columns, are needed only until `_update` has turned
# them into coefficients, so the candidate's overwrite them: with many
# covariates they are the largest thing the fit holds.
slot = 0
cur = start if start is not None else _State(init=True)
agg = self._row_pass(cur, yj, xj, slot, first=True)
# sums of the outcome, which do not change, and the fixed-effects-only
# model's log-likelihood when it is the groups' means
const = (agg.sy, agg.sw, agg.slg,
agg.ll_fe0 - agg.slg if agg.ll_fe0 is not None else None)
dev = agg.dev
path, dropped, gamma, halvings = [dev], [], None, 0
converged, last, it = False, None, 0
for it in range(1, self.iwls_maxiter + 1):
with self._quiet(it > 1):
gamma, A, info = self._wls(agg, ["(working response)", *names], gamma)
if it == 1 and self._fixed_beta is None:
keep = ~self._collinear(A[1:, 1:], raw=agg.raw[1:])
if len(keep) and not keep.any():
raise ValueError(f"{model['fml']}: all covariates are collinear with the "
"fixed effects")
if not keep.all() and not cur.init:
if np.any(cur.beta[~keep] != 0):
# the start (Newton's) kept a variable this drops: start
# over from the family's starting values
return self._irls(model)
cur = _State(cur.beta[keep], cur.fe, cur.a_id, cur.a_s)
if not keep.all():
dropped = [nm for nm, kp in zip(names, keep) if not kp]
warn(f"{model['fml']}: {len(dropped)} variables dropped due to "
f"multicollinearity: {dropped}", self.logger)
sel = np.concatenate(([0], 1 + np.flatnonzero(keep)))
A, gamma = A[np.ix_(sel, sel)], gamma[:, sel]
agg = self._select(agg, sel, slot)
xj = [j for j, kp in zip(xj, keep) if kp]
names = [nm for nm, kp in zip(names, keep) if kp]
new = self._update(agg, gamma, A)
cand = self._row_pass(new, yj, xj, 1 - slot)
if not self._accept(cand.dev, dev):
# step-halving, as pyfixest's feglm: back toward the current
# coefficients (for logit and probit the start is all zeros;
# Poisson's start is not a set of coefficients, so its first
# step is taken as it is)
if cur.init and not binary:
if not np.isfinite(cand.dev):
raise FloatingPointError(f"{model['fml']}: the first IRLS step "
"gives a non-finite deviance")
else:
base = cur.zeros_like() if cur.init else cur
target, t = new, 1.0
for _ in range(_MAX_HALVINGS):
t /= 2
halvings += 1
new = base.toward(target, t)
cand = self._row_pass(new, yj, xj, 1 - slot)
if np.isfinite(cand.dev) and cand.dev < dev:
break
else:
raise RuntimeError(f"{model['fml']}: IRLS step-halving failed "
f"(deviance {cand.dev:.10g} vs {dev:.10g})")
crit = abs(cand.dev - dev) / (0.1 + abs(dev))
# a later step that does not lower the deviance was accepted as
# rounding: nothing is left to gain, even when iwls_tol is below that
flat = not cur.init and cand.dev >= dev
# the step just taken is the final one if this converges: its
# weights (those of `cur`), Gamma and cross-products give the vcov
last = SimpleNamespace(cur=cur, gamma=gamma, A=A, info=info, raw=agg.raw)
cur, agg, dev, slot = new, cand, cand.dev, 1 - slot
path.append(dev)
self._log(f" IRLS {it}: deviance {dev:.12g}, relative change {crit:.2e}"
+ (f", solver iterations {info['iterations']}"
if info.get("solver") != "none" else ""))
if crit < self.iwls_tol or flat:
converged = True
break
if not converged:
warn(f"{model['fml']}: IRLS did not converge within iwls_maxiter="
f"{self.iwls_maxiter} steps", self.logger)
info = dict(last.info)
self._free_device_rows()
return SimpleNamespace(
prev=last.cur, final=cur, gamma=last.gamma, A=last.A, info=info, raw=last.raw,
xj=xj, names=names, dropped=dropped, dev=dev, ll=agg.ll, path=path,
iterations=it, converged=converged, halvings=halvings,
sy=const[0], sw=const[1], slg=const[2], ll_fe0=const[3],
seconds=round(time.time() - t0, 2))
def _accept(self, dev_new, dev):
"""A step that lowers the deviance, or changes it by less than the
tolerance or than rounding can (at convergence it can raise it by a
hair)."""
tol = max(self.iwls_tol, _DEV_ROUNDING)
return np.isfinite(dev_new) and (
dev_new < dev or abs(dev_new - dev) / (0.1 + abs(dev)) < tol)
# ------------------------------------------------------------ row pass
def _row_pass_plain(self, out, state, y, X, f, off, c, first):
"""The row pass for a chunk of a model without fixed effects: no
cells, so the moments are plain weighted sums, in one parallel sweep
(`_nb_glm_rows_plain`). Up to _SINGLE_BLAS_MAX_COLS columns, each
block of rows forms its own cross-products on its thread; beyond,
one BLAS product on all threads forms them."""
empty = np.zeros(0)
blockwise = c <= _SINGLE_BLAS_MAX_COLS
V = np.empty((len(y), c))
Wb = np.empty((_ROW_BLOCKS if blockwise else 0, c, c))
parts = np.empty((_ROW_BLOCKS, 6 + c))
_nb_glm_rows_plain(FAMILY_CODES[self.family.name], bool(state.init), float(self.ybar),
y, X, empty if f is None else f, empty if off is None else off,
empty if state.init else state.beta, V, Wb, parts)
p = parts.sum(axis=0)
out.dev += float(p[0])
out.ll += float(p[1])
if first:
out.sy += float(p[2])
out.sw += float(p[3])
out.slg += float(p[4])
out.wsum += float(p[5])
out.sv += p[6:]
out.W += Wb.sum(axis=0) if blockwise else V.T @ V
def _free_device_rows(self):
if self._dev_rows is not None:
self._dev_rows = None
from .core.gpu import cuda_backend
getattr(cuda_backend(), "release", lambda: None)()
def _release_intermediates(self):
self._dev_rows = None
super()._release_intermediates()
def _device_chunks(self, be, yj, xj):
"""The row chunks of a model without fixed effects as device arrays
(y, X, f, offset). The first pass uploads them and, if they fit in
the device's free memory with room to spare, keeps them: every later
step then runs without reading the rows again. If they do not fit,
each pass uploads them again, a chunk at a time."""
key = (yj, tuple(xj))
held = self._dev_rows
if held is not None and held["key"] == key and held["chunks"] is not None:
yield from held["chunks"]
return
budget = 0.6 * getattr(be, "free_bytes", lambda: float("inf"))()
keep = held is None or held["key"] != key or held["fits"]
chunks, used = [], 0
for ch, _, _ in self._row_chunks(self._columns(yj, xj)):
d = tuple(None if a is None else be.xp.asarray(a)
for a in self._chunk_data(ch, yj, xj))
used += sum(a.nbytes for a in d if a is not None)
if keep and used > budget:
keep, chunks = False, []
warn("device='cuda': the rows do not fit in the device's free memory, so "
"every IRLS step uploads them again", self.logger)
if keep:
chunks.append(d)
yield d
self._dev_rows = {"key": key, "chunks": chunks if keep else None, "fits": keep}
def _device_pass(self, out, state, yj, xj, first):
"""`_row_pass_plain` for every chunk on the device: the family's
terms and the cross-products there (`core.gpu.glm_rows_plain`), and
only their sums come back."""
from .core.gpu import cuda_backend, glm_rows_plain
be = cuda_backend()
for y, X, f, off in self._device_chunks(be, yj, xj):
p, sv, W = glm_rows_plain(be, self.family.name, bool(state.init), float(self.ybar),
y, X, f, off, None if state.init else state.beta)
out.dev += float(p[0])
out.ll += float(p[1])
if first:
out.sy += float(p[2])
out.sw += float(p[3])
out.slg += float(p[4])
out.wsum += float(p[5])
out.sv += sv
out.W += W
def _row_pass(self, state, yj, xj, slot, first=False):
"""Read the rows once at coefficients `state`: the deviance and
log-likelihood there, and the cell sums of the next weighted
least-squares step (column 0 the working response z, then the
covariates), with the within-cell cross-products and each column's
weighted total sum of squares (`raw`). The cell weights go to the
buffers of `slot`; the cell sums replace the previous ones."""
fam, k = self.family, len(xj)
c = k + 1
# with one fixed effect and no offset, the model with only the fixed
# effects fits each group's mean: its log-likelihood (less sum log y!)
means_null = first and bool(self.fe) and not self.o_fe and self.offset_name is None
out = SimpleNamespace(dev=0.0, ll=0.0, sy=0.0, sw=0.0, slg=0.0,
ll_fe0=0.0 if means_null else None,
W=np.zeros((c, c)), sv=np.zeros(c), bss=np.zeros(c), wsum=0.0,
n_id=self._buffer(("n_id", slot), (self.M_id,)),
sums_id=self._buffer(("sums_id", c), (self.M_id, c)),
n_s=self._buffer(("n_s", slot), (self.G_s,)),
sums_s=self._buffer(("sums_s", c), (self.G_s, c)))
if first and self.fe:
self.codes_s = np.zeros((self.G_s, len(self.o_fe)), np.int64)
cursor = [0, 0, 0]
code = FAMILY_CODES[fam.name]
empty = np.zeros(0)
on_device = self.device == "cuda" and not self.fe
if on_device:
self._device_pass(out, state, yj, xj, first)
with single_blas(c <= _SINGLE_BLAS_MAX_COLS):
for ch, starts, codes in (() if on_device
else self._row_chunks(self._columns(yj, xj))):
lay = self._cell_layout(starts, codes, cursor)
y, X, f, off = self._chunk_data(ch, yj, xj)
if lay is None:
self._row_pass_plain(out, state, y, X, f, off, c, first)
continue
# one fused sweep: the linear predictor, the family's weight and
# working response, the deviance and log-likelihood, and the cell
# moments, with V left holding the within-cell deviations
if means_null:
out.ll_fe0 += fam.loglik_means(y, f, starts)
a = self._group_effects(state, lay)
V = np.empty((len(y), c))
w = np.empty(len(y))
nc = np.empty(len(lay.cstarts) - 1)
sc = np.empty((len(nc), c))
parts = np.empty((_ROW_BLOCKS, 5 + c))
_nb_glm_rows(code, bool(state.init), float(self.ybar), lay.starts, lay.gcell,
lay.cstarts, y, X, empty if f is None else f,
empty if off is None else off, codes, self.offs,
empty if state.init else state.beta,
empty if state.init else state.fe, a, V, w, nc, sc, parts)
del X, w
p = parts.sum(axis=0)
out.dev += float(p[0])
out.ll += float(p[1])
if first:
out.sy += float(p[2])
out.sw += float(p[3])
out.slg += float(p[4])
out.bss += p[5:]
out.W += V.T @ V
del V
out.sv += sc.sum(axis=0)
out.wsum += nc.sum()
out.n_id[lay.c0:lay.c0 + lay.nc] = nc[lay.cell_id]
out.sums_id[lay.c0:lay.c0 + lay.nc] = sc[lay.cell_id]
out.n_s[lay.s0:lay.s0 + lay.ns] = nc[~lay.cell_id]
out.sums_s[lay.s0:lay.s0 + lay.ns] = sc[~lay.cell_id]
if first:
self.codes_s[lay.s0:lay.s0 + lay.ns] = codes[starts[:-1][~lay.is_id]]
if self.fe and (cursor[0], cursor[1], cursor[2]) != (self.M_id, self.G_id, self.G_s):
raise RuntimeError(
f"the row files hold {cursor[0]:,} identifying cells in {cursor[1]:,} groups "
f"and {cursor[2]:,} single-cell groups, where pass 1 found {self.M_id:,}, "
f"{self.G_id:,} and {self.G_s:,}")
# sum w v^2: within cells, plus between cells (none without fixed effects)
ss = np.diag(out.W) + out.bss
out.raw = np.maximum(ss - out.sv ** 2 / out.wsum, 0.0) if out.wsum > 0 else ss
return out
# ------------------------------------------------------------ pass 2
def _pass2_rows(self, yj, xk):
"""Pass 2 runs on the final step's working regression: z and W from
the coefficients before the step, Gamma and beta from it. The residual
file gets the outcome, the linear predictor, the fitted mean and the
response residual y - mu at the final coefficients, and the working
residual."""
p, fam = self._p2, self.family
cursor = [0, 0, 0]
fweights = self.weights is not None and self.weights_type == "fweights"
def rows(ch, starts, codes):
lay = self._cell_layout(starts, codes, cursor)
y, X, f, off = self._chunk_data(ch, yj, xk)
eta = self._eta(p.prev, y, X, off, codes, lay)
_, W, u = fam.irls(y, eta)
z = eta + u if off is None else eta + u - off
eta_f = self._eta(p.final, y, X, off, codes, lay)
mu_f = fam.mu(eta_f)
after = None
if self._bias_acc is not None:
acc = self._bias_acc
def after(h):
acc.add(h, y, eta_f, f, starts, codes)
return SimpleNamespace(
y=z, w=W if f is None else W * f, fw=f if fweights else None,
w_out=None if f is None else f / self._wscale, # as given
extra=lambda e: {p.yname: y, "eta": eta_f, "fitted": mu_f, "resid": y - mu_f,
"resid_working": e},
after=after)
rows.cols = ([vcol(self.vidx[_OFFSET])] if self.offset_name is not None else [])
rows.weighted = True
return rows
def _iid_factor(self, rss, N, K):
"""The bread alone, with pyfixest's (N - 1) / (N - K)."""
return (N - 1) / (N - K) if N > K else np.nan
class _BiasSums:
"""The sums of the incidental-parameter bias correction, accumulated in
pass 2 from its fixed-effect-adjusted regressors x~ and the family's
per-row terms at the final coefficients (score v, Fisher weight w and
its derivative z, times the weights): for each corrected dimension, the
sum over its levels of (sum x~ z) / (sum w) -- alpaca's `groupSums` --
and with lags, over the units, (sum_l sum_t T / (T - l) x~_t w_t
v_(t-l)) / (sum w) -- its `groupSumsSpectral`, the rows of each unit in
period order. The streamed dimension's levels are whole in each chunk;
the other's are summed into level-sized arrays."""
def __init__(self, est):
self.fam, self.lags = est.family, est.bias["lags"]
dims = est.bias["dims"]
self.o = [(j, est.n_levels[d]) for j, d in enumerate(est.o_fe) if d in dims]
self.jt = est.o_fe.index(est.bias["time"]) if self.lags else None
self.g_sum = self.spec = 0.0
self.o_num, self.o_den = {}, {}
self.sizes, self.gaps, self.short = [], 0, 0
def add(self, h, y, eta, f, starts, codes):
k = h.shape[1]
if not k:
return
v, w, z = self.fam.bias_terms(y, eta)
if f is not None:
v, w, z = v * f, w * f, z * f
Mz = h * z[:, None]
Ng = np.diff(starts)
den = np.add.reduceat(w, starts[:-1])
ok = den > 0
num = np.add.reduceat(Mz, starts[:-1], axis=0)
self.g_sum = self.g_sum + (num[ok] / den[ok, None]).sum(axis=0)
self.sizes.append(Ng)
for j, L in self.o:
if j not in self.o_num:
self.o_num[j], self.o_den[j] = np.zeros((L, k)), np.zeros(L)
c = codes[:, j]
for col in range(k):
self.o_num[j][:, col] += np.bincount(c, weights=Mz[:, col], minlength=L)
self.o_den[j] += np.bincount(c, weights=w, minlength=L)
if not self.lags:
return
M = h * w[:, None]
G, n = len(Ng), len(y)
gid = np.repeat(np.arange(G), Ng)
S = np.zeros((G, k))
for lag in range(1, min(self.lags, n - 1) + 1):
t = np.flatnonzero(gid[lag:] == gid[:-lag]) + lag
if not len(t):
continue
T = Ng[gid[t]]
C = M[t] * (v[t - lag] * T / (T - lag))[:, None]
for col in range(k):
S[:, col] += np.bincount(gid[t], weights=C[:, col], minlength=G)
self.spec = self.spec + (S[ok] / den[ok, None]).sum(axis=0)
same = gid[1:] == gid[:-1]
self.gaps += len(np.unique(gid[1:][same & (np.diff(codes[:, self.jt]) > 1)]))
self.short += int((Ng <= self.lags).sum())
def totals(self):
total = np.asarray(self.g_sum, dtype=float)
for j, _ in self.o:
ok = self.o_den[j] > 0
total = total + (self.o_num[j][ok] / self.o_den[j][ok, None]).sum(axis=0)
return 0.5 * total + np.asarray(self.spec, dtype=float)
def report(self, est):
"""The levels' median sizes, with warnings for very short panels and,
with lags, for gaps and units no longer than L."""
off, _ = est._offsets()
med = {est.g_fe: float(np.median(np.concatenate(self.sizes)))}
for j, L in self.o:
d = est.o_fe[j]
med[d] = float(np.median(est.cnt[off[d]:off[d] + L]))
small = {d: m for d, m in med.items() if m < 5}
if small:
warn("bias_correction: the analytic correction is O(1/T) in the rows per level, "
f"and unreliable for very short panels; median rows per level: {small}",
est.logger)
out = {"median_rows_per_level": med}
if self.lags:
if self.gaps:
warn(f"bias_correction: {self.gaps:,} units have gaps in "
f"{est.bias['time']!r}; lags are counted in observed periods",
est.logger)
if self.short:
warn(f"bias_correction: {self.short:,} units have no more periods than "
f"lags={self.lags}", est.logger)
out.update(units_with_gaps=self.gaps, units_not_longer_than_lags=self.short)
return out