# Cartesian frames as generalised models

Scott [presented](https://www.lesswrong.com/posts/BSpdshJWGAW6TuNzZ/introduction-to-cartesian-frames) Cartesian frames/Chu spaces as follows:$\newcommand{\E}{\mathcal{E}}\newcommand{\M}{\mathcal{M}}\newcommand{\W}{\mathcal{W}}\newcommand{\F}{\mathcal{F}}\newcommand{\L}{\mathcal{L}}\newcommand{\wF}{\overline{\F}}\newcommand{\forg}{\rightarrowtail}$

* Let $W$ be a set of possible worlds. A Cartesian frame $C$ over $W$ is a triple $C=(A,D,\cdot)$, where $A$ represents a set of possible ways the agent can be, $D$ represents a set of possible ways the environment can be, and $\cdot: A \times D \to W$ is an evaluation function that returns a possible world given an element of $A$ and an element of $D$.

In a [previous post](https://www.lesswrong.com/posts/nQxqSsHfexivsd6vB/generalised-models-as-a-category), I defined $\mathcal{GM}$, the category of generalised models.

In this post, I'll try and see how these two formalisms relate to each other.

# Equivalence with Cartesian frames

We'll now demonstrate the equivalence of [Cartesian frames morphisms](https://www.lesswrong.com/s/2A7rrZ4ySx6R8mfoT/p/ewkYgtZapQRtDPT2F) with the [morphisms of generalised models](https://www.lesswrong.com/posts/nQxqSsHfexivsd6vB/generalised-models-as-a-category). To do so, and avoid a collision of symbols, I've slightly tweaked the notation for Cartesian frames.

## Equivalence of morphisms

Let $C_0=(A_0,D_0,\star_0)$ and $C_1=(A_1,D_1, \star_1)$ be Cartesian frames over $W$: thus there are relations $\star_0: A_0 \times D_0 \to W$ (written as $a_0\star_0 d_0 = w$) and $\star_1: A_1 \times D_1 \to W$ (written as $a_1\star_1 d_1 = w'$).

A morphism between them is a pair of maps $(g_0: A_0 \to A_1, h_1: D_1\to D_0)$, such that, for all $a_0\in A_0$ and $d_1\in D_1$, $g_0(a_0)\star_1 d_1 = a_0 \star_0 h_1(d_1)$.

How can we express this in the generalised model formalism?

First, let $\E_i = A_i \times D_i \times W$. In terms of features, this can be defined by setting $\overline{f}_{A_i} = A_i$, $\overline{f}_{D_i}=D$ and $\overline{f}_W = W$. Then $\F_i = \{f_{A_i},f_{D_i},f_W\}$, and $\M_i=(\F_i,\E_i,Q_i)$ is the feature-split generalised model with $A_i \subset 2^{\overline{f}_{A_i}} = 2^{A_i}$, $D_i \subset 2^{\overline{f}_{D_i}} = 2^{D_i}$, and $W \subset 2^{\overline{f}_{W}} = 2^{W}$.

As we'll see in the bears example, there can be more interesting ways of defining the feature split $\M_i$.

Then the map pair $(g_0, h_1)$ is equivalent to the (feature-split) relation $r$, defined such that $(a_0,d_0,w) \sim_r (a_1,d_1,w')$ iff:

1. $g_0(a_0)=a_1$,
2. $h_1(d_1)=d_0$,
3. and $w=w'$.

Without loss of clarity, we can thus write $r$ as the feature-split relation $(g_0,h_0,Id_W)$.

Composing $(g_0,h_1)$ and $(g_1,h_2)$ generates $(g_1 \circ g_0, h_1 \circ h_2)$. Take $r$ as the relation defined by $(g_0,h_1)$ and $q$ as the relation defined by $(g_1,h_2)$. Then if $(a_0,d_0,w) \sim_{pr} (a_2,d_2,w'')$, there must exist an $(a_1,d_1,w')$ with $(a_0,d_0,w) \sim_r (a_1,d_1,w') \sim_p (a_2,d_2,w'')$. Then:

1. $g_1g_0(a_0)=g_1(a_1)=a_2$,
2. $h_1h_2(d_2)=h_1(d_1)=d_0$,
3. $w=w'=w''$.

So composition of morphisms $(g,h)$ for Cartesian frames is the same as the composition of corresponding relations $(g,h,Id_W)$.

## The extra structure

We have two structures to add: Cartesian frames have the $\star$ map, while generalised models have the probability measures $Q$; we need to relate them.

One natural way to relate them is to consider that if $a \star d = w$, then we should get $Q(w \mid a,d)=1$ and have $Q(w' \mid a,d)=0$ for $w'\neq w$. This reflects the fact that action $a$ and environment $d$ lead inevitably to world $w$.

Now $Q(w \mid a,d) = Q(a,d,w)/Q(a,d,W)$, where $Q(a,d,W)$ denotes $Q$ on the set $\{a\}\times\{d\}\times W$; this is $\sum_{w'\in W} Q(a,d,w')$.

Hence the desired condition on $Q(w \mid a,d)$ is equivalent with $Q(a,d,w) = 0$ iff $a \star d \neq w$. There are, of course, multiple possible $Q$s with that property for any given $\star$.

## The categorical equivalence

Now let's tie these together, and define $\mathcal{C}(W)$, a [subcategory](https://en.wikipedia.org/wiki/Subcategory) $\mathcal{C}(W)$ of the $\mathcal{GM}$, the category of generalised models. This $\mathcal{C}(W)$ will map surjectively to the category of Cartesian frames.

The objects of $\mathcal{C}(W)$ are those (feature-split) generalised models which have $\E = A \times D \times W$ for some sets $A$ and $D$, and have $Q(a,d,w) = 0$ iff $a \star d \neq w$ for some evaluation function $\star: A \times D \to W$.

The morphisms of $\mathcal{C}(W)$ are those morphisms of $\mathcal{GM}$ that map $\mathcal{C}(W)$ to itself, and that are of the form $r = (g,h,Id_W)$ for $(g,h)$ a morphism of Cartesian frames.

Thus morphisms of $\mathcal{C}(W)$ are derived from morphisms of $\mathrm{Chu}(W)$, and are also compatible with the $Q$ structures (since they are also morphisms of $\mathcal{GM}$). Also included are the identity morphisms $(Id_A, Id_D, Id_W)$, which trivially preserve the $Q$ structures.

To demonstrate that $\mathcal{C}(W)$ is a category, we need to show that $pr$ is a morphism of it whenever $r=(g_0,h_1,Id_W)$ and $p=(g_1,h_2,Id_W)$ are. We know that $pr$ must respect the $Q$ structures (since $r$ and $p$ are morphisms of $\mathcal{GM}$), while $pr=(g_1 \circ g_0, h_1 \circ h_2, Id_W)$, which derives from $(g_1 \circ g_0, h_1 \circ h_2)$, a morphism of $\mathrm{Chu}(W)$.

Thus $\mathcal{C}(W)$ is a category. Let $\Phi: \mathcal{C}(W) \to \mathrm{Chu}(W)$ be the map that sends $(\F, A \times D \times W, Q)$ to $(A,D,\star)$, and sends $r=(g,h,Id_W)$ to $(g,h)$.

This $\Phi$ is clearly a [functor](https://en.wikipedia.org/wiki/Functor) of categories, and it is surjective on the objects of $\mathrm{Chu}(W)$ (the Cartesian frames). Now we need to show that it's also surjective on the morphisms, which comes from the following result:

* Let $(g_0,h_1)$ be a morphism between $C_0=(A_0,D_0,\star_0)$ and $C_1=(A_1,D_1,\star_1)$. Then there exists $\M_0,\M_1\in\mathcal{C}(W)$ and a morphism $r=(g_0,h_1,Id_W)$ between them such that $\Phi(\M_i)=C_i$.

To show that, we need to choose $Q_0$ and $Q_1$ that are compatible with $\star_0$ and $\star_1$, and are compatible with $r$.

In fact, we'll show a slightly stronger result: that for any $\M_0$ with $\Phi(\M_0)=C_0$, we can pick an $\M_1$ (ie pick a $Q_1$) with the required properties.

To show this, note that $r=(g_0,h_1,Id_W)$ will relate every element of $(g_{-1}(a_1),d_0,w)$ with every element of $(a_1, h_1^{-1}(d_0),w)$. In fact, $r$ is defined by such relations, for any $a_1 \in A_1$, $d_0 \in D_1$ and $w\in W$. No other elements are related by $r$.

For compatibility of $r$ with the $Q$s, it suffices that $Q_0(g_{-1}(a_1),d_0,w)$ be equal to $Q_1(a_1, h_1^{-1}(d_0),w)$.

For any $d_1 \in D_1$, define $\#_{d_1}$ as the size of $h_1^{-1}(h_1(d_1))$; since $d_1 \in h_1^{-1}(h_1(d_1))$, $\#_{d_1} \geq 1$.

Then define $Q_1(a_1,d_1,w)$ as $Q_0(g_{-1}(a_1),h_1(d_1),w)/\#_{d_1}$. This will give the compatibility that we want.

Hence $\Phi: \mathcal{C}(W) \to \mathrm{Chu}(W)$ is a surjective functor of categories, from a subcategory of $\mathcal{GM}$, the category of generalised models.

## More functors

Given two sets $W$ and $V$, and a function $p: W \to V$, there is an induced functor $p:\mathrm{Chu}(W) \to \mathrm{Chu}(V)$, sending $(a,d,w)$ to $(a,d,p(w))$ and sending the morphism $(g,h)$ to the morphism with the same underlying functions, $(g,h)$.

Then by the above, we have $\mathcal{C}(W)$ and $\mathcal{C}(V)$ as distinct subcategories of $\mathcal{GM}$, with functors $\Phi_W$ and $\Phi_V$ sending these subcategories to $\mathrm{Chu}(W)$ and $\mathrm{Chu}(V)$.

Then $p$ also induces a functor $\mathcal{C}(W) \to \mathcal{C}(V)$, by sending $(a,d,w)\in\E=A\times D\times W$ to $(a,d,p(w))$. The induced $Q$ is given by $Q(a,d,v)=\sum_{w\in p^{-1}(v)}Q(a,d,v)$.

Note that $p$ is not only a functor $\mathcal{C}(W) \to \mathcal{C}(V)$, it also acts as a morphism between any $\M_0 \in \mathcal{C}(W)$ to $p(\M_0) \in \mathcal{C}(V)$, when both are seen as elements of $\mathcal{GM}$. We'll designate these various morphisms by $p$ as well. As a functor, $p$ maps the relation $r=(g,h,Id_W)$ to $p(r)(g,h,Id_V)$; seeing $p$ as a morphism, $p(r)$ is precisely $prp^{-1}$, where $p^{-1}$ is the opposite morphism to $p$: $(a,d,v) \sim_{p^{-1}} (a,d,w)$ iff $p(w)=v$.

We can see that the morphism $p$ commutes with the $\Phi$s:

* $\Phi_V \circ p = p \circ \Phi_W$.

This is probably enough exploration of the functorial properties of these spaces for one post.

# An example: colours and bears

To illustrate, let's use the Cartesian frame from [this post](https://www.lesswrong.com/s/2A7rrZ4ySx6R8mfoT/p/pWruFSY7494vnucCE); this construction will also show how features can figure non-trivially in this construction.

Here the agent has two unrelated choices: which colour to think about (green, $\mathbf{G}$ or red $\mathbf{R}$) and whether to go for a walk or stay home ($\mathbf{W}$ or $\mathbf{H}$). So $A=\{\mathbf{GH},\mathbf{GW},\mathbf{RH},\mathbf{RW}\}$. The environment is either safe or has bears: $D=\{\mathbf{S},\mathbf{B}\}$.

This gives the following frame $C_0$:

$\begin{array}{ccc}
&&\begin{array}{cc}\mathbf{S}&\mathbf{B}\end{array}\\
C_0=&\begin{array}{c}\mathbf{GH}\\\mathbf{GW}\\\mathbf{RH}\\\mathbf{RW}\end{array}&
\left(\begin{array}{cc}
w_0 & w_1 \\
w_2 & w_3 \\
w_4 & w_5 \\
w_6 & w_7
\end{array}\right)\\
\end{array}$

Of course, $w_0$ and $w_4$ only differ in the colour that the agent is thinking about (similarly for $w_1$ and $w_5$, etc...). We could choose a $C_1$ frame that doesn't distinguish between these thoughts:

$\begin{array}{ccc}
&&\begin{array}{cc}\mathbf{S}&\mathbf{B}\end{array}\\
C_1=&\begin{array}{c}\mathbf{GH}\\\mathbf{GW}\\\mathbf{RH}\\\mathbf{RW}\end{array}&
\left(\begin{array}{cc}
w_0 & w_1 \\
w_2 & w_3 \\
w_0 & w_1 \\
w_2 & w_3
\end{array}\right)\\
\end{array}$

Let $V=\{w_0,w_1,w_2,w_3\}$. Then we can define the various sets through features; specifically, in this example, $\F_A = \{f_{\mathbf{G}/\mathbf{R}}, f_{\mathbf{W}/\mathbf{H}}\}$. Similarly $\F_D=\{f_{\mathbf{S}/\mathbf{B}}\}$.

Adding a definition of $\F_V=\{f_{V}\}$ and $\F_W = \{f_{V}, f_{\mathbf{G} / \mathbf{R}} \}$, we can construct the feature split generalised models:

0. $\M_0 = \{\F_A \sqcup \F_D \sqcup \F_W, A \times D \times W, Q_0\}$.
1. $\M_1 = \{\F_A \sqcup \F_D \sqcup \F_V, A \times D \times V, Q_1\}$.

The $Q_i$ are defined by the matrix above; if we want them to make sense as traditional probability distributions, we might require that $Q_i(a,d,w)=1/8$ whenever it is non-zero, with $8 = ||A \times D||$ the size of the matrix. In that case, $Q_i(\E_i)=1$, as required.


# Notes on non-synonyms

Some of the terminology is repeated between the two formalisms, but doesn't mean the same things. Specifically:

* Environments: for Cartesian frames, this is $D$, the different columns of the matrix. For generalised models, this is the larger set $\E = A \times D \times W$.
* Worlds: for Cartesian frames, this is $W$, the possible values of the elements of the matrix. For generalised models, this is $\W = 2^{\overline{\F}}$, the set of all possible values all the features could take. At the very least, $\W$ contains $\E = A \times D \times W$, but it could be much larger.