Skip to main content
 首页 » 编程设计

python之使用 statsmodels 线性回归拟合下降趋势(负斜率)

2024年11月01日12cmt

我无法在 python StatsModels 中获得线性回归来拟合具有负斜率的数据系列 - RLM 和 OLS 都不适合我。举一个非常简单的例子,我希望斜率为 -1:

In [706]: ts12 = pandas.TimeSeries(data=[5,4,3,2,1],index=[1,2,3,4,5]) 
In [707]: ts12_h = sm.RLM(ts12.values, ts12.index, M=sm.robust.norms.HuberT()) 
In [708]: ts12_fit = ts12_h.fit() 
In [710]: ts12_fit.fittedvalues 
Out[710]: array([ 0.62321739,  1.24643478,  1.86965217,  2.49286956,  3.11608696]) 
 
In [729]: ts12_fit.params 
Out[729]: array([ 0.62321739]) 
 
In [733]: ts12_ols = sm.OLS(ts12.values, ts12.index) 
In [734]: ts12_ols_fit = ts12_ols.fit() 
In [736]: ts12_ols_fit.fittedvalues 
Out[736]: array([ 0.63636364,  1.27272727,  1.90909091,  2.54545455,  3.18181818]) 

RLM 和 OLS 的拟合参数都给出了 0.6 的斜率......并且拟合值反射(reflect)了上升趋势。来自 scipy 的普通最小二乘回归给出了斜率为 -1 的预期结果:

In [737]: from scipy import stats 
In [738]: stats.linregress([1,2,3,4,5], [5,4,3,2,1]) 
Out[738]: (-1.0, 6.0, -1.0, 1.2004217548761408e-30, 0.0) 

我一定遗漏了一些明显的东西,但通常的方法没有发现任何东西。

请您参考如下方法:

statsmodels 默认不添加常量,除非使用公式界面。

在这种情况下,您迫使回归线通过零。

>>> x = sm.add_constant(ts12.index) 
>>> x 
array([[ 1.,  1.], 
       [ 1.,  2.], 
       [ 1.,  3.], 
       [ 1.,  4.], 
       [ 1.,  5.]]) 
>>> ts12_ols_fit = sm.OLS(ts12.values, ts12.index).fit() 
>>> ts12_ols_fit.params 
array([ 0.63636364]) 
>>> ts12_ols_fit = sm.OLS(ts12.values, x).fit() 
>>> ts12_ols_fit.params 
array([ 6., -1.]) 
>>> ts12_ols_fit.fittedvalues 
array([ 5.,  4.,  3.,  2.,  1.]) 

编辑

OLS 参数估计可以处理完美拟合。

RLM 需要噪声尺度估计。完美拟合时,方差为零,RLM 不起作用。

添加一点噪音 RLM 得到基本相同的结果。

>>> ts12_rlm_fit = sm.RLM(ts12.values+ 1e-4*np.random.randn(5), x).fit() 
>>> print ts12_rlm_fit.summary() 
                    Robust linear Model Regression Results                     
============================================================================== 
Dep. Variable:                      y   No. Observations:                    5 
Model:                            RLM   Df Residuals:                        3 
Method:                          IRLS   Df Model:                            1 
Norm:                          HuberT                                          
Scale Est.:                       mad                                          
Cov Type:                          H1                                          
Date:                Mon, 04 Nov 2013                                          
Time:                        20:38:00                                          
No. Iterations:                    50                                          
============================================================================== 
                 coef    std err          z      P>|z|      [95.0% Conf. Int.] 
------------------------------------------------------------------------------ 
const          5.9999    9.8e-05   6.12e+04      0.000         6.000     6.000 
x1            -1.0000   2.96e-05  -3.38e+04      0.000        -1.000    -1.000 
==============================================================================