Skip to content

Fix numerical instability in demeaning with constant or near-constant varying slopes - #690

Open
tholden wants to merge 1 commit into
lrberge:masterfrom
tholden:fix-varying-slope-stability
Open

tholden wants to merge 1 commit into
lrberge:masterfrom
tholden:fix-varying-slope-stability

Conversation

@tholden

@tholden tholden commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

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 variable z is constant or nearly constant within the cluster.

Root Causes

  1. Elimination order in FEClass::FEClass (v = 0 .. V - 1):
    When a fixed effect includes both an intercept and $V - 1$ varying slopes (is_slope_fe_Q[q] == true), the intercept is placed at the last index V - 1 while the slopes are at 0 .. V - 2. Eliminating from v = 0 up to V - 1 pivots on the slope sum of squares $\sum_{i \in c} w_i z_i^2$ first, and only checks whether the intercept is redundant at v = 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_i = z_c \neq 0$ is constant within cluster $c$ and $z_c$ is not binary in decimal floating-point (e.g., z_c = 0.1 with 3 observations in the cluster), floating-point rounding causes piv to evaluate to $-4.44 \times 10^{-16} \neq 0$ instead of 0. Because the singular-pivot check was an exact equality if(my_system(c, v, v) == 0), compute_fe_coef_1 divided by $-4.44 \times 10^{-16}$, producing spurious coefficients of order $10^{15}$ that blew up to $10^{61}$ / NaN across alternating projections.
    Furthermore, even when $\text{piv}_{V-1}$ evaluated to exact 0.0, eliminating 0 .. V - 1 zeroed out the cluster intercept ($v = V - 1$) and modeled the cluster constant via $z_c \hat{\beta}_c$, scaling the raw coefficient by $1 / z_c$.

  2. Unweighted parameter-space inner products in dm_update_X_IronsTuck:
    dm_update_X_IronsTuck computed the Irons–Tuck step length $\text{coef} = \langle \Delta GX, \Delta^2 X \rangle / |\Delta^2 X|_2^2$ using unweighted Euclidean inner products over the raw coefficient vector X. For a cluster $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 vprod and ssq across the entire dataset and causing Irons–Tuck to extrapolate in an unstable direction and diverge exponentially.

Changes

  1. Pivot the FE intercept before slopes (v = V - 1 down to 0) with a relative collinearity tolerance:

    • In FEClass::FEClass, compute_fe_coef_1, and compute_fe_coef_2_internal, eliminate each cluster's $V \times V$ Gram matrix from v = V - 1 down to 0 (and backward-solve from 0 up to V - 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 cluster $c$, the slope coefficient is zeroed out while the cluster intercept absorbs the cluster mean.
    • Replace the exact check my_system(c, v, v) == 0 with a scale-invariant relative tolerance tol_piv = max(1e-12 * diag_init[v], 1e-24 * w_c * global_mean_sq[v]), setting my_system(c, v, v) = 0 whenever my_system(c, v, v) <= tol_piv.
    • Fix is_slope_Q vs is_slope_fe_Q in the allocation of sum_weights_noVS_C so that p_sum_weights_noVS_C[q] is properly populated when slope_flag_Q[q] < 0 (varying slopes without an FE intercept).
  2. Evaluate Irons–Tuck inner products in the $G_c / w_c$ prediction-space metric (FEClass::compute_IronsTuck_inner_products):

    • Using the $U_c^\top D_c U_c$ decomposition already precomputed in my_system(c, ., .), compute the Irons–Tuck inner products in the orthogonalized basis scaled by $d_{cv} / w_c$, where $(U_c \delta)v = \delta_v + \sum{u < v} \text{my_system}(c, u, v) \delta_u$.
    • This makes the Irons–Tuck step length invariant to arbitrary rescalings and shifts of the slope variables and gives every cluster weight proportional to its actual contribution to the fitted values rather than $1 / \text{Var}_c(z)$.
    • When no varying slopes are present (!is_slope), the inner product loop is unchanged.

@tholden
tholden force-pushed the fix-varying-slope-stability branch 2 times, most recently from db03013 to a0867d5 Compare November 5, 2026 16:38
tholden added a commit to tholden/fixest that referenced this pull request Nov 5, 2026
@tholden
tholden force-pushed the fix-varying-slope-stability branch from a0867d5 to 139cc8f Compare November 26, 2026 16:00

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant