# Chapter 41: Asymmetric, Advanced, and Realized Volatility
# Fundamentals of Python for Financial Econometrics - Asymmetric, Advanced, and Realized Volatility
# Dataset: Ceteris Lab teaching sample

# Asymmetric, Advanced, and Realized Volatility
#
# **Opening question:** Why can equally large negative and positive shocks have different volatility consequences, and what can intraday or OHLC data add?

# %% Cell 2
import numpy as np

returns = np.array([0.01, -0.02, 0.005, 0.015])
lam = 0.94
variance = returns.var()
path = []
for r in returns:
    variance = lam * variance + (1-lam) * r**2
    path.append(variance)
print(np.round(np.sqrt(path), 4))

# **Interpretation check:** Interpretation. Recent squared returns receive greater weight, while the recursion never fully forgets earlier variance.

# %% Cell 4
import numpy as np

open_, high, low, close = 100, 105, 98, 103
hl = np.log(high/low)
co = np.log(close/open_)
parkinson = hl**2 / (4*np.log(2))
gk = 0.5*hl**2 - (2*np.log(2)-1)*co**2
print(round(parkinson, 6), round(gk, 6))

# **Interpretation check:** Interpretation. The estimators use the same OHLC day but weight its range and open-to-close movement differently.

# %% Cell 6
import numpy as np
returns = np.array([0.01, -0.02, 0.005, 0.015])
variance = returns.var()
variance = lam * variance + (1-lam) * r**2
path.append(variance)
print(np.round(np.sqrt(path), 4))

# Verified source output
#
# ```text
# [0.0133 0.0138 0.0134 0.0135]
# ```
#
# ```text
# 0.001717 0.002042
# ```
