.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_dynamics_learning/plot_35_diffusion_consensus.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 Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_10_dynamics_learning_plot_35_diffusion_consensus.py: Diffusion and group consensus ============================= Linear diffusion with the random-walk Laplacian averages opinions inside every hyperedge. In the nonlinear group consensus model the pull of a group depends on how much its *other* members agree: with :math:`\lambda<0` coherent groups are more persuasive. On a planted-partition hypergraph the spread of opinions decays at different speeds. .. GENERATED FROM PYTHON SOURCE LINES 13-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import hatvis as hv H, truth = hv.models.planted_partition(60, 3, 120, sizes=(3, 4), p_in=0.85, random_state=0) x0 = np.random.default_rng(0).random(H.n_nodes) # [snippet:dynamics] lin = hv.dynamics.diffusion(H, x0, np.linspace(0, 15, 61)) runs = {lam: hv.dynamics.group_consensus(H, x0, t_max=3, dt=0.005, lam=lam) for lam in (-10.0, 0.0, 10.0)} # [/snippet:dynamics] for lam, df in runs.items(): print(f"lambda={lam:+.0f}: spread at t=3 is {df.iloc[-1].std():.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none lambda=-10: spread at t=3 is 0.0043 lambda=+0: spread at t=3 is 0.0051 lambda=+10: spread at t=3 is 0.0072 .. GENERATED FROM PYTHON SOURCE LINES 28-41 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 3.8)) for c in range(3): cols = truth.index[truth == c] axes[0].plot(lin.index, lin[cols], color=hv.plotting.PALETTE[c], lw=0.6, alpha=0.6) axes[0].set_xlabel("time") axes[0].set_ylabel("opinion") axes[0].set_title("linear diffusion (colors: communities)") for k, (lam, df) in enumerate(runs.items()): axes[1].semilogy(df.index, df.std(axis=1), label=rf"$\lambda={lam:+.0f}$", color=hv.plotting.PALETTE[k]) axes[1].set_xlabel("time") axes[1].set_ylabel("standard deviation of opinions") axes[1].legend(frameon=False) plt.tight_layout() .. image-sg:: /auto_examples/10_dynamics_learning/images/sphx_glr_plot_35_diffusion_consensus_001.png :alt: linear diffusion (colors: communities) :srcset: /auto_examples/10_dynamics_learning/images/sphx_glr_plot_35_diffusion_consensus_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.495 seconds) .. _sphx_glr_download_auto_examples_10_dynamics_learning_plot_35_diffusion_consensus.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/hatvis/main?urlpath=lab/tree/notebooks/auto_examples/10_dynamics_learning/plot_35_diffusion_consensus.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_35_diffusion_consensus.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_35_diffusion_consensus.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_35_diffusion_consensus.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_