.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statphys/random_walk/plot_diffusion_and_clt.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_statphys_random_walk_plot_diffusion_and_clt.py: Diffusion, the Einstein relation, and the central limit theorem ===================================================================== Einstein's 1905 analysis of Brownian motion related a microscopically invisible quantity -- the erratic jostling of a pollen grain by individual water molecules -- to a macroscopically measurable one, the diffusion coefficient :math:`D`, via :math:`\langle r^2(t) \rangle = 2 d D t` in :math:`d` spatial dimensions. This was the first quantitative link between atomic-scale motion and observable matter, and it played a central role in convincing skeptics that atoms were real. This example simulates an ensemble of 500 independent walkers on a 2D lattice, each taking unit steps along a randomly chosen axis at every time step, recovers :math:`D` from their mean squared displacement, and separately shows the central limit theorem in action: however non-Gaussian the individual steps are, the distribution of the walkers' displacement after many steps is Gaussian. .. GENERATED FROM PYTHON SOURCE LINES 18-29 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from physicskit.statphys.chapters.random_walk import RandomWalk from physicskit.statphys.visualizers.random_walk_render import ( plot_clt_histogram, plot_msd, plot_trajectories_2d, ) .. GENERATED FROM PYTHON SOURCE LINES 30-32 Trajectories and the Einstein relation -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-40 .. code-block:: Python walk = RandomWalk(n_walkers=500, n_steps=2000, dim=2, kind="lattice", seed=0) walk.run() fig, axes = plt.subplots(1, 2, figsize=(11, 4.5)) plot_trajectories_2d(walk, ax=axes[0], n_show=30) plot_msd(walk, ax=axes[1]) plt.tight_layout() .. image-sg:: /api/gallery/statphys/random_walk/images/sphx_glr_plot_diffusion_and_clt_001.png :alt: 30 random walk trajectories :srcset: /api/gallery/statphys/random_walk/images/sphx_glr_plot_diffusion_and_clt_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-47 The central limit theorem -------------------------------------------- Even though each lattice-walk step is a discrete jump along a random axis -- about as non-Gaussian a single-step distribution as one can construct -- the distribution of the walker's position after many steps is indistinguishable from a Gaussian. .. GENERATED FROM PYTHON SOURCE LINES 47-52 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 4)) plot_clt_histogram(walk, ax=ax, axis=0, bins=40) ax.set_title("Central limit theorem: final displacement distribution") plt.tight_layout() .. image-sg:: /api/gallery/statphys/random_walk/images/sphx_glr_plot_diffusion_and_clt_002.png :alt: Central limit theorem: final displacement distribution :srcset: /api/gallery/statphys/random_walk/images/sphx_glr_plot_diffusion_and_clt_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-61 The central limit theorem, caught in the act -------------------------------------------------- Rather than jumping straight to the final-time histogram above, the same already-simulated trajectories let the convergence itself be watched directly: after a single step the displacement distribution is just three discrete spikes (+1, -1, or 0 along this axis, from the two other lattice directions), and it visibly smooths into the continuous bell curve predicted by the CLT as more steps accumulate. .. GENERATED FROM PYTHON SOURCE LINES 61-73 .. code-block:: Python snapshot_steps = [1, 5, 50, walk.n_steps] fig, axes = plt.subplots(1, len(snapshot_steps), figsize=(13, 3.2)) for ax, t in zip(axes, snapshot_steps): displacement_t = walk.trajectories[t, :, 0] bins = min(40, len(np.unique(displacement_t)) * 2 + 1) ax.hist(displacement_t, bins=bins, density=True, color="steelblue", alpha=0.7) ax.set_title(f"t = {t}") ax.set_xlabel("x displacement") axes[0].set_ylabel("density") plt.suptitle("Lattice-walk displacement distribution converging to a Gaussian") plt.tight_layout() plt.show() .. image-sg:: /api/gallery/statphys/random_walk/images/sphx_glr_plot_diffusion_and_clt_003.png :alt: Lattice-walk displacement distribution converging to a Gaussian, t = 1, t = 5, t = 50, t = 2000 :srcset: /api/gallery/statphys/random_walk/images/sphx_glr_plot_diffusion_and_clt_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.255 seconds) .. _sphx_glr_download_api_gallery_statphys_random_walk_plot_diffusion_and_clt.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_diffusion_and_clt.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_diffusion_and_clt.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_diffusion_and_clt.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_