Forecasting Time Series Data - Stock Price Analysis (2024)

Time Series Forecasting

Forecasting Time Series Data - Stock Price Analysis (3)

In this article, we will be going through the stock prices of a certain company. However, this article does not encourage anyone to trade ONLY based on this forecast. Stock prices are dependent on various factors like supply and demand, company performance, the sentiment of the investors, etc.

What is Time-Series?

Time Series comprises of observations that are captured at regular intervals. Time Series datasets have a strong temporal dependence. It can be used to forecast future observations based on previous ones.

Decomposing the Time Series:

The Time-Series can be divided into several parts as follows:

Trend: The increase or decrease in the value of the data. This can further be divided into the global trend and local trend.

Seasonality: It is the repetitive pattern that is visible in the series. This rise or fall in the values of the data is of fixed frequency. For example, the sales of Christmas trees are always greater during December and less for the rest.

Cyclicity: It is the rise and fall in the value of the data which is not of a fixed frequency. It can be seen as an outcome of economic conditions or other external factors

Noise: It is just some random data that we obtain after extracting the trend and the seasonal components from the time series.

The components of the time series can be an additive or multiplicative version. The multiplicative model is preferred when the magnitude of the seasonal pattern increases or decreases with the increase or decrease in the data values. The additive model is preferred when the magnitude of the seasonal pattern does not correlate with the data values.

Forecasting Time Series Data - Stock Price Analysis (5)
Forecasting Time Series Data - Stock Price Analysis (6)

In the additive version, the magnitude of the seasonal pattern remains to be uncorrelated with the rising trend whereas it is increasing with the increase in the trend of the time series in the multiplicative version.

We can obtain stock prices by creating an API request using the requests package. We can also use the quandl package which is designed to provide financial and economic data.

We will extract the prices of US stocks from alphavantage website by creating an API call. Using the code snippet below, we have extracted the daily stock prices of IBM. You can refer to this link to alter your request needs.

For using quandl package to import data into python, you might need to install quandl package. Enter the command !pip install quandl in your Jupyter notebook and you’re good to go. We have imported the prices of Infosys (BOM500209) and will use these for our further analysis. More documentation on quandl and how to get the best out of it can be found here.

For both the methods, you will need to create an API key which you will use to obtain the data. You can easily get your API by clicking these links: alphavantage and quandl.

Now that we have got the data, we need to plot it to get an overview of how the trend looks like.

Forecasting Time Series Data - Stock Price Analysis (7)

The trend experiences some ups and downs as a stock generally does. It is not seasonal as the seasonal component does not give any clearer picture. The residuals’ variance seems to remain the same except for a few observations.

For ARIMA, time series has to be made stationary for further analysis. For a time series to be stationary, its statistical properties(mean, variance, etc) will be the same throughout the series, irrespective of the time at which you observe them. A stationary time series will have no long-term predictable patterns such as trends or seasonality. Time plots will show the series to roughly have a horizontal trend with the constant variance.

Rolling Statistics

We can plot the rolling mean and standard deviation to check if the statistics show an upward or downward trend. If these statistics vary over time, then the time series is highly likely to be non-stationary.

ADF and KPSS Test

To check the stationarity of the time series, we will also use the ADF (Augmented Dickey-Fuller) test and KPSS (Kwiatkowski–Phillips–Schmidt–Shintests) test. The null hypothesis of the ADF test is that the time series is not stationary whereas that for the KPSS is that it is stationary.

Forecasting Time Series Data - Stock Price Analysis (8)

We can see the rolling mean trending up and down over time as the price of the stock increases and decreases respectively. The p-value of the ADF test turns out to be greater than 0.05 which means we cannot rule out null hypothesis. The p-value of the KPSS test is below 0.05 which means we reject the null hypothesis. All these tests conclude that the time series is not stationary.

Differencing: A new series is constructed by calculating the value at the current time by differencing the value of actual observation of current time and its previous time.

value(t) = actual_observation(t) - actual_observation(t-1)

Transformation: Transforming the values using power, square root, log, etc can help to linearize the data. For example, taking a log of the values can help in obtaining a linear trend to the series with an exponential trend. log(exp(x))=x

Seasonal Differencing: The values of the time series are calculated by differencing between one observation and its previous Nth observation. This can help in removing the trend

value(t) = actual_observation(t) - actual_observation(t-N)

Fitting a model: We can fit a linear regression model to the time series. It will fit a linear trend on the time series. The values for the de-trended time series can be calculated by subtracting the actual observations with the values predicted by the model.

value(t) = actual_observation(t) - predicted(t)

Forecasting Time Series Data - Stock Price Analysis (9)
Forecasting Time Series Data - Stock Price Analysis (10)

After de-trending the time series, ADF and KPSS tests indicate that the time-series is stationary. Partial AutoCorrelation Function (PACF) Plot suggests that correlation exists at certain lags.

Splitting the Dataset

Forecasting Time Series Data - Stock Price Analysis (11)

To forecast the prices, we can use smoothing methods and ARIMA methods. Smoothing methods can be used for non-stationary data whereas ARIMA requires the time series to be stationary. We can also make use of auto_arima, which makes the series stationary and determines the optimal order for the ARIMA model.

For each of the methods, we will perform multiple fits for the optimization of the hyperparameters and use the optimal values for the final model.

Simple Exponential Smoothing

Simple Exponential Smoothing or SES is used when the data does not contain any trend or seasonality. Smoothing Factor for level (α) provides weightage to the influence of the observations. Larger values of α mean that more attention is given to the most recent past observation whereas smaller values indicate that more past observations are being considered for forecasting.

Forecasting Time Series Data - Stock Price Analysis (12)
Forecasting Time Series Data - Stock Price Analysis (13)

Holt’s Exponential Smoothing

Holt’s Exponential Smoothing takes the trend into account for forecasting the time series. It is used when there is a trend in the data and no seasonality. It calculates the Smoothing value (the first equation), which is the same used in SES for forecasting. Trend Coefficient (β) provides weightage to the difference in the consequent smoothing values and the previous trend estimate. The forecasting is a combination of the smoothing value and the trend estimate.

Forecasting Time Series Data - Stock Price Analysis (14)
Forecasting Time Series Data - Stock Price Analysis (15)

Holt-Winters Exponential Smoothing

Holt-Winters Exponential Smoothing takes trend as well as seasonality into account for forecasting the time series. It forecasts the values using equations for calculating the level component, trend component, and the seasonal component in the time series. According to the seasonal variations in the data, either additive or the multiplicative version is used.

Forecasting Time Series Data - Stock Price Analysis (16)
Forecasting Time Series Data - Stock Price Analysis (17)

Auto-Regressive Integrated Moving Average (ARIMA)

ARIMA model is a combination of Auto-Regressive model and Moving Average model along with the Integration of differencing. Auto-Regressive model determines the relationship between an observation and a certain number of lagged observations. The Integrated part is the differencing of the actual observations to make the time series stationary. Moving Average determines the relationship between an observation and residual error obtained by using a moving average model on the lagged observations.

Auto-Regressive (p) -> Number of lag observations in the model. Also called as the lag order.

Integrated (d) -> The number of times the actual observations are differenced for stationarity. Also called as the degree of differencing.

Moving Average (q) -> Size of the moving average window. Also called as the order of moving average.

Forecasting Time Series Data - Stock Price Analysis (18)

To evaluate the performance of the model, we will use Root Mean Squared Error (RMSE) and compare which model performed better than the rest.

Forecasting Time Series Data - Stock Price Analysis (19)

Out of the three models, the better performing model was Holt’s Exponential Smoothing method which obtained least RMSE.

In this article, we saw about time series and its components, fetching data using API call and packages, checking stationarity of the time series, detrending the time series, different types to model the time series, and finally its evaluation.

You can find the notebook for your reference here.

Forecasting Time Series Data - Stock Price Analysis (2024)

FAQs

Forecasting Time Series Data - Stock Price Analysis? ›

`time-sereis forecsting refers to predicting future values from historical data by statical analysis of trends and patterns from certain time-series data. ` it falls under unsupervised learning category but called as a self-supervised learning or supervised learning technique.

What is a time series analysis for forecasting stock prices? ›

In the context of stock prediction, time series analysis seeks to capture the inherent dynamics of stock market data and uncover meaningful patterns that can inform future price movements. Key concepts in time series analysis include: Trend: The long-term movement or direction of a stock's price over time.

What is the best time series forecasting model for stock prediction? ›

There are many algorithms that you can use to try to predict the returns of the stock market, but since the advent of deep learning, a particular type of recurrent neural network called LSTM (long short-term memmory) has been by far the most used for forecasting financial time series.

What is the time series dataset on stock prices? ›

Time series data is a sequence of data points in chronological order that is used by businesses to analyze past data and make future predictions. These data points are a set of observations at specified times and equal intervals, typically with a datetime index and corresponding value.

How to forecast the price of a stock? ›

Some of the common indicators that predict stock prices include Moving Averages, Relative Strength Index (RSI), Bollinger Bands, and MACD (Moving Average Convergence Divergence). These indicators help traders and investors gauge trends, momentum, and potential reversal points in stock prices.

What are the 4 uses of time series analysis? ›

The goal of time series analysis is four-fold: to make valuable predictions, explain what drives observed changes, identify cycles and durations, and monitor for anomalies. Key advantages include the ability to detect trends, quantify relationships, handle non-stationarity, and filter noise.

Is time series analysis useful for trading? ›

Industries like finance, retail, and economics frequently use time series analysis because currency and sales are always changing. Stock market analysis is an excellent example of time series analysis in action, especially with automated trading algorithms.

What is the best model to predict stock price? ›

Linear Regression. Linear regression is used for stock or financial market prediction to forecast the future price of stock regression and uses a model based on one or more attributes, such as closed price, open price, volume, etc., to forecast the stock price.

What is the most successful stock predictor? ›

1. AltIndex – Overall Most Accurate Stock Predictor with Claimed 72% Win Rate. From our research, AltIndex is the most accurate stock predictor to consider today. Unlike other predictor services, AltIndex doesn't rely on manual research or analysis.

Is ARIMA better than LSTM for stock market prediction? ›

The LSTM model provides better results when the data set is large and has fewer Nan values. Whereas, despite providing better accuracy than LSTM, the ARIMA model requires more time in terms of processing and works well when all the attributes of the data set provide legitimate values.

What is the best time series database for the stock market? ›

Time-series Databases
ProductScorePopularity
QuestDB7New
InfluxDB (wp)6IoT/monitoring
Druid (wp)6Click analytics
kdb+ (wp)8Finance
2 more rows

What is the best time series database for trading? ›

Best Time Series Databases At A Glance
  • Best for Small Businesses: InfluxDB.
  • Best for Mid-Market: Prometheus.
  • Best for Enterprise: KX.
  • Highest User Satisfaction: Epsilon3.
  • Best Free Software: InfluxDB.

Is there an algorithm for stock prices? ›

The LSTM algorithm has the ability to store historical information and is widely used in stock price prediction (Heaton et al. 2016). For stock price prediction, LSTM network performance has been greatly appreciated when combined with NLP, which uses news text data as input to predict price trends.

What is the AI tool for stock analysis? ›

Stockopedia is an AI tool for stock market analysis that makes use of a proprietary StockRank machine to assess and rank shares based totally on various monetary metrics. It aims to assist investors in picking out wonderful shares by providing a systematic approach to inventory selection.

How accurate is LSTM stock prediction? ›

This module predicts the average trend of the next three days from day t and achieves 66.32% accuracy. Although they have proved the effectiveness of sentiment analysis by improving prediction performance, they have not utilized the strength of the LSTM model by passing input data of succeeding days.

What is the AI tool for stock market analysis? ›

StockPulse is an AI tool that analyses financial news and other sources of data to predict the stock market. It provides insights on stocks that further help investors to take decisions of the overview of the market and the investment opportunities available.

Why is time series analysis important in forecasting? ›

Time series analysis has become a crucial tool for companies looking to make better decisions based on data. By studying patterns over time, organizations can understand past performance and predict future outcomes in a relevant and actionable way.

What is an example of a time series? ›

A time series is a group of observations on a single entity over time — e.g. the daily closing prices over one year for a single financial security, or a single patient's heart rate measured every minute over a one-hour procedure.

How do you measure time series forecasting? ›

MDAPE = Median(|(Actual — Forecast) / Actual|) * 100%

Actual represents the actual values or observations in the time series. Forecast represents the corresponding forecasted values. MDAPE is expressed as a percentage, and it measures the median percentage difference between the actual and forecasted values.

How do you evaluate a time series forecast? ›

Key metrics for evaluating a time series forecasting model include Mean Absolute Error (MAE) for average absolute errors, Root Mean Squared Error (RMSE) to highlight larger errors, Mean Absolute Percentage Error (MAPE) for error in percentage terms, R-squared (R²) for the variance explained by the model, and Forecast ...

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6705

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.