.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/stochastic_processes/plot_01_brownian_motion.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_probability_stochastic_processes_plot_01_brownian_motion.py: Brownian motion ======================================================== Sample paths of standard Brownian motion spread like :math:`\sqrt{t}`: at time :math:`t`, :math:`W(t) \sim \mathcal N(0, t)`, so about 95% of paths lie within :math:`\pm 2\sqrt t`. The quadratic variation :math:`\sum (\Delta W)^2` equals :math:`t`, the signature of a path that is continuous but nowhere differentiable. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.probability import brownian_motion .. GENERATED FROM PYTHON SOURCE LINES 19-21 Paths and the 2-sigma envelope ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-35 .. code-block:: Python result = brownian_motion(n_paths=2000, n_steps=1000, t_max=1.0, seed=0) t = result.times fig, ax = plt.subplots() ax.plot(t, result.paths[:30].T, lw=0.8) ax.plot(t, 2 * np.sqrt(t), "k--", t, -2 * np.sqrt(t), "k--") ax.set_xlabel("t") ax.set_ylabel("W(t)") ax.set_title(r"Brownian motion with $\pm 2\sqrt{t}$ envelope") inside = np.mean(np.abs(result.paths[:, -1]) <= 2.0) print(f"Var W(1) = {result.paths[:, -1].var():.4f} (theory 1)") print(f"fraction within 2 sd at t = 1: {inside:.4f} (theory 0.9545)") .. image-sg:: /api/gallery/probability/stochastic_processes/images/sphx_glr_plot_01_brownian_motion_001.png :alt: Brownian motion with $\pm 2\sqrt{t}$ envelope :srcset: /api/gallery/probability/stochastic_processes/images/sphx_glr_plot_01_brownian_motion_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Var W(1) = 1.0174 (theory 1) fraction within 2 sd at t = 1: 0.9560 (theory 0.9545) .. GENERATED FROM PYTHON SOURCE LINES 36-38 Quadratic variation ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python fine = brownian_motion(n_paths=1, n_steps=100000, t_max=1.0, seed=1) qv = np.cumsum(np.diff(fine.paths[0]) ** 2) print(f"quadratic variation on [0, 1]: {qv[-1]:.4f} (theory 1)") .. rst-class:: sphx-glr-script-out .. code-block:: none quadratic variation on [0, 1]: 0.9931 (theory 1) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.049 seconds) .. _sphx_glr_download_api_gallery_probability_stochastic_processes_plot_01_brownian_motion.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_brownian_motion.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_brownian_motion.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_brownian_motion.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_