.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/market_microstructure/dealers/plot_02_ho_stoll.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_dealers_plot_02_ho_stoll.py: Ho and Stoll: the dealer's inventory problem ============================================ Thomas Ho and Hans Stoll (1981) gave the dealer risk aversion. Holding inventory exposes it to price moves, so a dealer who is long lowers *both* quotes, making a sale to it less attractive and a purchase from it more so, until the inventory returns to zero. With order arrivals :math:`\lambda = \alpha - \beta\delta` on each side, the spread itself, :math:`\alpha/\beta`, does not depend on the inventory: only its position does. .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.market_microstructure import ho_stoll_quotes alpha, beta, sigma = 10.0, 40.0, 0.5 inventory = np.linspace(-30, 30, 61) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Quotes shade against inventory ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 26-38 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 4)) for g, ls in [(0.0, ":"), (0.05, "--"), (0.2, "-")]: b, a = ho_stoll_quotes(100.0, inventory, alpha, beta, g, sigma) ax.plot(inventory, a, "C1" + ls, label=f"ask, $\\gamma$ = {g}") ax.plot(inventory, b, "C0" + ls, label=f"bid, $\\gamma$ = {g}") ax.axhline(100.0, color="0.5", lw=0.8) ax.set_xlabel("dealer inventory") ax.set_ylabel("quote") ax.legend(ncol=3, fontsize="small") fig.tight_layout() .. image-sg:: /api/gallery/market_microstructure/dealers/images/sphx_glr_plot_02_ho_stoll_001.png :alt: plot 02 ho stoll :srcset: /api/gallery/market_microstructure/dealers/images/sphx_glr_plot_02_ho_stoll_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-45 Inventory control in action --------------------------- Each period, buy and sell orders arrive as Poisson counts at the rates the quotes imply. A risk-neutral dealer's inventory wanders like a random walk; the risk-averse dealer's is mean-reverting. .. GENERATED FROM PYTHON SOURCE LINES 45-69 .. code-block:: Python rng = np.random.default_rng(4) n_paths, n_periods = 400, 300 paths = {} for g in (0.0, 0.2): I = np.zeros((n_paths, n_periods + 1)) for t in range(n_periods): b, a = ho_stoll_quotes(100.0, I[:, t], alpha, beta, g, sigma) lam_a = np.clip(alpha - beta * (a - 100.0), 0, None) lam_b = np.clip(alpha - beta * (100.0 - b), 0, None) I[:, t + 1] = I[:, t] - rng.poisson(lam_a) + rng.poisson(lam_b) paths[g] = I print(f"gamma = {g}: inventory sd after {n_periods} periods {I[:, -1].std():.1f}") fig, ax = plt.subplots(figsize=(7, 3.5)) for g, color in [(0.0, "C3"), (0.2, "C0")]: ax.plot(paths[g][:10].T, color=color, lw=0.7, alpha=0.7) ax.plot([], [], color=color, label=f"$\\gamma$ = {g}") ax.set_xlabel("period") ax.set_ylabel("inventory") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/market_microstructure/dealers/images/sphx_glr_plot_02_ho_stoll_002.png :alt: plot 02 ho stoll :srcset: /api/gallery/market_microstructure/dealers/images/sphx_glr_plot_02_ho_stoll_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none gamma = 0.0: inventory sd after 300 periods 53.6 gamma = 0.2: inventory sd after 300 periods 3.5 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.117 seconds) .. _sphx_glr_download_api_gallery_market_microstructure_dealers_plot_02_ho_stoll.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/dealers/plot_02_ho_stoll.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_ho_stoll.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_ho_stoll.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_ho_stoll.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_