Python中的约束回归

mat*_*ast 1 python regression

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

 y = a + b * x + c * z + error
Run Code Online (Sandbox Code Playgroud)

对参数有约束:

c = b - 1
Run Code Online (Sandbox Code Playgroud)

在SO上也发布了类似的问题(例如Python中的约束线性回归)。但是,约束的类型为lb <= parameter =< ub

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

mat*_*ast 5

使用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())
Run Code Online (Sandbox Code Playgroud)