Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import the datareader
- import pandas_datareader.data as web
- # import the date conversion function
- import datetime
- # initialize the start date
- start = datetime.datetime(2014, 1, 1)
- # initialize the end date
- end = datetime.datetime(2016, 12, 30)
- # get the Alibaba stock data for a given period from Yahoo Finance
- data = web.DataReader("AMZN", 'yahoo',start,end)
- # show the first 5 observations
- data.head()
- # save the data for future usage
- data.to_csv("AMZN_data.csv")
- # import the plotting library
- import matplotlib.pyplot as plt
- %matplotlib inline
- # plot the opening and closing stock prices for 2015 only
- plt.plot(data['Open']['2015'])
- plt.plot(data['Close']['2015'])
- # importing libraries
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- %matplotlib inline
- from statsmodels.tsa.arima_model import ARIMA # ARIMA model
- from statsmodels.tsa.stattools import acf, pacf #ACF and PACF
- data.head()
- # apply logarithmic transformation to the dataset
- log_data = np.log(data)
- # calculate the one-period difference
- log_data_lagged = log_data - log_data.shift()
- # drop the missing values
- log_data_lagged.dropna(inplace=True)
- plt.plot(log_data_lagged['Open']['2015'])
- plt.plot(log_data_lagged['Close']['2015'])
- # Plot ACF for Stock: Open with confidence treshholds
- acf = acf(log_data_lagged['Open']['2015'])
- plt.plot(acf)
- plt.axhline(y=0,linestyle='--',color='gray')
- plt.axhline(y=-1.96/np.sqrt(len(log_data_lagged['Open']['2015'])),linestyle='--',color='gray')
- plt.axhline(y=1.96/np.sqrt(len(log_data_lagged['Open']['2015'])),linestyle='--',color='gray')
- plt.title('Autocorrelation Function')
- # take q = 1
- # Plot PACF for Stock: Open with confidence bounds
- log_pacf = pacf(log_data_lagged['Open']['2015'])
- plt.plot(log_pacf)
- plt.axhline(y=0,linestyle='--',color='gray')
- plt.axhline(y=-1.96/np.sqrt(len(log_data_lagged['Open']['2015'])),linestyle='--',color='gray')
- plt.axhline(y=1.96/np.sqrt(len(log_data_lagged['Open']['2015'])),linestyle='--',color='gray')
- plt.title('Partial Autocorrelation Function')
- # take p = 1
- # ARIMA on Stock: Open
- model = ARIMA(log_data['Open']['2015'], order=(1, 1, 1))
- results_ARIMA = model.fit(disp=-1)
- plt.plot(log_data_lagged['Open']['2015'])
- plt.plot(results_ARIMA.fittedvalues, color='red')
- from statsmodels.tsa.arima_model import ARIMA # ARIMA model
- from statsmodels.tsa.stattools import acf, pacf #ACF and PACF
- # Plot ACF for Stock: Close with confidence treshholds
- acf2 = acf(log_data_lagged['Close']['2015'])
- plt.plot(acf2)
- plt.axhline(y=0,linestyle='--',color='gray')
- plt.axhline(y=-1.96/np.sqrt(len(log_data_lagged['Close']['2015'])),linestyle='--',color='gray')
- plt.axhline(y=1.96/np.sqrt(len(log_data_lagged['Close']['2015'])),linestyle='--',color='gray')
- plt.title('Autocorrelation Function')
- # take q = 2 in case of Stock: Close
- # Plot PACF for Stock: Close with confidence bounds
- log_pacf2 = pacf(log_data_lagged['Close']['2015'])
- plt.plot(log_pacf2)
- plt.axhline(y=0,linestyle='--',color='gray')
- plt.axhline(y=-1.96/np.sqrt(len(log_data_lagged['Close']['2015'])),linestyle='--',color='gray')
- plt.axhline(y=1.96/np.sqrt(len(log_data_lagged['Close']['2015'])),linestyle='--',color='gray')
- plt.title('Partial Autocorrelation Function')
- # take p = 2 in case of Stock: Close
- # ARIMA on Stock: Close
- model2 = ARIMA(log_data['Close']['2015'], order=(2, 1, 2))
- results_ARIMA2 = model.fit(disp=-1)
- plt.plot(log_data_lagged['Close']['2015'])
- plt.plot(results_ARIMA2.fittedvalues, color='red')
- plt.plot(log_data_lagged['Open']['2015'])
- plt.plot(results_ARIMA.fittedvalues, color='red')
- plt.plot(log_data_lagged['Close']['2015'])
- plt.plot(results_ARIMA2.fittedvalues, color='red')
- # save the fittedvalues to a new variables
- predictions_log_lag = results_ARIMA.fittedvalues # stock: open
- predictions_log_lag2 = results_ARIMA2.fittedvalues # stock: close
- # To undo the differencing (shifting) we have to calculate the cumulative sum
- predictions_almost_log = predictions_log_lag.cumsum()
- predictions_almost_log2 = predictions_log_lag2.cumsum()
- # check the type, to be sure it is a Series
- type(predictions_almost_log)# series
- type(predictions_almost_log2)# series
- # Convert this series to DataFrame
- predictions_almost_log = pd.Series.to_frame(predictions_almost_log)
- predictions_almost_log2 = pd.Series.to_frame(predictions_almost_log2)
- # check the type to make sure conversion was correct
- type(predictions_almost_log)# frame now
- type(predictions_almost_log2)# frame now
- predictions_almost_log.columns=["OPEN"]
- predictions_almost_log.head()
- predictions_almost_log2.columns=["CLOSE"]
- predictions_almost_log2.head()
- # add that value to all rows
- predictions_almost_log = predictions_almost_log + log_data['Open']['2015'].ix[0]
- # first 5 observations
- predictions_almost_log.head()
- # add that value to all rows
- predictions_almost_log2 = predictions_almost_log2 + log_data['Close']['2015'].ix[0]
- # first 5 observations
- predictions_almost_log2.head()
- # Concatenate the very first observation and the above dataframe
- predictions_log = pd.concat([log_data['Open']['2015'][0:1],predictions_almost_log])
- predictions_log.head()
- # Concatenate the very first observation and the above dataframe
- predictions_log2 = pd.concat([log_data['Close']['2015'][0:1],predictions_almost_log2])
- predictions_log2.head()
- # transform back
- predictions = np.exp(predictions_log)
- predictions.head()
- # transform back
- predictions2 = np.exp(predictions_log2)
- predictions2.head()
- # plot real data
- plt.plot(data['Open']['2015'])
- # plot predictions in red circles
- plt.plot(predictions,'ro')
- # plot real data
- plt.plot(data['Close']['2015'])
- # plot predictions in green circles
- plt.plot(predictions2,'go')
- plt.plot(data['Open']['2015'])
- plt.plot(predictions,'ro')
- plt.plot(data['Close']['2015'])
- plt.plot(predictions2,'go')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement