Repository navigation
Conversation
tholden
force-pushed
the
fix-varying-slope-stability
branch
2 times, most recently
from
November 5, 2026 16:38
db03013 to
a0867d5
Compare
tholden
added a commit
to tholden/fixest
that referenced
this pull request
Nov 5, 2026
tholden
force-pushed
the
fix-varying-slope-stability
branch
from
November 26, 2026 16:00
a0867d5 to
139cc8f
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NOTE: This pull request is entirely AI generated. It seems reasonable, but as always with AI, particular care should be taken before merging it.
Summary
Fixes severe numerical instability and divergence in the alternating-projections / Irons–Tuck demeaning solver (
src/demeaning.cpp) when a fixed effect with varying slopes (fe[z]) has clusters where the slope variablezis constant or nearly constant within the cluster.Root Causes
Elimination order in$V - 1$ varying slopes ($\sum_{i \in c} w_i z_i^2$ first, and only checks whether the intercept is redundant at $z_i = z_c \neq 0$ is constant within cluster $c$ and $z_c$ is not binary in decimal floating-point (e.g., $-4.44 \times 10^{-16} \neq 0$ instead of $-4.44 \times 10^{-16}$ , producing spurious coefficients of order $10^{15}$ that blew up to $10^{61}$ / $\text{piv}_{V-1}$ evaluated to exact $v = V - 1$ ) and modeled the cluster constant via $z_c \hat{\beta}_c$ , scaling the raw coefficient by $1 / z_c$ .
FEClass::FEClass(v = 0 .. V - 1):When a fixed effect includes both an intercept and
is_slope_fe_Q[q] == true), the intercept is placed at the last indexV - 1while the slopes are at0 .. V - 2. Eliminating fromv = 0up toV - 1pivots on the slope sum of squaresv = V - 1:$$\text{piv}{V-1} = \sum{i \in c} w_i - \frac{\left(\sum_{i \in c} w_i z_i\right)^2}{\sum_{i \in c} w_i z_i^2} = \frac{\sum_{i \in c} w_i \sum_{i \in c} w_i (z_i - \bar{z}c)^2}{\sum{i \in c} w_i z_i^2}.$$
When
z_c = 0.1with 3 observations in the cluster), floating-point rounding causespivto evaluate to0. Because the singular-pivot check was an exact equalityif(my_system(c, v, v) == 0),compute_fe_coef_1divided byNaNacross alternating projections.Furthermore, even when
0.0, eliminating0 .. V - 1zeroed out the cluster intercept (Unweighted parameter-space inner products in
$\text{coef} = \langle \Delta GX, \Delta^2 X \rangle / |\Delta^2 X|_2^2$ using unweighted Euclidean inner products over the raw coefficient vector $c$ where the slope $z$ has very small within-cluster variance $\text{Var}c(z) = \varepsilon^2 > 0$, a small change in fitted values $\Delta \hat{\mu}{ic} \sim O(1)$ corresponds to a raw slope change $\Delta \beta_c \sim O(1/\varepsilon)$ and intercept change $\Delta \alpha_c = -\bar{z}_c \Delta \beta_c \sim O(\bar{z}_c / \varepsilon)$ . In unweighted parameter space, a single cluster with small $\varepsilon$ receives weight $\propto (1 + \bar{z}_c^2) / \varepsilon^2$ , completely dominating
dm_update_X_IronsTuck:dm_update_X_IronsTuckcomputed the Irons–Tuck step lengthX. For a clustervprodandssqacross the entire dataset and causing Irons–Tuck to extrapolate in an unstable direction and diverge exponentially.Changes
Pivot the FE intercept before slopes (
v = V - 1down to0) with a relative collinearity tolerance:FEClass::FEClass,compute_fe_coef_1, andcompute_fe_coef_2_internal, eliminate each cluster'sv = V - 1down to0(and backward-solve from0up toV - 1). When an FE intercept is present (V - 1), it is pivoted first, reducing the remaining entries to centered within-cluster cross-products $\sum_{i \in c} w_i (z_{iv} - \bar{z}{cv})(z{iu} - \bar{z}_{cu})$. If a slope is constant within clustermy_system(c, v, v) == 0with a scale-invariant relative tolerancetol_piv = max(1e-12 * diag_init[v], 1e-24 * w_c * global_mean_sq[v]), settingmy_system(c, v, v) = 0whenevermy_system(c, v, v) <= tol_piv.is_slope_Qvsis_slope_fe_Qin the allocation ofsum_weights_noVS_Cso thatp_sum_weights_noVS_C[q]is properly populated whenslope_flag_Q[q] < 0(varying slopes without an FE intercept).Evaluate Irons–Tuck inner products in the$G_c / w_c$ prediction-space metric (
FEClass::compute_IronsTuck_inner_products):my_system(c, ., .), compute the Irons–Tuck inner products in the orthogonalized basis scaled by!is_slope), the inner product loop is unchanged.