.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/market_microstructure/information/plot_01_kyle.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_information_plot_01_kyle.py: Kyle's insider and the price impact lambda ========================================== Albert Kyle (1985) placed an insider who knows the liquidation value :math:`v \sim N(p_0, \sigma_v^2)` in a market with noise traders whose orders :math:`u \sim N(0, \sigma_u^2)` camouflage it. A competitive market maker sees only the total flow :math:`y = x + u` and sets :math:`p = E[v \mid y] = p_0 + \lambda y`. In equilibrium .. math:: \lambda = \frac{\sigma_v}{2\sigma_u}, \qquad x = \frac{\sigma_u}{\sigma_v}(v - p_0), so the market's depth :math:`1/\lambda` rises with noise trading, the insider earns :math:`\sigma_v\sigma_u/2` at the noise traders' expense, and the price reveals exactly half of the insider's information. .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.market_microstructure import kyle_equilibrium, kyle_lambda, simulate_kyle eq = kyle_equilibrium(sigma_v=2.0, sigma_u=1.0, p0=50.0) print(f"lambda {eq.lam}, beta {eq.beta}, insider profit {eq.insider_profit}, Var(v | p) {eq.posterior_variance}") .. rst-class:: sphx-glr-script-out .. code-block:: none lambda 1.0, beta 0.5, insider profit 1.0, Var(v | p) 2.0 .. GENERATED FROM PYTHON SOURCE LINES 30-36 Order flow moves the price -------------------------- Regressing the value on order flow across many auctions recovers the market maker's lambda: the price is the best linear forecast of the value. .. GENERATED FROM PYTHON SOURCE LINES 36-48 .. code-block:: Python v, y, p = simulate_kyle(eq, 3000, rng=7) fit = kyle_lambda(v - eq.p0, y) fig, ax = plt.subplots(figsize=(6, 4)) ax.plot(y, v, ".", ms=2, color="0.6", label="value $v$") ax.plot(np.sort(y), eq.p0 + eq.lam * np.sort(y), "C3", label=f"price, $\\lambda$ = {eq.lam}") ax.set_xlabel("total order flow $y$") ax.set_ylabel("price / value") ax.set_title(f"fitted slope {fit.coef[1]:.3f} ± {fit.std_errors[1]:.3f}") ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/market_microstructure/information/images/sphx_glr_plot_01_kyle_001.png :alt: fitted slope 0.994 ± 0.018 :srcset: /api/gallery/market_microstructure/information/images/sphx_glr_plot_01_kyle_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-56 The insider's best response --------------------------- Given lambda, trading :math:`\beta(v - p_0)` earns :math:`\beta\sigma_v^2(1 - \lambda\beta)` on average, maximised at the equilibrium :math:`\beta = 1/(2\lambda)`: trading harder moves the price against itself. .. GENERATED FROM PYTHON SOURCE LINES 56-68 .. code-block:: Python betas = np.linspace(0.05, 1.0, 20) sim_profit = [np.mean(b * (vv - eq.p0) * (vv - pp)) for b in betas for vv, _, pp in [simulate_kyle(eq, 4000, beta=b, rng=8)]] fig, ax = plt.subplots(figsize=(6, 3.5)) ax.plot(betas, betas * eq.sigma_v**2 * (1 - eq.lam * betas), "k-", label="expected profit") ax.plot(betas, sim_profit, "o", ms=4, color="C0", label="simulated") ax.axvline(eq.beta, color="C3", ls="--", label="equilibrium $\\beta$") ax.set_xlabel(r"insider intensity $\beta$") ax.set_ylabel("insider profit") ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/market_microstructure/information/images/sphx_glr_plot_01_kyle_002.png :alt: plot 01 kyle :srcset: /api/gallery/market_microstructure/information/images/sphx_glr_plot_01_kyle_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-71 More noise, deeper market ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 71-81 .. code-block:: Python sigma_u = np.linspace(0.25, 4, 50) fig, ax = plt.subplots(figsize=(6, 3.5)) ax.plot(sigma_u, [kyle_equilibrium(2.0, s).lam for s in sigma_u], label=r"$\lambda$") ax.plot(sigma_u, [kyle_equilibrium(2.0, s).insider_profit for s in sigma_u], label="insider profit") ax.set_xlabel(r"noise trading $\sigma_u$") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/market_microstructure/information/images/sphx_glr_plot_01_kyle_003.png :alt: plot 01 kyle :srcset: /api/gallery/market_microstructure/information/images/sphx_glr_plot_01_kyle_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.101 seconds) .. _sphx_glr_download_api_gallery_market_microstructure_information_plot_01_kyle.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/information/plot_01_kyle.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_kyle.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_kyle.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_kyle.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_