Sunday, 1 December 2019

quantopian lecture linear regression breakpoint

start = '2003-01-01'
end = '2009-02-01'
pricing = get_pricing('SPY', fields='price', start_date=start, end_date=end)

pricing.plot()
plt.title('SPY Price')
plt.ylabel('Price');

stock rise and fall, break graph in two at price peak
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

breakpoint_index = pricing.idxmax()
print breakpoint_index

2007-10-09 00:00:00+00:00

breakpoint = len(pricing.loc[start:breakpoint_index])
print breakpoint

1201

x1 = np.arange(breakpoint)
x2 = np.arange(len(pricing)- breakpoint)
slope1, intercept1 = linreg(x1, pricing[:breakpoint])
slope2, intercept2 = linreg(x2, pricing[breakpoint:])

slope:  0.0523940950553 intercept:  81.1660883457
slope:  -0.193784089177 intercept:  154.267603882

line1 = pd.Series(x1*slope1 + intercept1, index=pricing.index[:breakpoint], name='line1')
line2 = pd.Series(x2*slope2 + intercept2, index=pricing.index[breakpoint:], name='line2')

pricing.plot()
line1.plot(color='g')
line2.plot(color='r')
plt.title('SPY Price')
plt.ylabel('Price');

reference:
http://chuanshuoge2.blogspot.com/2019/11/quantopian-lecture-linear-regression.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.idxmax.html
https://www.quantopian.com/lectures/regression-model-instability

No comments:

Post a Comment