.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/market_microstructure/spreads/plot_01_demsetz_spreads.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_market_microstructure_spreads_plot_01_demsetz_spreads.py: Demsetz: the bid-ask spread as the price of immediacy ===================================================== Harold Demsetz (1968) argued that a trader who wants to trade *now* pays the dealer the spread for standing ready, and that the spread shrinks as trading activity grows, because a busy dealer turns over inventory faster. Today the spread is measured three ways: the *quoted* spread on the screen, the *effective* spread actually paid relative to the midquote, and the *realized* spread the liquidity provider keeps once the price has moved; the difference is the *price impact*, lost to better-informed traders. .. GENERATED FROM PYTHON SOURCE LINES 16-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.market_microstructure import effective_spread, price_impact, quoted_spread, realized_spread from financialkit.market_microstructure.visualizers import plot_quotes rng = np.random.default_rng(0) .. GENERATED FROM PYTHON SOURCE LINES 25-31 Quoted, effective and realized spreads -------------------------------------- A dealer quotes 10 cents wide around the efficient price. Trades fill at the quotes or, sometimes, inside them; each trade then moves the efficient price by a cent in its own direction (information) plus noise. .. GENERATED FROM PYTHON SOURCE LINES 31-50 .. code-block:: Python n, half, impact = 2000, 0.05, 0.01 side = rng.choice([-1.0, 1.0], n) m = 100 + np.cumsum(impact * side + 0.01 * rng.standard_normal(n)) mid = np.concatenate([[100.0], m[:-1]]) # the quote before each trade bid, ask = mid - half, mid + half price = mid + side * np.where(rng.random(n) < 0.3, rng.uniform(0, half, n), half) later = np.concatenate([m[5:], np.full(5, m[-1])]) # midquote five trades later for name, x in [ ("quoted", quoted_spread(bid, ask)), ("effective", effective_spread(price, bid, ask, side)), ("realized", realized_spread(price, later, side)), ("price impact", price_impact(bid, ask, later, side)), ]: print(f"{name:13s} {x.mean():.4f}") plot_quotes(price[:40], bid[:40], ask[:40], side=side[:40]) .. image-sg:: /api/gallery/market_microstructure/spreads/images/sphx_glr_plot_01_demsetz_spreads_001.png :alt: plot 01 demsetz spreads :srcset: /api/gallery/market_microstructure/spreads/images/sphx_glr_plot_01_demsetz_spreads_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none quoted 0.1000 effective 0.0843 realized 0.0649 price impact 0.0194 .. GENERATED FROM PYTHON SOURCE LINES 51-57 Demsetz's cross-section ----------------------- Demsetz regressed the spreads of 192 NYSE stocks on their number of transactions and found a strongly negative slope. Here a synthetic cross-section with an elasticity of -0.4 is recovered by a log-log fit. .. GENERATED FROM PYTHON SOURCE LINES 57-71 .. code-block:: Python trades_per_day = 10 ** rng.uniform(1, 4, 150) rel_spread = 0.02 * trades_per_day**-0.4 * np.exp(0.25 * rng.standard_normal(150)) slope, intercept = np.polyfit(np.log(trades_per_day), np.log(rel_spread), 1) fig, ax = plt.subplots(figsize=(6, 4)) ax.loglog(trades_per_day, 1e4 * rel_spread, ".", color="0.5") grid = np.logspace(1, 4, 50) ax.loglog(grid, 1e4 * np.exp(intercept) * grid**slope, "C3", label=f"elasticity {slope:.2f}") ax.set_xlabel("transactions per day") ax.set_ylabel("relative spread (bp)") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/market_microstructure/spreads/images/sphx_glr_plot_01_demsetz_spreads_002.png :alt: plot 01 demsetz spreads :srcset: /api/gallery/market_microstructure/spreads/images/sphx_glr_plot_01_demsetz_spreads_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.071 seconds) .. _sphx_glr_download_api_gallery_market_microstructure_spreads_plot_01_demsetz_spreads.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/market_microstructure/spreads/plot_01_demsetz_spreads.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_demsetz_spreads.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_demsetz_spreads.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_demsetz_spreads.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_