【问题标题】:Simple linear regression with constraint带约束的简单线性回归
【发布时间】:2018-10-01 10:31:50
【问题描述】:

我开发了一种算法来循环遍历 15 个变量并为每个变量生成一个简单的 OLS。然后算法再循环 11 次以产生相同的 15 个 OLS 回归,但 X 变量的滞后每次增加 1。我选择具有最高 r^2 的自变量,并使用 3,4 或 5 个变量的最佳滞后

Y_t+1 - Y_t = B ( X_t+k - X_t) + e

我的数据集如下所示:

Regression = pd.DataFrame(np.random.randint(low=0, high=10, size=(100, 6)), 
                columns=['Y', 'X1', 'X2', 'X3', 'X4','X5'])

到目前为止我拟合的 OLS 回归使用以下代码:

Y = Regression['Y']
X = Regression[['X1','X2','X3']]

Model = sm.OLS(Y,X).fit()
predictions = Model.predict(X)

Model.summary()

问题在于,使用 OLS,您可以获得负系数(我就是这样做的)。我很感激通过以下方式限制此模型的帮助:

sum(B_i) = 1

B_i >= 0

【问题讨论】:

  • 您是否愿意使用非线性方法,例如 scipy.optimize.curve_fit(),初始参数估计可能来自 scipy.optimize.differential_evolution()?这将允许一个简单的“砖墙”类型的约束,其中为约束之外的参数值返回一个硬编码的大值,因此返回一个大错误。实际上,优化遇到了在离开约束区域时无法克服的“砖墙”。虽然简单,但这种有点粗略的技术可以成为约束参数值的有效方法,并且易于编码和测试。
  • 我试试看
  • 如果有帮助,我可以提供一个例子。
  • 不胜感激,我整天都在阅读有关 QP、套索和其他可能的方法的信息,现在我的大脑很累
  • 如果您想走那么远,可以使用结构方程建模来进行参数约束。不确定是否有使用 OLS 回归的好方法

标签: python linear-regression statsmodels


【解决方案1】:

这很好用,

from scipy.optimize import minimize

# Define the Model
model = lambda b, X: b[0] * X[:,0] + b[1] * X[:,1] + b[2] * X[:,2]

# The objective Function to minimize (least-squares regression)
obj = lambda b, Y, X: np.sum(np.abs(Y-model(b, X))**2)

# Bounds: b[0], b[1], b[2] >= 0
bnds = [(0, None), (0, None), (0, None)]

# Constraint: b[0] + b[1] + b[2] - 1 = 0
cons = [{"type": "eq", "fun": lambda b: b[0]+b[1]+b[2] - 1}]

# Initial guess for b[1], b[2], b[3]:
xinit = np.array([0, 0, 1])

res = minimize(obj, args=(Y, X), x0=xinit, bounds=bnds, constraints=cons)

print(f"b1={res.x[0]}, b2={res.x[1]}, b3={res.x[2]}")

#Save the coefficients for further analysis on goodness of fit

beta1 = res.x[0]

beta2 = res.x[1]

beta3 = res.x[2]

【讨论】:

    【解决方案2】:

    根据 cmets,这是一个使用 scipy 的差分进化模块来确定有界参数估计的示例。该模块在内部使用拉丁超立方体算法来确保对参数空间的彻底搜索,并需要搜索范围,尽管这些范围可能很大。默认情况下,differential_evolution 模块将在内部以使用边界对 curve_fit() 的调用结束 - 这可以禁用 - 并确保最终拟合的参数不受限制,此示例稍后调用 curve_fit 而不通过边界。您可以从打印的结果中看到,对differential_evolution 的调用显示第一个参数以-0.185 为界,而后来调用curve_fit() 的结果并非如此。在您的情况下,您可以将下限设为零,以便参数不是负数,但如果代码导致参数处于或非常接近界限,则这不是最佳的,如本例所示。

    import numpy, scipy, matplotlib
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    from scipy.optimize import differential_evolution
    import warnings
    
    xData = numpy.array([19.1647, 18.0189, 16.9550, 15.7683, 14.7044, 13.6269, 12.6040, 11.4309, 10.2987, 9.23465, 8.18440, 7.89789, 7.62498, 7.36571, 7.01106, 6.71094, 6.46548, 6.27436, 6.16543, 6.05569, 5.91904, 5.78247, 5.53661, 4.85425, 4.29468, 3.74888, 3.16206, 2.58882, 1.93371, 1.52426, 1.14211, 0.719035, 0.377708, 0.0226971, -0.223181, -0.537231, -0.878491, -1.27484, -1.45266, -1.57583, -1.61717])
    yData = numpy.array([0.644557, 0.641059, 0.637555, 0.634059, 0.634135, 0.631825, 0.631899, 0.627209, 0.622516, 0.617818, 0.616103, 0.613736, 0.610175, 0.606613, 0.605445, 0.603676, 0.604887, 0.600127, 0.604909, 0.588207, 0.581056, 0.576292, 0.566761, 0.555472, 0.545367, 0.538842, 0.529336, 0.518635, 0.506747, 0.499018, 0.491885, 0.484754, 0.475230, 0.464514, 0.454387, 0.444861, 0.437128, 0.415076, 0.401363, 0.390034, 0.378698])
    
    
    def func(t, n_0, L, offset): #exponential curve fitting function
        return n_0*numpy.exp(-L*t) + offset
    
    
    # function for genetic algorithm to minimize (sum of squared error)
    def sumOfSquaredError(parameterTuple):
        warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
        val = func(xData, *parameterTuple)
        return numpy.sum((yData - val) ** 2.0)
    
    
    def generate_Initial_Parameters():
        # min and max used for bounds
        maxX = max(xData)
        minX = min(xData)
        maxY = max(yData)
        minY = min(yData)
    
        parameterBounds = []
        parameterBounds.append([-0.185, maxX]) # seach bounds for n_0
        parameterBounds.append([minX, maxX]) # seach bounds for L
        parameterBounds.append([0.0, maxY]) # seach bounds for Offset
    
        # "seed" the numpy random number generator for repeatable results
        result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
        return result.x
    
    # by default, differential_evolution completes by calling
    # curve_fit() using parameter bounds
    geneticParameters = generate_Initial_Parameters()
    print('fit with parameter bounds (note the -0.185)')
    print(geneticParameters)
    print()
    
    # second call to curve_fit made with no bounds for comparison
    fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters)
    
    print('re-fit with no parameter bounds')
    print(fittedParameters)
    print()
    
    modelPredictions = func(xData, *fittedParameters) 
    
    absError = modelPredictions - yData
    
    SE = numpy.square(absError) # squared errors
    MSE = numpy.mean(SE) # mean squared errors
    RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
    
    print()
    print('RMSE:', RMSE)
    print('R-squared:', Rsquared)
    
    print()
    
    
    ##########################################################
    # graphics output section
    def ModelAndScatterPlot(graphWidth, graphHeight):
        f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
        axes = f.add_subplot(111)
    
        # first the raw data as a scatter plot
        axes.plot(xData, yData,  'D')
    
        # create data for the fitted equation plot
        xModel = numpy.linspace(min(xData), max(xData))
        yModel = func(xModel, *fittedParameters)
    
        # now the model as a line plot
        axes.plot(xModel, yModel)
    
        axes.set_xlabel('X Data') # X axis data label
        axes.set_ylabel('Y Data') # Y axis data label
    
        plt.show()
        plt.close('all') # clean up after using pyplot
    
    graphWidth = 800
    graphHeight = 600
    ModelAndScatterPlot(graphWidth, graphHeight)
    

    【讨论】:

      【解决方案3】:

      我不知道您可以轻松地限制系数,我有两种解决方案,

      1- 使用导致负系数的时间序列的倒数 (1/x)。这将要求您首先进行正态回归,然后反转具有负面关系的回归。获取权重并执行 wi/sum(wi)。

      2-您似乎正在处理时间序列,使用对数差异(np.log(ts).diff().dropna()) 作为输入并获取权重。如有必要,将其除以权重之和,然后通过 np.exp(predicted_ts.cumsum()) 恢复您的估计。

      【讨论】:

      • 我也要去试试log差异。但是,它不会严格满足我的第二个约束 sum(beta)=1
      猜你喜欢
      • 1970-01-01
      • 2018-11-18
      • 2012-04-26
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多