Skip to content

BUG: Use dependent groups for positional panel inputs - #871

Merged
bashtage merged 4 commits into
bashtage:mainfrom
AryanRG:fix-positional-panel-groups
Dec 3, 2026
Merged

bashtage merged 4 commits into
bashtage:mainfrom
AryanRG:fix-positional-panel-groups

Conversation

@AryanRG

@AryanRG AryanRG commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Positional arrays can retain generated entity/time groups that disagree with the dependent data. This can reverse FirstDifferenceOLS coefficient signs and misgroup within/between transformations or weights.

Use the dependent index for validated, positionally paired regressors and weights before transformations. Added coverage for ordered/shuffled rows, missing observations, labelled/array inputs and weighted models.

The new regressions produce 33 baseline failures. Final panel tests pass 6,858 cases with 367 skips and 47 expected failures. Independent chronological and weighted least-squares checks pass 627 tests. Ruff passes.

Validation used the pure-Python runtime with slow tests excluded. Native/Cython, wheel, full-repository and additional-platform validation remain pending.

Prepared with AI assistance.

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.69%. Comparing base (d29628d) to head (73b38d4).
⚠️ Report is 45 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #871    +/-   ##
========================================
  Coverage   99.68%   99.69%            
========================================
  Files         201      204     +3     
  Lines       38176    39016   +840     
  Branches     2442     2513    +71     
========================================
+ Hits        38057    38897   +840     
  Misses         51       51            
  Partials       68       68            
Flag Coverage Δ
adder 99.68% <100.00%> (+<0.01%) ⬆️
subtractor 99.68% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bashtage

Copy link
Copy Markdown
Owner

Seem to be some style issues that need to be addressed.

I'm still trying to fully understand this issue and to think about what the correct way forward is. It may be that the right way is to raise some sort of ambiguity error in the case of ambiguous positional arguments.

AryanRG commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

@bashtage pushed the formatting fixes. Black, isort, Ruff and flake8 pass locally. I've left the positional-input behavior unchanged for now and will wait for your direction on the ambiguity error.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow reply. For inputs that are rows (an unlabelled nobs x k array, or a Series/DataFrame without a panel index) this change is right: main paired them with the rows of dependent incorrectly in some models, and here they match the labelled path.

For a grid (an unlabelled k x T x N array for exog, or T x N) I'd like to keep the rule that weights, clusters and other_effects follow since 7.1: the values are placed by time period and entity (_grid_to_rows). When the rows of dependent are sorted by time, this PR pairs a grid exog by stacked row position, so the values land on the wrong observations. PooledOLS is wrong as on main, and BetweenOLS, which main gets right, becomes wrong too. A weights grid in the same call is placed correctly, so exog and weights would follow different rules:

import numpy as np
import pandas as pd

from linearmodels import BetweenOLS, PooledOLS

rng = np.random.default_rng(871)
nent, ntime = 30, 5
grid = rng.standard_normal((2, ntime, nent))  # variables x time x entities
grid[0] += 2 * rng.standard_normal(nent)
y_grid = 1 + 2 * grid[0] - grid[1] + rng.standard_normal((ntime, nent))
index = pd.MultiIndex.from_product(
    [[f"e{i:02d}" for i in range(nent)], pd.date_range("2000", periods=ntime, freq="YS")]
)
y = pd.Series(y_grid.T.ravel(), index=index)
exog = pd.DataFrame({"x0": grid[0].T.ravel(), "x1": grid[1].T.ravel()}, index=index)
y_by_time = y.sort_index(level=1)  # same data, rows sorted by time
for model in (PooledOLS, BetweenOLS):
    expected = model(y, exog).fit().params.to_numpy()
    got = model(y_by_time, grid).fit().params.to_numpy()
    print(model.__name__, np.max(np.abs(got - expected)))

On main this prints 2.09 for PooledOLS and 4e-16 for BetweenOLS; on this branch 2.09 and 2.20.

Please pass a grid-shaped exog (and dependent) through _grid_to_rows, as weights are, so that every model matches the labelled exog, and replace the "shuffled" grid cases with a test where the rows of dependent are sorted by time, like the one above. This also needs a 7.2 note under "Changes that alter results".

@AryanRG

AryanRG commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the example. I reproduced both differences. The change now places an unlabelled grid by time and entity for either exog or dependent, and pairs row inputs in observation order. On your example the maximum coefficient differences are 4.4e-16 for PooledOLS and zero for BetweenOLS. I replaced the shuffled-grid cases with a time-sorted regression across all six estimators and added the 7.2 release note.

@bashtage

bashtage commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Thanks

@bashtage
bashtage merged commit 1610615 into bashtage:main Dec 3, 2026
15 checks passed
bashtage added a commit to echuq/linearmodels that referenced this pull request Dec 3, 2026
Follow-up to bashtage#871, which places an unlabelled grid of exog or dependent
by time period and entity, as the grids of weights, clusters and
other_effects are placed. "Aligning Inputs" in the panel FAQ only
described the grids of the other inputs; it now describes these too,
with a versionchanged note. The 7.2 note of bashtage#871 is rewrapped to the
width of the other notes.
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.

2 participants