Documentation

Introduction

disensa simulates how opinions change in a community of agents connected by a network. The same experiment can run with classic numeric rules or with LLM agents that exchange messages, each possibly driven by a different model.

Each agent holds an opinion on a single statement, a number on \([-2, 2]\) from strongly against to strongly in favour. At every step agents interact with their neighbours and update their opinion. In a classic An agent-based model (ABM) simulates a system as many individual agents, each following simple rules. The behaviour of the whole is not specified anywhere: it emerges from the interactions. the update is an equation: an agent moves towards the average opinion of its neighbours (DeGroot), or only of those close enough to its own (bounded confidence). These rules are transparent and fast, but they reduce every interaction to a number.

LLM agents replace the equation with language. Each agent reads the messages its neighbours shared, reasons about them, and replies with a new opinion, a new message and an updated memory.

Two features set disensa apart. The first is model diversity: LLM-agent simulations usually run every agent on the same model (e.g. Wang et al., 2025), so agents differ only in their prompt while the reasoning underneath is identical. Here different agents can run on different models, each bringing its own tendencies and susceptibility to persuasion. The second is individual-level inspection: every reply is saved with its reasoning and memory, so a run can be followed agent by agent, showing not only where opinions end up but why each agent moved.

Both kinds of agents share the same engine, networks and metrics, so a numeric run and an LLM run of the same setup can be compared directly.

Quick start gets a first run going and How a run works describes the loop. The sections after that cover each part of a run in turn: the network, the numeric and LLM agents, the initial opinions, the metrics, and the files a run produces.

Quick start

disensa runs on your own computer. Numeric runs need nothing else. LLM runs also need Ollama, a free program that runs open-weight language models locally, so no account, API key or payment is involved.

  1. Install disensa (Python 3.11 or newer): pip install disensa, or uv tool install disensa.
  2. Install Ollama from ollama.com/download. Nothing else to set up: disensa starts Ollama when it needs it and stops it again when it closes.
  3. Open the lab with disensa app. It opens in the browser at http://127.0.0.1:8765.
  4. Get a model. In the Topic and models step of a new LLM run, download one of the suggested models with one click, or see Local models below to use the terminal.
  5. Run. Go through the remaining steps and start the run. It appears under Previous runs and can be followed step by step while it goes.

Local models

Any model from the Ollama library can drive agents. Download it once from a terminal, then check that it answers:

ollama pull llama3.2:3b
disensa ping ollama_chat/llama3.2:3b

Installed models appear in the lab’s model list. In a config file a model is written ollama_chat/<name>, e.g. ollama_chat/qwen3:4b (see Models). A model has to fit in memory while it runs, and its download size is a rough guide to how much it needs: models of 3–4B parameters (2–3 GB) run comfortably on a laptop with 16 GB, larger ones need more memory or a GPU.

Without the lab, a run can be started from a config file: disensa run configs/quick.yaml. This is also how disensa runs on a server, such as a university computing cluster: install disensa and Ollama there, run the configs, and copy the run folders back into runs/ to open them in the lab.

How a run works

A run is one simulation: a group of agents, linked by a network, each holding an opinion on the same statement. Time moves in rounds called steps. At every step each agent looks at what its neighbours think and updates its own opinion. Everything about a run is set in a config file: how many agents there are, how they are linked, how many steps to run, and what kind of agents to use.

Every run goes through two phases. The setup builds the network and gives every agent a starting opinion. The loop then runs one step after another, as many as the config asks for (steps, default 10), and saves the opinions after each one.

Agents come in two kinds, and each run uses one of them. Numeric agents update their opinion with a mathematical rule, such as moving towards the average of their neighbours. LLM agents each ask a language model: they read messages from their neighbours and decide on a new opinion. The kind is chosen in the config (dynamics.type), and each kind brings its own settings: a rule and its parameters for numeric agents, the statement and the models for LLM agents. The kind only changes how an agent updates its opinion inside a step. The network, the starting opinions, the loop and what gets saved work the same way for both, so runs of the two kinds can be compared directly.

Configthe settings of the run
Networkthe agents and who is linked to whom
Initial opinionsone per agent, from −2 to +2
Starting pointsaved as step 0, before any update

↻ repeat for every step (1, 2, 3, …)

Look aroundeach agent sees its neighbours’ opinions from the previous step
Updateeach agent picks a new opinion, with a rule or a language model
Savethe new opinions and the metrics
Only the highlighted box differs between numeric and LLM agents. Everything else is the same for both.

Setup

  1. Read the config. The file is checked before anything runs: a missing or out-of-range parameter stops the run with an error. The seed fixes every random draw, so the same config always gives the same network and the same starting opinions.
  2. Build the network. Agents are its nodes, numbered \(0\) to \(N-1\), and an agent only ever interacts with its neighbours. The network is redrawn until it is connected. See Network topologies.
  3. Draw the initial opinions, one per agent. Together they are written \(x(0)\), the opinions at step 0; in general \(x(t)\) are the opinions after step \(t\). See Initial opinions.
  4. Prepare the agents. Numeric agents need no further setup. With LLM agents, two more things happen. First, every agent is given a model: each model gets the share of agents set by its weight, placed at random on the network. Then every agent writes a first message stating its initial opinion. These are the messages the others will read at step 1.

One step

At step \(t\), every agent computes its new opinion from the state at step \(t-1\): its own opinion and what its neighbours held or said. Updates are Synchronous update: every agent works from the same snapshot, the state at step \(t-1\), and all new opinions replace the old ones at once. In an asynchronous update agents would go one at a time, each seeing the changes made before it, so the order in which they go would matter.: no agent sees another’s step-\(t\) opinion until the step is over, so the order in which agents are processed never matters. How an agent turns what it sees into a new opinion depends on its kind:

Numeric agents

Every agent applies the same equation to its own opinion and its neighbours’ opinions at \(t-1\). The whole step is one matrix operation on the opinion vector, with no randomness, so it takes no time. The three rules are in Numeric algorithms.

LLM agents

Every agent reads the messages of a few neighbours, reasons about them with its model, and replies with a new opinion, a new message and an updated memory. One step is one model call per agent. How this works is explained in LLM agents.

Then, for both kinds of agents, the step ends the same way. Opinions are kept on the scale from \(-2\) to \(+2\): if a language model replies with a value outside it, say \(3\), it is set to the nearest end, \(+2\). Then the metrics are computed, and the step is written to disk. A run with steps: 10 records 11 states: step 0 and ten updates.

At the end

Each run gets its own folder, runs/<timestamp>_<name>_s<seed>/, filled step by step while the run goes: if a run is stopped, every finished step is kept. When the loop is done the run is marked as finished, with its duration and, for LLM runs, the number of model calls. What each file holds is in Run files; how to follow a run in the lab, agent by agent, is in Reading a run.

Reproducibility. A numeric run is fully determined by its config. An LLM run is too, once its replies are cached: every reply is stored on disk under its exact request, so running the same config again replays the same replies instead of calling the models, and the run comes out identical.

Network topologies

Each agent is a node in the network, with its own independent state. In an LLM run, different nodes can be driven by different models (e.g. Llama, Qwen, Gemma); see LLM agents for how models are assigned to nodes. A link between two nodes means those agents interact. Networks are undirected and unweighted: if \(i\) is linked to \(j\), then \(j\) is linked to \(i\), and every link counts the same. Three classic models are available to generate the network:

Random (Erdős–Rényi)

Every pair of nodes is linked with the same probability \(p\). Because links are placed purely at random, clustering is low and there are no hubs. Clustering means an agent's neighbours are also linked to each other, forming triangles: tight groups where everyone knows everyone. Hubs are single agents with far more links than average. The two are independent: a network can have one without the other.

Small-world (Watts–Strogatz)

A ring where each agent links to its \(k\) nearest neighbours, and a share \(\beta\) of the links are rewired at random. Neighbours share neighbours, so clustering is high, while the rewired links act as shortcuts that keep paths short. Tight local groups, no hubs.

Scale-free (Barabási–Albert)

A few hubs hold many links while most agents have only a few: An agent's degree \(k_i\) is its number of links, i.e. how many neighbours it has. The degree distribution \(p_k\) is the share of agents with exactly \(k\) links. follow a \(p_k \propto k^{-\gamma}\): the share of agents with \(k\) links falls off slowly, as a power of \(k\), not exponentially as in a random network. Highly connected agents stay rare but not vanishingly so, which is why hubs appear. Here \(\gamma = 3\)., with no typical value. Hubs arise from The better connected an agent is, the more likely it is to gain new links, so the rich get richer. An agent's chance of receiving a link is proportional to its degree \(k_i\).. Clustering is low; \(m\) sets the density (average degree \(2m\)).

Each model isolates one structural feature while keeping paths short, so they can be compared one feature at a time:

ClusteringHubsShort paths
RandomLowNoYes
Small-worldHighNoYes
Scale-freeLowYesYes (even shorter)

Describing a network

A network has \(N\) nodes (here, agents) and \(L\) links. It is stored as the adjacency matrix \(A\), with \(A_{ij} = 1\) if \(i\) and \(j\) are linked and \(0\) otherwise; for an undirected network \(A\) is symmetric.

QuantityDefinitionMeaning
Degree \(k_i\)\(k_i = \sum_j A_{ij}\)Number of neighbours of \(i\).
Average degree \(\langle k \rangle\)\(\langle k \rangle = 2L / N\)Average number of neighbours per agent. The factor 2 is there because each link counts toward the degree of both its ends.
Degree distribution \(p_k\)\(p_k = N_k / N\)Share of nodes with exactly \(k\) links. The main property separating the three models.
Distance \(d_{ij}\)links on the shortest path from \(i\) to \(j\)Averaged over all pairs it gives the average path length \(\langle d \rangle\); the largest is called the diameter.
Clustering coefficient \(C_i\)\(C_i = \dfrac{2L_i}{k_i(k_i - 1)}\)\(L_i\) is the number of links among \(i\)'s neighbours: the share of \(i\)'s neighbours that are also linked to each other. Averaged over nodes it gives \(\langle C \rangle\).

A network is connected when there is a path between every pair of nodes; otherwise it splits into components. Real networks are sparse: \(L\) is far below the maximum possible number of links, which for an undirected network is \(N(N-1)/2\), every pair linked once.

Random (Erdős–Rényi)

Every pair of the \(N\) nodes is linked independently with probability \(p\).

\[ \langle L \rangle = p\,\frac{N(N-1)}{2} \qquad \langle k \rangle = p\,(N-1) \]

Degrees. The degree distribution is binomial, and for sparse networks (\(\langle k \rangle \ll N\)) it is well approximated by a Poisson distribution, narrowly peaked around \(\langle k \rangle\). High degrees are exponentially rare, so there are no hubs: most agents have about the same number of neighbours.

\[ p_k = e^{-\langle k \rangle} \frac{\langle k \rangle^{k}}{k!} \]

Connectedness. As \(\langle k \rangle\) grows the network goes through distinct regimes: below \(\langle k \rangle = 1\) it is a set of small fragments; above \(1\) a giant component appears; around \(\langle k \rangle = \ln N\) the whole network becomes connected. The transition is sharp only for very large \(N\): with 50 agents it is gradual (see Choosing \(p\) below).

Small world. Distances are short and grow only logarithmically with size:

\[ \langle d \rangle \approx \frac{\ln N}{\ln \langle k \rangle} \]

Clustering. Two neighbours of a node are linked with the same probability \(p\) as any other pair, so clustering is low, independent of degree, and shrinks as the network grows:

\[ \langle C \rangle = p = \frac{\langle k \rangle}{N-1} \approx \frac{\langle k \rangle}{N} \]

Real networks have far more clustering and far broader degree distributions than this model predicts. It is used as the null model: what a network looks like when links are placed purely at random.

Choosing \(p\). The network becomes connected around \(\langle k \rangle = \ln N\). With 50 agents \(\ln 50 \approx 3.9\), so \(p \approx 3.9 / 49 \approx 0.08\). That is the threshold, not a safe value: at \(p = 0.08\) only about 40% of draws are connected, and the rest get redrawn (see below). With \(p \approx 0.12\) (\(\langle k \rangle \approx 6\)), over 90% of draws are connected.

Small-world (Watts–Strogatz)

Real networks are both clustered (your neighbours know each other) and The small-world property: the average distance between two nodes grows only like \(\ln N\), so even in a huge network anyone is a few steps from anyone (“six degrees of separation”). Random networks already have it: in \(d\) steps a node reaches about \(\langle k \rangle^d\) others, so a few steps cover the whole network. Network Science, ch. 3 → (short paths between anyone). A Ring, \(k = 4\)Square gridA regular lattice places nodes on a regular pattern and links each only to its nearest neighbours, so every node looks the same. It has high clustering but long paths: distances grow like \(N\) on a ring or \(\sqrt{N}\) on a grid, not \(\ln N\). has the first but not the second; a random network has the second but not the first. The Watts–Strogatz model interpolates between the two:

  1. Start from a ring where each node is linked to its \(k\) nearest neighbours.
  2. Rewire each link with probability \(\beta\), moving one end to a randomly chosen node.

Every node starts with degree \(k\) and rewiring moves only one end of a link, so degrees stay close to \(k\): \(\langle k \rangle = k\) and there are no hubs.

Clustering. On the ring, \(C(0) = \dfrac{3(k-2)}{4(k-1)}\): \(0.5\) for \(k = 4\), \(0.6\) for \(k = 6\), and \(0\) for \(k = 2\), where an agent's two neighbours are never linked. A triangle survives only if none of its three links is rewired, so clustering falls as

\[ C(\beta) \approx C(0)\,(1-\beta)^3 \]

At \(\beta = 0.1\) about 73% of the ring's clustering is left; past \(\beta \approx 0.2\) less than half is, and the network looks increasingly random.

Scale-free (Barabási–Albert)

This topology introduces the scale-free property: as in many real networks, the degree distribution follows a power law rather than a Poisson distribution:

\[ p_k \sim k^{-\gamma} \]

A power law decays slowly, so very high degrees are rare but not negligible: the network has hubs, nodes with far more links than average. \(\langle k \rangle\) is then a poor description of a typical node.

Random (Poisson)Scale-free, Barabási–Albert (\(\gamma = 3\))Pure power law \(24\,k^{-3}\)
Linear axes
00.10.20.30.405101520degree kpkRandom, k = 0: p = 0.0025Random, k = 1: p = 0.015Random, k = 2: p = 0.045Random, k = 3: p = 0.089Random, k = 4: p = 0.13Random, k = 5: p = 0.16Random, k = 6: p = 0.16Random, k = 7: p = 0.14Random, k = 8: p = 0.1Random, k = 9: p = 0.069Random, k = 10: p = 0.041Random, k = 11: p = 0.023Random, k = 12: p = 0.011Random, k = 13: p = 0.0052Random, k = 14: p = 0.0022Random, k = 15: p = 0.00089Random, k = 16: p = 0.00033Random, k = 17: p = 0.00012Random, k = 18: p = 3.9e-05Random, k = 19: p = 1.2e-05Random, k = 20: p = 3.7e-06Scale-free, k = 3: p = 0.4Scale-free, k = 4: p = 0.2Scale-free, k = 5: p = 0.11Scale-free, k = 6: p = 0.071Scale-free, k = 7: p = 0.048Scale-free, k = 8: p = 0.033Scale-free, k = 9: p = 0.024Scale-free, k = 10: p = 0.018Scale-free, k = 11: p = 0.014Scale-free, k = 12: p = 0.011Scale-free, k = 13: p = 0.0088Scale-free, k = 14: p = 0.0071Scale-free, k = 15: p = 0.0059Scale-free, k = 16: p = 0.0049Scale-free, k = 17: p = 0.0041Scale-free, k = 18: p = 0.0035Scale-free, k = 19: p = 0.003Scale-free, k = 20: p = 0.0026
Log–log axes
10-610-410-21110100degree kpkRandom, k = 1: p = 0.015Random, k = 2: p = 0.045Random, k = 3: p = 0.089Random, k = 4: p = 0.13Random, k = 5: p = 0.16Random, k = 6: p = 0.16Random, k = 7: p = 0.14Random, k = 8: p = 0.1Random, k = 9: p = 0.069Random, k = 10: p = 0.041Random, k = 12: p = 0.011Random, k = 14: p = 0.0022Random, k = 16: p = 0.00033Random, k = 18: p = 3.9e-05Random, k = 20: p = 3.7e-06Scale-free, k = 3: p = 0.4Scale-free, k = 4: p = 0.2Scale-free, k = 5: p = 0.11Scale-free, k = 6: p = 0.071Scale-free, k = 7: p = 0.048Scale-free, k = 8: p = 0.033Scale-free, k = 9: p = 0.024Scale-free, k = 10: p = 0.018Scale-free, k = 12: p = 0.011Scale-free, k = 14: p = 0.0071Scale-free, k = 16: p = 0.0049Scale-free, k = 18: p = 0.0035Scale-free, k = 20: p = 0.0026Scale-free, k = 22: p = 0.002Scale-free, k = 24: p = 0.0015Scale-free, k = 26: p = 0.0012Scale-free, k = 28: p = 0.00099Scale-free, k = 30: p = 0.00081Scale-free, k = 32: p = 0.00067Scale-free, k = 34: p = 0.00056Scale-free, k = 36: p = 0.00047Scale-free, k = 38: p = 0.0004Scale-free, k = 40: p = 0.00035Scale-free, k = 42: p = 0.0003Scale-free, k = 44: p = 0.00026Scale-free, k = 46: p = 0.00023Scale-free, k = 48: p = 0.0002Scale-free, k = 50: p = 0.00018Scale-free, k = 52: p = 0.00016Scale-free, k = 54: p = 0.00014Scale-free, k = 56: p = 0.00013Scale-free, k = 58: p = 0.00012Scale-free, k = 60: p = 0.00011Scale-free, k = 62: p = 9.6e-05Scale-free, k = 64: p = 8.7e-05Scale-free, k = 66: p = 8e-05Scale-free, k = 68: p = 7.3e-05Scale-free, k = 70: p = 6.7e-05Scale-free, k = 72: p = 6.2e-05Scale-free, k = 74: p = 5.7e-05Scale-free, k = 76: p = 5.3e-05Scale-free, k = 78: p = 4.9e-05Scale-free, k = 80: p = 4.5e-05Scale-free, k = 82: p = 4.2e-05Scale-free, k = 84: p = 3.9e-05Scale-free, k = 86: p = 3.6e-05Scale-free, k = 88: p = 3.4e-05Scale-free, k = 90: p = 3.2e-05Scale-free, k = 92: p = 3e-05Scale-free, k = 94: p = 2.8e-05Scale-free, k = 96: p = 2.6e-05Scale-free, k = 98: p = 2.5e-05Scale-free, k = 100: p = 2.3e-05
Degree distribution \(p_k\) of both models with the same average degree, \(\langle k \rangle = 6\). On linear axes both tails seem to vanish past \(k \approx 15\). On log–log axes the difference shows: the Poisson tail collapses, while the power law falls along a straight line of slope \(-3\), so very high degrees stay orders of magnitude more likely. The dashed line is the pure power law \(2m(m+1)\,k^{-3}\): the exact curve starts slightly below it and settles onto it as \(k\) grows. The scale-free network also has many more agents with few links: it starts at \(k = m = 3\) and its peak is there.

The degree exponent. \(\gamma\) is not fixed by the definition, but it The shares \(p_k = C\,k^{-\gamma}\) must add up to \(1\), so the sum \(\sum_{k \ge 1} k^{-\gamma}\) has to be finite. It is only for \(\gamma > 1\). At \(\gamma = 1\) it is the harmonic series \(1 + \tfrac12 + \tfrac13 + \dots\), which grows forever, and smaller \(\gamma\) only makes it grow faster. With an infinite sum, no constant \(C\) can make the shares add up to \(1\). for \(p_k\) to be a valid distribution, and its value decides how the network behaves. Most real networks have \(2 < \gamma < 3\); Barabási–Albert always gives \(\gamma = 3\).

Barabási–Albert produces a power-law degree distribution with two mechanisms:

  1. Growth. Start from a few nodes and add one node at a time, each with \(m\) links.
  2. Preferential attachment. Each new link goes to existing node \(i\) with probability proportional to its degree:
\[ \Pi(k_i) = \frac{k_i}{\sum_j k_j} \]

Well-connected nodes get more new links, so they become better connected (“the rich get richer”). A node that arrived at time \(t_i\) has, at time \(t\),

\[ \langle k_i(t) \rangle \approx m \left( \frac{t}{t_i} \right)^{1/2} \]

so the oldest nodes become the hubs (first-mover advantage). The resulting degree distribution is a power law with \(\gamma = 3\), whatever the value of \(m\):

\[ p_k \approx 2m(m+1)\,k^{-3} \qquad \langle k \rangle \approx 2m \]

Both mechanisms are needed. Growth without preferential attachment gives an exponential degree distribution (no hubs). Preferential attachment without growth gives no stationary power law: the network eventually becomes fully connected.

Clustering is low, \(\langle C \rangle \sim (\ln N)^2 / N\). That is higher than in a random network of the same size, but it still vanishes as \(N\) grows.

Comparison

RandomSmall-worldScale-free
Parameters\(p\)\(k,\ \beta\)\(m\)
\(\langle k \rangle\)\(p\,(N-1)\)\(k\)\(\approx 2m\)
Degree distributionPoisson, narrowNarrow, around \(k\)Power law, \(k^{-3}\)
HubsNoNoYes
ClusteringLow, \(= p\)High (small \(\beta\))Low, \(\sim (\ln N)^2 / N\)
Average path lengthShort, \(\sim \ln N / \ln \langle k \rangle\)Short once \(\beta > 0\)Shorter, \(\sim \ln N / \ln \ln N\)
Real social networks combine high clustering, short paths and hubs; each model reproduces only part of this. Random networks are the usual baseline; small-world adds clustering, scale-free adds hubs.
Small networks. The results above are asymptotic, for large \(N\). With 50 agents they are approximate: a scale-free network of 50 nodes has visible hubs, but too few nodes to show a clean power law.

In disensa

Networks are built with NetworkX (network.py). Every network is seeded, so the same seed gives the same network, and always connected: a disconnected draw is redrawn with a new seed derived from the original, up to 100 times. This matters for two reasons. Opinions only travel along links, so separate parts of a network would never influence each other and would evolve as independent simulations. And only Erdős–Rényi can split (Barabási–Albert is connected by construction, and Watts–Strogatz is built connected), so allowing it would add a difference between topologies that has nothing to do with their structure. All topologies take n, the number of agents (default 50).

TopologyConfig parametersTypical values (50 agents)
erdos_renyip: link probability \(p \in [0, 1]\)\(p = 0.12\text{–}0.15\)  \((\langle k \rangle \approx 6\text{–}7)\)
watts_strogatzk: neighbours on the ring (even); beta: rewiring probability \(\beta \in [0, 1]\)\(k = 4\text{–}6\), \(\beta = 0.1\)
barabasi_albertm: links per new agent\(m = 2\text{–}3\)  \((\langle k \rangle \approx 3.8\text{–}5.6)\)
Why Barabási–Albert gives slightly fewer links. NetworkX starts the network from a small star of \(m + 1\) agents (NetworkX docs). Every agent added afterwards brings \(m\) links, but the starting agents have fewer. For \(m = 3\) and 50 agents the average is \(5.64\) links per agent instead of \(6\).
Comparing topologies. To see the effect of structure alone, every topology should have the same number of agents and about the same number of links per agent; otherwise one network is simply denser than another. For 50 agents and about 6 links per agent:
  • Random: \(p = 0.12\)
  • Small-world: \(k = 6\)
  • Scale-free: \(m = 3\) (5.64 links per agent, see above)

Numeric algorithms

Numeric algorithms are the classic way to model how agents (the nodes of the network) update their opinions. Where an LLM agent changes its opinion by prompting a language model, a numeric agent applies an algebraic equation that computes its new opinion deterministically from its own opinion and those of its neighbours. disensa has three algorithms, all in dynamics/numeric.py: DeGroot is plain averaging, and the other two each add one mechanism to it, a similarity filter (bounded confidence) or stubbornness (Friedkin–Johnsen). Like LLM agents, every agent reads the opinions of step \(t-1\) and all updates apply together, using the adjacency matrix \(A\) from Describing a network. All three belong to the family of Assimilative influence: when an agent updates, its opinion moves towards the opinions of the neighbours it listens to, never away from them. Models where opinions can also be pushed apart (repulsion) are a separate family, not implemented here. See Flache et al. (2017), §2. models.

DeGroot (averaging)

At each step, every agent replaces its opinion with the average of its own opinion and those of its neighbours \(N(i)\) at the previous step. Every opinion in the average counts the same: an agent with \(k_i\) neighbours averages \(k_i + 1\) values, its own included.

\[ x_i(t) = \frac{1}{k_i + 1} \sum_{j \in N(i) \cup \{i\}} x_j(t-1) \]

Matrix form. The same update can be written for all agents at once. Put every opinion in a column vector \(x(t) = (x_1(t), \dots, x_N(t))^\top\) and every weight in an \(N \times N\) matrix \(W\), where \(W_{ij}\) is how much agent \(i\) listens to agent \(j\) (in disensa, the same for itself and each of its neighbours). Then the whole step is one multiplication:

\[ x(t) = W\,x(t-1) \]

Element \(i\) of the resulting vector is \(\sum_j W_{ij}\, x_j(t-1)\), agent \(i\)’s new opinion: the same update as above. In disensa, \(W_{ij} = 1/(k_i + 1)\) if \(j\) is \(i\) itself or one of its neighbours, and \(0\) otherwise. With the adjacency matrix \(A\) and the identity matrix \(I\) (which links each agent to itself), that is

\[ W_{ij} = \frac{A_{ij} + I_{ij}}{k_i + 1} \]

Every entry of \(W\) is \(\ge 0\) and every row sums to \(1\): \(W\) is Row-stochastic matrix: a square matrix whose entries are all \(\ge 0\) and whose rows each sum to \(1\), \(\sum_j W_{ij} = 1\) for every \(i\). Each row is a set of weights, so multiplying a vector by it turns every entry into a weighted average of the vector’s entries.. That is what makes each new opinion a weighted average of the old ones. DeGroot’s general model allows any row-stochastic \(W\), so agents can weigh each other differently; disensa uses the equal weights above.

Example: one step on a path of three agents

Three agents on a path \(1 - 2 - 3\): agent 2 has two neighbours, agents 1 and 3 have one each. They start from \(x(0) = (-2,\ -1,\ 1.5)^\top\).

t = 01−22−131.5t = 11−1.52−0.530.25

Build \(W\) from the network. The adjacency matrix \(A\) marks who is linked to whom; adding \(I\) puts a \(1\) on the diagonal, so each agent also counts itself:

\[ A + I = \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix} + \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 1 & 0 \\ 1 & 1 & 1 \\ 0 & 1 & 1 \end{pmatrix} \]

Row \(i\) now has \(k_i + 1\) ones: one for each opinion agent \(i\) averages. Dividing each row by that count makes the row sum to \(1\):

\[ k + 1 = \begin{pmatrix} 2 \\ 3 \\ 2 \end{pmatrix} \quad\Longrightarrow\quad W = \begin{pmatrix} \frac12 & \frac12 & 0 \\[6pt] \frac13 & \frac13 & \frac13 \\[6pt] 0 & \frac12 & \frac12 \end{pmatrix} \]

One step is one multiplication:

\[ x(1) = W\,x(0) = \begin{pmatrix} \frac12 & \frac12 & 0 \\[6pt] \frac13 & \frac13 & \frac13 \\[6pt] 0 & \frac12 & \frac12 \end{pmatrix} \begin{pmatrix} -2 \\[6pt] -1 \\[6pt] 1.5 \end{pmatrix} = \begin{pmatrix} \tfrac{-2-1}{2} \\[6pt] \tfrac{-2-1+1.5}{3} \\[6pt] \tfrac{-1+1.5}{2} \end{pmatrix} = \begin{pmatrix} -1.5 \\[6pt] -0.5 \\[6pt] 0.25 \end{pmatrix} \]

Every agent moves towards its neighbours. Agent 2, in the middle, hears both sides; agents 1 and 3 only hear agent 2. The gap between the most extreme opinions halves, from \(3.5\) to \(1.75\), and keeps halving at every step as the three opinions approach the same value.

Where it goes. On a connected network, averaging converges to consensus: as the steps go on, all opinions approach the same value (DeGroot, 1974; Flache et al., 2017, §2). They get arbitrarily close but, in general, never become exactly equal: the gap between the highest and lowest opinion shrinks by a roughly constant factor every step (in the example above it halves). disensa networks are always connected, so a long enough DeGroot run always gets close to consensus. Because each update is an average, opinions also never leave the range of the initial ones.

The consensus value. The value they approach, \(x^\ast\), is a weighted average of the initial opinions \(x_i(0)\) of the agents \(i = 1, \dots, N\), with weights proportional to \(k_i + 1\):

\[ x^\ast = \sum_i w_i\, x_i(0), \qquad w_i = \frac{k_i + 1}{\sum_j (k_j + 1)} \]

The weights sum to \(1\). Well-connected agents are listened to by more agents, so they pull the consensus towards their own starting opinion; in a scale-free network, hubs get a visibly larger say. The reason is that the sum of opinions weighted by \(k_i + 1\) stays the same at every step, so at consensus it still equals its starting value. In the example, \(x^\ast = (2 \cdot (-2) + 3 \cdot (-1) + 2 \cdot 1.5) / 7 \approx -0.57\), not the plain average \(-0.5\): agent 2 has the most neighbours, so its opinion counts more.

That is also the model's limit. It cannot produce polarization or echo chambers, only consensus, which makes it the reference point: anything else a run shows comes from what the other rules add.

Bounded confidence

Agents only listen to neighbours whose opinion is already close to their own: within a confidence bound \(\varepsilon\). Neighbours further away are ignored. For agent \(i\), the neighbours it listens to, together with itself, form its Agent \(i\) itself plus the neighbours \(j\) with \(|x_i - x_j| \le \varepsilon\) at step \(t-1\). It changes every step as opinions move, so two agents can stop listening to each other once they drift apart, and start again if they come close. \(C_i(t-1) = \{\, j \in N(i) \cup \{i\} : |x_i(t-1) - x_j(t-1)| \le \varepsilon \,\}\), and the agent moves towards the average of that set, but only part of the way: it covers a fraction \(\mu\) of the distance between its opinion and that average. With \(\mu = 1\) it jumps straight to the average; with \(\mu = 0.5\) it goes halfway.

\[ x_i(t) = x_i(t-1) + \mu \left( \frac{1}{|C_i|} \sum_{j \in C_i} x_j(t-1) \;-\; x_i(t-1) \right) \]

If no neighbour is within \(\varepsilon\), the set holds only the agent itself, so it keeps its opinion.

Two classic versions. Bounded confidence has two standard forms; disensa uses a variant that combines them:

ModelWho updatesUpdate
Deffuant et al. (2000)One random pair at a timeEach moves \(\mu\) towards the other, if within \(\varepsilon\).
Hegselmann–Krause (2002)Everyone at onceEach takes the plain average of all agents within \(\varepsilon\), itself included (\(\mu = 1\)), originally with everyone able to see everyone.
Inertial Hegselmann–Krause (Chazelle & Wang, 2017), used by disensaEveryone at onceAverages all agents within \(\varepsilon\), itself included, like Hegselmann–Krause, but moves only \(\mu\) of the way towards that average, like Deffuant. The original has no network (anyone within \(\varepsilon\) counts); in disensa only linked agents count.

Where it goes. If \(\varepsilon\) is large, everyone is eventually linked by chains of agents who listen to each other and opinions converge to consensus, as with DeGroot. If \(\varepsilon\) is small, the population splits into separate opinion clusters that no longer listen to each other; the smaller \(\varepsilon\), the more and smaller the clusters (Flache et al., 2017, §2). Hegselmann and Krause (2002, §4.1) describe three phases as \(\varepsilon\) grows:

On a network the bound is not the only obstacle: an agent can only be pulled by its own neighbours, so clusters also form where the network itself keeps groups apart.

As with DeGroot, every update is an average (for \(\mu \le 1\)), so opinions stay within the initial range: the model can split a population into camps, but not make it more extreme than its initial extremists (Flache et al., 2017, §2).

Large \(\varepsilon\). Opinions are at most \(4\) apart, so with \(\varepsilon \ge 4\) every neighbour is always in the confidence set; with \(\mu = 1\) the rule is then exactly DeGroot.

Friedkin–Johnsen (stubbornness)

Agents average with their neighbours, but never fully let go of where they started. Each agent keeps its initial opinion \(s_i = x_i(0)\) as its The innate opinion \(s_i\) is the view an agent holds on its own, before any influence; it never changes. What changes is the opinion it expresses, \(x_i(t)\), which mixes \(s_i\) with what its neighbours say. Friedkin and Johnsen call the pull of \(s_i\) the agent's prejudice or stubbornness. and mixes it with its neighbours' current opinions, as in Chitra & Musco (2020, eq. 1):

\[ x_i(t) = \frac{\lambda\, s_i + \sum_{j \in N(i)} x_j(t-1)}{\lambda + k_i} \]

The sum runs over agent \(i\)’s neighbours \(N(i)\), and \(k_i\) is its degree (how many neighbours it has).

\(\lambda\) (stubbornness) is how much the innate opinion weighs compared with one neighbour. Chitra & Musco use \(\lambda = 1\): the innate opinion counts as one extra neighbour. Since the sum over \(k_i\) neighbours is \(k_i\) times their average \(\bar{x}_{N(i)}(t-1)\), the update is a weighted average of the innate opinion and the neighbours’ average:

\[ x_i(t) = \frac{\lambda}{\lambda + k_i}\, s_i \;+\; \frac{k_i}{\lambda + k_i}\, \bar{x}_{N(i)}(t-1) \]

The more neighbours an agent has, the less weight its innate opinion gets, so well-connected agents are pulled harder by the group. In the original model (Friedkin & Johnsen, 1990) the weight on each agent’s innate opinion is a separate parameter, set freely per agent; here it follows from the degree.

Where it goes. Not to a common consensus. Every agent keeps being pulled back towards its own innate opinion \(s_i\), so each agent approaches its own equilibrium \(x_i^\ast\): the opinion at which the pull of its innate opinion and the pull of its neighbours’ average balance, so that it no longer changes from one step to the next. Neighbours end up closer than they started, but disagreement never disappears: large \(\lambda\) keeps agents near their innate opinions, small \(\lambda\) brings them close to consensus. Opinions stay within the initial range, as with the other two rules.

Computing the equilibrium. The equilibrium can be computed directly, without running the simulation, using the Graph Laplacian: the matrix \(L = D - A\), with each agent’s degree on the diagonal, \(-1\) for each link and \(0\) elsewhere, so every row sums to \(0\). It measures differences across links: \(L\,x\) is \(0\) for every agent exactly when all neighbours agree. \(L = D - A\). Here \(A\) is the adjacency matrix and \(D\) is the degree matrix: a diagonal matrix with each agent’s degree \(k_i\) on the diagonal and zeros elsewhere. Multiplied by the opinion vector \(x\), \(L\) gives each agent’s total difference from its neighbours: positive if it sits above them, negative if below. For agent \(i\), that is element \(i\) of the vector \(L\,x\), i.e. row \(i\) of \(L\) times \(x\):

\[ (L\,x)_i = k_i\, x_i - \sum_{j \in N(i)} x_j = \sum_{j \in N(i)} (x_i - x_j) \]

Row \(i\) of \(L\) has \(k_i\) on the diagonal, \(-1\) for each neighbour and \(0\) for everyone else, so non-neighbours drop out. The result is \(0\) when the agent sits exactly at its neighbours’ average.

At equilibrium opinions stop changing, so \(x_i(t) = x_i(t-1) = x_i^\ast\). Putting this into the update and rearranging gives, for every agent,

\[ \lambda\,(x_i^\ast - s_i) + (L\,x^\ast)_i = 0 \]

its distance from its innate opinion balances its distance from its neighbours. For all agents at once this is one linear system, solved for \(x^\ast\):

\[ (\lambda I + L)\, x^\ast = \lambda\, s \qquad\Longrightarrow\qquad x^\ast = \lambda\, (\lambda I + L)^{-1}\, s \]
Derivation, step by step
  1. At equilibrium nothing changes, \(x_i(t) = x_i(t-1) = x_i^\ast\), so every opinion in the update becomes \(x^\ast\):
    \[ x_i^\ast = \frac{\lambda\, s_i + \sum_{j \in N(i)} x_j^\ast}{\lambda + k_i} \]
  2. Multiply both sides by \(\lambda + k_i\):
    \[ \lambda\, x_i^\ast + k_i\, x_i^\ast = \lambda\, s_i + \sum_{j \in N(i)} x_j^\ast \]
  3. Move everything to the left and group. The first term is the agent’s distance from its innate opinion, the second its distance from its neighbours, and they cancel:
    \[ \underbrace{\lambda\,(x_i^\ast - s_i)}_{\text{from innate opinion}} \;+\; \underbrace{k_i\, x_i^\ast - \sum_{j \in N(i)} x_j^\ast}_{(L\,x^\ast)_i\text{, from neighbours}} = 0 \]

Stacking the equations for all agents gives \(\lambda\,(x^\ast - s) + L\,x^\ast = 0\), and since \(\lambda\, x^\ast = \lambda I\, x^\ast\) this is \((\lambda I + L)\, x^\ast = \lambda\, s\). \(\lambda I + L\) can be inverted because \(\lambda > 0\).

Example: the equilibrium on a path of three agents

The same path \(1 - 2 - 3\) as in the DeGroot example, with innate opinions \(s = x(0) = (-2,\ -1,\ 1.5)^\top\) and \(\lambda = 1\).

t = 01−22−131.5t → ∞1−1.312−0.6330.44

Build the Laplacian from the degrees and the links:

\[ L = D - A = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 1 \end{pmatrix} - \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix} = \begin{pmatrix} 1 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -1 & 1 \end{pmatrix} \]

At the start, \(L\,s\) shows how far each agent sits from its neighbours: agent 1 is \(1\) below agent 2, agent 3 is \(2.5\) above agent 2, and agent 2 is pulled both ways:

\[ L\,s = \begin{pmatrix} 1 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -1 & 1 \end{pmatrix} \begin{pmatrix} -2 \\ -1 \\ 1.5 \end{pmatrix} = \begin{pmatrix} -1 \\ -1.5 \\ 2.5 \end{pmatrix} \]

The equilibrium solves \((\lambda I + L)\, x^\ast = \lambda\, s\). With \(\lambda = 1\), \(\lambda I + L\) adds \(1\) to each diagonal entry of \(L\), and \(\lambda\, s = s\):

\[ \lambda I + L = 1 \cdot \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} + \begin{pmatrix} 1 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -1 & 1 \end{pmatrix} = \begin{pmatrix} 2 & -1 & 0 \\ -1 & 3 & -1 \\ 0 & -1 & 2 \end{pmatrix} \]
\[ \lambda\, s = 1 \cdot \begin{pmatrix} -2 \\ -1 \\ 1.5 \end{pmatrix} = \begin{pmatrix} -2 \\ -1 \\ 1.5 \end{pmatrix} \]

Solving:

\[ \begin{pmatrix} 2 & -1 & 0 \\ -1 & 3 & -1 \\ 0 & -1 & 2 \end{pmatrix}\, x^\ast = \begin{pmatrix} -2 \\ -1 \\ 1.5 \end{pmatrix} \qquad\Longrightarrow\qquad x^\ast = \begin{pmatrix} -1.3125 \\ -0.625 \\ 0.4375 \end{pmatrix} \]

Each agent settles at its own value, between its innate opinion and its neighbours. The gap between the most extreme opinions shrinks from \(3.5\) to \(1.75\) and stays there; DeGroot, on the same path, brings all three to \(-0.57\). Agent 1, for example, balances a pull of \(0.69\) back towards its innate \(-2\) against a pull of \(0.69\) towards agent 2.

In disensa

Set dynamics.type: numeric and pick a rule. Numeric runs take no LLM calls and finish instantly (configs/numeric_degroot.yaml, numeric_bc.yaml, numeric_fj.yaml).

ParameterValuesDefault
ruledegroot, bounded_confidence or friedkin_johnsenbounded_confidence
epsilonconfidence bound \(\varepsilon > 0\) (bounded confidence only)\(1.0\)
mustep size \(0 < \mu \le 1\) (bounded confidence only)\(0.5\)
stubbornnessweight of the innate opinion \(\lambda > 0\) (Friedkin–Johnsen only)\(1.0\)

LLM agents

An LLM agent replaces the update equation with a language model. Instead of computing a new opinion from numbers, it reads what a few of its neighbours said, reasons about it, and replies with a new opinion, a new message and an updated memory. Everything else in a run (the network, the initial opinions, the loop, the metrics) is the same as with numeric agents.

Agents in the same run can use different models: every agent is assigned a model, so one community can mix models from different labs, each reasoning in its own way. Agents are members of a community who exchange messages about a single statement; nothing in the prompts refers to a specific platform or medium. The code is in dynamics/llm_agent.py, and every prompt is in llm/prompts.py, quoted in full below.

What an agent is

Each agent is a node of the network with a model attached and three things that carry over from one step to the next:

Opinion

A number on \([-2, 2]\), exactly as for numeric agents. This is what the metrics measure.

Message

The last thing it said, 1–3 sentences. This is what its neighbours read at the next step.

Memory

A short summary of what it has heard and thought so far, which the agent rewrites itself at every step.

The model call is A language model remembers nothing between calls. Everything the agent “knows” at step \(t\) has to be written into the prompt of that call: its opinion, its memory and the messages it hears.: the agent only knows what is written in its prompt. Its identity is minimal: the system prompt calls it “Agent \(i\), a member of a community”, with no name, age or personality.

No personas. Wang et al. give each agent a gender, an age, an education level and Big Five personality traits; Chuang et al. add political leaning, ethnicity and occupation. disensa leaves them out on purpose: without personas, two agents that start from the same opinion can only differ because of their model, which is one of the effects under study.

Initial opinion and first message

An LLM agent does not choose its starting opinion: it is drawn from opinions.distribution, exactly as for numeric agents (see Initial opinions). The number is then put into words. It is rounded to the nearest integer and turned into one of five stance labels, and both the label and the exact value go into the prompt:

Opinion (rounded)\(-2\)\(-1\)\(0\)\(+1\)\(+2\)
Labelstrongly opposesomewhat opposeneutral / undecidedsomewhat supportstrongly support

At step 0 every agent writes a first message from that position, e.g. “You somewhat oppose this statement (your position: \(-0.8\) on a scale from \(-2\) to \(+2\))”. These first messages are what the agents hear at step 1. Step 0 does not change any opinion.

The starting opinion is given, not chosen. The model does not pick its own starting position. This keeps LLM and numeric runs comparable, since both start from the same opinions. The cost is that a model’s own leaning on the statement is hidden at the start, and can only show up later.
Prompt: first message
[system]
You are Agent {i}, a member of a community. You interact with other people and share your views with them.
Think and speak as this person would, in plain everyday language.

[user]
Topic: "{statement}"

You {label} this statement (your position: {value} on a scale from -2 = strongly oppose to +2 = strongly support).

Write a short message (1-3 sentences) stating your view.
Respond ONLY with JSON: {"message": "<your message>"}

Interactions

At each step an agent interacts with a few of its neighbours: it hears the message each of them shared at step \(t-1\). How many is random, drawn every step between interactions.min and interactions.max (default \(1\) to \(5\)), and capped at the agent’s number of neighbours: an agent with 3 neighbours that draws 5 hears all 3. The neighbours are then picked uniformly at random: every neighbour is equally likely, whatever its opinion or degree. Both draws are new at every step and separate for each agent, so hearing is not mutual: agent \(i\) can hear \(j\) at a step without \(j\) hearing \(i\). A message from a neighbour reaches the agent as one line of the prompt: - Agent 7 said: "...".

This is a different kind of influence from the numeric rules. There, an agent sees the opinions of all its neighbours, every step. Here it sees the messages of some of them, and never their numbers: whatever a neighbour’s opinion is, only the words reach the agent. A hub can still hear only five others per step, but it is heard by many.

Updating the opinion

Pick interactionsa few neighbours and their step \(t-1\) messages
Write the promptstatement, own position, memory, interactions
Call the modelone call per agent, all agents at once
Read the replynew opinion, message and memory, applied together
One step for one LLM agent. All agents do this from the same step \(t-1\) state.

The prompt gives the agent its current position (label and value), its memory and its interactions, and asks it to reflect on them “as the person you are”: it may keep, soften, strengthen or change its position. It must reply with JSON holding four fields:

FieldWhat it isWhat happens to it
reasoning2–4 sentences on how the interactions affect its viewSaved for inspection only; never shown to other agents
beliefIts new opinion, a number from \(-2\) to \(2\)Becomes \(x_i(t)\), clipped to \([-2, 2]\)
message1–3 sentencesWhat its neighbours hear at step \(t+1\)
memoryAn updated summary, at most memory_words words (default 60)Replaces the old memory in the next prompt

There is no update rule. Unlike the numeric algorithms, there are no parameters that shape the update: no confidence bound \(\varepsilon\), no step size \(\mu\), no stubbornness. The new opinion is whatever number the model reports. If LLM agents end up listening only to similar views (as in bounded confidence) or clinging to where they started (as in Friedkin–Johnsen), that behaviour comes from the model, not from the code.

Self-reported opinions. The opinion is what the agent says it is. Chuang et al. found that scoring the agent’s message with a separate classifier model is more reliable than asking for a number. disensa uses the self-report for now; the reasoning and message are saved, so a classifier could be run on them afterwards.
Prompt: one step
Topic: "{statement}"

What you remember from previous interactions: {memory}

Your current position: you {label} the statement ({value} on a scale from -2 = strongly oppose to +2 = strongly support).

In this step you interacted with:
- Agent {j} said: "{message}"
- ...

Reflect on these interactions, as the person you are. You may keep, soften, strengthen or change your position.
Respond ONLY with JSON:
{"reasoning": "<2-4 sentences: how these interactions affect your view>",
 "belief": <number from -2 to 2>,
 "message": "<your new message, 1-3 sentences>",
 "memory": "<updated summary of what you have heard and thought so far, at most {memory_words} words>"}

The system prompt is the same as for the first message. At step 1 the memory is “(nothing yet)”; an agent with no interactions sees “(nobody in this step)” and can still reconsider on its own.

Models

The models of a run are listed under dynamics.models. Each has a weight, and each model gets that share of the agents: three models with equal weight and 20 agents give \(7 + 7 + 6\). Shares are rounded so the counts always add up to \(N\) (Each model first gets the whole part of its exact share (\(20/3 = 6.67 \to 6\)). The agents left over go one each to the models with the largest fractional parts.). The agents are then placed on the network at random: the list of model labels is shuffled and handed out to the agents. The shuffle has its own random generator, separate from the rest of the run, so two runs with the same seed but different models have the same network, initial opinions and interactions; only the models differ.

A model is written as ollama_chat/<name>, e.g. ollama_chat/llama3.2:3b: ollama_chat says the model runs on Ollama, on the same computer as disensa, and the rest is the model’s name there. Only local models are used, so runs are free and no data leaves the machine. How to install and download models is in Local models. disensa sends every request through LiteLLM, a library that talks to model servers in a uniform way.

Each request also carries settings that are not part of the prompt. The Temperature \(T\): a model writes one token at a time, giving each candidate \(k\) a score \(z_k\) and drawing it with probability \(p_k \propto e^{z_k / T}\). As \(T \to 0\) the top token almost always wins; at \(T = 1\) the model’s own probabilities are used; above \(1\) unlikely tokens get through more often. (default \(0.7\)) sets how varied the replies are: low values give nearly the same reply to the same prompt, higher values more varied ones. max_tokens caps the length of the reply. Every model gets the same prompts and the same settings unless set otherwise, so that differences between agents come from the models. The same temperature does not make two models equally random, though: it reshapes each model’s own word probabilities, so it treats them the same way, not makes them behave the same.

Error handling and reproducibility

Models do not always follow the format. disensa asks for JSON mode when the provider supports it, and repairs common mistakes (text around the JSON, trailing commas). If that fails, it asks the model once more to fix its reply. If the reply is still unusable, or the provider returns an error, the run does not stop: the agent keeps its previous opinion, message and memory for that step, and the error is saved with the step so it can be counted and inspected. A missing message or memory also keeps the previous one. The one case with nothing to keep is step 0: an agent whose first message fails has no message yet, so its neighbours cannot hear it until it says something.

Every reply is cached on disk under its exact request: the model, the full prompt, the temperature, max_tokens and the seed. Running the same config again replays the same replies without calling any model, so an LLM run is reproducible once it has been run once. Changing anything in the prompt (the statement, a neighbour’s message, the memory) makes it a new request.

In disensa

Set dynamics.type: llm, a statement and at least one model. configs/quick.yaml is the smallest run, one small Ollama model; configs/mixed_local.yaml mixes three Ollama models on a Mac.

dynamics:
  type: llm
  statement: Euthanasia should be legal for terminally ill adults who clearly request it.
  models:
    - {name: ollama_chat/llama3.1:8b}
    - {name: ollama_chat/gemma2:9b}
    - {name: ollama_chat/mistral:7b, weight: 2, temperature: 0.7}
  interactions: {min: 1, max: 4}
ParameterValuesDefault
statementthe statement agents take a position oneuthanasia statement
modelslist of name, weight, temperaturerequired
interactions.min, .maxhow many neighbours an agent hears per step\(1\), \(5\)
memory_wordsword limit asked for the memory\(60\)
max_tokenslongest reply allowed\(400\)
max_concurrencyparallel requests per provider\(4\)
cache_dirwhere replies are cached.cache/llm

To follow a run agent by agent, with each step’s interactions, reasoning and memory, see Reading a run.

Initial opinions

Before the first step, every agent gets a starting opinion \(x_i(0)\) on the scale from \(-2\) (strongly oppose) to \(+2\) (strongly support), with \(0\) as neutral. Together they form \(x(0)\), the input to every rule. The opinions are drawn at random from a distribution chosen in the config, once per run, from the run’s seed.

Distributions

DistributionDrawWhat you get
uniform\(x_i(0) \sim \mathcal{U}(-2, 2)\)Every opinion on the scale is equally likely. No starting leaning and no camps.
normal\(x_i(0) \sim \mathcal{N}(m, \sigma^2)\)Most agents start near \(m\). A small \(\sigma\) is close to consensus from the start; \(m \ne 0\) gives the whole group a leaning.
bimodal\(x_i(0) \sim \mathcal{N}(\pm m, \sigma^2)\), each sign with probability \(\tfrac12\)Two camps centred at \(-m\) and \(+m\), \(2m\) apart: the group is polarized from the start. The camps are only roughly equal in size, since each agent’s side is a coin flip.

\(m\) is mean and \(\sigma\) is std. For bimodal only the size of mean matters (\(-1.5\) and \(1.5\) give the same camps), and with mean: 0 it is just a normal distribution.

Clipping. A normal draw can fall outside \([-2, 2]\). Those values are set to the nearest end, so wide distributions stack agents at exactly \(\pm 2\). The default normal (\(m = 0\), \(\sigma = 1\)) clips about \(5\%\) of agents. bimodal with \(m = 1.5\), \(\sigma = 1\) clips about \(30\%\) of each camp, which puts a spike of extremists at both ends. The lab warns when more than \(10\%\) of agents would be clipped, or when the two camps overlap so much (\(\sigma \ge m\)) that they are not really two camps.

Where opinions land on the network

Opinions are given out independently of the network: agent \(i\)’s opinion does not depend on its links or its position. Neighbours are no more alike than any two agents, and hubs get opinions drawn like everyone else’s. Any group of like-minded neighbours that appears later was produced by the dynamics, not by the setup.

The network and the opinions use separate random draws from the same seed. Changing the distribution keeps the same network, so distributions can be compared on one network. Numeric and LLM runs with the same seed also start from exactly the same \(x(0)\).

Why the starting point matters

Each rule uses \(x(0)\) differently:

All three numeric rules are weighted averages, so opinions never leave the range of \(x(0)\) (see Numeric algorithms). Whoever starts most extreme sets the limit for the whole run.

In disensa

Opinions are drawn by initial_opinions in network.py. All example configs use uniform.

opinions: {distribution: uniform}

opinions: {distribution: normal, mean: 0.5, std: 0.6}

opinions: {distribution: bimodal, mean: 1.2, std: 0.4}
ParameterValuesDefault
distributionuniform, normal or bimodaluniform
mean\(m\): centre (normal) or camp position \(\pm m\) (bimodal)\(0\)
std\(\sigma \ge 0\): spread around \(m\)\(1\)

mean and std are ignored by uniform.

Metrics

After every step, including step 0, disensa sums up the opinions \(x(t)\) in three numbers that together measure polarization and echo chambers. They are the three metrics of Wang et al. (2025, §4.2). One looks only at the opinions; the other two also look at who is linked to whom. They are in metrics.py.

Polarization

How spread out the opinions are across the whole group: the variance of \(x\) (Chitra & Musco, 2020).

\[ P_z = \frac{1}{N} \sum_{i=1}^{N} \left( x_i - \bar{x} \right)^2, \qquad \bar{x} = \frac{1}{N} \sum_{i=1}^{N} x_i \]

It is \(0\) at consensus and largest, \(4\), when half the agents sit at \(-2\) and half at \(+2\). A uniform start gives about \(1.33\). It ignores the network: the same opinions shuffled across the agents give the same \(P_z\).

Global disagreement

How much linked agents disagree. Each agent’s local disagreement is the average squared gap to its neighbours, and the global value averages it over all agents:

\[ DG_i = \frac{1}{k_i} \sum_{j \in N(i)} (x_i - x_j)^2, \qquad DG = \frac{1}{2N} \sum_{i=1}^{N} DG_i \]

It is \(0\) when every agent agrees with all its neighbours, even if the group as a whole is split. The factor \(\tfrac12\) follows Wang et al.: with it, if opinions are placed on the network at random, \(DG\) comes out at about \(P_z\).

Neighbour correlation

How closely an agent’s opinion matches its neighbours’: the Pearson correlation between each agent’s opinion and the average opinion of its neighbours, across all agents (Cinus et al., 2021).

\[ NC = \operatorname{corr}\!\left( x_i,\ \bar{x}_{N(i)} \right), \qquad \bar{x}_{N(i)} = \frac{1}{k_i} \sum_{j \in N(i)} x_j \]

It runs from \(-1\) to \(+1\). Near \(0\), an agent’s opinion says nothing about its neighbours’, which is how every run starts (see Where opinions land on the network). Close to \(+1\), agents are surrounded by people who think like them: the signature of an echo chamber. At full consensus there is nothing left to correlate, so it is undefined and left empty.

Name. Wang et al. call it the “Normalized Clustering Index (NCI)” in §4.2 and “Neighbors Correlation” in their results table. disensa uses the second name.

Reading them together

No single metric tells the story; the combination does:

Outcome\(P_z\)\(DG\)\(NC\)
Consensusnear \(0\)near \(0\)undefined
Echo chambers: camps that agree inside and are apart from each otherhighlow, well below \(P_z\)high
Scattered disagreement, no structurehighabout \(P_z\)near \(0\)

The key comparison is \(DG\) against \(P_z\). If the group disagrees but linked agents agree, opinions have sorted themselves along the network. This is what Wang et al. observe on small-world and scale-free networks, where neighbour correlation rises sharply, and not on random ones.

In disensa

Metrics are saved to metrics.parquet, one row per step (see Run files), and plotted over time in the lab. Besides the three metrics, each row has:

ColumnMeaning
polarization, global_disagreement, neighbor_correlation\(P_z\), \(DG\), \(NC\)
mean_opinion\(\bar{x}\), the average opinion: shows whether the group drifts towards one side
mean_opinion[<model>]runs with several models only: the average opinion of the agents of each model
errorsLLM runs: agents whose reply failed at that step (see Error handling)

Reading a run

Work in progress

Run files

Work in progress

All parameters

Work in progress

References

Work in progress