[关闭]
@Channelchan 2017-03-08T11:26:42.000000Z 字数 1218 阅读 25861

StatsModels 一元线性回归分析

未分类


Linear Regression
计算000001的斜率与截距

  1. import numpy as np
  2. from statsmodels import regression
  3. import statsmodels.api as sm
  4. import matplotlib.pyplot as plt
  5. import tushare as ts
  6. import pandas as pd
  7. import math
  1. def linreg(X,Y):
  2. # Running the linear regression
  3. X = sm.add_constant(X)
  4. model = regression.linear_model.OLS(Y, X).fit()
  5. # print(model.params)
  6. a = model.params[0]
  7. b = model.params[1]
  8. X = X[:, 1]
  9. # Return summary of the regression and plot results
  10. X2 = np.linspace(X.min(), X.max(), 100)
  11. Y_hat = X2 * b + a
  12. plt.scatter(X, Y, alpha=0.3) # Plot the raw data
  13. plt.plot(X2, Y_hat, 'r', alpha=0.9) # Add the regression line, colored in red
  14. plt.xlabel('X Value')
  15. plt.ylabel('Y Value')
  16. plt.show()
  17. return model.summary()
  1. asset = ts.get_k_data('000001', start='2016-01-01', end='2016-12-31', ktype='D',autype='qfq')
  2. asset.index = pd.to_datetime(asset['date'], format='%Y-%m-%d')
  3. asset = asset['close']
  4. benchmark = ts.get_hist_data('sz', start='2016-01-01', end='2016-12-31', ktype='D')[::-1]
  5. benchmark = benchmark['close']
  6. new = pd.concat([asset, benchmark],join='inner', axis=1)
  7. new.columns = ['asset', 'benchmark']
  8. # print(new)
  9. # We have to take the percent changes to get to returns
  10. # Get rid of the first (0th) element because it is NAN
  11. r_a = new['asset'].pct_change()[1:]
  12. r_b = new['benchmark'].pct_change()[1:]
  13. linreg(r_b.values, r_a.values)

MA策略与ChannelBreakOut策略的一元回归

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注