语言: 中文
最后更新: 2026-04-17
页面定位: 模型文档
切换: English
语言切换:English
LinearRegression 提供 OLS 估计与可选推断,支持 CPU/GPU 统一接口。当前版本覆盖单目标与多目标回归;多目标下支持参数与推断张量输出,但 summary() 仅支持单目标。
statgpu.linear_model.LinearRegression
最小化残差平方和:
其中 fit_intercept=True 时同时估计截距项 b。
采用 OLS 正规方程/等价线性代数求解;compute_inference=True 时基于残差与设计矩阵计算标准误、统计量与置信区间。
cov_type="nonrobust":经典 OLS 协方差(t 统计口径)cov_type="hc0":White 异方差稳健cov_type="hc1":HC0 + 自由度修正n/(n-k)cov_type="hc2":基于 leverage 的修正cov_type="hc3":更保守的 jackknife 风格修正cov_type="hac":Newey-West(Bartlett kernel)自相关稳健协方差hac_maxlags:仅cov_type="hac"生效;不设时按样本规模自动推断
上述协方差类型在 CPU/GPU 路径均可用。
| 参数 | 默认值 | 说明 |
|---|---|---|
fit_intercept |
True |
是否拟合截距 |
device |
"auto" |
cpu / cuda / auto |
compute_inference |
True |
是否计算推断统计(SE/t/p/CI) |
cov_type |
"nonrobust" |
nonrobust / hc0 / hc1 / hc2 / hc3 / hac |
hac_maxlags |
None |
cov_type="hac" 时的最大滞后阶 |
gpu_memory_cleanup |
False |
fit 后尝试释放 CuPy memory pool |
from statgpu.linear_model import LinearRegression
# CPU: HC1 robust SE
m_cpu = LinearRegression(device="cpu", cov_type="hc1", compute_inference=True)
m_cpu.fit(X, y)
# GPU: HAC robust SE
m_gpu = LinearRegression(
device="cuda",
cov_type="hac",
hac_maxlags=4,
compute_inference=True,
)
m_gpu.fit(X, y)当前文档对应默认 strict 推断路径。LinearRegression 未提供独立 approx 开关;若出现 CPU/GPU 微小差异,主要来源于浮点与线性代数实现差异。
- 拟合返回:
fit(X, y) -> self - 预测与评分:
predict(X)、score(X, y)(R^2) - 主要属性:
coef_,intercept_,nobs,df_resid,aic,bic - 推断属性(
compute_inference=True):_bse,_tvalues,_pvalues,_conf_int - 诊断统计:
r_squared,adj_r_squared,f_statistic - 汇总:
summary()(仅单目标)
多目标 y 为 (n_samples, n_targets) 时:
coef_为(n_targets, n_features),intercept_为(n_targets,)_bse/_tvalues/_pvalues为(n_params, n_targets),_conf_int为(n_params, n_targets, 2)
- GPU 与 CPU 的 p 值轻微不同是否正常?
正常。通常由浮点累计误差和底层线性代数实现差异引起。 cov_type如何选择?
异方差场景优先hc1/hc3;高 leverage 场景可用hc2/hc3;存在时序相关时使用hac并显式设置hac_maxlags。
与 statsmodels.OLS 的对齐测试位于:
dev/tests/test_external_consistency.pytest_linear_estimation_and_inference_match_statsmodelstest_linear_robust_covariance_matches_statsmodelstest_linear_robust_covariance_gpu_matches_statsmodelstest_linear_hac_covariance_matches_statsmodels
- Greene, W. H. (2018). Econometric Analysis (8th ed.). Pearson.
- White, H. (1980). A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity. Econometrica, 48(4), 817-838. https://doi.org/10.2307/1912934
- MacKinnon, J. G., & White, H. (1985). Some heteroskedasticity-consistent covariance matrix estimators with improved finite sample properties. Journal of Econometrics, 29(3), 305-325. https://doi.org/10.1016/0304-4076(85)90158-7
- Newey, W. K., & West, K. D. (1987). A simple, positive semi-definite, heteroskedasticity and autocorrelation consistent covariance matrix. Econometrica, 55(3), 703-708. https://doi.org/10.2307/1913610