.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/volatility/plot_01_parkinson_range.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_time_series_volatility_plot_01_parkinson_range.py: Parkinson's range volatility ============================ A daily close-to-close return uses two prices out of the thousands traded that day. Michael Parkinson (1980) used two more, the high and the low: for Brownian motion with volatility :math:`\sigma`, .. math:: E\bigl[(\ln H_t / L_t)^2\bigr] = 4 \ln 2 \,\sigma^2\,\Delta t, so :math:`(\ln H/L)^2 / (4\ln 2)` is an unbiased variance estimate whose variance is about 5.2 times smaller than the squared return's. One month of ranges is as informative as about five months of closes. .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.constants import TRADING_DAYS_PER_YEAR from financialkit.time_series import close_to_close_variance, parkinson_variance from financialkit.time_series.utils import simulate_ohlc .. GENERATED FROM PYTHON SOURCE LINES 25-29 One year of daily bars ---------------------- Prices follow a geometric Brownian motion with 25% volatility, sampled every minute of a 390-minute session. .. GENERATED FROM PYTHON SOURCE LINES 29-44 .. code-block:: Python sigma = 0.25 ohlc = simulate_ohlc(1000, sigma, steps_per_day=390, rng=1980) pk = parkinson_variance(ohlc.high, ohlc.low) cc = close_to_close_variance(np.r_[ohlc.open[0], ohlc.close]) fig, ax = plt.subplots(figsize=(8, 3)) d = np.arange(60) ax.vlines(d, ohlc.low[:60], ohlc.high[:60], color="0.4") ax.plot(d, ohlc.close[:60], "_", color="C0", ms=6, label="close") ax.set_xlabel("day") ax.set_ylabel("price") ax.set_title("High-low bars carry more information than the closes") ax.legend() .. image-sg:: /api/gallery/time_series/volatility/images/sphx_glr_plot_01_parkinson_range_001.png :alt: High-low bars carry more information than the closes :srcset: /api/gallery/time_series/volatility/images/sphx_glr_plot_01_parkinson_range_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-49 Rolling 21-day volatility estimates ----------------------------------- Both estimators are centred on the true 25%, but the range-based one is far less noisy. .. GENERATED FROM PYTHON SOURCE LINES 49-65 .. code-block:: Python window = 21 kernel = np.ones(window) / window ann = TRADING_DAYS_PER_YEAR fig, ax = plt.subplots(figsize=(8, 3.5)) ax.plot(100 * np.sqrt(ann * np.convolve(cc, kernel, "valid")), lw=1, label="close-to-close") ax.plot(100 * np.sqrt(ann * np.convolve(pk, kernel, "valid")), lw=1, label="Parkinson") ax.axhline(100 * sigma, color="k", ls="--", lw=1) ax.set_xlabel("day") ax.set_ylabel("annualized volatility (%)") ax.legend() print(f"mean annualized vol: close-to-close {100 * np.sqrt(ann * cc.mean()):.1f}%, Parkinson {100 * np.sqrt(ann * pk.mean()):.1f}%") print(f"relative efficiency Var(cc) / Var(Parkinson) = {cc.var() / pk.var():.1f} (theory 5.2)") plt.show() .. image-sg:: /api/gallery/time_series/volatility/images/sphx_glr_plot_01_parkinson_range_002.png :alt: plot 01 parkinson range :srcset: /api/gallery/time_series/volatility/images/sphx_glr_plot_01_parkinson_range_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none mean annualized vol: close-to-close 24.8%, Parkinson 24.1% relative efficiency Var(cc) / Var(Parkinson) = 5.0 (theory 5.2) .. GENERATED FROM PYTHON SOURCE LINES 66-69 With one-minute sampling the observed high and low slightly understate the true extremes, which biases the Parkinson estimate down by a few percent; finer sampling removes the bias. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.059 seconds) .. _sphx_glr_download_api_gallery_time_series_volatility_plot_01_parkinson_range.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/time_series/volatility/plot_01_parkinson_range.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_parkinson_range.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_parkinson_range.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_parkinson_range.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_