Active Inference InstituteSimulations

Chapters 3 & 7BeginnerNew edition

A school with no leader

No fish here is told to school, and none is told how to flee. Each one only predicts how far away its neighbours and a predator should be, and moves to make those predictions come true. Watch the school meet a hunter, then take the hunter's place.

Predator

To hunt, press on the water: the predator swims after your pointer or finger, and each press (or tap) is a strike. With the keyboard, focus the water: ← → turn, ↑ ↓ faster and slower, Space strikes.

How far away a fish expects the neighbours in each sector to be, in body lengths.
How much a fish trusts what it sees of its neighbours. At zero it ignores them. The predator channel keeps its own precision.
How quickly a fish expects a wrong distance to return to η, per second.
All fish run the same model. Changing the number keeps the fish already there.

The encounter

School
Strikes, and fish caught
Fish that sense the predator now
Vacuole no fish within 2.5 body lengths of its body, fish on at least 9 of 12 sides

Last strike

Free energy per fish the second before → peak within 2 s
Fastest fish within 1 s
Flash expansion fish within 8 body lengths: their spread reaches 1.5 × within 1 s
Fountain fish ahead part, pass both sides, and join behind the tail within 8 s
Split two or more groups, the largest under 80%, within 5 s
Time to re-form one group again, holding over 95%
Area occupied before → peak within 3 s

AII: EduActive · Watch, then do

Three short clips, three things to try

These three passages come from an Institute interview and an Institute lecture. Both discuss the paper whose model this page follows in reduced form, Heins et al. (2024). Each is one to two and a half minutes. Watch one, then press its button and look for the same thing in the water above.

  1. Plays here, from 18:40. Nothing loads from YouTube until you press play.Open on YouTube at 18:40

    Before you start

    Roles in a school keep changing

    In a school of fish nobody holds a fixed job. A fish at the edge sees danger first. A moment later it may be in the middle and another fish is at the edge. What a fish does depends on where it happens to be.

    Listen for
    The position and the role that individuals play within the school are not fixed. So fish can go to different sides of the school. Some fish can kind of act as the eyes and ears of the school.
    Then try
    One fish will be marked, with the distances it senses on each side. Say which fish you expect to carry the largest errors, those in the middle or those at the edge. Then press Another fish a few times and compare.
    In the model equations 1 and 2

    On this page every fish runs the same model with the same numbers. Differences between fish come only from position: which of the four sectors hold neighbours, and how far the sensed distances y are from the preferred distance η. A fish at the edge may have sectors with no neighbour in them, and those sectors drop out of its free energy. When it moves inward that changes.

    The speaker’s wider point is about where to draw the boundary of a group treated as one agent, when its members keep changing places. The page does not attempt this. The outline round each group is a convex hull, and no statistical boundary (Markov blanket) is computed.

    Equation 1, prediction errors · Equation 2, free energy of one fish

  2. Plays here, from 47:36. Nothing loads from YouTube until you press play.Open on YouTube at 47:36

    While it runs

    Each fish reduces its own error

    A fish in this kind of model does not need to know where it is, or to think about itself at all. It expects its neighbours to be a certain distance away. When they are not, it changes its own speed and direction until the mismatch shrinks.

    Listen for
    All they do is they have some velocity vector, and that induces prediction errors, and what they can do is they can just change that velocity to basically reduce prediction errors.
    Then try
    Take that one ability away. With action off, the fish still sense and still update their beliefs, but cannot turn or change speed in response. Say whether you think the school will hold together. Then watch the free energy per fish.
    In the model equation 4

    In equation 4 the heading θ and speed s of a fish descend its own free energy F. Action enters only through its effect on what the fish senses: turning or changing speed alters how fast each sector’s distance is changing. No fish has access to another’s errors, and none holds a belief about its own position.

    The page departs from the published model in ways that touch this passage. It adds a prior on the fish’s own cruising speed, limits on turning and speed, and random noise on heading, so behaviour here is not quite a fixed function of what is sensed. The small memory the speaker mentions is the belief μ carried from one step to the next.

    Equation 4, action · What this model leaves out

  3. Plays here, from 30:49. Nothing loads from YouTube until you press play.Open on YouTube at 30:49

    Afterwards

    The paper’s summary, read aloud

    Older models of schooling give each fish rules: move away from close neighbours, move towards distant ones, line up. The paper behind this page writes in no such rules. Each fish only tries to keep its surprise low, and the familiar group patterns appear.

    Listen for
    We demonstrate that many empirically observed collective phenomena, including cohesion, milling and directed motion, emerge naturally when considering behaviour as driven by active Bayesian inference, without explicitly building behavioural rules or goals into individual agents.
    Then try
    The fish will start scattered, each heading its own way, with no predator. Say which of the three you expect to see: a group that holds together, a group that circles, a group that travels one way. Then watch polarisation and rotation.
    In the model equations 4 and 6

    The speaker is reading the summary of the preprint that became Heins et al. (2024). The “social forces” it mentions are not written into equation 4. They appear in the gradient of F: a neighbour that is too far or receding produces an error that turns the fish towards it, and one that is too close or approaching turns it away. Cohesion, milling and directed motion are read off the group with polarisation P and rotation M, which the fish do not compute.

    This page is a reduced form of that model. Beliefs are kept to two orders, each fish attends to at most eight neighbours per sector, and the fish do not learn while swimming. The predator and the looming term are additions made for this page.

    Equation 4, action · Equation 6, polarisation and rotation

The quoted lines are taken from the automatic captions, with punctuation added and hesitations removed. The speakers are discussing the published model, not this page; where the two differ, the notes say so. Each clip stops at the end of the passage. The full recordings are listed under Sources.

Learn by doing

Try this

  1. Watch a school form. The fish start scattered, each heading its own way, with no predator. Nothing in the model mentions a school. Predict whether they will stay scattered. Then watch the groups merge, the spacing tighten and polarisation rise. Sometimes the group circles as a mill on the way, and now and then it stays one.
  2. Inspect one fish. A fish divides the water around it into four sectors. In each it senses the mean distance to its nearest neighbours, holds a belief about that distance, and compares both with the distance η it expects. Predict which fish carry the largest errors: those in the middle or those at the edge. Pick several and compare. Then move the preferred distance η and watch the measured spacing follow.
  3. Take away its trust in its senses. Sensory precision is the weight a fish gives to what it sees of its neighbours. Predict what the school does when that weight is zero. The fish still swim, but those prediction errors no longer move them, and polarisation and spacing drift to the values for fish that ignore each other.
  4. Switch action off. Now the fish still sense and still update their beliefs, but cannot turn or change speed in response. Predict whether believing alone can hold a school together. Watch the free energy per fish: it has only one of its two ways down.
  5. Watch a strike. A predator is one more thing a fish expects to be far away, and a thing that looms is one more prediction error. The predator circles, turns in and lunges at about eight times the fish's cruising speed. Predict what the nearest fish will do, what the far ones will do, and what the trace of free energy will do. A manoeuvre is named on the water only when its measurement is met. In our seeded tests of 20 strikes by the autopilot, flash expansion was named in 19, a split in 18, a fountain in 6 and a vacuole in 3. Free energy per fish peaked at a median of 4.9 times its value in the second before, and the fastest fish reached a median of 8.6 body lengths a second. Of 14 splits that could be followed, 7 were one group again, after a median of 11 seconds.
  6. Hunt, and bet first. Now you are the predator. Before you start, make two bets. How many of ten strikes will catch a fish? And will it be easier once the fish stop attending to each other? Strike ten times and note the count. Then turn sensory precision down, restock, and strike ten more. We bet that a strike on fish that hardly attend to each other (precision 0.05) would catch at least one and a half times as often. We lost. The autopilot caught a fish in 7 of 33 strikes on the coordinated school and in 7 of 32 with precision turned down. In this model each fish still senses the predator for itself, so its escape owes little to its neighbours.

The mathematics

What is being computed

Every fish runs the same small model, a reduced form of the one in Heins et al. (2024). The numbers under each equation are those of one fish at this moment: . Choose another with “Inspect one fish”.

1 · What a fish expects, and its prediction errors εy=y−μ,εy′=y′−μ′,εω=μ′+α(μ−η)

In each sector, y is the sensed mean distance to the nearest neighbours and y′ its sensed rate of change. μ and μ′ are the fish's beliefs about that distance and its rate of change. The fish expects any distance to relax towards its preferred value η at rate α. A prediction error is the gap between what is sensed and what is believed (the first two), or between a belief and what the fish's own model expects of it (the third).

2 · Free energy of one fish F=∑l12[Πyεy2+Πyεy′2+Πωεω2]+12Πs(s−s0)2

Variational free energy F is, under Gaussian assumptions, the sum of squared prediction errors, each weighted by its precision Π (an inverse variance: how much that error is trusted). The sum runs over the sectors that contain a neighbour, and over the predator when it is sensed (with the looming term of equation 5). The last term is the fish's expectation of its own cruising speed s0. Terms that depend on neither belief nor action are left out, so only differences in F are meaningful.

3 · Perception: beliefs descend F μ˙=μ′+κμ(Πyεy−Πωαεω)μ˙′=κμ(Πyεy′−Πωεω)

This is generalised filtering: each belief moves down the gradient of F, and the belief about distance is also carried along by the belief about its rate of change.

4 · Action: heading and speed descend the same F θ˙=−κθ∂F∂θ=κθs∑lΠyεy′(r^l·n^)s˙=−κs∂F∂s=κs[∑lΠyεy′(r^l·t^)−Πs(s−s0)]

A fish cannot change its beliefs' targets, but it can change what it senses. Turning (heading θ) or changing speed s alters how fast each sector's distance is changing: moving towards a sector's neighbours makes that distance shrink faster. The unit vector r̂l points from the fish towards the neighbours in sector l; t̂ points along the fish's heading and n̂ to its left. Action enters only through this effect on sensation, weighted by the same precision and the same error as in equation 2. There is no rule here for attraction, repulsion or alignment. A neighbour that is too far, or receding, produces an error that turns the fish towards it; one that is too close, or approaching, turns it away.

5 · Looming: one more prediction error when the predator closes λ=max(0,−yp′)max(yp,ymin)=1τ,F←F+12Πλλ2

The predator is a fifth channel, treated like a sector: a sensed distance yp to the nearest part of its body, with its own large preferred distance. On top of that the fish senses how fast the predator is closing relative to how far away it is. This ratio λ is the rate at which the predator's image grows, and it equals one over the time to collision τ at the present closing speed (Lee 1976). The fish's model of calm water predicts λ = 0, with precision Πλ, so λ itself is the prediction error. It is small for a predator cruising at a distance and grows steeply in the last moments of a lunge. It moves the fish by the same rule as equation 4: turning or speeding up changes yp′, and so changes λ. A fish answering it may swim far above cruising speed for a moment, a fast start, until a reserve that takes three seconds to refill is spent. That reserve is a limit on the body, not a rule of behaviour. This term and that limit are ours; they are not in Heins et al. (2024).

6 · What is measured of the group P=|1N∑iv^i|,M=|1N∑ir^ic×v^i|

Polarisation P is the length of the average heading: 1 when all fish point the same way. Rotation M is the average turning of headings about the group's centre: 1 for a perfect mill. Here v̂i is the unit heading of fish i and r̂ic the unit vector from the centre of the group to that fish (Couzin et al. 2002). The fish do not compute these; they are an observer's summary.

Reading

Where this comes from

What is established

Fish schools, bird flocks and insect swarms move between a few collective states: a disordered swarm, a rotating mill and a polarised group. Couzin et al. (2002) showed these states in a model of local attraction, repulsion and alignment, and gave the order parameters used here. Tunstrøm et al. (2013) found the same three states in real golden shiners.

Heins et al. (2024) showed that such rules need not be written in. Agents that each minimise surprise about the distance to their neighbours, by updating beliefs and by moving, produce cohesion and coordinated motion; the familiar social forces appear in the gradient of the free energy. This page follows that model in reduced form.

Schools of real fish answer an attack with a small repertoire of collective manoeuvres. Magurran & Pitcher (1987) and Pitcher & Parrish (1993) describe, among others, flash expansion, in which fish burst outward from the point of attack; the fountain effect, in which the school parts in front of the predator, passes it on both sides and joins again behind it; and the vacuole, a clear space that opens round a predator inside the school. A single fish answers a sudden threat with a fast start, a brief burst far above cruising speed (Domenici & Blake 1997). The image of an approaching object grows at a relative rate equal to one over the time to collision (Lee 1976).

Under the free energy principle, groups of this kind have also been modelled as agents in their own right, with the individuals in the role of the group's states (Friston & Frith 2015; Palacios et al. 2020; Heins et al. 2024). That is a modelling proposal. The outline drawn round each group on this page is only the convex hull of its fish. No statistical boundary (Markov blanket) is computed here, and whether such boundaries exist in loosely coupled systems is debated (Aguilera et al. 2022; Biehl et al. 2021).

The free energy principle is a principle and a modelling framework. This page illustrates it; it is not evidence for it, and it does not show what real fish compute.

What this model leaves out

Compared with Heins et al. (2024): each fish attends to at most its eight nearest neighbours in each of four quarter-circle sectors within a sensing range of 8 body lengths, where the published observation is an average over the neighbours in a sector. Beliefs are kept to two generalised orders (distance and its rate), with one sensory precision for both; the paper's separate smoothness parameters are not modelled. The fish here do not learn or adjust their model's parameters while swimming.

Added here, and not taken from that paper: the prior on a fish's own speed; limits on turning rate and on speed, and the fast-start reserve that lifts them for a moment; random noise on heading; the predator, treated as one more sensed distance with a large preferred value; and the looming term. These choices are ours.

The predator is not an active inference agent. It is a body with limits on turning, speed and acceleration, steered by you or by a short autopilot that circles the densest part of the school and lunges into it. The fish sense the nearest point of a line from its snout to its tail, not its outline. A fish within 0.8 body lengths of the snout as a lunge ends counts as caught. The predator is never confused by numbers, and no fish hides behind another, so two of the benefits claimed for real schools are absent. In our test a school whose fish hardly attended to each other was no easier to catch from (7 of 32 strikes against 7 of 33), although we had bet that it would be.

The names of the manoeuvres come from descriptions of real fish, which are in words. The criteria used here to decide when to show a name are ours: the distances, fractions and time limits beside each measurement. One threshold of the fountain test was set after looking at trial runs. A predator that the fish cannot sense was run as a control and earned none of the names.

Real schools can pass a turn from fish to fish as a wave that outruns the fish themselves (Herbert-Read et al. 2015). This model does not reliably do so. In a test with a predator held still in front of the school, the fish that sensed it turned sharply, but among fish that never sensed it the time of turning was only weakly tied to distance from the predator: a clear front appeared in one run of four. So nothing on this page is called a wave. In a strike most of the nearby fish sense the predator directly.

The water is flat and wraps round at its edges; fish have no bodies to collide and cannot hide one another from view. The picture is drawn as if seen from the side, and the view stays with the predator when there is one. Fish and predator are drawn larger than life so that they can be seen. The differences in depth and size, the roll of a turning fish and the flash of its flank, the bubbles and the pale wake of a lunge, the screen shake and the sound are for appearance only. Slow motion changes the speed of the model's clock and nothing else. The settings for speed, noise and rates were chosen to give a clear picture, not fitted to data.