0% found this document useful (0 votes)
19 views5 pages

Markets

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Markets

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

## Time Series Analysis and Forecasting with Prophet

Time Series Analysis is a statistical technique used to make predictions based on historical data and trend analysis. It is prevalent in
finance and economics because it enables us to forecast trends accurately and make informed decisions. In this tutorial, we will use
Python to explore economic and financial data, perform statistical analysis, and create time series forecasts

We will use different Python libraries like NumPy, Pandas, and Matplotlib for this tutorial. Additionally, we will introduce the yfinance
library, which we will use to download financial data from Yahoo Finance.

Getting Started Before we begin, let’s install the necessary libraries. Open your favorite terminal and run the following commands: !pip
install pandas numpy matplotlib yfinance

Data Collection

To demonstrate Time Series Analysis and Forecasting with Python, we will use Microsoft’s daily adjusted close price as provided by
Yahoo Finance data. Before we download the data, let’s import the required libraries:

import pandas as pd
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt

# Set the style for the visualizations.


plt.style.use('fivethirtyeight')

# Next, let’s define the start and end dates, and the stock symbol for 'VOO' etf.

# Define the stock symbol and download the data.


VOO = yf.download('VOO', start='2010-01-01', end='2024-04-18')

# The yfinance library provides an easy way to download financial data for a specific stock symbol.

[*********************100%%**********************] 1 of 1 completed

## Exploratory Data Analysis

# Now that we have downloaded the data let’s explore it to get a better understanding of its structure
# and characteristics. We can use Pandas to analyze the data.

# Display the top 5 rows of the data.


print(VOO.head())

# Display statistical summary of the data.


print(VOO.describe())

Open High Low Close Adj Close Volume


Date
2010-09-09 102.500000 102.500000 101.139999 101.320000 78.490959 26500
2010-09-10 101.680000 101.860001 101.300003 101.779999 78.847298 8600
2010-09-13 102.959999 103.139999 102.500000 103.059998 79.838921 33750
2010-09-14 102.839996 103.480003 102.379997 103.040001 79.823433 59400
2010-09-15 102.620003 103.379997 102.400002 103.300003 80.024826 9250
Open High Low Close Adj Close \
count 3424.000000 3424.000000 3424.000000 3424.000000 3424.000000
mean 243.974247 245.232296 242.586446 244.008078 223.084106
std 100.331382 100.920933 99.700369 100.349474 106.549114
min 99.139999 101.860001 98.239998 100.339996 78.490959
25% 168.055004 168.709999 167.330002 168.200005 140.231815
50% 223.025002 224.004997 221.930000 223.294998 198.206238
75% 328.595001 330.969994 326.227509 328.477509 311.578506
max 482.619995 483.239990 481.209991 481.350006 480.760010

Volume
count 3.424000e+03
mean 2.629838e+06
std 2.381911e+06
min 8.600000e+03
25% 9.848500e+05
50% 2.152050e+06
75% 3.584025e+06
max 2.466930e+07

The first line of code prints the first 5 rows of the data to the console. It gives us a quick view of the data’s structure- displaying date,
open, high, low, close, and adjusted close prices.

The second line of code provides a statistical summary of the data, showing the count, mean, standard deviation, minimum, maximum,
and quartiles values.

# Next, let’s plot the daily adjusted close price to visualize the data.

# Plot VOO s adjusted close price.


plt.figure(figsize=(12,6))
plt.plot(VOO['Adj Close'], label='Adjusted Close')
plt.title('VOO Adjusted Close Price')
plt.xlabel('Date')
plt.ylabel('Adjusted Close Price ($)')
plt.legend(loc='upper left')
plt.xticks(rotation=45)
plt.show()

Looking at the plot, we can observe that VOO s adjusted close price experienced some dips in the past, particularly during the 2008
Global Financial Crisis. However, the value has since grown considerably, reflecting a positive trend.

# To get a better understanding of the trends, let us consider computing and plotting the moving averages.

# Compute the short-term moving average 'MA20'


VOO['MA20'] = VOO['Adj Close'].rolling(window=20).mean()

# Compute the long-term moving average 'MA50'


VOO['MA50'] = VOO['Adj Close'].rolling(window=50).mean()

# Plot Microsoft's adjusted close price and moving averages.


plt.figure(figsize=(12,6))
plt.plot(VOO['Adj Close'], label='Adjusted Close')
plt.plot(VOO['MA20'], label='MA20')
plt.plot(VOO['MA50'], label='MA50')

plt.title('VOO Adjusted Close Price with Moving Averages')


plt.xlabel('Date')
plt.ylabel('Adjusted Close Price ($)')
plt.xticks(rotation=45)
plt.legend(loc='upper left')
plt.show()
The code above computes the short-term (20-day window) and long-term (50-day window) moving averages and plots them alongside
the daily adjusted close price.

We can see that the moving averages are less erratic and better reflect the overall stock trend. The MA20 trend seems to be very close
to the stock price trend, while the MA50 gives us a general understanding of the stock’s long-term trend.

Time-Series Analysis
Our next step is to conduct a time series analysis to gain insights into the time-based behavior of our data. Autocorrelation Function
(ACF) and Partial Autocorrelation Function (PACF) are used to identify the relationships between consecutive observations and forecast
values. We can use the statsmodels library to compute ACF and PACF.

from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

# Plot autocorrelation and partial autocorrelation functions


fig, ax = plt.subplots(2, figsize=(12,6))
plot_acf(VOO['Adj Close'], lags=20, ax=ax[0])
plot_pacf(VOO['Adj Close'], lags=20, ax=ax[1])
plt.show()

The above code plots the ACF and PACF of Microsoft’s adjusted close price with lags up to 20.

From the ACF plot, we can observe that the autocorrelation values are slowly decreasing. The PACF plot shows significant
autocorrelation only at lag 1, indicating that the first lag or time period has the strongest correlation with the current observation.
Forecasting
Now that we have analyzed the data and understand its structure and trends let us now perform some forecasts using the Prophet library.

# !pip install prophet


from prophet import Prophet

First, we will create a new DataFrame that captures only the necessary columns for our analysis.

# Create a new DataFrame


data = VOO.loc[:, ['Adj Close']]
data.head()

Adj Close

Date

2010-09-09 78.490959

2010-09-10 78.847298

2010-09-13 79.838921

2010-09-14 79.823433

2010-09-15 80.024826

Next, let us rename the column to suit the prophet’s naming convention.

# Rename the column to suit the Prophet's naming convention


data = data.reset_index()
data = data.rename(columns={'Date':'ds', 'Adj Close':'y'})
data.head()

ds y

0 2010-09-09 78.490959

1 2010-09-10 78.847298

2 2010-09-13 79.838921

3 2010-09-14 79.823433

4 2010-09-15 80.024826

The code above renames the necessary columns in the DataFrame to suit the Prophet’s naming conventions. The ds column represents
the date, and the y column represents the values we want to predict.

We will now split the data into training and test sets and use the Prophet library to perform the forecast.

train_data = data[data['ds'] < '2022-02-01']


test_data = data[data['ds'] >= '2022-02-01']

# Create model and fit on training data


model = Prophet()
model.fit(train_data)

# Define a new DataFrame aligned with the forecast horizon


future = test_data[['ds']]
forecast = model.predict(future)

# Plot the forecast


model.plot(forecast)
plt.title('VOO Adjusted Close Price: Actual vs. Forecast')
plt.xlabel('Date')
plt.ylabel('Adjusted Close Price ($)')
plt.show()

00:48:05 - cmdstanpy - INFO - Chain [1] start processing


00:48:07 - cmdstanpy - INFO - Chain [1] done processing
/Users/abdelkarimabdallah/anaconda3/lib/python3.11/site-packages/prophet/plot.py:72: FutureWarning: The behavio
r of DatetimeProperties.to_pydatetime is deprecated, in a future version this will return a Series containing p
ython datetime objects instead of an ndarray. To retain the old behavior, call `np.array` on the result
fcst_t = fcst['ds'].dt.to_pydatetime()
/Users/abdelkarimabdallah/anaconda3/lib/python3.11/site-packages/prophet/plot.py:73: FutureWarning: The behavio
r of DatetimeProperties.to_pydatetime is deprecated, in a future version this will return a Series containing p
ython datetime objects instead of an ndarray. To retain the old behavior, call `np.array` on the result
ax.plot(m.history['ds'].dt.to_pydatetime(), m.history['y'], 'k.',
The code above creates a Prophet model and fits the training data. The future DataFrame is defined with the ds column representing our
forecast dates. We then perform our forecast and save the result in the forecast DataFrame. Finally, we plot the forecast using the
model's plot() function.

We can observe that the model has accurately tracked the overall trend and captured major dips and peaks.

Conclusion
Time-Series Analysis and Forecasting are powerful statistical techniques to gain insights into data from different fields. In this tutorial, we
explored and collected data from Yahoo Finance and performed data analysis, including exploratory data analysis and forecasting, using
Python. We demonstrated various visualization techniques, such as plotting moving averages, ACF, and PACF plots. We also performed
a time-series forecast, which is vital in finance and economics to make informed investment decisions. The techniques covered in this
tutorial are just but a tip of the iceberg, and we invite you to explore further the data analysis and forecasting techniques in Python.
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like