Markets
Markets
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
# Next, let’s define the start and end dates, and the stock symbol for 'VOO' etf.
# The yfinance library provides an easy way to download financial data for a specific stock symbol.
[*********************100%%**********************] 1 of 1 completed
# 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.
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.
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.
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.
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.
First, we will create a new DataFrame that captures only the necessary columns for our analysis.
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.
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.
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