Time Series Analysis on Stock Market Forecasting(ARIMA & Prophet)
By: Joseph Azhikannickel
Tags: Auto Arima, Time Series Analysis, Data Science, Stock Market Forecasting, Stock Market Prediction

Photo by joseph on Unsplash

What is Time Series?

Time series is a set of observations or data points taken at specified time usually at equal intervals and it’s used to predict the future values based on the previous observed values. In time series there must be one variable, that is Time. Time series are very frequently plotted via line charts.Time series are used in statistics, stock market forecasting, signal processing, pattern recognition, weather forecasting, earthquake prediction, astronomy, and largely in any domain of applied science and engineering which involves the measurements should be in equal intervals like a day, a week, a month and a year.

Components of Time Series Analysis:

1.Trend

2.Seasonality

3.Irregularity

4.Cyclic

Here it deals with the technical analysis part. The real time data has be taken from ‘Yahoo Finance’ (you can find historical data for various stocks here) and for this project dataset of ‘Amazon Inc’ has been taken. The data is recorded on 1st of every month from 2007 to March 2018. The dataset has 143 observation and 7 attributes.

The profit or loss calculation is usually determined by the closing price of a stock; hence we will consider the closing price as the target variable. Here date is set as the index value.

ARIMA Model:

The ARIMA model is a form of Regression analysis. An ARIMA model can be better understood by looking into its individual components: Auto regression (AR), Integrated (I) and Moving Averages (MA). In AR model, Partial Auto Correlation Function(PACF) graph is used to find P value and in MA model, Auto Correlation Function(ACF) graph to find q value. Integration Function is used to find the d value. ie, the differentiation

What is Stationarity?

Time series is said to be stationary if its statistical properties such as mean, variance remain constant over time. A model that shows stationarity is one that shows there is constancy to the data. Most economic and market data show trends, so the purpose of differencing is to remove any trends or seasonal structures. If not Stationary, It has to perform transformations on the data to make it Stationary. For check stationarity it have:

Rolling Statistics Dickey-Fuller test

Movement of data(Clossing stock):

It shows an Upward trend so, the data is not stationary. Now we go with rolling mean and rolling standard deviation.

Rolling Mean and Standard deviation:

To check stationarity with rolling statistics and Dickey-Fuller test(ADCF) as below:

The above mean value is increasing with the data and in the ADCF test the p-value is greater than 0.05. so, data is not stationary.

Log value of the data:

Here mean is moving along with time but quite far better from the previous one.

Subtract Moving Average from LogScale:

Here we are subtracting the moving average value from the log scale value.

After that we are removing the Nan values(null) from the data.

The data does not show any trend here and the p-value is comparatively less as 0.003 and the critical values and test statistics are almost equal to determine the data is stationary.

Subtract Weighted Average from LogScale:

Here weighted average mean has been calculated using evm() function.

Then the weighted average has subtracted from the log scale value.

P-value shows less than 0.05, and the test statistics and critical values are almost equal to determine stationarity.

Shifting the data:

Here using the shift() function to shifting all the values.

Here it has taken lag of 1, so here shifting values by one; that means the differentiation value will be 1.

From the diagram it says that there is no trend in data so the null hypothesis is rejected and the data is stationary now.

Seasonal trend, Trend Line and Residuals in the data:

It shows the components of time series, trend line shows an upward trend

From the seasonality graph shows the yearly seasonality of the data.

Residuals are the irregularity in the data, that it didn’t have any shape in it.

Auto correlation and Partial Auto-Correlation Functions:

the values has been taken when the line first cuts the 0.

The ACF graph is drawn to determine the q value and the PACF graph is drawn to determine the p value.

From the above graphs it can be see that the value of p is 2 and the value of q is 2 respectively.

Now these values have to be substituted in the ARIMA model to get the predictions.

Plotting ARIMA model:

The ARIMA model captures the movement of data correctly.

Residual Sum Of Squares(RSS) should be less as possible.

The RSS value has been less as compared to AR and MA models.

Here the p value as 2, d value as 2 and q value as 2.

Future Forecasting:

From the above graph it says that the price of Amazon stock price will be increase for the future upto 2021.

Here forecasting the closing stock price for three years from 2019 to 2021.

The confidence level means that the predicted line will not be fall beyond that interval.

Prophet

Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, monthly and weekly seasonality effects. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well. Prophet model is developed by facebook and it is fast, accurate and fully automatic. we have to install fbprophet library in the command prompt to work with prophet.

The input to Prophet is always a dataframe with two columns: ‘ds’ and ‘y’. The ‘ds’ (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp. The ‘y’ column must be numeric, and represents the measurement has to forecast. The ‘predict’ method will assign each row in future a predicted value which it names ‘yhat’.

Forecasted values:

Model.predict() function helps to calculate the future stock price.

Yhat:- is the target variable that means it’s the closing stock price.

Yhat_lower:- is the confidence level where the predicted value which does not fall beyond this lowest limit.

Yhat_upper:- is the confidence level where the predicted value which does not go beyond this highest limit.

Future Forecasting:

Here from the graph it says that the forecasted value shows an upward trend.

Black dots represent the distribution of data from 2007 to 2018.

Thick blue line is the forecasted line up to 2021 and the light shaded line is the confidence level.

Trend, Yearly and Weekly Seasonality:

Trend line shows an upward trend up to 2021.

In weekly seasonality, the closing price has been much more in Wednesday’s, because Sunday is a holiday and avoid holidays.

In monthly wise seasonality it shows during February and October has the highest stock price for Amazon Inc.

End of the blog. Enjoy every moment of learning….

Thank you all..!