Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#831 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
analytics

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
1.1k
派生
203
平均合并
3 小时 26 分钟
30 天内合并 PR
115

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bashtage/linearmodels 的其他 Issue

查看 bashtage/linearmodels 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。