Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Two-way clustered covariance of the panel models can have negative variances

Aperta
#831 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
analytics

Direzione di ricerca

Start with the reproducer in the issue: PooledOLS(...).fit(cov_type='clustered', cluster_entity=True, cluster_time=True) yields a negative diagonal in res.cov. Read the clustered covariance code path that fit reaches, where the one-way covariances are summed and the intersection term is subtracted, and where std_errors takes the square root. Done means a maintainer-chosen behaviour (eigenvalue clipping by default or as an option, or a named warning) and a test that covers the small-panel case. The default-versus-option decision is still open, so confirm it before writing code.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

The two-way clustered covariance of the panel models (cluster_entity=True, cluster_time=True, or two cluster variables) is the sum of the two one-way clustered covariances minus the covariance clustered by their intersection (Cameron, Gelbach and Miller, 2011). It is not guaranteed to be positive semi-definite. In small panels a variance can be negative, and std_errors is then NaN, with a RuntimeWarning from np.sqrt:

import numpy as np
import pandas as pd
from linearmodels.panel import PooledOLS

rng = np.random.default_rng(4)
idx = pd.MultiIndex.from_product([range(6), range(5)], names=["entity", "time"])
df = pd.DataFrame({"x": rng.standard_normal(30), "y": rng.standard_normal(30)}, index=idx)
df["const"] = 1.0
res = PooledOLS(df.y, df[["const", "x"]]).fit(
    cov_type="clustered", cluster_entity=True, cluster_time=True
)
print(np.diag(res.cov))  # [-0.005253  0.019529]
print(res.std_errors)    # const is NaN, RuntimeWarning: invalid value encountered in sqrt

Options:

  • Replace the negative eigenvalues of the covariance with zero, as Cameron, Gelbach and Miller (2011) suggest and as Stata's reghdfe and vcemway do. This needs a decision on whether it is the default or an option, and on how to report that it happened.
  • Keep the covariance, and say clearly that a variance is negative (a warning naming the parameters) instead of a numpy RuntimeWarning.

Deferred from 7.1.

Lingua principale
Python
Stelle
1.1k
Fork
203
Merge medio
3h 26m
PR unite (30g)
115

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bashtage/linearmodels

Tutte le issue di bashtage/linearmodels

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.