Recommended resources
The freely available resources this lesson will direct you to.
Welcome to the next lesson in our journey through advanced temporal feature engineering. In our last session, we focused on identifying when the underlying dynamics of a time series change, using the PELT algorithm to detect structural breaks. This allowed us to partition a time series into distinct regimes. Now, we will build directly on that idea by learning how to measure and quantify the magnitude of turbulence within those regimes, a concept formally known as volatility.
Volatility is one of the most critical concepts in finance, serving as a primary proxy for risk. Understanding and forecasting it is essential for everything from options pricing to risk management and algorithmic trading. This lesson will equip you with two powerful, complementary approaches to create volatility features for financial time series. First, we'll explore model-based estimates using GARCH models, and second, we'll implement direct measurements using realized volatility calculations.
The Nature of Volatility
At its core, volatility is a statistical measure of the dispersion of returns for a given security or market index. The higher the volatility, the riskier the security. A key empirical observation about financial returns is the phenomenon of volatility clustering: periods of large price changes (high volatility) tend to be followed by more large price changes, and periods of small price changes (low volatility) tend to be followed by more small price changes. Volatility is not constant; it is itself a time-varying process.
This is immediately apparent when we plot the daily volatility of a market index.

This plot perfectly illustrates volatility clustering. Our goal as feature engineers is to capture this dynamic behavior in a numerical feature that a machine learning model can use.
To formalize the concept, let's start with a foundational lecture from MIT OpenCourseWare.
Lecture 19: Volatility Modeling
This lecture on Volatility Modeling provides a clear, academic introduction to the concept.
Please watch the first two minutes (overview) for a formal definition of volatility and how it's typically annualized for comparability. Then, jump to the section on computing volatility from historical prices, which introduces the standard practice of using log returns.
As the video explains, the standard approach involves two key steps:
- Transforming a price series into a return series , typically using log returns: .
- Calculating the standard deviation of these returns over some period.
Now, let's dive into the two main methodologies for creating these volatility features.
Method 1: Model-Based Volatility with GARCH
Instead of just measuring past volatility, what if we could build a model that describes how volatility evolves? This is precisely what the Generalized Autoregressive Conditional Heteroskedasticity (GARCH) model does. The name, while a mouthful, perfectly describes its function:
- Autoregressive (AR): Tomorrow's volatility is related to today's volatility.
- Conditional (C): The volatility is conditional on past information.
- Heteroskedasticity (H): The variance of the series is not constant over time.
The GARCH model captures volatility clustering by defining the variance at time as a function of past variances and past squared returns. The most common and often sufficient variant is the GARCH(1,1) model, shown in the image above:
Let's break this down:
- is the conditional variance for the current period (our volatility forecast).
- is a constant term, representing the long-run average variance.
- is the squared return from the previous period (). This is the ARCH term. The parameter governs our reaction to the previous period's market shock. A large means volatility spikes sharply after a large price move.
- is the variance from the previous period. This is the GARCH term. The parameter governs the persistence of volatility. A large means that shocks to volatility take a long time to die out.
Together, the ARCH and GARCH terms allow the model to generate the persistent, clustering behavior we see in real financial data.
To understand the family of GARCH models and their place in volatility modeling, let's return to the MIT lecture.
Lecture 19: Volatility Modeling
This lecture provides context for where ARCH and GARCH models fit within the broader landscape of volatility modeling.
Watch the segment from time dynamics to understand that ARCH/GARCH models are designed to handle time-dependent volatility. Then, for a more detailed explanation of the model structure, watch the section from ARCH and GARCH models. This part covers the motivation for ARCH, its formulation, and the extension to GARCH(1,1).
Implementing GARCH in Python
Let's turn this theory into a practical feature. We'll use the arch library in Python, a standard tool for this task. The following video provides an excellent, concise walkthrough of the entire process, from data fetching to model evaluation.
Stock Forecasting with GARCH : Stock Trading Basics
This tutorial from the channel ritvikmath is a clear, practical guide to fitting a GARCH model and using it for rolling forecasts.
First, watch the introduction to understand the goal. Then, skip to the implementation details: Data preparation: See how to fetch stock data and convert it to a return series, which is the necessary input for the model. Model fitting: This is the core of the process. Observe how the Partial Autocorrelation Function (PACF) of squared returns is used to get a hint for the model order (p,q). Pay close attention to how the model is iteratively refined by checking the statistical significance of the coefficients in the summary table. Rolling forecast: This section shows how to use the fitted model to generate a time series of one-step-ahead volatility forecasts. The resulting plot, which overlays predicted volatility (orange) on actual returns (blue), is the key output. The orange line in the final plot is our GARCH-based volatility feature.
As a complement to the video, the documentation for the statsforecast library also provides a superb guide to implementing GARCH models, including how to use time-series cross-validation to select the best model order.
This documentation from Nixtla's statsforecast provides a comprehensive guide to GARCH modeling, covering theory, data preparation, implementation, and evaluation.
I recommend reviewing these sections to solidify your understanding: Read the Introduction for context. Study the formal definition of the GARCH(p,q) process. Review the table of Pros and Cons. The sections on calculating returns and squared returns are crucial pre-processing steps. Finally, skim the sections on Implementation and Cross-validation. The idea of using a sliding window to find the best GARCH(p,q) configuration is a robust and important technique.
Method 2: Direct Measurement with Realized Volatility
While GARCH is powerful, it's a parametric model with specific assumptions. A simpler and more direct approach is to calculate realized volatility (also called historical volatility). This method doesn't try to model how volatility evolves; it simply measures what it was over a recent, fixed period.
The calculation is straightforward: it is the standard deviation of returns over a rolling window.

The QuantStart article below provides an excellent explanation of this technique.
Calculating Realised Volatility with Polygon Forex data
This article clearly explains the concept and implementation of realized volatility.
Read the introduction to understand what it is and why it's useful. Then, review the section on Calculating Realised Volatility to see the Python implementation using pandas.rolling().std().
The primary hyperparameter for this method is the window size. A smaller window (e.g., 21 days for monthly volatility) makes the feature more responsive to recent events, while a larger window (e.g., 252 days for annual volatility) produces a smoother, more stable feature. The choice depends on the specific problem and the time horizon of the phenomenon you are trying to predict.
Going Deeper: Advanced Realized Volatility Estimators
Your interest in more complex methods is well-served here. The simple realized volatility calculation, which uses only closing prices, discards a significant amount of information about what happened during the trading day. Advanced estimators incorporate the Open, High, Low, and Close (OHLC) prices to achieve much greater statistical efficiency. This means they can provide a more accurate volatility estimate using the same amount of data.
The MIT lecture introduces several of these advanced estimators.
Lecture 19: Volatility Modeling
Let's revisit the MIT lecture to learn about more sophisticated volatility estimators.
Watch the segment from Garmin class estimator. You don't need to absorb all the mathematical details. The key takeaway is that estimators like Parkinson (using High-Low), Garman-Klass (using OHLC), and the modern preferred method, Yang-Zhang, provide significantly more precise estimates of volatility than simple close-to-close measures. Being aware of these methods distinguishes a novice from an expert.
Volatility as a Feature
Whether you use a GARCH forecast or a realized volatility calculation, the output is a new time series. This time series is your new feature. You can now add it as a column to your dataset and use it as an input for any downstream machine learning model.
However, as with any feature, it's crucial to perform exploratory data analysis (EDA) before using it.
Calculating Realised Volatility with Polygon Forex data
The QuantStart article concludes with an important discussion on analyzing the features you've just created.
Read the section on Exploring the Features to understand the importance of examining the distribution of your new volatility feature. Then, review the section on correlation, which emphasizes the need to check whether your new features are redundant.
Interview Alert: You're asked to design a feature set for a model that predicts short-term, extreme price drops (e.g., >5% in a day). How would you incorporate risk into your features?
A strong answer would demonstrate knowledge of both methods discussed today:
- Acknowledge Volatility: "I'd start by engineering volatility features, as they are a direct proxy for market risk and are known to precede large price movements."
- Propose Both Methods: "I would implement two types of volatility features to capture different aspects of risk. First, a GARCH(1,1) forecast to get a model-based, one-step-ahead prediction of conditional volatility. This feature is inherently forward-looking. Second, I would calculate a short-term realized volatility, for example, over a 10-day rolling window. This provides a direct, non-parametric measure of the current risk environment."
- Show Advanced Knowledge: "For the realized volatility, I would use an advanced estimator like the Yang-Zhang method, which leverages OHLC prices to provide a more statistically efficient estimate than a simple close-to-close standard deviation."
- Mention Interaction: "Finally, I would consider interaction features between the volatility features and other factors, like momentum, to capture state-dependent effects (e.g., high momentum in a high-volatility environment)."
Conclusion
In this lesson, we've moved from detecting when market regimes change to measuring how turbulent they are. You have learned two cornerstone techniques for creating volatility features, a critical input for nearly any quantitative financial model.
Key Takeaways:
- Volatility Clustering is the empirical observation that financial volatility is time-dependent and persistent, making it predictable.
- GARCH models provide a formal statistical framework for modeling and forecasting conditional volatility based on past shocks and past volatility.
- Realized Volatility is a direct, non-parametric measurement of past volatility, typically calculated as the rolling standard deviation of returns.
- Advanced Estimators like Garman-Klass and Yang-Zhang leverage OHLC data to produce more precise volatility estimates than those based only on closing prices.
- Both GARCH forecasts and realized volatility calculations result in a time series that can be used as a powerful feature in downstream machine learning models.
In our previous lessons, we've focused on capturing temporal dynamics like cycles, structural breaks, and now volatility. In our next session, we will approach time series from a completely different angle: the frequency domain. We'll learn how to apply wavelet transforms to decompose a signal into various frequency components, allowing us to create multi-scale features that can capture phenomena occurring over different time horizons simultaneously.