Active Inference InstituteSimulations

Chapter 2BeginnerNew edition

Apple or frog? Perception as inference

Something green sits among the apples. You expect an apple; then it jumps. What you see next is not read off the eye: it is worked out, from what you expected and from how well each explanation predicts the jump. Press It jumps again and watch the belief move.

P(apple) is the rest. The textbook uses 0.10.
How often a frog jumps while you watch.
How often an apple appears to jump.
Really on the bough setting

Free energy of any belief

Hold a belief Q of your own. The curve is its variational free energy. It touches the surprise line at one point only: the exact posterior.

Dragging this holds the belief where you put it.

Look again, or look away

Each new observation uses the last posterior as its prior. The other route is action: look at a different tree, where what is seen is likely to fit what is expected.

ChoiceExpects a jumpExpected surpriseChosen with
Keep looking here
Look at another tree

Expected surprise is how badly the outcomes each choice predicts fit the outcomes the observer prefers, in nats. Lower is better.

How strongly the observer expects still, apple-like sights. 0.50 is no preference.

AII: EduActive · Watch, then do

Three short clips, three things to try

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

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

    Before you start

    Bayes’ rule in plain words

    Bayes’ rule is the exact way to change a belief when new evidence arrives. It has four parts: what you believed before, how well each explanation predicts what you saw, how likely that sight was overall, and what you believe afterwards. You do not need the algebra to follow the idea.

    Listen for
    Bayes’ rule is the formal mathematical way in which we go about changing our beliefs in accordance with sensory evidence.
    Then try
    The observer is 90% sure of an apple. Say how sure of a frog it should be after one jump. Then press the button, and press “It jumps” under the picture.
    In the model equation 1

    The four parts are the four terms of equation 1: the prior P(x), the likelihood P(y | x), the evidence P(y) and the posterior P(x | y). In the square on the stage the evidence is the lit area, and the posterior is each lit rectangle as a share of it.

    The speaker is reading a different textbook, and its equation 1.2 is the same rule as this page’s equation 1. The speaker adds that the rule is too hard to apply in most models of interest, because of the evidence term. That is not the case here. With two states the evidence is a sum of two products, and the page computes it exactly.

    Equation 1, Bayes’ rule

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

    While it runs

    The frog, the apple and the jump

    The textbook returns to one small example again and again. A thing is either a frog or an apple, and it either jumps or does not. Each time you look, you adjust your belief about which it is. That adjusting is the core of this whole family of models.

    Listen for
    This example that’s going to come back again and again is a person who’s guessing the object that’s in their hand, and it could be a frog or an apple, and the object is going to jump or not.
    Then try
    A frog really is on the bough. It jumps on most looks, but not on all. Predict what one still moment does to a belief built on several jumps. Then press the button, and press “Watch again” a few times.
    In the model equation 4

    Looking again is equation 4: the posterior after one observation is the prior for the next. The numbers on the sliders are the textbook’s, a prior of 0.9 on apple and likelihoods of jumping of 0.81 and 0.01.

    The speaker has the object held in the hand; this page puts it on a bough, which changes nothing in the model. The passage also mentions surprise and says that large problems need approximations. Here nothing is approximated, and the two kinds of surprise are kept apart in equation 2.

    Equation 4, observing again

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

    Afterwards

    An upper bound on surprise

    Surprise measures how badly your model predicted what you saw. In most models it is too hard to work out exactly. Free energy is a number that can be worked out, and it is never lower than surprise. So pushing free energy down brings it towards surprise from above.

    Listen for
    We know that surprise might not be equivalent, but we know that it won’t be any higher than variational free energy.
    Then try
    The belief is now yours to hold. Predict where the curve is lowest. Then press the button, drag “Your belief Q(frog)” and try to bring free energy below the dashed surprise line.
    In the model equation 3

    The last line of equation 3 writes free energy as a divergence plus surprise. A divergence is never negative, so F sits on or above −ln P(y), and the two meet only when Q is the exact posterior. The curve under the stage is F as a function of Q(frog), and the dashed line is the surprise.

    The speaker’s reason for using the bound is that surprise cannot be computed in large models, and mentions Jensen’s inequality and other approximations. None of that is needed on this page. With two states the surprise is computed exactly, which is why both it and the bound can be drawn.

    Equation 3, variational free energy

    Further viewing: why an upper bound is used, and textbook equation 2.5, 20:04 · a toy example in which F is lowered step by step until it equals surprise, 42:28 · surprise and Bayesian surprise as two distinct ideas, 3:43

The quoted lines are taken from the automatic captions, with punctuation added and hesitations removed. The speakers are working through the textbooks, 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. Predict the posterior before you see it.

    The observer is 90% sure of an apple. Frogs jump 81% of the time, apples 1%. If the thing jumps, how sure of a frog should the observer be? Set your guess, then press It jumps. In the square, the answer is the share of the lit area that lies in the frog column.

  2. Make the prior stubborn.

    Now the observer is 99% sure of an apple. Guess how many jumps it takes before a frog is the better explanation, then press It jumps and count. Afterwards drag the prior to exactly 0: the frog column has no width, so no amount of jumping can ever fill it.

  3. Try to get free energy below surprise.

    The belief Q is now yours to hold. Drag Your belief Q(frog) anywhere you like and watch the dot ride the curve. Predict where it is lowest. The gap above the dashed line is the divergence between your belief and the exact posterior, and it is never negative.

  4. Make the observation useless, then impossible.

    Both likelihoods are set to 0.30, so a jump says nothing about what jumped. Predict the posterior. It equals the prior, and the Bayesian surprise is 0, although the surprise is not. Then drag both likelihoods to 0: the model now says a jump cannot happen, and the page reports that instead of a number.

  5. Keep watching a real frog.

    A frog is really there, and the textbook’s numbers are back. Press Watch again many times. Each outcome is drawn with the frog’s own probabilities, so it sometimes sits still. Predict what one still moment does to a belief built on several jumps, and watch the accumulated log evidence fall with every look.

  6. Take the other route: act.

    The thing has jumped and the observer could change its mind. Instead, press Look at another tree. Compare the surprise of what is seen there with the surprise of the jump. Then weaken the preference to 0.50 and see the two choices become equally good.

The mathematics

What is being computed

The notation is that of chapter 2 of the textbook: x is the hidden state (frog or apple) and y is the observation (jumps or does not jump). Later chapters write s and o for the same things. Every value below is recomputed from the sliders and the latest observation.

1 · Bayes’ rule (textbook eq. 2.1 and 2.2) P(x|y)=P(x)P(y|x)P(y) P(y)=∑xP(x)P(y|x)

The prior P(x) is the belief before looking. The likelihood P(y | x) says how probable each observation is under each explanation. Their product is a joint probability: one rectangle of the square. The evidence P(y) is the lit area, and the posterior P(x | y) is each lit rectangle as a share of it.

2 · Two kinds of surprise (eq. 2.3 and 2.4) ℑ(y)=−ln⁡P(y) DKL[P(x|y)‖P(x)]=∑xP(x|y)[ln⁡P(x|y)−ln⁡P(x)]

Surprise ℑ scores how poorly the model predicted the observation. Bayesian surprise is a Kullback–Leibler divergence: it scores how far the belief had to move. They are different things. An observer certain of apples has zero Bayesian surprise when the thing jumps, because it does not update at all, yet its surprise is large.

3 · Variational free energy (eq. 2.5)
F[Q,y]=EQ(x)[ln⁡Q(x)−ln⁡P(y,x)]energy − entropy
F[Q,y]=DKL[Q(x)‖P(x)]−EQ(x)[ln⁡P(y|x)]complexity − accuracy
F[Q,y]=DKL[Q(x)‖P(x|y)]−ln⁡P(y)divergence + surprise

Q(x) is any belief at all about the hidden state; EQ is an average taken under it. A divergence is never negative, so the last line gives F ≥ −ln P(y): free energy is an upper bound on surprise, and the bound is exact when Q is the exact posterior. Perception lowers F by moving Q. With Let perception run pressed, the log-odds of Q move at a rate proportional to −∂F/∂Q(frog), which carries Q to the posterior.

4 · Observing again P(x|y1:t)=P(x|y1:t−1)P(yt|x)P(yt|y1:t−1) ln⁡P(y1:t)=∑τ=1tln⁡P(yτ|y1:τ−1)

The thing on the bough does not change between looks, so the posterior after one observation is the prior for the next. The log evidence of the whole run is the sum of the log evidence of each look, which is minus the sum of the surprises. After Look at another tree the thing is a new one, so the prior starts again from the slider.

5 · The choice between looking here and looking away S(u)=−∑yQ(y|u)ln⁡C(y) Q(y|u)=∑xP(y|x)Q(x|u) Q(u)=σ(−S)

Here u is an action: keep looking here, or look at another tree. Q(x | u) is what the observer expects to be there: its current belief if it stays, its original prior if it looks at something new. C(y) is the distribution of outcomes it prefers, and σ is the softmax, which turns scores into probabilities. S is the expected surprise of the outcomes under those preferences. This is only half of the quantity used in active inference. The full score, expected free energy G, also subtracts the information a look is expected to yield; it is shown in the live line for comparison and taught in later simulations.

Reading

Where this comes from

By acting on the world to change the way in which data are generated, we can ensure a model is fit for purpose by choosing those data that are least surprising under our model.Parr, Pezzulo & Friston (2022), Active Inference, chapter 2, p. 24

What is established

The example, its numbers and its equations are those of chapter 2 of Parr, Pezzulo and Friston (2022): a prior of 0.9 on apple, likelihoods of jumping of 0.81 for a frog and 0.01 for an apple, a posterior of 0.9 on frog after a jump, a surprise of about 2.4 nats and a Bayesian surprise of about 1.8 nats. Bayes’ rule and the identity F = divergence + surprise are mathematics, true of any probabilistic model.

That free energy bounds surprise, and that it can be lowered either by changing beliefs (perception) or by changing what is sampled (action), is the central construction of the free energy principle (Friston 2010) and of active inference (Friston et al. 2017). The term Bayesian surprise for the divergence between posterior and prior follows Itti and Baldi (2009).

The free energy principle is a principle and a modelling framework. This page illustrates it; it is not evidence that brains work this way.

What this model leaves out

With two states the exact posterior is easy to compute, so nothing here needs to be approximated. Free energy earns its place in models where the evidence P(y) cannot be computed and Q must come from a restricted family. That is the subject of chapter 4.

The choice between looking here and looking away uses expected surprise only, one step ahead, with a preference you set by hand. Active inference scores policies by expected free energy, which adds the value of information, plans over several steps, and includes habits and a precision on policy beliefs (chapters 4 and 7; Da Costa et al. 2020; Smith, Friston & Whyte 2022).

The world here is the simplest possible. The thing never changes between looks, and the other tree holds a frog with the same probability as the prior slider says. The observer’s model and the process that generates its observations share the same likelihoods, which the textbook is careful to say need not be true. Nothing is learned: the likelihoods stay where you put them. The picture of the frog and apple is a drawing of the belief Q, not a model of vision.