Two-way clustered covariance of the panel models can have negative variances
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
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 内容生成。
描述
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
reghdfeandvcemwaydo. 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
bashtage/linearmodels 的其他 Issue
-
enhancement
难度 5/5 一周以上 新手友好度 15/100
bashtage/linearmodels#832 ·
维护者通常 1 天内回复
-
IV: LIML kappa with an endogenous regressor spanned by the instruments, kappa's unit dependence, scale-dependent instrument rank check可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug
难度 4/5 3-5 天 新手友好度 25/100
bashtage/linearmodels#823 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 18/100
bashtage/linearmodels#817 ·
维护者通常 1 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 48/100
bashtage/linearmodels#780 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 25/100
bashtage/linearmodels#714 · 2 条评论 ·
维护者通常 1 天内回复
查看 bashtage/linearmodels 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
developmentseed/titiler-cmr#278 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 2 天内回复