# Intuitive Explanation of AIXI

[AIXI](https://www.lesswrong.com/tag/aixi) is a mathematical notion of an ideal RL agent, developed by Marcus Hutter. The famous formula for AIXI is as follows: 

$$
a_k = \text{argmax}_{a_k} \sum_{o_kr_k} \text{max}_{a_{k+1}} \sum_{o_{k+1} r_{k+1}} \dots \sum_{o_mr_m} [r_k + \dots + r_m] \sum_{q : q(a_1 \dots a_k) = o_1r_1 \dots o_mr_m} 2^{-l(q)}
$$

This math seems unintuitive at first (at least to me -- why are there so many sums???), and I couldn't find any simple introductory resources, so this post is my attempt at an intuitive explanation of what this formula means. I try to convey the intuition that AIXI is the obvious formula for an expected reward maximizer in a discrete, finite environment, following the Solomonoff prior. 

Setting
=======

AIXI works in the following setting: There is an *agent*, and an *environment*, which is a computable function. There is a cartesian boundary between the agent and the environment, meaning that the agent is fully separate from the environment. The agent  is uncertain about the environment.

There are a $m$ timesteps, and at each time, each timestep, the agent will perform an action and then receive an observation (a bitstring) and a reward (a number). We will denote the action, reward, and observation taken at the $k$-th timestep as $a_k$, $r_k$, and $o_k$ respectively. 

We want our agent to maximize the total reward: $r_1 + \dots + r_m$. We assume that the environment is a program with source code $Q$, which takes as inputs the agents actions and outputs a sequence of observations and reward:  $Q(a_1 \dots a_k) = o_1r_1 \dots o_kr_k$ .[^vgtrbh2d6y] 

Motivating Example
==================

Suppose that we have the following game, where again at each timestep, you can choose between $R$ and $L$. However, this time, you are not sure which world you are in -- you have multiple codes that might be running the universe. For example, consider the following game tree: 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/2d0d333b83af6393afe6c6437131fd0b9c0c70f21e02a052.jpg)

There are 2 timesteps, and at each timestep you can choose from two actions: L or R. However, in this game you also have uncertainty: after some of your actions, you have uncertainty, represented by the pink and purple branches. We are completely uncertain over each of these splits, and they are independent. At each timestep, you recieve the reward given in green. Think about how you would which action to take first. (You might need to get out pencil and paper!) 

. 

. 

. 

Seriously think for at least 5 minutes (by a timer!) about this, or until you get it. 

. 

. 

. 

Here's the most intuitive approach to me: we proceed bottom up, computing how much expected reward there is at each node. The first step is to collapse all of the uncertainty in the last layer. There is only uncertainty in the bottom left node, in this example, and so we simply compute the expectation: $0(\frac{1}{2}) + 4(\frac{1}{2}) = 2$, and replace that node with a $+2$ expected value: 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/a0f0c6c9152ba3c827f12023a5ed6373b481882e41795cd9.jpeg)

Now, at the bottom of the graph, we are faced with 4 decisions over which action to take. However, it is pretty clear what our agent should do in each of these situations: take the action that maximizes expected reward. So going form the left of the graph to the right, we take L (+2), L (+3), R (-1), and R (+3). Thus, we can add these into the rewards from the first layer.  

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/6957d26320c65d3516ac8f94db3c0e332710415de8033fad.jpeg)

The added values represent total future reward at that point in the game tree.  Now, we are back to uncertainty, so we take the expectation at each node: 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/96f43549f5cf4a3d83c181d8d0f39538fc73c35e1fd15284.jpg)

And thus we see that taking L is better than R, for expected total reward of $+4$. So an ideal agent will choose action L at the first timestep, in this example. 

Formalization of 2-step environment
===================================

We followed a specific, deterministic procedure, which we can write down into a formula for finding the action that gives us the largest expected reward. The procedure we followed was starting at the bottom later of the graph, and then 

1.  taking the expectation over rewards. 
2.  choosing the action with maximum expected future reward. 
3.  repeating 1-2 for the layer above. 

For this example with two actions and two timesteps, writing this in our notation gives: [^9r69xequ0aa]

$$
a_1 = \text{argmax}_{a_1 \in \{L, R\}} \sum_{r_1} \mathbb{P}(r_1 | a_1) [ r_1 + \mathbb{E}(r_2 |a_1) ]
$$

We can evaluate this expectation, which is just the EV of the best action, because we are an ideal agent. That is: 

$$
\mathbb{E}(r_2 |a_1) = \text{max}_{a_2 \in \{L, R\}} \sum_{r_2} \mathbb{P}(r_2 | r_1, a_1, a_2) r_2
$$

Substituting this in yields: 

$$
a_1 = \text{argmax}_{a_1 \in \{L, R\}} \sum_{r_1} \mathbb{P}(r_1 | a_1) [ r_1 + \text{max}_{a_2 \in \{L, R\}} \sum_{r_2} \mathbb{P}(r_2 | r_1, a_1, a_2) r_2 ]
$$

We can rearrange the above expression to something that is more algebraically naturally by moving the $r_1$ inside the second sum, and by realizing that the second action doesn't effect the first reward, so $\mathbb{P}(r_1 | a_1) = \mathbb{P}(r_1 | a_1, a_2)$. Then we combine the two probabilities to get $\mathbb{P}(r_1 | a_1, a_2)\mathbb{P}(r_2|r_1, a_1, a_2) = \mathbb{P}(r_1, r_2 | a_1, a_2)$. Substituting this in gives a cleaner algebraic representation of the ideal action: 

$$
a_1 = \text{argmax}_{a_1 \in \{L, R\}} \sum_{r_1} \text{max}_{a_2 \in \{L, R\}} \sum_{r_2} [r_1 + r_2] \mathbb{P}(r_1, r_2 | a_1, a_2)
$$

Generalization to $m$-step environments
=======================================

Generalizing this to the $k$-th timestep with $m$ total actions is straightforward, we just continue to mix these expectations.  

$$
a_k = \text{argmax}_{a_k} \sum_{r_k} \text{max}_{a_{k+1}} \sum_{r_{k+1}} \dots \text{max}_{a_{m}} \sum_{r_{m}}(r_k + \dots + r_m)\mathbb{P}(o_1r_1 \dots o_mr_m | a_1 \dots a_m)
$$

To see this, start reading the formula from right to left. 

*   $(r_k + \dots + r_m)$ gives the future reward conditional on a specific actions $a_1 \dots a_m$. 
*   $\mathbb{P}(o_1r_1 \dots o_mr_m | a_1 \dots a_m)$ gives the probability of seeing the output $o_1r_1 \dots o_mr_m$ after taking actions $a_1\dots a_m$. 
*   The term  $\sum_{r_m}(r_k + \dots + r_m)\mathbb{P}(o_1r_1 \dots o_mr_m | a_1 \dots a_m)$ gives the expected future reward, added up over the last times. 
*   Each sum runs over all of the possible rewards for a given step. The sums correspond to the expectation over the future reward. 
*   We know that we will take the best action in the future, so we aren't doing to average over all of the possible actions, we compute based off of the max EV action. 
*   After this interleaving of sums and max, we are at a node at the very bottom of the tree, which we simply weight by its probability $\mathbb{P}(o_1r_1 \dots o_kr_k | a_1 \dots a_k)$. The observations here simply refer to which branch of the graph we are in. 

One last point: Even though a gave a tree with probabilities in it, the environment here is not stochastic. There is a true program, $q$, that deterministically maps actions to rewards, and our uncertainty comes from just not knowing which $q$ is correct. All of our [uncertainty is in the mind](https://www.lesswrong.com/posts/f6ZLxEWaankRZ2Crv/probability-is-in-the-mind). 

The Solomonoff Prior 
=====================

In the previous section, I simply gave you the probabilities that you believed over the possible world. But what if we didn't know these probabilities? We need a prior that we can then update. 

Recall that we modeled the world as some program $Q$. [Occam's razor](https://www.lesswrong.com/posts/f4txACqDWithRi7hs/occam-s-razor) gives us a natural way to weight the likelihoods of these different programs according to their simplicity. We'll use length as a measure of simplicity. The [Solomonoff prior](https://www.lesswrong.com/posts/Kyc5dFDzBg4WccrbK/an-intuitive-explanation-of-solomonoff-induction) assigns probability to each program  equal to the inverse of the length of the code that outputs them. 

$$
\mathbb{P}(Q =q) = 2^{-l(q)}
$$

Here, $l(q)$ denotes the length of the program $q$. Recall that we defined: 

$$
\mathbb{P}(o_1r_1 \dots o_kr_k | a_1 \dots a_k) = \mathbb{P}(Q(a_1 \dots a_k) = o_1r_1 \dots o_kr_k )
$$

Expanding over the possible programs that could output that sequence gives: 

$$
\mathbb{P}(Q(a_1 \dots a_k) = o_1r_1 \dots o_kr_k ) = \sum_{q : q(a_1 \dots a_k) = o_1r_1 \dots o_kr_k} \mathbb{P}(Q = q)
$$

$$
= \sum_{q : q(a_1 \dots a_k) = o_1r_1 \dots o_kr_k} 2^{-l(q)}
$$

The probability of seeing the output $o_1r_1 \dots o_kr_k$ is equal to all of the different universe source codes $q$, added up according to their probability.

And finally... AIXI
===================

Plugging in the Solomonoff prior into the previous formula gives 

$$
a_k = \text{argmax}_{a_k} \sum_{o_kr_k} \text{max}_{a_{k+1}} \sum_{o_{k+1} r_{k+1}} \dots \sum_{o_mr_m} [r_k + \dots + r_m] \sum_{q : q(a_1 \dots a_k) = o_1r_1 \dots o_mr_m} 2^{-l(q)}
$$

The key things to understand here are: 

*   $\text{argmax}_{a_k}$ gives the action that returns the highest expected utility. 
*   The rest of $\sum_{o_kr_k}$ sums over the possible outputs $o_k$ and rewards $r_k$ at timestep $k$. 
*   The sum over $q$ just represents the agent's probability of receiving those rewards and observations, conditional on taking all of these actions. Upon seeing a new observation, the updating happens by deleting all of the probability mass on programs that are inconsistent with the observation. This is how AIXI learns.  

Conclusion 
===========

In this post, we saw that AIXI arises very naturally from trying to maximize expected value on any discrete step finite environment, so long as the environment is a computable function.  

So this seems pretty great, but of course one has to remember a few key limitations: 

1.  The Solomonoff Prior is uncomputable.
2.  An AIXI agent is completely separated from the environment, and in particular, it lacks a self model. This causes problems if other agents are logically correlated with it, or simply because it might [drop an anvil on itself](https://www.lesswrong.com/tag/anvil-problem). 

For more information, see [Hutter](http://www.hutter1.net/ai/uaibook.htm#oneline), and these articles about [lots](https://www.lesswrong.com/posts/8Hzw9AmXHjDfZzPjo/failures-of-an-embodied-aixi) [more](https://www.alignmentforum.org/posts/Tr7tAyt5zZpdTwTQK/the-solomonoff-prior-is-malign) [problems](https://www.lesswrong.com/posts/zB4f7QqKhBHa5b37a/introduction-to-the-infra-bayesianism-sequence) with AIXI.

[^vgtrbh2d6y]: Technically, we use a Universal Turing Machine U, and U with tape input \(q\) and then \(a_1 \dots a_k\). Then we say that the output is \(o_1r_1 \dots o_kr_k\) if the output tape of U begins with a string corresponding to \(o_1r_1 \dots o_kr_k\).  

[^9r69xequ0aa]: All of these probabilities are defined as \(\mathbb{P}(o_1r_1 \dots o_kr_k | a_1 \dots a_k) = \mathbb{P}(Q(a_1 \dots a_k) = o_1r_1 \dots o_kr_k )\), but I find it easier to work with the equation on the LHS. We assume that we have some probability distribution over the environment \(Q\). This is implicitly given by the tree in the example, and more generally is given by the Solomonoff prior.