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 \(\lambda<0\) coherent groups are more persuasive. On a planted-partition hypergraph the spread of opinions decays at different speeds.

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}")
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
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()
linear diffusion (colors: communities)

Total running time of the script: (0 minutes 2.495 seconds)

Gallery generated by Sphinx-Gallery