Micah Zarin's Blog

Thoughts, Opinions, & Interesting Things

12,596 viewers

how to calculate free energy

This post is a supplement to my upcoming substack post, Beauty is a Loophole.

For those of you who are curious, I’ll explain how free energy is calculated. I will assume very little mathematical background.

So, your brain has a model of the world that includes countless variables, how loud your street usually is, what time of night it is, whether your neighbors are home, how often cars drive by, etc.

Now, imagine a function (a function, for those unaware, you can think of as a transformation, something that transforms one type of value to another type of value, kind of like an input-output relationship: input one number, output another,) over all possible states of the world, every combination of values like “the street is quiet,” “it’s 2 am,” “the neighbors are asleep.” The function will take a set of beliefs, or a model of the world, which we will call x, and spit out a number between 0 and 1, q(x) (q of x).

This number, q(x), represents how strongly your brain believes that state is the true. So if q(x) = 1, the brain thinks there is a 100% chance that the state of the world is “x.”

Now notice, if there are 2 states of the world, x1 and x2, the probability that the state of the world is either x1 or x2, is 100%. So q(x1)+q(x2) = 1. And in general, if you sum all possible values of q(x) (q(x1)+q(x2)+q(x3)…), you should get 1. We call this set of states of the world, the x’s, and their corresponding probabilities, the q(x)’s, a probability distribution.

Now, your brain doesn’t just have one probability distribution. It has two that are important to us. The first is q(x), your current beliefs about the world, right now, and their probabilities, after say hearing a bang sound. It’s the distribution your brain landed on after processing the new sensory data.

The second is p(x), your prior beliefs, the distribution your brain had before the bang. Think of it as your default model of a quiet night at 2am. Before anything happened, your brain had already assigned probabilities to every possible state of the world, and most of that weight was sitting on quiet, uneventful states.

Now we want to measure how far q has shifted from p. In other words, how much did your brain have to move its belief point?

For any given state of the world x, we can ask: how much does q(x) disagree with p(x)? A natural way to measure that is to look at the ratio q(x)/p(x). If q and p agree on that state, both assign it similar probability, the ratio is close to 1. If q assigns a state much higher probability than p did, the ratio is large. If q assigns it much lower, the ratio is small, close to zero.

This is kind of annoying, because q(x) differing with p(x) could make the ratio really large or really small. Luckily there is a function called a logarithm which may be useful here. All you need to know logarithm will transform small numbers between 0 and 1 into very negative numbers (like -1,000,000), and it will transform very large numbers, greater than 1, into very positive numbers.

You should also know that a logarithm converts multiplication into addition, and division into subtraction. So if I input 6*5 into the logarithm (log(6*5), the log of 6 times five), I will output log(6) + log(5).

Notice that log(a) = log(a*1), since multiplying by 1 does nothing. And by our addition property, log(a) = log(a*1) = log(a) + log(1). Since log(a)=log(a)+log(1), subtracting log(a) from both sides gives log(1)=0.

So if I input 1 into the logarithm, I will get 0. So, to summarize, when q and p agree perfectly, or q(x)/p(x) = 1, log(1) = 0, meaning no disagreement. When they differ wildly, the log gets large.

But we don’t care equally about every state of the world. We care more about disagreements in states that q actually thinks are likely. There’s no point penalizing disagreement in states your brain considers nearly impossible. So we weight each disagreement by q(x) itself, multiplying log(q(x)/p(x)) by q(x). So when q(x) = 0, this value will be zero.

Then, all we need to do is sum up all these values of q(x)*log(q(x)/p(x)). The result is a single number that tells you how far your beliefs have shifted from your priors. This is called the KL divergence, and it is always zero or positive, zero only when q and p are identical everywhere, meaning your beliefs didn’t move at all. This is complexity.

Now, complexity only tells you how far your beliefs moved. But moving far isn’t necessarily bad, it’s worth it if your new belief point actually explains what you’re sensing.

So, your current beliefs q(x) make a prediction about what sensory data you should be experiencing. For any given state of the world x, there’s a function p(y|x) (read this as “the probability of observing sensory data y, given that the world is in state x.”) So if x is “quiet night, 2am, neighbors asleep,” then p(loud bang|x) is a very small number, close to zero. If x is “a car just backfired outside,” then p(loud bang|x) is close to 1.

Now, you actually heard a bang. So you want to know: does your current belief q(x) put most of its weight on states of the world that would make a bang likely? If yes, your model explains the data well. If no, your model is stranded somewhere that still can’t account for what you heard.

To measure this, we take log(p(y|x)) for each state x. Since p(y|x) is always between 0 and 1, its logarithm is always zero or negative, zero when that state perfectly predicts the data, and shooting toward negative infinity when that state makes the data seem nearly impossible.

Then, just like with complexity, we weight by q(x), we care most about states your brain currently finds plausible, and sum across all states. The result is a single number that is always zero or negative. Zero means your current beliefs perfectly explain the sensory data. The more your model fails to account for what you’re sensing, the further negative this number gets.

That number is accuracy. Notice it has a ceiling of zero but no floor, a sufficiently shocking sensory event can drive it arbitrarily far negative.

So now you have both pieces. Complexity is a non-negative distance measuring how far your beliefs moved. Accuracy is a non-positive number measuring how well your current beliefs explain the data. Free energy is complexity minus accuracy, subtracting a non-positive number from a non-negative one, which means both terms push free energy up when things go wrong. Your brain is always trying to find beliefs that minimize the total: don’t move too far from your priors, but move far enough to actually explain what you’re sensing.

Leave a comment