Skip to content

Instantly share code, notes, and snippets.

#Shifting the Time Series
ts.shift(2)
from pandas import read_csv
from matplotlib import pyplot
from statsmodels.graphics.tsaplots import plot_acf
series = read_csv ('Series dj$Index’.csv', header=0, index_col=0)
plot_acf(series, lags=20)
pyplot.show()
from pandas import read_csv
from matplotlib import pyplot
from statsmodels.graphics.tsaplots import plot_pacf
series = read_csv ('Series dj$Index’.csv', header=0, index_col=0)
plot_pacf(series, lags=20)
pyplot.show()
#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)
#Computes the forecasted values
stock['forecast'] = intercept + slope*stock['t']
#Computes the error
stock['error'] = stock['Adj Close'] - stock['forecast']
mean_error=stock['error'].mean()
print ('The mean error is: ', mean_error)
import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
import numpy as np
stock = web.DataReader('MRF.BO','yahoo', start = "01-01-2012", end="06-01-2012")
stock = stock.dropna(how=’any’)
We can check the data using the head() function.
stock.head()
stock[‘Adj Close’].plot(grid = True)
stock['ret'] = stock['Adj Close'].pct_change()
stock['ret'].plot(grid=True)
stock['20d'] = stock['Adj Close'].rolling(window=20, center=False).mean()
stock['20d'].plot(grid=True)