Saturday 30 November 2019

quantopian lecture linear regression

start = '2014-01-01'
end = '2015-01-01'
price = get_pricing('SPY', fields='price', start_date=start, end_date=end)

import numpy as np
from statsmodels import regression
import statsmodels.api as sm
import matplotlib.pyplot as plt
import pandas as pd

def linreg(X, Y):
    # Running the linear regression
    X = sm.add_constant(X)
    model = regression.linear_model.OLS(Y, X).fit()
    a = model.params[0]
    b = model.params[1]
    print 'slope: ', b, 'intercept: ', a
    return b, a

x = np.arange(len(price))
print x
y = price.values
slope, intercept = linreg(x, y )

[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35
  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53
  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71
...
 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251]
slope:  0.111633445889 intercept:  176.977343811

y_line = x*slope + intercept
print y_line

[ 176.97734381  177.08897726  177.2006107   177.31224415  177.42387759
  177.53551104  177.64714449  177.75877793  177.87041138  177.98204482
  178.09367827  178.20531172  178.31694516  178.42857861  178.54021205
  178.6518455   178.76347895  178.87511239  178.98674584  179.09837928
  179.21001273  179.32164617  179.43327962  179.54491307  179.65654651
  179.76817996  179.8798134   179.99144685  180.1030803   180.21471374
  180.32634719  180.43798063  180.54961408  180.66124753  180.77288097
  180.88451442  180.99614786  181.10778131  181.21941475  181.3310482
...
  200.42036745  200.53200089  200.64363434  200.75526779  200.86690123
  200.97853468  201.09016812  201.20180157  201.31343501  201.42506846
  201.53670191  201.64833535  201.7599688   201.87160224  201.98323569
  202.09486914  202.20650258  202.31813603  202.42976947  202.54140292
  202.65303637  202.76466981  202.87630326  202.9879367   203.09957015
  203.21120359  203.32283704  203.43447049  203.54610393  203.65773738
  203.76937082  203.88100427  203.99263772  204.10427116  204.21590461
  204.32753805  204.4391715   204.55080495  204.66243839  204.77407184
  204.88570528  204.99733873]
 
line = pd.Series(y_line, index=price.index, name='linear fit')
 
pd.concat([r_b, line], axis = 1).plot()
plt.xlabel('Time')
plt.ylabel('Value');

reference:
https://www.quantopian.com/lectures/linear-regression
https://www.quantopian.com/lectures/regression-model-instability
https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.html

No comments:

Post a Comment