Active Inference InstituteSimulations

ArcadeChapters 4 and 8Perception and movement

Rally: be where the ball will be

To hit a ball you must be where it will be, not where it was. The player holds a belief about the ball, predicts ahead, and moves so that the outcome it prefers, ball meets paddle, comes true. Watch the agent do it, then take the paddle yourself.

How far each glimpse of the ball lands from the ball (standard deviation). A setting.
Random pushes on the ball's vertical speed. A setting.
The sensory precision the agent assumes, relative to the true one. A setting.

Learn by doing

Try this

  1. Play. Predict first: where should your paddle be while the ball is still far away? Then play against the wall. Drag on the screen, or use W and S, or the arrow keys. After each miss, one line under “Now” reports what your paddle did.
  2. Watch the agent, with its beliefs shown. Predict: will the blue band on the paddle line be wide or narrow just after the ball leaves the wall? The blue bar on the ball is the agent's belief about its height. The dashed line is the predicted path. The band on the paddle line is where it expects the ball to cross. Watch the band narrow as the ball comes closer, and the paddle wait at its centre.
  3. Turn on the fog. Predict: what happens to the blue bar while the ball cannot be seen? In the fog no glimpses arrive, so the belief can only spread, by exactly the amount the gusts allow. When the ball reappears, watch the first prediction error and the jump in free energy.
  4. Raise the sensory noise. Predict: will the agent follow each noisy glimpse (the green crosses) or smooth over them? Watch the gain K fall: each glimpse now moves the belief less, because the eyes have become less precise than the prediction.
  5. Mis-set the trust, both ways. Predict which mistake makes the paddle tremble and which makes it late. First tell the agent its noisy eyes are sharp. Then tell it they are dull while gusts are strong. Then set the trust right, and measure all three in the panel below.
  6. Play against the agent in fog. Predict: who loses more points in the fog, the player who follows the last seen position or the one who predicts? With “Show beliefs” on, the agent's prediction is drawn on your side too. Use it as a coach, then turn it off. A match is first to 5 points. “Skill” sets how hard the agent is to beat, and the note beside it says exactly what it changes.

Measure it

How much to trust the eyes

Precision is the inverse of variance: how reliable a signal is taken to be. The agent weighs each prediction error by the precision of its senses relative to the precision of its own prediction. That weight is the gain K.

The chart replays one and the same ball, with the same noise and the same gusts, to an agent whose assumed sensory precision runs from 64 times too low to 64 times too high. The tracking error is the root-mean-square distance between the believed and the true height of the ball. If the assumed precision is too high, the belief chases the noise. If it is too low, the belief is slow to notice a gust. On average the error is least where the assumed precision is the true one. With a finite sample the measured minimum can land one notch to either side.

Tracking error (px, measured)Your trust setting

An experiment

Neurons in a dish that learned to rally

In 2022 Kagan and colleagues grew cortical neurons, from mouse embryos and from human stem cells, on a chip of electrodes, and connected each culture to a simple bat-and-ball game. Eight electrodes in a sensory area were stimulated to report where the ball was. Activity in two motor areas moved the paddle up or down.

There was no reward. When the paddle met the ball, the culture received a brief, predictable stimulus, on all the sensory electrodes together at 100 Hz for 100 ms, and the game carried on. When it missed, it received four seconds of stimulation at random sites, and the ball restarted in a random direction. The reasoning came from the free energy principle: a system that minimises the surprise of its input should come to act in ways that avoid unpredictable input.

The authors report that the average rally grew longer between the first five minutes of play and the following fifteen in the cultures, and not in their control conditions. When the feedback was removed, so that the ball simply bounced on after a miss, cultures did not learn. How the neurons achieve this is not established, and the paper's use of the word “sentience” was disputed in published correspondence (Balci et al. 2023).

The principle, small enough to compute

The chart is not a model of neurons. It is a paddle controller with four adjustable weights, which set where the paddle goes for a ball seen low, lower-middle, upper-middle or high. After each ball it receives feedback on eight channels: the same pattern after a hit, random values on random channels after a miss. It predicts its feedback with a running average and scores each ball by its squared prediction error, which is the surprise of that feedback under a Gaussian model. It is never told what a hit is. Its one learning rule is to try a small random change of its weights, then the opposite change, and to shift the weights towards whichever gave the less surprising input. In the control, the feedback is random after every ball, so nothing the paddle does makes the input more predictable.

Predictable after a hit, random after a missControl: random after every ball

The mathematics

What is being computed

The agent's generative model is its account of how its sensations are caused. Here it is the simplest moving thing: a height y and a vertical speed v, pushed by random gusts, seen through noise. The ball's horizontal progress is taken as known. Every number below is read from the running game.

1 · The generative model (model code: kfMove, kfDiffuse) xt+1=Fxt+w,x=(y,v),F=(1Δt01),w∼N(0,Q) yobs=Hx+ν,H=(10),ν∼N(0,Πy−1)

F moves the ball on by one time step Δt. Q is the variance the gusts add to the speed in one step. Πy is the sensory precision. A bounce off the top or bottom wall is a reflection, so the path with walls is a straight path folded like a concertina. The belief is carried in the unfolded space, where the equation holds with no walls. The agent hears each bounce, and uses it only to know which fold of the concertina it is looking at. A reflection flips height and speed together, which leaves the covariance unchanged, so filtering and prediction through any number of bounces are exact.

2 · Perception: weigh the prediction error by relative precision (kfUpdate) ε=yobs−Hμ^,K=ΠyΠy+Πprior,μy=μ^y+Kε

μ̂ is the predicted state before the glimpse and μ the belief after it. The prediction error ε is the glimpse minus the prediction. Πprior is the precision of the predicted height, one over its variance. The gain K is the share of the total precision that belongs to the senses. This is the Kalman filter (Kalman 1960), the exact Bayesian answer for a linear model with Gaussian noise. Predictive coding under the same assumptions settles on the same mean (Bogacz 2017). The speed is corrected by the same error through its covariance with height.

3 · The free energy of one glimpse (kfUpdate, field F) F=12ε2S+12ln(2πS)=−lnp(yobs),S=Πprior−1+Πy−1

S is the variance the agent expected of its prediction error. Free energy is an upper bound on surprise, the negative log probability of a sensation under the model. Because the filter is exact here, the bound is tight and the two are equal. A glimpse far from the prediction, or a prediction that was unsure, costs more. If trust is set correctly, ε²/S averages one; if the eyes are over-trusted it averages more than one.

4 · Predicting ahead to the paddle line (intercept, foldedMoments) uT∼N(μy+μvT,sT2),sT2=Pyy+2TPyv+T2Pvv+q∑jtj2

T is the time until the ball reaches the paddle line. P is the covariance of the belief. The last term adds the gusts still to come: each future gust of variance q moves the crossing point by its size times the time tj it has left to act. The crossing point is uT folded back into the court. That folded distribution is the blue band. In the fog no glimpses arrive to shrink P, so the belief spreads by the same rule.

5 · Action: a reflex fulfils the prediction (refresh, movePaddles) E[(yT−a)2]=Var[yT]+(E[yT]−a)2⇒a*=E[yT] p˙=clamp(κ(a*−p),±umax)

The agent's prior preference is that the ball meets the centre of its paddle. For a paddle at height a, the expected squared miss is the variance of the crossing point plus the squared distance from its mean, so the best place to be is the mean a*. The agent predicts that its paddle is there. The difference between that prediction and the sensed paddle position p is a proprioceptive prediction error, and the paddle's velocity is proportional to it, up to a top speed. Nothing is commanded: a prediction is issued and a reflex fulfils it (Friston et al. 2010; Adams, Shipp and Friston 2013). “P(out of reach)” is the share of the blue band that the paddle could not cover before the ball arrives, even at top speed.

In this setting, Active Inference is just predictive coding plus reflex arcs.Parr, Pezzulo and Friston (2022), Active Inference, chapter 4, p. 83

Reading

Where this comes from

What is established

The Kalman filter is the exact Bayesian estimator for a linear system with Gaussian noise (Kalman 1960). The textbook gives the same pair of equations as the generative model that underwrites such filters (chapter 4, equations 4.15 and 4.16), and Bogacz (2017) shows step by step how a predictive coding network arrives at the same precision-weighted estimate.

In active inference, movement is described as the fulfilment of proprioceptive predictions by reflex arcs, not as the execution of motor commands (Friston, Daunizeau, Kilner and Kiebel 2010; Adams, Shipp and Friston 2013; textbook chapters 4 and 8).

Kagan and colleagues (2022) report that cultured neurons improved at a bat-and-ball game when misses were followed by unpredictable stimulation and hits by predictable stimulation. The free energy principle (Friston 2010) is a principle and a modelling framework. This page illustrates it. It is not evidence for it.

What this model leaves out

The agent is told the ball's horizontal position and speed, and the strength of the gusts. Only the height and vertical speed are inferred. That keeps the model linear and the filter exact. A fuller model would infer all of these.

The agent hears each bounce off a wall and so knows which mirror image of the court it is seeing. It does not use the bounce as a measurement of the ball's height, which it could. When the agent predicts through your return, it assumes your strike point is random about the centre of your paddle, which is an approximation.

The textbook's continuous-time scheme uses generalised coordinates of motion and smooth fluctuations. This page uses discrete time steps and white noise, the classical filtering assumptions. The agent does not learn its precisions; you set them. It has no plan beyond the next crossing, and does not aim its returns.

“Skill” is a handicap made of three model settings and nothing else: the top speed of the agent's paddle, a factor on the noise in its glimpses (it assumes the matching precision, so its filter stays exact for its own eyes), and a delay between prediction and movement. The delay is applied to the agent's target, the predicted crossing point, which is already a prediction forward in time. When you play alone, or the agent plays alone, it is at Master. The levels were set against a scripted follower, not against people: it sees the drawn ball, reacts 0.2 s late, moves at up to 600 px/s and tracks the ball. Over 60 seeded matches it took 72 % of points from Novice, 43 % from Club and 5 % from Master (the page's test file, bets 11a to 11c). Top speed does most of the work; noise and delay of this size change the result by a few points.

The four-weight controller is a demonstration of a principle and makes no claim about how neurons learn. Its constants were tuned on one set of seeds and its claim then tested on another.