Forecasting Realized Volatility with HAR-RV and Machine Learning
Summary
The document outlines a basic approach to forecasting realized variance (RV) from an existing RV series. It constructs daily, weekly, and monthly lagged features: the previous observation and rolling averages over five and twenty-two observations, each shifted to avoid using the current value. A linear regression on these features serves as a HAR-RV model, and a random forest is offered as a machine-learning alternative.
The example uses simulated random data only to illustrate model setup; it reports no forecasting results or comparison. It does not explain how to calculate realized variance from one-minute returns, despite that being part of the question, and it gives no neural network or LSTM implementation. The snippet also omits a train/test split, forecast evaluation, tuning, and safeguards against leakage beyond shifting the features. Its usefulness is therefore as a minimal feature-building example, not as evidence that either model forecasts volatility effectively.
Key ideas
- HAR-RV features can represent daily, weekly, and monthly realized variance patterns.
- Rolling averages should be lagged so the forecast inputs exclude the target observation.
- A linear regression provides a basic HAR-RV specification.
- A random forest can use the same lagged features as a nonlinear alternative.
- The example does not evaluate forecast accuracy or explain intraday realized variance construction.
Tags
Full text
# Forecasting Realized Volatility with Machine Learning
# Forecasting Realized Volatility with Machine Learning
How is the daily realized variance calculated for an intraday one minute data.
How can realized volatility be forecasted using machine learning techniques such as neural network and LSTM. Any detailed sample code for the forecasting technique as well as the code for HAR-RV.
## Answer by Amit Kumar Jha (score 0)
https://quant.stackexchange.com/a/77139
Just try to execute considering you have RV column in your dataset
```
For HAR-RV:
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
# Simulate some data for df['RV']
np.random.seed(0)
df = pd.DataFrame({'RV': np.random.rand(100)})
# Calculate lagged RV features
df['RV_lag_d'] = df['RV'].shift(1)
df['RV_lag_w'] = df['RV'].rolling(window=5).mean().shift(1)
df['RV_lag_m'] = df['RV'].rolling(window=22).mean().shift(1)
# Drop NaN rows
df.dropna(inplace=True)
# Prepare data
X = df[['RV_lag_d', 'RV_lag_w', 'RV_lag_m']]
y = df['RV']
# Fit HAR-RV model
model = LinearRegression()
model.fit(X, y)
# For ML :
from sklearn.ensemble import RandomForestRegressor
# Fit Random Forest model
rf_model = RandomForestRegressor(n_estimators=100)
rf_model.fit(X, y)
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.