Пример #1
0
 def setup_class(cls):
     endog = mm.m_marietta
     exog = add_constant(mm.CRSP)
     mod = TLinearModel(endog, exog)
     res = mod.fit(method='bfgs', disp=False)
     modf = TLinearModel.from_formula("price ~ CRSP",
                             data={"price":mm.m_marietta, "CRSP":mm.CRSP})
     resf = modf.fit(method='bfgs', disp=False)
     from .results_tmodel import res_t_dfest as res2
     cls.res2 = res2
     cls.res1 = res  # take from module scope temporarily
     cls.resf = resf
"""L1"""

dfx = pd.DataFrame(x, columns = ['x'])
dfy = pd.DataFrame(y, columns = ['y'])
exog = sm.add_constant(dfx['x'])
endog = dfy['y']
dft = pd.DataFrame(x_test, columns = ['test'])

qrmodel = QuantReg(endog, exog)
result = qrmodel.fit(q=0.5)

ypred_qr = np.dot(dft, result.params[1]) + result.params[0] #results.predict(dft)

"""Student-t"""

tmodel = TLinearModel(endog, exog)
results = tmodel.fit(df=0.6)

ypred_t = np.dot(dft, results.params[1]) + results.params[0] #results.predict(dft)

"""Plot"""

plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.yticks(np.arange(ymin, ymax, 1.0))
plt.scatter(x, y, color="none", edgecolor="black")
plt.plot(x_test, y_pred1, '-.', color='black') #Least squares
plt.plot(x_test, y_pred2, '--', color='green') #Huber
plt.plot(x_test, ypred_t, color='red')         #student
plt.plot(x_test, ypred_qr, '--', color='blue') 
plt.legend(["Least squares", "Huber, \u0394 =1", "Student-t, \u03BD =0.6", "Laplace"])