Skip to main content
 首页 » 编程设计

python之Python 中的约束回归

2025年05月04日25insus

我有这个简单的回归模型:

 y = a + b * x + c * z + error 

对参数有约束:

c = b - 1 

SO 上也有类似的问题(例如 Constrained Linear Regression in Python)。但是,约束的类型是 lb <= parameter =< ub .

处理这个特定约束线性回归问题的可用选项是什么?

请您参考如下方法:

这是使用 GLM 完成的方法:

import statsmodels 
import statsmodels.api as sm 
import numpy as np 
 
# Set the link function to identity 
statsmodels.genmod.families.links.identity() 
 
OLS_from_GLM = sm.GLM(y, sm.add_constant(np.column_stack(x, z))) 
 
 '''Setting the restrictions on parameters in the form of (R, q), where R  
 and q are constraints' matrix and constraints' values, respectively. As 
 for the restriction in the aforementioned regression model, i.e.,  
 c = b - 1 or b - c = 1, R = [0, 1, -1] and q = 1.''' 
 
res_OLS_from_GLM = OLS_from_GLM.fit_constrained(([0, 1.0, -1.0], 1)) 
 
print(res_OLS_from_GLM.summary())