Time-series analysis and stock price forecast modeling: All you need to know

Avikumar Talaviya
11 min readNov 14, 2022

In this blog, you will learn how to analyze time-series data and build forecasting models using the prophet library

Photo by Agê Barros on Unsplash

Introduction

Time-series analysis contains a set of techniques and methods to analyze time-series data and extract meaningful insights from it. On the other hand, time-series forecasting is a predictive analysis approach that predicts future values based on historical data which is collected over a period of time. A time series is sequential data observed at a certain interval. Time-series data is widely collected by financial services companies, governments, and weather forecasting agencies to plan future policies and contingencies. Owing to its diverse applications, there are multiple techniques and tools available to analyze the time-series data and forecast future values.

Among different tools and techniques, we will see some of the important techniques such as comparative analysis of stocks, the growth rate of stocks during a certain period of time, daily return hypothesis testing, and many others. We will then use the open-source tool called Prophet - developed and released by Meta’s core data science team, to develop a forecasting model on time-series data. In the end, we will even understand various use cases of time-series forecasting in real-world applications. So let’s get started!

Table of contents:

  1. What are time-series and time-series forecasting?
  2. A complete guide to time-series analysis
  3. Technical analysis of stock price data
  4. Stock price forecasting using Prophet
  5. Other use-cases of time-series forecasting in real-world applications
  6. Conclusion

1) What are time-series and time-series forecasting?

Time series is a series of observations that are recorded over a period of time. these observations are dependent on the time component which can not be neglected thus we have to analyze this data keeping the time component in mind.

Time series data comprises 4 different components which should be considered while analyzing time series data.

  1. Trend component (Tt): The trend component can be measured as the overall movement of time series over a period of time. it could be upward to downward movement.
  2. Seasonal Component (St): Seasonal component is measured using the seasonality index. It is the repetitive upward or downward movement from the trend that occurs during fixed time intervals. time interval could be monthly, quarterly, or yearly (e.g. Sales of consumer goods spike during festival season)
  3. Cyclical component (Ct): Cyclical component is movement or fluctuation around the trend line at a random interval that happens due to economic changes (e.g. recession-boom period, employment changes, etc.)
  4. Irregular component (It): Irregular component is the random uncorrelated fluctuation in time series data with a mean of zero.

Time-series forecasting

Time series forecasting is perhaps one of the most common types of machine learning techniques used in real-world scenarios. time-series forecasting is a significant part of organizations' processes to forecast future revenue and profits.

Time-series forecasting can be defined as predicting the future value of certain metrics which depends upon the time. time-series forecasting is the most popular and widely used application of predictive analytics.

It falls under the unsupervised learning category but it is also called self-supervised learning or supervised learning technique. time-series data can be much more complex to find patterns, this is because of the irregular and seasonality components of time series that we learned in the previous section.

2) A complete guide to time-series analysis

Time-series analysis is the study of data sets over time. It’s commonly used in finance and business to understand how the price of stock changes over time and how different strategies affect performance.
So let’s explore the different types of time-series analysis techniques and approaches.

  1. to_datetime() method of the pandas library

Time-series data can have multiple columns such as the open, close, high, and a low stock price of the day along with the trade volume of the day. you will need to check missing values in data and datatypes of the DateTime column. In most cases, the DateTime column won’t have the ‘DateTime’ datatype which you can change using the pandas “to_datetime” method. Below is an example of the ‘to_datetime’ function in python.

# example of to_datetime function of pandas
df.loc[:, 'date'] = pd.to_datetime(df.loc[:, 'date'], format='%Y/%m/%d')

2. Closing stock price of stocks and finding the maximum stock price in a given period

We can visualize the closing stock price of your time-series data using the matplotlib library of python and using an ax. annotate() method you can annotate a matplotlib chart with the maximum price during a certain period of time. In the below example, we will see the example of such a closing stock price visualization of the “Apple(AAPL)” ticker during the period of 2013–2018.

fig, ax = plt.subplots(figsize=(10,6), facecolor='#4bd659')
ax.plot(df['date'], df['close'], color='#0f2113')
ax.set_title("Apple stock price", fontsize=20)
ax.set_xlabel("Date", fontsize=15)
ax.set_ylabel("Daily closing stock price", fontsize=15)
ax.annotate(f"All time high price during\nfive year period\nwas ${high}", xy=(datetime, high),xytext=(datetime,high-35),
bbox=dict(boxstyle="round",facecolor='#f5d3bf', edgecolor='#d0d5db'),
arrowprops=dict(facecolor='#f0190a',headlength=25, shrink=0.1))
plt.show()

The output of the above code is as below:

source: https://www.kaggle.com/code/avikumart/timeseries-stock-price-analysis-forecasting

3. Trade volume of Apple stock over five years period.

The quantity of shares of securities that were traded overall in a specific time frame is known as the trading volume. Because it captures the whole activity of a security or market, the trading volume serves as a technical indicator of that stock. let’s look at an example of the trade volume of Apple stock during 2013–2018 and find what was the highest trade volume during the period.

# x and y coords for average trade volume
ave_x = df['date'].mean()
ave_y = df['volume'].mean()
# y coord for max trade vol
max_y = df['volume'].max()
# y coord for min trade vol
min_y = df['volume'].min()

fig, ax = plt.subplots(figsize=(10,6), facecolor='#4bd659')
ax.plot(df['date'], df['volume'], color='#283954')
ax.set_title(f"{Apple stock trade volume", fontsize=20)
ax.set_xlabel("Date", fontsize=15)
ax.set_ylabel("Daily trade volume", fontsize=15)
ax.axhline(y=df['volume'].max(), linestyle='--', lw=2.2, color='green')
ax.axhline(y=df['volume'].min(), linestyle='--',lw=2.2, color='red')
ax.axhline(y=df['volume'].mean(), linestyle='--',lw=2.8, color='yellow')
ax.axvline(x=df[df['volume'] == max_y]['date'].values, ls='--', lw='2.2', color='#0aebff')
ax.annotate(f"Average trade volume {round(df['volume'].mean(),2)}",
xy=(ave_x,ave_y),xytext=(ave_x,ave_y + 10000000),
bbox=dict(boxstyle="round",facecolor='#e8e0ba', edgecolor='#d0d5db')
)
ax.annotate(f"Maximum trade volume {df['volume'].max()}",
xy=(ave_x,max_y),xytext=(ave_x,max_y - 1000000),
bbox=dict(boxstyle="round",facecolor='#e8e0ba', edgecolor='#d0d5db')
)
ax.annotate(f"Minimum trade volume {df['volume'].min()}",
xy=(ave_x,min_y),xytext=(ave_x,min_y - 1000000),
bbox=dict(boxstyle="round",facecolor='#e8e0ba', edgecolor='#d0d5db')
)
plt.show()

The output of the above code is as below:

source: https://www.kaggle.com/code/avikumart/timeseries-stock-price-analysis-forecasting

4. Comparative analysis of stocks

Comparing stocks in the same industry is a crucial component of the fundamental analysis of publicly listed stocks. The simplest method for analyzing and comparing stocks in the same industry is to compare their prices in relation to their earnings per share (EPS), price-to-earnings (P/E), return on equity (ROE), return on capital employed (ROCE), debt-to-equity ratios, and trade volume.

Below is the illustration of a comparative analysis of stocks

Source: https://www.kaggle.com/code/avikumart/timeseries-stock-price-analysis-forecasting

5. How to find the growth of stock during a certain period of time

In the finance and investment industry, stock price growth is a really important metric one needs to measure to find out the how is stock or investment of an individual is growing

Below is the formula to find out the growth of stock prices

source: educba.com

6. Daily return of stock price hypothesis testing

In the stock market, you will often hear that the daily return of any stock price is 0% which means you will get zero return on your investment in one day. So let’s prove the hypothesis by analyzing the 10 stocks and assessing their daily return distribution in this section.

  • H0: Daily return is zero
  • Ha: Daily return is not zero

We will prove this hypothesis as a one-sample t-test as we know the population mean but are not aware of the standard deviation. if the p-value is greater than 0.05 then we can not reject the null hypothesis and if it is less than 0.05 then we have to reject the null hypothesis.

# python's scipy.stats module has ttest_1samp method we allows to prove this hypothesis
result_dict = {}
result = stats.ttest_1samp(df['daily return'], 0)
result_dict['AAPL'] = result
result_dict
-----------------------------[Output]-------------------------------{'AAPL': Ttest_1sampResult(statistic=0.41429430560560787, pvalue=0.6787292124343913)}

In the above example, we can see that the p-value is greater than 0.05 so we can not reject the null hypothesis and it proves that the daily return on stock price is zero.

3) Technical analysis of stock price data

In the finance industry, technical analysis of stocks plays a key role in the investment and purchase decisions of certain stocks. we will look at two such techniques namely the OHLC or candle stick chart and the Moving average.

An open-high-low-close or candle stick chart is a type of chart typically used to represent tendencies in the price of a financial instrument over time. Each vertical line on the chart shows the price range (the highest and lowest prices) over one unit of time, e.g., one day or one month. upper wick and lower wick of each candle stick indicate the either opening price or closing price for that time period. The bars may be shown in different hues depending on whether prices rose or fell in that period.

source: https://www.kaggle.com/code/avikumart/timeseries-stock-price-analysis-forecasting

let’s look at an example using the ‘mplfinance’ package of the matplotlib library.

# using matplotlib/mplfinance toolimport mplfinace as mpl
Fdf = df.copy()
Fdf.set_index('date', inplace=True)
Fdf
mpf.plot(Fdf.iloc[:60,:], type='candle', mav=(5,7), figratio=(9,5))

The output of the above code is as below:

Moving average of stocks

A moving average (MA) is a stock indicator commonly used in technical analysis, used to help smooth out price data by creating a constantly updated average price. A rising moving average indicates that the security is in an uptrend, while a declining moving average indicates a downtrend.

credit: investopedia.com

To calculate the moving average you can use the pandas’ ‘rolling()’ method. below is a code example of the moving average of the closing price.

# calclulate moving averages of 10,50 and 200 days
df['10_d_avg'] = df['close'].rolling(window=10).mean()
df['50_d_avg'] = df['close'].rolling(window=50).mean()
df['200_d_avg'] = df['close'].rolling(window=200).mean()

4) Stock-price forecasting using Prophet

Forecasting is a complicated and ever-changing industry. As new data sets are released, old methods of analysis often need to be updated. In the face of this, one of the most effective ways to remain up-to-date on the latest research and best practices is by using forecasting libraries and tools.

In this section, we’ll discuss what Prophet library is and how it can help you to forecast future time-series values over a period of time.

Forecasting using prophet

Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

source: https://facebook.github.io/prophet/

Facebook’s (now Meta) Prophet is a super useful library to solve forecasting problems. it is tunable, automatic, accurate, and fast to code as well as to get results.

we will look at an example of forecasting time-series data using the prophet library.

# create function to return formatted dataframe for forecating
def df_formatting(df):
df = df.loc[:, ['date','close']]
df.rename(columns={'date':'ds', 'close':'y'}, inplace=True)
return df
# forecasting using prophet library, function that returns the
# forecasting of future and plots

def price_forecasting(df, period):

prophet = Prophet(yearly_seasonality = 'auto')
prophet.fit(df)
future_price = prophet.make_future_dataframe(periods=period)
forecasts = prophet.predict(future_price)
forecast = forecasts[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

# plot the foreasts
fig = prophet.plot(forecasts)
a = add_changepoints_to_plot(fig.gca(), prophet, forecasts)

# plot the components
fig2 = prophet.plot_components(forecasts)

return forecasts
# call the the above function with our dataframe and 365 days of period for forecasting.forecast_aapl = price_forecasting(df, 365)

The output of the above code is as below:

source: https://www.kaggle.com/code/avikumart/timeseries-stock-price-analysis-forecasting

5) Other use-cases of time-series forecasting in real-world applications

  • Forecast product demand

Product demand forecasting is a process of using predictive analysis of historical and past data to predict future demand for a product or service. demand forecasting helps businesses make better supply decisions and manage inventories based on the future demand for products.

  • Economic growth forecasting

Economic growth forecasting is a process of predicting the future conditions of an economy based on a combination of certain indicators such as GDP, Income per capita, etc. Governments use economic forecasting to determine fiscal and monetary policies to plan future operating activities.

  • Weather forecasting

Weather forecasting is an application of scientific and technological processes to predict future conditions of the atmosphere for a given location and time. weather forecasting helps to take necessary decisions on precautions to be taken if it’s going to be extreme weather conditions.

  • Sales/Revenue forecasting

Sales and revenue forecasting is a process of estimating future sales and revenue of an organization based on past data. sales forecasting is used in sales projections and preparing a budget for the forthcoming financial year.

  • Web-traffic forecasting

Web traffic forecasting is an application for predicting website visitors during a certain period of time based on patterns of historical data. web traffic forecasting is used to make decisions on better congestion control and load balancing to avoid unexpected downtime.

6) Conclusion

In conclusion, time-series analysis and forecast modeling is a powerful analytical tool, but it is also one of the most difficult to master. Forecasting is an inexact science, and there are always going to be exceptions to the rule. Even the best forecasts occasionally miss by a few days or weeks, leaving us all feeling slightly less optimistic about the future. in this article we briefly learned about time series analysis methods and forecasting to find insights and forecast the future. here are the key takeaways from this article.

Key takeaways:

  1. Time series is an analysis of data observed over a period of time and time-series forecasting refers to forecasting future values using historical data observed over a period of time.
  2. We learned how to analyze time series using various methods of python’s libraries like pandas, matplotlib, etc.
  3. We learned about forecasting future values using the Prophet library of python.

This article is part of the project on time-series analysis and forecasting. you can find the GitHub repo of the project here

Have any questions or want to reach out to me? you can connect with me on Twitter and LinkedIn

--

--