Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created February 21, 2020 06:48
Show Gist options
  • Save quantra-go-algo/7966fdba0e973833db9c8cc4a0251218 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/7966fdba0e973833db9c8cc4a0251218 to your computer and use it in GitHub Desktop.
#Populates the time period number in stock under head t
stock['t'] = range (1,len(stock)+1)
#Computes t squared, tXD(t) and n
stock['sqr t']=stock['t']**2
stock['tXD']=stock['t']*stock['Adj Close']
n=len(stock)
#Computes slope and intercept
slope = (n*stock['tXD'].sum() - stock['t'].sum()*stock['Adj Close'].sum())/(n*stock['sqr t'].sum() - (stock['t'].sum())**2)
intercept = (stock['Adj Close'].sum()*stock['sqr t'].sum() - stock['t'].sum()*stock['tXD'].sum())/(n*stock['sqr t'].sum() - (stock['t'].sum())**2)
print ('The slope of the linear trend (b) is: ', slope)
print ('The intercept (a) is: ', intercept)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment