Active Inference InstituteSimulations

Chapters 4 & 8BeginnerNew

A murmuration, one expectation at a time

Dusk over a reed bed. Each starling watches its seven nearest neighbours and expects them to stay about two metres away. No bird is told to follow, to line up or to flee. Watch the flock first. Then build it from a single bird, in six small steps, underneath.

This sky needs WebGL 2, which this browser has switched off. The six steps, the clips and the mathematics below still work.

How many nearest birds each one attends to.
How far away a bird expects each neighbour to be.
Sensory precision: how much weight a bird gives to what it sees.
Gusts that carry the birds and pull the flock out of shape.

Free energy and flying together, the last minute

Free energy per bird (each gridline is ten times the one below)Flying together, 0 to 1Falcon sent, or action switched

Step by step

Build the flock from one bird

Each step below is a small flock seen from above, running the same model as the sky. Each adds one idea to the one before and comes back to the same short sum. Guess what will happen before you press anything. The numbers under each picture are read from that picture as it runs.

  1. 1A gap and an expectation
  2. 2A belief in between
  3. 3Acting on the error
  4. 4Why birds line up
  5. 5Seven neighbours
  6. 6Many birds
  1. One neighbour

    A gap and an expectation

    Two birds. The one in front flies straight on. The one behind senses how far ahead it is, and expects that distance to be a particular value. Everything else on this page grows from the difference between those two numbers.

    The difference y−ηsensed distance minus expected distance
    Predict first
    Start the rear bird far behind, then very close. Where does it end up each time? Then change what it expects.

    New hereThe sensed distance y and the expected distance η. The rear bird settles where the two agree. Steps 2 and 3 open up how.

  2. Inside the bird

    A belief in between

    Hold the rear bird where it is, so it cannot move closer. It still has to settle what it thinks the distance is. Its belief μ is pulled one way by what it senses and the other way by what it expects. How hard each pulls is its precision.

    Two errors and their sum εy=y−μ,εx=μ′+α(μ−η)F=12[Πyεy2+Πyεy′2+Πxεx2]
    Predict first
    Raise trust in the senses. Which way does the belief move along the ruler? Now raise trust in the expectation instead.

    New hereA belief μ, two prediction errors, and free energy F: the errors squared, each weighted by its precision Π. The belief moves to make F small. That is perception. The middle term, about how fast the distance is changing, arrives in step 3.

  3. The other way out

    Acting on the error

    A belief can only meet the senses halfway. The bird has a second way to shrink the same free energy: move, so that what it senses changes. It cannot change the distance in an instant. It can change how fast the distance is closing, so that is the error its wings answer.

    Action follows one error εy′=y′−μ′,v˙=−κa∂F∂v=κaΠyεy′r^
    Predict first
    The bird starts held, six metres back, with its free energy high. Release it. Which of the three parts of F falls first, and does any part rise on the way?

    New hereThe sensed rate of change y′, the belief about it μ′, and action. Too far away, the bird believes the gap should be closing; if it is not, the error pushes it along r̂, the direction of its neighbour. Too close, the same error has the opposite sign. No rule says “approach” or “keep away”.

  4. No rule for it

    Why birds line up

    Put the two birds side by side, already the expected distance apart. Turn one of them. At that instant the distance is still right, but it has started to change, and the other bird did not expect that. Cancelling the surprise means turning the same way.

    What the rate of change contains y′=r^·(vj−v)
    Predict first
    When the upper bird turns away, will the lower one follow at once, overshoot, or lag? Watch both headings in the numbers.

    New hereNothing in the sum. The rate of change y′ was already there in step 3. It holds the neighbour’s velocity vj as well as the bird’s own v, measured along the line between them, so matching it means matching the neighbour’s course. This works for a neighbour to the side. A neighbour straight ahead that turns changes y′ very little at first.

  5. The same sum, seven times

    Seven neighbours

    A starling attends to its six or seven nearest neighbours, however far away they are (Ballerini et al. 2008). Give the bird one belief and one set of errors for each. Each neighbour pushes along its own line. The bird feels the average.

    Average over the neighbours F=1k∑j=1kFj,v˙=κak∑j=1kΠyεy′,jr^j
    Predict first
    Drag the orange bird out of place, or press the button. Which arrows grow? Then lower the number of neighbours to one and try again.

    New hereOnly the average. The seven neighbours in this picture hold their course so that the arrows are easy to read. In the sky above, every neighbour is doing the same sum about its own seven.

  6. Everyone at once

    Many birds

    Ninety birds, each doing the sum of step 5 about its own seven neighbours, start out pointing up to 85 degrees either side of straight ahead. Nobody measures the flock. From outside, one number sums up how well they agree.

    Flying together, as an observer measures it P=|1N∑iv^i|
    Predict first
    How long until they share a course: one second, five, or thirty? Then scatter them fully, in every direction, and see whether one flock or several comes out.

    New herePolarisation P: the length of the average heading, 1 when every bird points the same way. The birds do not compute it. Now go back up to the sky and send the falcon. To the birds it is one more neighbour, expected to be 18 metres away.

AII: EduActive · Watch, then do

Three short clips, three things to try

These passages come from three Institute recordings: an interview with Conor Heins, a guest stream on collective intelligence, and a lecture on collective behaviour. Each is between half a minute and a minute and a half. Watch one, then press its button and look for the same thing in the sky at the top of the page.

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

    Before you start

    Folded over on themselves

    Many fish schools are close to flat. A starling flock fills the sky in three dimensions, and its shape folds and shimmers. Researchers film whole flocks to recover where every bird is. What the display is for is still an open question.

    Listen for
    Starling flocks have very interesting three-dimensional structure. You’ve probably seen what they call murmurations, these kind of shimmering… they almost look like chaotic strange attractors, the way that they’re folded over on themselves.
    Then try
    Fine lines will join every bird to the neighbours it is attending to. Say whether you expect the lines to stay inside one sheet of the flock or to cross between sheets where it folds. Then look.
    In the model equation 1

    The lines are the neighbour sets of equation 1: for each bird, the k birds nearest to it at that moment. They are recomputed as the birds move, and a bird keeps its belief about a neighbour only for as long as that bird stays in its set.

    The shape of the flock on this page is not something the birds hold a belief about. It comes from the roost they circle, the height band they keep to and the moving air, which are plain steering terms here and are listed under “What this model leaves out”. The darkness of the cloud is the birds overlapping on the screen and showing more wing as they bank.

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

    While it runs

    Simple rules, or something more

    A murmuration is a classic example of a group doing something no member plans. The usual way to model it is to give every animal a few simple rules for reacting to its neighbours. The birds on this page are given no such rules. Each has an expectation, a belief and a body.

    Listen for
    To be able to do that in a computable way, you assume that individual agents are following simple rules to react to the environment and to their peers.
    Then try
    Take action away. The birds will still sense their neighbours and still update their beliefs, but will no longer steer by them. Say what you expect to happen to the flock and to the free energy line. Then switch action back on.
    In the model equations 3 and 4

    With action off, equation 4 is multiplied by zero for the neighbour channels. Equation 3 keeps running, so beliefs still follow the senses, but nothing brings the senses back toward what is expected. The roost and height terms still steer the birds, which is why they keep circling.

    The speaker is setting out two traditions, simple reactive agents and sophisticated social ones, before presenting a model of his own that is not the one on this page. The contrast drawn here, between writing rules in and letting them fall out of one quantity, follows Heins et al. (2024).

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

    Afterwards

    How sure is each bird

    In this kind of model an animal carries more than a guess. It also carries a sense of how far to trust its guess and how far to trust its eyes. The paper being read aloud found that these private settings change what the whole group does.

    Listen for
    We also explore how individual beliefs about uncertainty… determine collective decision-making accuracy.
    Then try
    Trust in the senses will drop to a tenth of its usual value. Say whether you expect the flock to tighten, loosen or stay much the same. Then watch “Flying together” for half a minute.
    In the model equation 2

    “Trust in the senses” is the sensory precision Πy of equation 2. It weights the two sensory errors in F, and so it sets both how fast a belief follows what is seen (equation 3) and how hard the same error pushes on the body (equation 4).

    The speaker is reading the summary of the preprint that became Heins et al. (2024). The result he reads concerns how accurately a group chooses between options. This page does not test that. In our runs of 2,000 birds, lowering Πy from 2 to 0.2 lowered “Flying together” from 0.88 to 0.84, a small change.

The quoted lines are taken from the automatic captions, with punctuation added and hesitations removed. The speakers are discussing published work, 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 in the sky

  1. Take action away, then give it back. With action off, free energy per bird climbs from about 0.2 to above 10 within twenty seconds, and the birds drift apart. Switch it back on and the line falls again within about ten seconds. Perception alone cannot keep the flock; the birds have to move.
  2. Watch one neighbour only. With a single neighbour the birds pair off. They still circle the roost, because it steers each of them alike, but “In one flock” falls to a few per cent. With two neighbours most are linked again.
  3. Change what they expect. Slide “Expected distance” up to 4 metres and the cloud swells and thins. Nothing else was changed: the birds are making a different expectation come true. “Nearest neighbour” follows it.
  4. Send the falcon. A bird that senses the falcon inside 18 metres has one more channel, with a long expected distance and a neighbour closing at speed. Watch the free energy line jump twentyfold or more, and the flock open and close.
  5. Turn the sound on. The calls are synthesised, two voices to a bird as in the starling’s two-sided syrinx. They are not recordings. Calls come faster when the flock turns hard, and change when the falcon is near.
  6. Put it all back.

The mathematics

All of it in one place

Every bird 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 bird in the sky at this moment and of the nearest of its neighbours: bird 1.

1 · What a bird senses of each neighbour yj=‖rj−r‖+z,yj′=r^j·(vj−v)+z′,j=1…k

The bird is at r with velocity v; neighbour j is at rj with velocity vj, and r̂j is the unit vector pointing at it. The bird senses the distance y and how fast it is changing y′, each with a little random error z. Its neighbours are the k nearest birds, whatever their distance.

2 · Prediction errors and free energy εy=y−μ,εy′=y′−μ′,εx=μ′+α(μ−η)F=1k∑j12[Πyεy2+Πyεy′2+Πxεx2]

For each neighbour the bird holds a belief μ about the distance and μ′ about its rate of change. Its model of the world says that a distance relaxes toward the expected value η at rate α. The first two errors compare the senses with the beliefs. The third compares the beliefs with that model. Under Gaussian assumptions, variational free energy F is the sum of the squared errors, each weighted by a precision Π (an inverse variance). Terms that depend on neither belief nor action are left out, so only differences in F mean anything.

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

This is generalised filtering (Friston et al. 2010). 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: velocity descends the same F v˙=−κa∂F∂v=κak∑jΠyεy′,jr^j

The bird’s velocity enters F only through y′ in equation 1, and the derivative of y′ with respect to v is −r̂j. So each neighbour pushes along the line that joins it to the bird, by an amount set by one error. There is no term for attraction, repulsion or alignment. This is the push asked of the body. The body answers it with the limits listed below.

5 · What is measured of the flock P=|1N∑iv^i|,F¯=1N∑iFi

Polarisation P is the length of the average heading. F̄ is the free energy of equation 2 averaged over the birds; it is the line on the chart. The birds compute neither. “In one flock” is the share of birds in the largest set joined, directly or through others, by who is watching whom. “A change of course is shared across” is the distance at which two birds’ departures from the mean heading stop agreeing, as a share of the flock’s width (Cavagna et al. 2010).

Reading

Where this comes from

What is established

Starlings gather over their roost at dusk in flocks of hundreds to many thousands. Filming flocks over Rome in three dimensions showed that each bird interacts with a fixed number of nearest neighbours, six or seven, however near or far they happen to be, and not with everything inside a fixed distance (Ballerini et al. 2008; Bialek et al. 2012).

A small change in one bird’s course is correlated with changes across the whole flock, whatever the flock’s size (Cavagna et al. 2010). A turn starts with a few birds and crosses the rest as a wave that keeps its speed and hardly fades, which needs the birds’ turning to have inertia (Attanasi et al. 2014). When a falcon attacks, dark bands run through the flock away from it (Procaccini et al. 2011); a model suggests they are birds banking as they turn, so that more wing is seen (Hemelrijk et al. 2015).

Flocks have long been modelled by giving each animal rules: keep apart, stay close, line up (Reynolds 1987; Vicsek et al. 1995; Couzin et al. 2002; for starlings, Hildenbrandt et al. 2010). 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, and the familiar social forces appear in the gradient of the free energy. This page follows that model in reduced form.

Is the flock itself an agent? Under the free energy principle a collective of inferring agents can, if it keeps a statistical boundary of its own (a Markov blanket), be treated as one larger agent with a generative model of its own. Waade et al. (2025) propose a way to estimate that group-level model from the behaviour of the individuals, and find, in a simple task that is not flocking, that it is not a plain copy or sum of the individuals’ models. This page stops one level down. Every belief on it belongs to a single bird, and polarisation and mean free energy are an observer’s summaries.

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 starlings compute.

What this model leaves out

Compared with Heins et al. (2024): that model is in two dimensions, was compared with fish, and has each agent sense the average distance to the others in four visual sectors. Here each of the k nearest neighbours is its own channel, in three dimensions, which is our choice and follows the starling finding. Beliefs are kept to two generalised orders, with one sensory precision for both. The birds do not learn or adjust their model as they fly.

Added here, and not derived from free energy: the pull back toward the roost, the height band, and a liking for level flight (after Hildenbrandt et al. 2010); a cruising speed that each bird returns to, with speed held between three quarters and one and a half times that; a turn rate that lags the turn asked for by about a fifth of a second and cannot exceed a limit; random jitter on turning; slow eddies in the air and the wind. The falcon is treated as one more channel of equation 2, with an expected distance of 18 metres, sensed only inside that range. The falcon itself is not an inferring agent: it flies at the middle of the flock twice and leaves.

A turn does not cross this flock as it crosses a real one. In a test, 160 birds flew as a flat sheet and the birds along one edge were held on a course 35 degrees off for four seconds. The far edge turned by less than 10 degrees, and the held birds separated from the rest. The reason is in equation 1: a bird senses only how fast each distance is changing, and a neighbour ahead or behind that turns changes that very little. In real flocks a turn crosses the whole flock with almost no loss (Attanasi et al. 2014). Heins et al. show that an explicit alignment term can be recovered by also giving agents a model of the angle between headings; that is not done here.

Also absent: what a bird can and cannot see through the others; the flattened shape of real flocks and the way neighbours sit to the side more than ahead; tiredness, hunger and differences between birds; the descent into the reeds. No boundary around the flock is computed and no model is fitted to the flock as a whole, so the page makes no claim that this flock is an agent. Without the roost to hold it, this flock would travel in a straight line. The birds are drawn about twice life size so that they can be seen. The reeds, the water, the banking and the sound are for appearance. The settings for speed, noise and rates were chosen to give a clear picture and were not fitted to data.