{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Asymmetric, Advanced, and Realized Volatility\n",
        "\n**Opening question:** Why can equally large negative and positive shocks have different volatility consequences, and what can intraday or OHLC data add?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "returns = np.array([0.01, -0.02, 0.005, 0.015])\n",
        "lam = 0.94\n",
        "variance = returns.var()\n",
        "path = []\n",
        "for r in returns:\n",
        "    variance = lam * variance + (1-lam) * r**2\n",
        "    path.append(variance)\n",
        "print(np.round(np.sqrt(path), 4))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. Recent squared returns receive greater weight, while the recursion never fully forgets earlier variance.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "open_, high, low, close = 100, 105, 98, 103\n",
        "hl = np.log(high/low)\n",
        "co = np.log(close/open_)\n",
        "parkinson = hl**2 / (4*np.log(2))\n",
        "gk = 0.5*hl**2 - (2*np.log(2)-1)*co**2\n",
        "print(round(parkinson, 6), round(gk, 6))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The estimators use the same OHLC day but weight its range and open-to-close movement differently.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "returns = np.array([0.01, -0.02, 0.005, 0.015])\n",
        "variance = returns.var()\n",
        "variance = lam * variance + (1-lam) * r**2\n",
        "path.append(variance)\n",
        "print(np.round(np.sqrt(path), 4))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n[0.0133 0.0138 0.0134 0.0135]\n```\n\n```text\n0.001717 0.002042\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "source_derived": true,
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 41,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
