Active Inference InstituteSimulations

Chapters 4 and 7BeginnerStart here

The thermostat that plans

This radiator takes half an hour to get hot, and a cold front is on its way. A thermostat that only reacts starts heating once the cold has arrived, which is too late. A thermostat that plans imagines the next half hour and starts early. Switch between React and Plan, and compare the two counts.

The gold band is the comfort band, 20 to 22 °C. Blue blocks along the top of the chart are cold fronts, past and forecast. Everything to the right of “now” is what the thermostat imagines.

The thermostat

Settings (changing one restarts the evening)

The planning horizon, in steps of 5 minutes. React always looks one step ahead.
Time from cold to full heat. At 5 minutes the radiator has no lag in this model.

AII: EduActive · Watch, then do

Two short clips, two things to try

These two passages come from the Institute’s study groups, where members read the textbooks together and talk them through. Each is under two minutes. Watch one, then press its button and look for the same thing in the room above.

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

    Before you start

    The thermostat loop

    A thermostat is one small loop. The room has a real temperature, which nothing sees directly. The thermostat takes a reading that may be wrong, works something out, and switches the heater. That changes the room, and the loop goes round again.

    Listen for
    You have a thermostat agent. So it’s going to make some kind of observations of temperature. It could be a noisy thermometer.
    Then try
    Three things are easy to mix up: the true temperature, the reading, and the belief. Say which of the last two you expect to stay closer to the truth. Then press, and compare the white line, the green dots and the blue line in the chart.
    In the model equations 1 and 2

    The passage separates the generative process, which is the room, from the agent’s model of it. On this page the process is equation 1, a heat balance in continuous time. The model is two matrices: A for the thermometer, and B, a copy of the same physics in half-degree steps.

    The speaker lists a threshold, a moving average or a Kalman filter as parts that could be swapped in to process the readings. This page uses exact Bayesian updating over 29 temperature bins, a discrete relative of the Kalman filter. In the tests the belief’s typical error was 0.22 °C, against 0.43 °C for a single reading.

    Equation 1, the room · Equation 2, the belief

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

    Afterwards

    Reacting against planning

    A plain thermostat reacts. When the reading falls below its set point it heats, moment by moment, with no view of what comes next. Give it whole sequences of actions to compare, each scored against the state it is drawn to, and it begins to plan.

    Listen for
    That’s a particular sequence of actions defining a policy, and each one of them can get scored relative to some kind of attracting state.
    Then try
    Planning is only as good as its reach. Here Plan compares 4 sequences over ten minutes, where before it compared 64 over thirty, and the radiator still needs thirty to warm up. Predict where its count will fall between the other two. Then press.
    In the model equations 3 and 4

    The “attracting state” of the passage is this page’s preference distribution P(o), a bell curve over readings centred on 21 °C. Each policy is scored by its expected free energy G, summed over the horizon H, and the scores become probabilities through a softmax.

    Two differences. The speaker imagines moves an hour apart; here a move is the valve open or shut for five minutes, and the horizon is at most forty minutes. And the speaker’s reactive thermostat is a bare set point. React on this page is the same agent with H = 1, so it still holds a belief and still scores its two options.

    Equation 3, scoring a plan · Equation 4, choosing

The quoted lines are taken from the automatic captions, with punctuation added and hesitations removed. The speakers are talking about thermostats in general, not about 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

Before each step, say what you think will happen. Then press the button and watch the room, the chart beneath it and the two counts.

  1. Watch React meet a cold front. The chart shows a front on its way, but React looks only five minutes ahead. It opens the valve when the cold arrives. The radiator then needs half an hour to reach full heat, and the room drops below the gold band while it waits.Predict: how cold will the room get before the radiator catches up?
  2. Run the same evening with Plan. The weather and the thermometer errors are exactly the same. Plan imagines the next half hour under every sequence of valve moves it could make. It opens the valve while the evening is still mild, so the radiator is hot when the front arrives.Predict: when will Plan first open the valve, and will the room leave the band?
  3. Send a front yourself and watch the plans change. Your front arrives 20 minutes after you send it. Look at the table called Plans compared. As soon as the forecast changes, the best plan gains filled squares: the thermostat now means to have the radiator hot in time.Predict: is 20 minutes of warning enough for a radiator that needs 30?
  4. Shorten the lookahead to ten minutes. The radiator still takes thirty minutes to warm up. A plan that sees only ten minutes ahead learns about the front too late to use the warning in full.Predict: will Plan still beat React, and by how much?
  5. Take the lag away. Now the radiator reaches full heat within one step. Heating at the last moment is soon enough, so looking further ahead gains nothing. The two counts come out about the same.Predict: which mode will have the lower count?

The mathematics

What is being computed

There are two things here: a room, which obeys physics, and a thermostat, which holds a model of the room. Time moves in steps of five minutes. Every number below is the current value in the simulation, for the thermostat you have chosen.

1 · The room CroomdTdt=Q(h)−Uw(T−Tout,w) Q(h)=QmaxhK,h←h±1per step

T is the air temperature in the room. The radiator puts heat in at a rate Q, in watts. The window and walls let heat out at a rate that grows with the difference between inside and outside. Croom is the heat the room stores for each degree, and Uw is the heat lost per second for each degree of difference, in weather w. The radiator level h runs from 0 (cold) to K (full heat). While the valve is open it rises by one level every step, and while the valve is shut it falls by one. That is the lag: K steps from cold to full heat.

Heat stored by the room, Croom (air, furniture and wall surfaces)1,000 kJ/K
Radiator at full heat, Qmax4,800 W
Mild evening: loss U, outside temperature60 W/K, 6 °C
Cold front: loss U (wind drives air through the window frame), outside150 W/K, −8 °C
Heat lost at 21 °C: mild, cold front900 W, 4,350 W
Thermometer error (standard deviation); small disturbances per step0.4 °C; 0.1 °C
2 · The belief Q(st)∝P(ot|st)∑st−1P(st|st−1,h,w)Q(st−1) surprise=−lnP(ot)

The thermostat never sees T. It sees a reading o, which is the true temperature plus an error. So it keeps a belief Q(s): a probability for each of 29 possible temperatures s, half a degree apart from 14 to 28 °C. Each step it first predicts, using its copy of the physics in equation 1 (the matrix B). Then it weighs the prediction against the new reading, using what it knows about the thermometer’s errors (the matrix A). This is Bayes’ rule, done exactly. Surprise is how improbable the reading was under the prediction. Because the belief is the exact answer, the free energy F of the belief equals the surprise. The thermostat knows its own radiator level and it is given the forecast, so the temperature is the only thing it has to infer.

3 · Scoring a plan: expected free energy G(π)=∑τ=1H[DKL[Q(oτ|π)‖P(oτ)]+EQ(sτ|π)[H[P(oτ|sτ)]]]

A plan, written π and called a policy in the literature, is a sequence of valve moves for the next H steps: open or shut at each one. With H = 6 there are 64 plans. For each, the thermostat rolls its belief forward through its model, using the forecast, and asks what readings it would expect at each step τ. The score G is the expected free energy, and lower is better. Its first part is risk: how far the expected readings are from the readings the thermostat prefers, P(o). Those preferences are a bell curve centred on 21 °C, which falls to 14 % of its peak at the edges of the comfort band. Its second part is ambiguity: how uninformative the readings would be. This thermometer is equally noisy at every temperature, so ambiguity is almost the same for every plan, and risk decides.

React is the same calculation with H = 1. It has two plans, open or shut, and each looks five minutes ahead.

4 · Choosing what to do now Q(π)=σ(−γG(π)) P(open now)=∑π that open firstQ(π)

The scores become probabilities through the softmax function σ, which gives more weight to plans with lower G. The number γ = 4 is a setting, the precision of the thermostat’s beliefs about plans. The thermostat then makes the first move that carries more of the probability: it opens the valve if P(open now) is above one half. Five minutes later it has a new reading, and it scores every plan again. Only the first move of any plan is ever carried out.

“It can use predictive, allostatic strategies (Sterling 2012, Barrett and Simmons 2015, Corcoran et al. 2020) that go beyond homeostasis and preemptively control physiology in an allostatic fashion before interoceptive prediction errors are triggered—for example, finding shade before overheating.”Parr, Pezzulo & Friston (2022), Active Inference, chapter 10, p. 216, on what bodies do. The room on this page is a far simpler case of the same idea.

Reading

Where this comes from

What is established

In active inference, an agent holds a model of how its observations are caused, keeps beliefs about what it cannot see, and prefers some observations to others. It chooses among sequences of actions, called policies, by their expected free energy. The scheme used here, with its matrices A, B and C, is the standard one for discrete time (Friston and colleagues 2017; Da Costa and colleagues 2020; Smith, Friston and Whyte 2022; chapters 4 and 7 of Parr, Pezzulo and Friston 2022). Planning as the evaluation of imagined futures follows Kaplan and Friston (2018).

The difference between correcting an error and acting before it arrives is an old one in physiology. Regulation that waits for an error is called homeostasis. Regulation that predicts a need and meets it early is called allostasis (Sterling 2012). Pezzulo, Rigoli and Friston (2015) set both out in terms of active inference. Baltieri and Buckley (2019) show how a classical feedback controller can itself be read as active inference with a very simple model.

The room follows Newton’s law of cooling for a single store of heat, which is standard building physics. The tests for this page check its claims: with a slow radiator, Plan spent fewer minutes outside the band than React in every one of 20 seeded evenings. With no lag, the two came out about the same.

The free energy principle is a principle and a modelling framework. This page illustrates it. It is not evidence for it.

What this model leaves out

  • The forecast is certain and the thermostat is given it. A real forecast is uncertain, and an agent would have to hold beliefs about the weather too. Because nothing is hidden except the temperature, and the thermometer is equally noisy everywhere, there is nothing here for the thermostat to find out by acting. The curiosity that expected free energy also describes plays no part.
  • The thermostat’s model of the room is correct. It knows the heat capacity, the losses and the lag. Nothing is learned. Its only approximations are the half-degree steps of its temperature scale, which make its predictions slightly more spread out than the room really is.
  • React is not a bare switch with a set point. It is the same agent with a horizon of one step, so it knows what the weather is doing in the next five minutes.
  • The room is one store of heat at one temperature. Real rooms have warm and cold corners, walls that store heat slowly, and sunshine. The radiator rises and falls in equal steps; a real one warms and cools along curves. The front arrives all at once.
  • The valve is either open or shut and is set every five minutes. Cost of fuel is not counted. In the tests Plan had the valve open for about as long as React, within a tenth.
  • The picture is drawn from the model’s numbers: the glow of the radiator follows its level, and the colour of the light follows the true temperature. The mist on the window is drawn from a rough estimate of how cold the glass is. The snow, the tree and the sky are scenery.