[summary: The [-5z9] may be described by a [-600] which is very similar to that of the [5zv universal property of the product]. Specifically, the disjoint union of sets $A$ and $B$ is a set denoted $A \sqcup B$ together with maps $i_A : A \to A \sqcup B$ and $i_B: B \to A \sqcup B$, such that for every set $X$ and every pair of maps $f_A: A \to X$ and $f_B: B \to X$, there is a unique map $\gamma: A \sqcup B \to X$ such that $\gamma \circ i_A = f_A$ and $\gamma \circ i_B = f_B$.]

[summary(Technical): The [-5z9] may be described as the [-coproduct] in the [4c7 category] of [3jz sets]. Specifically, the disjoint union of sets $A$ and $B$ is a set denoted $A \sqcup B$ together with maps $i_A : A \to A \sqcup B$ and $i_B: B \to A \sqcup B$, such that for every set $X$ and every pair of maps $f_A: A \to X$ and $f_B: B \to X$, there is a unique map $\gamma: A \sqcup B \to X$ such that $\gamma \circ i_A = f_A$ and $\gamma \circ i_B = f_B$.]

Where could we start if we were looking for a nice "easy" [-600] describing the [5s8 union of sets]?

The first thing to notice is that universal properties only identify objects up to [-4f4], but there's a sense in which the union is *not* [-5ss] [-65y]: it is possible to find sets $A$ and $B$, and sets $A'$ and $B'$, with $A$ isomorphic to $A'$ and $B$ isomorphic to $B'$, but where $A \cup B$ is not isomorphic to $A' \cup B'$. %%note: In this case, when we're talking about [3jz sets], an isomorphism is just a [499 bijection].%%

%%hidden(The union is not well-defined up to isomorphism in this sense):
Consider $A = \{ 1 \}$ and $B = \{ 1 \}$.
Then the union $A \cup B$ is equal to $\{1\}$.

On the other hand, consider $X = \{1\}$ and $Y = \{2\}$.
Then the union $X \cup Y = \{1,2\}$.

So by replacing $A$ with the isomorphic $X$, and $B$ with the isomorphic $Y$, we have obtained $\{1,2\}$ which is *not* isomorphic to $\{1\} = A \cup B$.
%%

So it's not obvious whether the union could even in principle be defined by a universal property.%%note: Actually [union_universal_property it *is* possible].%%

But we can make our job easier by taking the next best thing: the *[5z9 disjoint union]* $A \sqcup B$, which is well-defined up to isomorphism in the above sense: the definition of $A \sqcup B$ is constructed so that even if $A$ and $B$ overlap, the intersection still doesn't get treated any differently.

Before reading this, you should make sure you grasp the [-5z9] well enough to know the difference between the two sets $\{ 2, 3, 5 \} \cup \{ 2, 6, 7 \}$ and $\{ 2, 3, 5 \} \sqcup \{ 2, 6, 7 \}$.

%%hidden(Brief recap of the definition):
Given sets $A$ and $B$, we define their disjoint union to be $A \sqcup B = A' \cup B'$, where $A' = \{ (a, 1) : a \in A \}$ and $B' = \{ (b, 2) : b \in B \}$.
That is, "tag each element of $A$ with the label $1$, and tag each element of $B$ with the label $2$; then take the union of the tagged sets".
%%

The important fact about the disjoint union here is that if $A \cong A'$ and $B \cong B'$, then $A \sqcup B \cong A' \sqcup B'$.
This is the fact that makes the disjoint union a fairly accessible idea to bring under the umbrella of category theory, and it also means we are justified in using a convention that simplifies a lot of the notation: we will assume from now on that $A$ and $B$ are disjoint.
(Since we only care about them up to isomorphism, this is fine to do: we can replace $A$ and $B$ with some disjoint pair of sets of the same size if necessary, to ensure that they *are* disjoint.)

[toc:]

# Universal property of disjoint union

How can we look at the disjoint union solely in terms of the maps to and from it?
First of all, how can we look at the disjoint union *at all* in terms of those maps?

There are certainly two maps $A \to A \sqcup B$ and $B \to A \sqcup B$: namely, the [inclusion_map inclusions] $i_A : a \mapsto a$ and $i_B : b \mapsto b$.
But that's not enough to pin down the disjoint union precisely, because those maps exist not just to $A \sqcup B$ but also to any superset of $A \sqcup B$. %%note: If this is not obvious, stop and think for a couple of minutes about the case $A = \{ 1 \}$, $B = \{ 2 \}$, $A \sqcup B = \{1,2\}$, and the superset $\{1,2,3\}$ of $A \sqcup B$.%%
So we need to find some way to limit ourselves to $A \sqcup B$.

The key limiting fact about the disjoint union is that any map *from* the disjoint union can be expressed in terms of two other maps (one from $A$ and one from $B$), and vice versa.
The following discussion shows us how.

- If we know $f: A \sqcup B \to X$, then we know the [restriction_map restriction maps] $f \big|_A : A \to X$ %%note: This is defined by $f \big|_A (a) = f(a)$; that is, it is the function we obtain by "restricting" $f$ so that its [3js domain] is $A$ rather than the larger $A \sqcup B$.%% and $f \big|_B : B \to X$.
- If we know two maps $\alpha: A \to X$ and $\beta: B \to X$, then we can *uniquely* construct a map $f: A \sqcup B \to X$ which is "$\alpha$ and $\beta$ glued together". Formally, the map is defined as $f(x) = \alpha(x)$ if $x \in A$, and $f(x) = \beta(x)$ if $x \in B$.

The reason this pins down the disjoint union exactly (up to isomorphism) is because the disjoint union is the *only* set which leaves us with no choice about how we construct $f$ from $\alpha$ and $\beta$:

- We rule out *strict subsets* of $A \sqcup B$ because we use the fact that every element of $A$ is in $A \sqcup B$ and every element of $B$ is in $A \sqcup B$, to ensure that the restriction maps make sense.
- We rule out *strict supersets* of $A \sqcup B$ because we use the fact that any element in $A \sqcup B$ is either in $A$ or it is in $B$ %%note: If we replaced $A \sqcup B$ by any strict superset, this stops being true: by passing to a superset, we introduce an element which is neither in $A$ nor in $B$.%%, to ensure that $f$ is defined everywhere on its [3js domain]. Moreover, we use the fact that every element of $A \sqcup B$ is in *exactly one* of $A$ or $B$, because if $x$ is in both $A$ and $B$, then we can't generally decide whether we should define $f(x)$ by $\alpha(x)$ or $\beta(x)$.

## The property

One who is well-versed in category theory and universal properties would be able to take the above discussion and condense it into the following statement, which is the **universal property of the disjoint union**:

> Given sets $A$ and $B$, we define the *disjoint union* to be the following collection of three objects: $$\text{A set labelled }\ A \sqcup B \\ i_A : A \to A \sqcup B \\ i_B : B \to A \sqcup B$$ with the requirement that for every set $X$ and every pair of maps $f_A: A \to X$ and $f_B: B \to X$, there is a *unique* map $f: A \sqcup B \to X$ such that $f \circ i_A = f_A$ and $f \circ i_B = f_B$.

[comment: Imgur was down when I made this picture, so I'm hosting it instead.]
![Disjoint union universal property](https://www.patrickstevens.co.uk/images/Coproduct.png)

## Aside: relation to the product

Recall the [-5zv]:

> Given objects $A$ and $B$, we define the *product* to be the following collection of three objects, if it exists: $$A \times B \\ \pi_A: A \times B \to A \\ \pi_B : A \times B \to B$$ with the requirement that for every object $X$ and every pair of maps $f_A: X \to A, f_B: X \to B$, there is a *unique* map $f: X \to A \times B$ such that $\pi_A \circ f = f_A$ and $\pi_B \circ f = f_B$.

Notice that this is just the same as the universal property of the disjoint union, but we have reversed the [3js domain] and [3lg codomain] of every function, and we have correspondingly reversed any function compositions.
This shows us that the product and the disjoint union are, in some sense, "two sides of the same coin".
If you remember the [-5zr], we saw that the empty set's universal property also had a "flip side", and this flipped property characterises the one-element sets.
So in the same sense, the empty set and the one-point sets are "two sides of the same coin".
This is an instance of the concept of [duality_category_theory duality], and it turns up all over the place.

# Examples

## {1} ⊔ {2}

Let's think about the very first example that came from the top of the page: the disjoint union of $A=\{1\}$ and $B=\{2\}$.
(This is, of course, $\{1,2\}$, but we'll try and tie the *universal property* point of view in with the *elements* point of view.)

The definition becomes:

> The *disjoint union* of $\{1\}$ and $\{2\}$ is the following collection of three objects: $$\text{A set labelled } \{1\} \sqcup \{2\} \\ i_A : \{1\} \to \{1\} \sqcup \{2\} \\ i_B : \{2\} \to \{1\} \sqcup \{2\}$$ with the requirement that for every set $X$ and every pair of maps $f_A: \{1\} \to X$ and $f_B: \{2\} \to X$, there is a *unique* map $f: A \sqcup B \to X$ such that $f \circ i_A = f_A$ and $f \circ i_B = f_B$.

This is still long and complicated, but remember from [5zv when we discussed the product] that a map $f_A: \{1\} \to X$ is precisely picking out a single element of $X$: namely $f_A(1)$.
To every element of $X$, there is exactly one such map; and for every map $\{1\} \to X$, there is exactly one element of $X$ it picks out.
(Of course, the same reasoning goes through with $B$ as well: $f_B$ is just picking out an element of $X$, too.)

Also, $i_A$ and $i_B$ are just picking out single elements of $A \sqcup B$, though we are currently pretending that we don't know what $A \sqcup B$ is yet.

So we can rewrite this talk of *maps* in terms of *elements*, to make it easier for us to understand this definition in the specific case that $A$ and $B$ have just one element each:

> The *disjoint union* of $\{1\}$ and $\{2\}$ is the following collection of three objects: $$\text{A set labelled }\ \{1\} \sqcup \{2\} \\ \text{An element } i_A \text{ of } \{1\} \sqcup \{2\} \\ \text{An element }i_B \text{ of } \{1\} \sqcup \{2\}$$ with the requirement that for every set $X$ and every pair of elements $x \in X$ and $y \in X$, there is a *unique* map $f: A \sqcup B \to X$ such that $f(i_A) = x$ and $f(i_B) = y$.

From this, we can see that $i_A$ and $i_B$ can't be the same, because $f$ acts differently on them (if we take $X = \{x,y\}$ where $x \not = y$, for instance).
But also if there were any $z$ in $\{1\} \sqcup \{2\}$ *other* than $i_A$ and $i_B$, then for any attempt at our unique $f: A \sqcup B \to X$, we would be able to make a *different* $f$ by just changing where $z$ is sent to.

So $A \sqcup B$ is precisely the set $\{i_A, i_B\}$; and this has determined it up to isomorphism as "the set with two elements".

### Aside: [-4w5]

Notice that the disjoint union of "the set with one element" and "the set with one element" yields "the set with two elements".
It's actually true for general finite sets that the disjoint union of "the set with $m$ elements" with "the set with $n$ elements" yields "the set with $m+n$ elements"; a few minutes' thought should be enough to convince you of this, but it is an excellent exercise for you to do this from the category-theoretic *universal properties* viewpoint as well as from the (much easier) *elements* viewpoint.
We will touch on this connection between "disjoint union" and "addition" a bit more later.

## The empty set

What is the disjoint union of the [-5zc] with itself?
(Of course, it's just the empty set, but let's pretend we don't know that.)
The advantage of this example is that we already know a lot about the [-5zr], so it's a good testing ground to see if we can do this without thinking about *elements* at all.

Recall that the empty set is defined as follows:

> The empty set $\emptyset$ is the unique set such that for every set $X$, there is precisely one function $\emptyset \to X$.

The disjoint union of $\emptyset$ with itself would be defined as follows (where I've stuck to using $A$ and $B$ in certain places, because otherwise the whole thing just fills up with the $\emptyset$ symbol):

> The following collection of three objects: $$\text{A set labelled }\ A \sqcup B \\ i_A : \emptyset \to A \sqcup B \\ i_B : \emptyset \to A \sqcup B$$ with the requirement that for every set $X$ and every pair of maps $f_A: \emptyset \to X$ and $f_B: \emptyset \to X$, there is a *unique* map $f: A \sqcup B \to X$ such that $f \circ i_A = f_A$ and $f \circ i_B = f_B$.

Now, since we know that for every set $X$ there is a *unique* map $!_X: \emptyset \to X$ %%note: In general, the exclamation mark $!$ is used for a uniquely-defined map.%%, we can replace our talk of $f_A$ and $f_B$ by just the same $!_X$:

> The following collection of three objects: $$\text{A set labelled }\ A \sqcup B \\ i_A : \emptyset \to A \sqcup B \\ i_B : \emptyset \to A \sqcup B$$ with the requirement that for every set $X$, there is a *unique* map $f: A \sqcup B \to X$ such that $f \circ i_A = (!_{X})$ and $f \circ i_B = (!_{X})$.

Moreover, we know that $i_A$ and $i_B$ are also uniquely defined, because there is only one map $\emptyset \to A \sqcup B$ (no matter what $A \sqcup B$ might end up being); so we can remove them from the definition because they're forced to exist anyway.
They're both just $!_{A \sqcup B}$.

> The set $$A \sqcup B$$ with the requirement that for every set $X$, there is a *unique* map $f: A \sqcup B \to X$ such that $f \circ (!_{A \sqcup B}) = (!_X)$.

A diagram is in order.

![Universal property of the disjoint union of the empty set with itself](http://i.imgur.com/B72uW9o.png)

The universal property is precisely saying that for every set $X$, there is a unique map $f: A \sqcup B \to X$ such that $f \circ (!_{A \sqcup B})$ is the map $!_X : \emptyset \to X$.

But since $!_X$ is the *only* map from $\emptyset \to X$, we have $f \circ (!_{A \sqcup B}) = (!_X)$ *whatever $f$ is*.
Indeed, $f \circ (!_{A \sqcup B})$ is a map from $\emptyset \to X$, and $!_X$ is the only such map.
So we can drop that condition from the requirements, because it automatically holds!

Therefore the disjoint union is:

> The set $$A \sqcup B$$ with the requirement that for every set $X$, there is a *unique* map $f: A \sqcup B \to X$.

Do you recognise that definition?
It's just the universal property of the empty set!

So $A \sqcup B$ is the empty set in this example.

# More general concept

We saw earlier that the disjoint union is characterised by the "reverse" (or "[duality_category_theory dual]") of the universal property of the product.
In a more general [4c7 category], we say that something which satisfies this property is a *[-coproduct]*. %%note: Prepending "co" to something means "reverse the maps".%%

In general, the coproduct of objects $A$ and $B$ is written not as $A \sqcup B$ (which is specific to the category of [3jz sets]), but as $A + B$.
Among other things, the coproduct unifies the ideas of addition of [48l integers], [least_upper_bound least upper bounds] in [3rb posets], disjoint union of sets, [-65x] of [45h naturals], [-3w3], and [-free_product] of [3gd groups]; all of these are coproducts in their respective categories.


Examination through isomorphism

[4f4 Isomorphism] is the correct notion of equality between objects in a [4cx category]. From the category-theoretic point of view, if you want to distinguish between two objects which are isomorphic but not equal, it means that the morphisms in your category don't preserve whatever aspect of the objects allows you to make this distinction, and hence the category doesn't really capture what you want to be working with. If you want to talk about it categorically, you should consider a category with morphisms that preserve all of the structure you care about, including whatever allowed the distinction to be made.

For example (this example is due to [Qiaochu Yuan](https://www.quora.com/In-what-sense-are-topological-spaces-a-generalization-of-metric-spaces/answer/Qiaochu-Yuan-1)), consider the category with objects [metric_space metric spaces], and morphisms [continuous_function continuous maps]. The diameter of a metric space $(X,d)$, which is the maximum value of $d(x,y)$ for $x,y \in X$, is a feature of metric spaces which is not invariant under isomorphism in this category; for example, the subsets $[0,1]$ and $[0,2]$ of $\mathbb{R}$, equipped with the usual metrics inherited from $\mathbb{R}$, are isomorphic in this category. There is a continuous map $f : [0,1]  \to [0,2]$ and a continuous map $g : [0,2] \to [0,1]$ such that $fg$ and $gf$ are identities. For example, one could take $f$ to be multiplication by $2$, and $g$ to be division by $2$. However, the diameter of $[0,1]$ is $1$, and the diameter of $[0,2]$ is $2$. Therefore, insofar as "diameter" is a property of metric spaces, the objects of these categories are not metric spaces. The correct name for them is "metrizable spaces", since this category is [equivalence_of_categories equivalent] to the category whose objects are topological spaces whose topology is induced by some metric and whose morphisms are continuous maps.

For a less realistic (but more obvious) example, consider the category of [3gd groups] and arbitrary functions between their [3gz underlying sets]. The objects of this category are, supposedly, groups, but properties of groups, such as "simple", do not respect isomorphism in this category.

Another example of this is the [4mj product] of, say, sets. It determines a functor $\text{Set}\times\text{Set}\to\text{Set}$. We would like to say that this is [3h4 associative], but this is false; a typical element of $A \times (B \times C)$ looks like $(a,(b,c))$, while a typical element of $(A \times B) \times C$ looks like $((a,b),c)$. Since these sets have different elements, they are not [618 equal]. However, they are [4f4 isomorphic]. In fact, the two functors $\text{Set}\times\text{Set}\times\text{Set}\to\text{Set}$  given by $(A,B,C) \mapsto A \times (B \times C)$ and $(A,B,C) \mapsto (A \times B) \times C$ are isomorphic in the category of functors $\text{Set}\times\text{Set}\times\text{Set}\to\text{Set}$. That is, they are naturally isomorphic. [todo: link to a "natural isomorphism" page]


Modal combat

Modal combat is a way of studying the special case of the [5py one-shot prisoner's dilemma] in which the players are programs which have read access to each other source code. There is a class of such agents, defined by expressions in modal logic, which can do general provability reasoning about themselves and each other, and furthermore there is a quick way to evaluate how two such agents will behave against each other.

While they are not allowed to run each other's source code %%note: Otherwise we risk infinite regression as $A$ simulates $B$ that simulates $A$...%%, we are going to generously give each of them access to a [ halting oracle] so that they can make powerful reasoning about each other's behavior.

Wait, why is this interesting? Are not your assumptions a bit overpowered? Well, they are. But thinking about this machines which are obviously way more powerful than our current capabilities is a useful, albeit unrealistic, proxy for superintelligence. We should furthermore remark that while we are going to stick with halting oracles through this article there exist bounded versions of some of the results.

[toc:]

##Optimality in modal combat

We are especially interested in figuring out how would one go about making ideal competitors in this setup, because that may shed light on [ decision theory] issues.

For that, we need a good optimality criterion to allow us to compare different players. Let's think about some conditions we would like our player to satisfy:

1. **Unexploitability**: We would like our player to never end in the sucker's payoff (we cooperate and the opponent defects).
2. **Robust cooperation**: Whenever it is possible to achieve mutual cooperation, we would like to do so. A particular way of highly desirable robust cooperation is achieving cooperation with one self. We call this property of self-trust **Löbian cooperation**.
3. **Robust exploitation**: Whenever it is possible to exploit the opponent (tricking him into cooperating when we defect) we wish to do so.

The question now becomes: it is possible to design a program which performs optimally according to all those metrics? How close can we get?

##DefectBot and CliqueBot

The simplest program that achieves unexploitability is the program which defects against every opponent, commonly known as $DefectBot$. This is a rather boring result, which does not manage to even cooperate with itself.

Taking advantage of the access to source codes, we could build a program $CliqueBot$ which cooperates if and only if the opponent's source code is equal to his own code (wait, how do you do that? With the magic of [322 quining] we can always treat our own code as data!). Unexploitable? Check. Achieves Löbian cooperation? Check. But can't we do better? Innocuous changes to $CliqueBot$'s source code (such as adding comments) result in agents which fail to cooperate, which is rather lame.

##Modal agents
Before we start making more complicated program designs we need to develop a good reasoning framework to figure out how the matches will end. Simple simulation will not do, because we have given them access to a halting oracle to make proofs about their opponent's program.

Fortunately, there is one formal system which nicely handles reasoning about provability: cue to [5l3].

in provability logic we have a modal operator $\square$ which [5n5 Solovay's theorem] tells us can be read as "it is provable in $PA$ that". Furthermore, this system of logic is fully decidable, though that comes at the expense of some expressive power (we, for example, do not have quantifiers in modal logic).

The next step is to express agents in modal combat as expressions of modal logic.

For that we need to extend provability logic with second-order formulas. A second-order formula $\phi(x)$ is a fixed point expression in the language of modal logic in which $x$ appears as a formula and $\phi$ can be used as a variable.

For example, we could have the formula $\phi(x) \leftrightarrow \square x(\phi)$ and the formula $\psi(x)\leftrightarrow \neg\square x(\psi)$. To evaluate a formula with its free variable instantiated we substitute their variables its fixed point expression its body as many times as required until the formula with its argument its expressed in terms of itself, and then we compute its [5lx fixed point in GL].

For example, $\psi(\phi)\leftrightarrow \neg\square\phi(\psi)\leftrightarrow  \neg\square\square\psi (\phi)$, and the fixed point can be calculated to be $GL\vdash \psi(\phi)\leftrightarrow \neg [\square  \bot \vee  \square\square\bot \wedge \neg\square \bot] $, which is arithmetically false and thus we say that $\phi(\psi)$ evaluates to false.

It is possible to reference other formulas in the fixed point expression of a formula, though one precaution must be taken of lest we fall into circular dependencies. We are going to say that a formula is of rank $0$ if it is defined in terms of itself and no other formula. Then we say that a formula can only be defined in terms of itself and lower rank formulas, and define the rank of a formula as the biggest rank in its definition plus one. If the rank is well defined, then the formula is also well defined. To guarantee the existence of a fixed point it will also be convenient to restrict ourselves to [5m4 fully modalized] formulas.

>A **modal agent** of rank $k$ is a one place [5m4 fully modalized] modal formula of the kind we have just defined of rank $k$, whose free variable represents the opponent's source code. To simulate a match, we substitute the free variable by the opponents formula, and evaluate it according to the procedure described in the previous paragraph. If the fixed point is arithmetically true, then the agent cooperates, and if it is false it defects. The rank of the agent is the maximum number of nested boxes that occur in the sentence.

Thus we have that we can express $DefectBot$ as a modal agent with the modal formula $DB(x)\leftrightarrow \bot$.

##FairBot and PrudentBot
To try to beat CliqueBot's performance, we will define a bot which takes advantage of the halting oracle to achieve robust cooperation with a greater class of agents.

    FairBot(x): if Prv(x(FairBot))=C then output C else return D

$FairBot$ tries to find a proof in $PA$ that its opponent is going to cooperate with him, and if it succeeds then it cooperates as well.

We can express $FairBot$ as a modal agent represented by the modal formula $FB(x) \leftrightarrow \square x(FB)$.

So, does $FairBot$ cooperate with himself? Let's find out!

The result of the match against itself is the same as the fixed point of $FB(FB)\leftrightarrow \square FB(FB)$. According to the methods for computing fixed points, the fixed point is thus $\top$, which is of course arithmetically true%%note: This can also be figured out using [55w]%%. Thus $FairBot$ satisfies the Löbian cooperation condition!

Furthermore, this holds no matter how $FairBot$ is implemented, so it is way more robust than $CliqueBot$!

What happens if $FairBot$ faces $DefectBot$? Then we have to find the fixed point of $FB(DB)\leftrightarrow \square \bot$, which is obviously $\square \bot$, and since this expression is arithmetically false %%note: PA+1 disproves it!%%, then it is the case that FairBot does not cooperate with $DefectBot$.

$FairBot$ is an impressive formalism, and it achieves very robust cooperation. However, it fails to exploit other agents. For example, it fails to exploit the simple program $CooperateBot$ %%note: $CB(x)\leftrightarrow \top$%%, which always cooperates.

An improvement can be made by considering the program represented by the rank $1$ modal formula $PB(x)\leftrightarrow\square [x(PB)\wedge \neg\square\bot \rightarrow \neg x(DB)]$, which we call $PrudentBot$. By its definition, $PrudentBot$ cooperates with another modal agent if and only if it can prove that the opponent will cooperate with him, and furthermore $PA+1$ proves that its opponent will not cooperate with $DefectBot$.

Thus $PrudentBot$ cooperates against itself and with $FairBot$, but defects against $DefectBot$ and $CooperateBot$. In fact, $PrudentBot$ dominates $FairBot$.

##Can we do better?
One question that comes to mind: is it possible to design a modal agent which achieves cooperation with every agent for which there is at least an input which will make it cooperate without being exploitable? Sadly no. Consider the bot TrollBot$ defined by $TB(x)\leftrightarrow \square x(DB)$%%note: Exercise for the reader: does $TrollBot$ cooperate with himself?%%. That is, $TrollBot$ cooperates with you if and only if you cooperate with $DefectBot$.

Then $CooperateBot$ achieves cooperation with $TrollBot$, but no bot cooperates with $TrollBot$ while defecting against $DefectBot$.

In general, there is no good optimality criteria developed. An open question is to formalize a good notion of optimality in the class of modal agents and design a bot which achieves it.


Least common multiple

[summary: The **least common multiple (LCM)** of two positive  [45h natural numbers]  a, b is the smallest natural number that both a and b divide, so for instance LCM(12,10) = 60.]

Given two positive natural numbers $a$ and $b$, their **least common multiple** $\text{LCM}(a,b)$ is the smallest natural number divided by both $a$ and $b$. As an example take $a=12, b=10$, then the smallest number divided by both of them is $60$.

There is an equivalent definition of the LCM, which is strange at first glance but turns out to be mathematically much more suited to generalisation: the LCM $l$ of $a$ and $b$ is the natural number such that for every number $c$ divisible by both $a$ and $b$, we have $l$ divides $c$.
This describes the LCM as a [3rc poset least upper bound] (namely the [-3rb] $\mathbb{N}$ under the relation of divisibility).

Note that for $a$, $b$ given, their product $ab$ is a natural number divided by both of them. The least common multiple $\text{LCM}(a,b)$ divides the product $ab$ and for $\text{GCD}(a,b)$ the [-5mw] of $a, b$ we have the formula
$$a\cdot b = \text{GCD}(a,b) \cdot \text{LCM}(a,b). $$
This formula offers a fast way to compute the least common multiple: one can compute $\text{GCD}(a,b)$ using the [euclidean_algorithm] and then divide the product $ab$ by this number.

In practice, for small numbers $a,b$ it is often easier to use their factorization into [4mf prime numbers]. In the example above we have $12=2 \cdot 2 \cdot 3$ and $10=2 \cdot 5$, so if we want to build the smallest number $c$ divided by both of them, we can take $60=2 \cdot 2 \cdot 3 \cdot 5$. Indeed, to compute $c$ look at each prime number $p$ dividing one of $a,b$ (in the example $p=2,3,5$). Then writing $c$ as a product we take the factor $p$ the maximal number of times it appears in $a$ and $b$. The factor $p=2$ appears twice in $12$ and once in $10$, so we take it two times. The factor $3$ appears once in $12$ and zero times in $10$, so we only take it once, and so on.


Up to isomorphism

[summary(brief):A phrase mathematicians use when saying "we only care about the [structure_mathematics structure] of an [object_mathematics object], not about specific implementation details of the object".]

[summary: "The property $P$ holds up to isomorphism" is a phrase which means "we might say an object $X$ has property $P$, but that's an abuse of notation. When we say that, we really mean that there is an object [4f4 isomorphic] to $X$ which has property $P$". Essentially, it means "the property might not hold as stated, but if we replace the idea of *equality* by the idea of *isomorphism*, then the property holds".

Relatedly, "The object $X$ is [5ss well-defined] up to isomorphism" means "if we replace $X$ by an object isomorphic to $X$, we still obtain something which satisfies the definition of $X$."]

"The property $P$ holds up to isomorphism" is a phrase which means "we might say an object $X$ has property $P$, but that's an abuse of notation. When we say that, we really mean that there is an object [4f4 isomorphic] to $X$ which has property $P$". Essentially, it means "the property might not hold as stated, but if we replace the idea of *equality* by the idea of *isomorphism*, then the property holds".

Relatedly, "The object $X$ is [5ss well-defined] up to isomorphism" means "if we replace $X$ by an object isomorphic to $X$, we still obtain something which satisfies the definition of $X$."

# Examples

## Groups of order $2$

There is only one [-3gd] of [3gg order] $2$ *up to isomorphism*.
We can define the object "group of order $2$" as "the group with two elements"; this object is well-defined up to isomorphism, in that while there are several different groups of order $2$ %%note: Two such groups are $\{0,1\}$ with the operation "addition [5ns modulo] $2$", and $\{e, x \}$ with [-54p] $e$ and the operation $x^2 = e$.%%, any two such groups are isomorphic.
If we don't think of isomorphic objects as being "different", then there is only one distinct group of order $2$.


Greatest lower bound in a poset

[summary: In a [-3rb], the greatest lower bound of two elements $x$ and $y$ is the "largest" element which is "less than" both $x$ and $y$, in whatever ordering the poset has.]

In a [-3rb], the greatest lower bound of two elements $x$ and $y$ is the "largest" element which is "less than" both $x$ and $y$, in whatever ordering the poset has.
In a rare moment of clarity in mathematical naming, the name "greatest lower bound" is a perfect description of the concept: a "lower bound" of two elements $x$ and $y$ is an object which is smaller than both $x$ and $y$ (it "bounds them from below"), and the "greatest lower bound" is the greatest of all the lower bounds.

Formally, if $P$ is a [3jz set] with partial order $\leq$, and given elements $x$ and $y$ of $P$, we say an element $z \in P$ is a **lower bound** of $x$ and $y$ if $z \leq x$ and $z \leq y$.
We say an element $z \in P$ is the **greatest lower bound** of $x$ and $y$ if:

- $z$ is a lower bound of $x$ and $y$, and
- for every lower bound $w$ of $x$ and $y$, we have $w \leq z$.

[todo: examples in different posets]
[todo: example where there is no greatest lower bound because there is no lower bound]
[todo: example where there is no GLB because while there are lower bounds, none of them is greatest]


An introductory guide to modern logic

Welcome! We are about to start a journey through modern logic, in which we will visit two central results of this branch of mathematics, which are often misunderstood and sometimes even misquoted: [ Löb's theorem] and [ Gödel's second incompleteness theorem].

Modern logic can be said to have been born in the ideas of Gödel regarding the amazing property of self-reference that plagues arithmetic. Badly explained, this means that we can force an interpretation over natural numbers which relates how deductions are made in arithmetic to statements about arithmetical concepts such as divisibility.

[toc:]

This guide is targets people who are already comfortable with mathematical thinking and mathematical notation. It requires however no specific background knowledge about logic or math to be read. The guide starts lightweight and gets progressively more technical.

#Formal proofs
What is logic anyway? A short explanation refers to the fact that we humans happen to be able to draw conclusions about things we have not experienced by using facts about how the world is. For example, if you see somebody entering the room and is dripping water, then you infer that it is raining outside, even though you have not seen the rain yourself.

This may seem trivial, but what if we are trying to program that into a computer? How can we capture this intuition of what reasoning is and write down the rules for proper reasoning in a way precise enough to be turned into a program?

To accomplish that is to do logic. The ultimate goal is to have an efficient procedure that if followed will allow to **deduce** every true consequence of a set of premises, so simple that it could be taught to a dumb machine which only follows mechanical operations.

So let's try to formalize reasoning!

We are going to draw inspiration from mathematicians and see what they are doing when proving a result.

Though the method is often convoluted, non linear and hard to understand, in essence what they are doing is writing down some hypothesis and facts that they assume to be true. Then they apply some manipulation rules which they assume that when applied on true premises allow to derive true facts. After iterating this process, they arrive to the wanted result.

## Getting formal

Well, let's wrap together this intuitive process in a formal definition.

> A [ proof of a sentence $\phi$]  is a sequence of [ well formed sentences] such that every sentence in the sequence is either an [-6cd] or can be derived of the previous sentences using a [ derivation rule], and the final sentence in the sequence is $\phi$. A sentence which has a proof is called a [ theorem].

Don't be scared by all the new terms! Let's go briefly over them.

A **well formed sentence** is just a combination of letters which satisfies a certain criteria which makes them easy to interpret. The sentences we will be dealing with are logical formulas, which use a particular set of symbols such as $=, \wedge, \implies$. Our goal is to assign truth values to sentences. That is, to say if particular sentences are true or false.

An **axiom** is something that we assume to be true without requiring a proof. For example, a common axiom in arithmetic is assuming that $0$ is different than $n+1$ for all natural numbers $n$. Or expressed as a well formed sentence, $\forall n. 0 \not = n+1$. %%note:The symbol $\forall$ is an upside-down A and means "[ for all]".%%

A **derivation rule** is a valid basic step in a proof. Perhaps the simplest example is [ modus ponens] . Modus ponens tells you that if you have deduced somehow that $A\implies B$ and also $A$, then you can deduce from both facts $B$.

Those three components constitute the basis of a **logical system** for reasoning: a criteria for constructing [ well formed sentences], a set of  [6cd axioms] which we know to be true %%note: The set of axioms does not have to be finite! In particular, we can specify a criteria to identify infinitely many axioms. This is called an [ axiom schema]%% and some [ derivation rules] which allow us to deduce new conclusions.

%%hidden(Show example):
For a rather silly example, let's consider the system $A$ with the following components:
* Well formed sentences: the set of words in the Webster dictionary.
* Axioms: The word 'break'.
* Deduction rules: If a word $w$ is a theorem of $A$, then so it is a word which only differs in one letter of $w$, either by adding, substracting or replacing it anywhere in $w$.

Thus the word 'trend' is a theorem of $A$, as shown by the 4 steps long proof 'break - bread - tread - trend'. The first sentence in the proof is an axiom. Every sentence after is deduced from our deduction rule using the previous sentence as a premise.
%%

## Interpretations

Given how we defined a logical system, there is a certain degree of freedom we have, as we can choose the underlying sets of well formed sentences, the axioms and the deduction rules. However, as shown in the example above, if we want to make meaningful deductions we should be careful in what components do we choose for our logical system.

The criteria we use to choose these components is intertwined with the [-model] we are trying to reason about. Models can be anything that captures an aspect of the world, or just a mathematical concept. To put it simply, they are the thing we are trying to reason *about*.

Depending on what kind of entities the model has, we will lean towards a particular choice of sentences. For example, if our model consists in just facts which are subject to some known logical relations we could use the language of [-propositional_logic]. But if our model contains different objects which are related in different ways, we will prefer to use the language of [first_order_logic].

Each model has an interpretation associated, which relates the terms in the sentences to aspects of the model. For all practical purposes, a model and its interpretation are the same once we have fixed the language, and thus we will use the terms as synonyms.

Then, depending on how the model evolves and what things can we infer from what facts we should choose some deduction rules. Together, the sentences and the deduction rules specify a class of possible models. If the deduction rules correctly capture the properties of the class of models we had in mind, in the sense that from true premises we really derive true consequences, then we will say that they are [-sound].

Intuitively, we say that the choice of language and deduction rules decides the "shape" that our models will have. Every model with such a shape will be part of the universe of models specified by those components of a logical system.

With this imagery of a class of models, we can think of axioms as an attempt to reduce the class of models and pin down the concrete model we are interested in, by choosing as axioms sentences whose interpretation is true in only the model we are interested in and in no other model %%note: sadly, there are occasions in which this is just not possible%%.

A property tightly related to soundness is [-completeness]. A system is complete if every sentence which is true under all interpretations which satisfy our axioms has a proof.

It is important to realize that logical systems can be talking about many models at once. In particular, if two interpretations satisfy the axioms but disagree on the truth of a particular sentence, then that sentence will be [ undecidable] in our model.

This suggest a technique for proving independence of logical statements. To show that a certain sentence is independent from some axioms, construct two models which satisfy those axioms but contradict each other on the value of the statement. A nice example of this is how Lobachevsky showed that [Euclides Fifth postulate is independent from the other four postulates](https://en.wikipedia.org/wiki/Parallel_postulate#History).

[todo: add more examples everywhere]

## Introducing PA

For our purposes, we will stick to a particular logical system called [3ft Peano arithmetic]. This particular choice of axioms and deduction rules is interesting because it reflects a lot of our intuitions about how numbers work, which in turn can be used to talk about many phenomena in the real world. In particular, *they can talk about themselves*.

Before we move on to this "talking about themselves" business I am going to introduce more notation. We will refer to Peano Arithmetic as $PA$. If a sentence $\phi$ follows from the axioms of $PA$ and its deduction rules %%note: ie, there is a proof of $\phi$ using only axioms and deduction rules from $PA$%%, we will say that $PA\vdash \phi$, read as "$PA$ proves $\phi$".

#Self reference and the provability predicate
Let's try to get an intuition of what I mean when I say that numbers can talk about themselves.

The first key intuition is that we can refer to arbitrary sequences of characters using numbers. For example, I can declare that from now onwards the number $1$ is going to refer to the sequence of letters "I love Peano Arithmetic". Furthermore, using a clever rule to relate numbers and sentences I can make sure that every possible finite sentence gets assigned a number %%note: This is called [-encoding]%%.

A simple encoding goes by the name of **Gödel encoding**, and consists of assigning to every symbol we are going to allow to be used in sentences a number. For example, $=$ could be $1$, and $a$ could be $0$, and so on and so forth. Then we could encode a sentence consisting of $n$ symbols as the number $2^{a_1}3^{a_2}5^{a_3}\cdots p(n)^{a_n}$. That is, we take the number which is the product of the $n$th first primes with exponents equal to the assigned numbers.%%note:This is possible because every number can be [-5rh decomposed as a unique product of primes].%%

The process can be repeated to encode sequences of sentences as single numbers. Thus, we can encode whole proofs as single numbers. We could also encode sequences of sequences of sentences, but that is going too far for our purposes.

[todo: whole example of Gödel encoding, define carefully notation of godelquotes]

So the point is, we can exchange sentences by numbers and vice versa. Can we also talk about deduction using numbers?

It turns out we can! With the encoding we have chosen it is cumbersome to show, but we can write a **predicate** $Axiom(x)$ %%note: A predicate is a well formed sentence in which we have left one or more "holes" in the form of variables which we can substitute for literal numbers or quantify over. For example, we can have the predicate $IsEqualTo42(x)$ of the form $x = 42$. Then $PA\vdash IsEqualTo42(42)$ and $PA\vdash \exists x IsEqualTo42(x)$, but $PA\not\vdash IsEqualTo42(7)$%% in the language of Peano Arithmetic such that $PA\vdash Axiom(\textbf{n})$ if and only if $n$ is a number which encodes an axiom of $PA$.

Furthermore, deduction rules which require $n$ premises can be represented by $n+1$ predicates $Rule(p_1, p_2,..., p_n, r)$ which is provable in $PA$ if and only if $p_1, ...., p_n$ are numbers encoding valid premises for the rule and $r$ is the corresponding deduced fact.

A bit more of work and we can put together a predicate $Proof(x,y)$, which is provable in $PA$ if and only if $x$ encodes the valid proof of the sentence $y$. Isn't that neat!?

Since we are not too interested in the proof itself, and more in the fact that there is a proof at all, we are going to construct the **provability predicate** $\exists x. Proof(x,y)$,  which we will call $\square_{PA}(y)$.%%note: $\exists$ is a backwards E and means "there exists"%%. So then, $\square_{PA}(x)$ literally means "There is a proof in $PA$ of $x$".

Thus we can say that if the number $\ulcorner 1+1=2 \urcorner$ corresponds to the sentence $1+1=2$%%note: This notation remains in effect for the rest of the article.%%, then $PA\vdash \square_{PA}(\ulcorner 1+1=2 \urcorner)$. That is, $PA$ proves that there is a proof in $PA$ of $1+1=2$.

Now, the predicates we have seen so far are quite intuitive and nicely behaved, in the sense that their deducibility from $PA$ matches quite well what we would expect from our intuition. However, adding the existential quantifier in front of $Proof(x,y)$ we get some nasty side effects.

Warning! Really bad analogy incoming!

The thing is that $PA$ "hallucinates" numbers which it is not sure whether they exist or not. Those are no mere natural numbers, but infinite numbers further in the horizon of math. While $PA$ cannot prove their existence, it neither cannot prove its non existence. So $PA$ becomes wary of asserting that proofs do not exist for a certain false sentence. After all, one of those **non standard** numbers may encode the proof of the false sentence! Who is to prove otherwise?

Can we patch this somehow? Maybe by adding more axioms or deduction rules to $PA$ so that it can prove that those numbers do not exists? The answer is yes but not really. While it is doable in principle, the resulting theory becomes too difficult to manage, and we can no longer use it for effective deduction %%note: Technically, $PA$ loses its [ semidecidability] that way.%%.

We will now proceed to prove two technical results which better formalize this idea: Löb's theorem and Gödel's Second Incompleteness Theorem.

#Löb's theorem

Löb's result follows from the intuitive properties of deduction that the provability predicate actually manages to capture. This points to the fact that it is not our definition what is wrong, but rather to a fundamental impossibility in logic.

The intuitive, good properties of $\square_{PA}$ are known as the Hilbert-Bernais derivability conditions, and are as follows:

1. If $PA\vdash A$, then $PA\vdash \square_{PA}(\ulcorner A\urcorner)$.
2. $PA\vdash \square_{PA}(\ulcorner A\rightarrow B\urcorner) \rightarrow [\square_{PA}(\ulcorner A \urcorner)\rightarrow \square_{PA}(\ulcorner B \urcorner)]$
3. $PA\vdash \square_{PA}(\ulcorner A\urcorner) \rightarrow \square_{PA} \square_{PA} (\ulcorner A\urcorner)$.

Let's go over each of them in turn.

1) says reasonably that if $PA$ proves a sentence $A$, then it also proves that there is a proof of $A$.

2) affirms that if you can prove that $A$ implies $B$ then the existence of a proof of $A$ implies the existence of a proof of $B$. This is quite intuitive, as we can concatenate a proof of $A$ with a proof of $A\rightarrow B$ and deduce $B$ from an application of *modus ponens*.

3) is a technical result which states that the formalization of 1) is provable when we are dealing with sentences of the form $\square_{PA}(\ulcorner A \urcorner)$.

One more ingredient is needed to derive Löb's: the [-59c], which states that for all predicates $\phi(x)$ there is a formula $\psi$ such that $PA\vdash \psi \leftrightarrow \phi(\ulcorner \psi \urcorner)$.

The details of the proof can be found in [59b].

The details are not essential to the main idea, but it can be illustrative to work through the formal proof. Plus the intuition behind related to the non-standard numbers we talked about before.

What is really interesting is that now we are in position to enunciate and understand Löb's theorem!

> **Löb's theorem**

> If $PA\vdash \square_{PA}(\ulcorner A\urcorner) \rightarrow A$, then $PA\vdash A$.

> (or equivalently, if $PA\not\vdash A$ then $PA\not\vdash \square_{PA}(\ulcorner A\urcorner) \rightarrow A$).

Talk about an unintuitive result! Let's take a moment to ponder about its meaning.

Intuitively, we should be able to derive from the existence of a proof of $A$ that $A$ is true. After all, proofs are guarantees that something really follows from the axioms, so if we got those right and our derivation rules are correct then $A$ should be true. However, $PA$ does not trust that being able to find the existence of a proof is enough to make $A$ true!

Indeed, he needs to be able to see by himself the proof. In other words, there must be a $n$ such that $PA\vdash Proof(\textbf n, \ulcorner A\urcorner)$. Then he will trust that it is indeed the case that $A$.

I will repeat that again because it looks like a tongue twister. Suppose somebody came and assured $PA$ that from the axioms of $PA$ it follows that there exists a number $n$ satisfying $Proof(\textbf n,\ulcorner A\urcorner)$. Then $PA$ would say, show me the proof or I am going to assume that that $n$ is actually a non standard number and you a friggin' liar.

If somebody just comes saying he has a proof, but produces none, $PA$ becomes suspicious. After all, the proof in question could be a non-standard proof encoded by one of the dreaded non standard numbers! Who is going to trust that!

#Gödel II
And finally we arrive to Gödel's Second Incompleteness Theorem, perhaps the most widely misunderstood theorem of all mathematics.

We first need to introduce the notion of [ consistency.] Simply enough, a logical system is consistent if it does not prove a contradiction, where a contradiction is something impossible. For example, it cannot ever be the case that $P\wedge \neg P$, so $P\wedge \neg P$ is a contradiction no matter what $P$ is. We use the symbol $\bot$ to represent a contradiction.

The statement of GII is as follows:

> Gödel Second Incompleteness Theorem (concrete form)
> If $PA$ is consistent, then $PA\not \vdash \neg \square_{PA}(\bot)$

Notice that GII follows quite directly from Löb's theorem. Actually, it is the case that Löb's theorem also follows from GII, so [5hs both results are equivalent].

This result can be interpreted as follows: you cannot make a system as complex as $PA$ in which you can talk about deduction with complete certainty, and thus about consistency. In particular, such a system cannot prove that he himself is consistent.

The result is startling, but in the light of our previous exposition is clear what is going on! There is always the shadow of non standard numbers menacing our deductions.

#Summary
And that concludes our introduction to formal logic!

To recall some important things we have learned:

* Logical systems capture the intuition behind deductive reasoning. They are composed of axioms and deductive rules that are chained to compose proofs.
* Simple logical systems that are used to talk about numbers, such as $PA$, can be interpreted as talking about many things through encodings, and in particular they can talk about themselves.
* There are expressions in logic that capture the concepts involved in deductions. However, the most important of them, the provability predicate $\square_{PA}$, fails to satisfy some intuitive properties due to the inability of $PA$ to prove that non standard numbers do not exists.
* Löb's theorem says that if $PA$ cannot prove $A$, then it can neither prove that from $\square_{PA}(\ulcorner A\urcorner$ follows $A$.
* $PA$ cannot prove its own consistency, in the sense that it cannot prove that the standard predicate is never satisfied by a contradiction.

If you want to get deeper in the rabbit hole, read about [ model theory] and [-semantics] or [-534].


Wants to get straight to Bayes

A simple requisite page to mark whether the user has selected wanting to get straight into Bayes on the [1zq Bayes' Rule Guide start page].


High-speed intro to Bayes's rule

(This is a high-speed introduction to [1lz] for people who want to get straight to it and are good at math.  If you'd like a gentler or more thorough introduction, try starting at the [1zq Bayes' Rule Guide] page instead.)

# Percentages, frequencies, and waterfalls

Suppose you're screening a set of patients for a disease, which we'll call Diseasitis.%note:Lit. "inflammation of the disease".% Your initial test is a tongue depressor containing a chemical strip, which usually turns black if the patient has Diseasitis.

- Based on prior epidemiology, you expect that around 20% of patients in the screening population have Diseasitis.
- Among patients with Diseasitis, 90% turn the tongue depressor black.
- 30% of the patients without Diseasitis will also turn the tongue depressor black.

What fraction of patients with black tongue depressors have Diseasitis?

%%hidden(Answer): 3/7 or 43%, quickly obtainable as follows:  In the screened population, there's 1 sick patient for 4 healthy patients.  Sick patients are 3 times more likely to turn the tongue depressor black than healthy patients.  $(1 : 4) \cdot (3 : 1) = (3 : 4)$ or 3 sick patients to 4 healthy patients among those that turn the tongue depressor black, corresponding to a probability of $3/7 = 43\%$ that the patient is sick.%%

(Take your own stab at answering this question, then please click "Answer" above to read the answer before continuing.)

Bayes' rule is a theorem which describes the general form of the operation we carried out to find the answer above.  In the form we used above, we:

- Started from the *prior odds* of (1 : 4) for sick versus healthy patients;
- Multiplied by the *likelihood ratio* of (3 : 1) for sick versus healthy patients blackening the tongue depressor;
- Arrived at *posterior odds* of (3 : 4) for a patient with a positive test result being sick versus healthy.

Bayes' rule in this form thus states that **the prior odds times the likelihood ratio equals the posterior odds.**

We could also potentially see the positive test result as **revising** a *prior belief* or *prior probability* of 20% that the patient was sick, to a *posterior belief* or *posterior probability* of 43%.

To make it clearer that we did the correct calculation above, and further pump intuitions for Bayes' rule, we'll walk through some additional visualizations.

## Frequency representation

The *frequency representation* of Bayes' rule would describe the problem as follows:  "Among 100 patients, there will be 20 sick patients and 80 healthy patients."

![prior frequency](https://i.imgur.com/gvcoqQN.png?0)

"18 out of 20 sick patients will turn the tongue depressor black.  24 out of 80 healthy patients will blacken the tongue depressor."

![posterior frequency](https://i.imgur.com/0XDbrYi.png?0)

"Therefore, there are (18+24)=42 patients who turn the tongue depressor black, among whom 18 are actually sick. (18/42)=(3/7)=43%."

(Some experiments show %%note: E.g. "[Probabilistic reasoning in clinical medicine](https://faculty.washington.edu/jmiyamot/p548/eddydm%20prob%20reas%20i%20clin%20medicine.pdf)" by David M. Eddy (1982).%% that this way of explaining the problem is the easiest for e.g. medical students to understand, so you may want to remember this format for future use.  Assuming you can't just send them to Arbital!)

## Waterfall representation

The *waterfall representation* may make clearer why we're also allowed to transform the problem into prior odds and a likelihood ratio, and multiply (1 : 4) by (3 : 1) to get posterior odds of (3 : 4) and a probability of 3/7.

The following problem is isomorphic to the Diseasitis one:

"A waterfall has two streams of water at the top, a red stream and a blue stream.  These streams flow down the waterfall, with some of each stream being diverted off to the side, and the remainder pools at the bottom of the waterfall."

![unlabeled waterfall](https://i.imgur.com/D8EhY65.png?0)

"At the top of the waterfall, there's around 20 gallons/second flowing from the red stream, and 80 gallons/second flowing from the blue stream.  90% of the red water makes it to the bottom of the waterfall, and 30% of the blue water makes it to the bottom of the waterfall.  Of the purplish water that mixes at the bottom, what fraction is from the red stream versus the blue stream?"

![labeled waterfall](https://i.imgur.com/QIrtuVU.png?0)

We can see from staring at the diagram that the *prior odds* and *likelihood ratio* are the only numbers we need to arrive at the answer:

- The problem would have the same answer if there were 40 gallons/sec of red water and 160 gallons/sec of blue water (instead of 20 gallons/sec and 80 gallons/sec).  This would just multiply the total amount of water by a factor of 2, without changing the ratio of red to blue at the bottom.
- The problem would also have the same answer if 45% of the red stream and 15% of the blue stream made it to the bottom (instead of 90% and 30%).  This would just cut down the total amount of water by a factor of 2, without changing the *relative* proportions of red and blue water.

![wide vs narrow waterfall](https://i.imgur.com/6FOndjc.png?0)

So only the *ratio* of red to blue water at the top (prior odds of the proposition), and only the *ratio* between the percentages of red and blue water that make it to the bottom (likelihood ratio of the evidence), together determine the *posterior* ratio at the bottom:  3 parts red to 4 parts blue.

## Test problem

Here's another Bayesian problem to attempt.  If you successfully solved the earlier problem on your first try, you might try doing this one in your head.

10% of widgets are bad and 90% are good.  4% of good widgets emit sparks, and 12% of bad widgets emit sparks.  What percentage of sparking widgets are bad?

%%hidden(Answer):
- There's $1 : 9$ bad vs. good widgets.  (9 times as many good widgets as bad widgets; widgets are 1/9 as likely to be bad as good.)
- Bad vs. good widgets have a $12 : 4$ relative likelihood to spark, which simplifies to $3 : 1.$  (Bad widgets are 3 times as likely to emit sparks as good widgets.)
- $(1 : 9) \cdot (3 : 1) = (3 : 9) \cong (1 : 3).$  (1 bad sparking widget for every 3 good sparking widgets.)
- Odds of $1 : 3$ convert to a probability of $\frac{1}{1+3} = \frac{1}{4} = 25\%.$ (25% of sparking widgets are bad.) %%

(If you're having trouble using odds ratios to represent uncertainty, see [561 this intro] or [1rb this page].)

# General equation and proof

To say exactly what we're doing and prove its validity, we need to introduce some notation from [1rf probability theory].

If $X$ is a proposition, $\mathbb P(X)$ will denote $X$'s probability, our quantitative degree of belief in $X.$

$\neg X$ will denote the negation of $X$ or the proposition "$X$ is false".

If $X$ and $Y$ are propositions, then $X \wedge Y$ denotes the proposition that both X and Y are true.  Thus $\mathbb P(X \wedge Y)$ denotes "The probability that $X$ and $Y$ are both true."

We now define [1rj conditional probability]:

$$\mathbb P(X|Y) := \dfrac{\mathbb P(X \wedge Y)}{\mathbb P(Y)}   \tag*{(definition of conditional probability)}$$

We pronounce $\mathbb P(X|Y)$ as "the conditional probability of X, given Y".  Intuitively, this is supposed to mean "The probability that $X$ is true, *assuming* that proposition $Y$ is true".

Defining conditional probability in this way means that to get "the probability that a patient is sick, given that they turned the tongue depressor black" we should put all the sick *plus* healthy patients with positive test results into a bag, and ask about the probability of drawing a patient who is sick *and* got a positive test result from that bag.  In other words, we perform the calculation $\frac{18}{18+24} = \frac{3}{7}.$

![diseasitis frequency](https://i.imgur.com/0XDbrYi.png?0)

Rearranging [1rj the definition of conditional probability], $\mathbb P(X \wedge Y) = \mathbb P(Y) \cdot \mathbb P(X|Y).$  So to find "the fraction of all patients that are sick *and* get a positive result", we multiply "the fraction of patients that are sick" times "the probability that a sick patient blackens the tongue depressor".

We're now ready to prove Bayes's rule in the form, "the prior odds times the likelihood ratio equals the posterior odds".

The "prior odds" is the ratio of sick to healthy patients:

$$\frac{\mathbb P(sick)}{\mathbb P(healthy)}  \tag*{(prior odds)}$$

The "likelihood ratio" is how much more *relatively* likely a sick patient is to get a positive test result (turn the tongue depressor black), compared to a healthy patient:

$$\frac{\mathbb P(positive | sick)}{\mathbb P(positive | healthy)} \tag*{(likelihood ratio)}$$

The "posterior odds" is the odds that a patient is sick versus healthy, *given* that they got a positive test result:

$$\frac{\mathbb P(sick | positive)}{\mathbb P(healthy | positive)} \tag*{(posterior odds)}$$

Bayes's theorem asserts that *prior odds times likelihood ratio equals posterior odds:*

$$\frac{\mathbb P(sick)}{\mathbb P(healthy)} \cdot \frac{\mathbb P(positive | sick)}{\mathbb P(positive | healthy)} = \frac{\mathbb P(sick | positive)}{\mathbb P(healthy | positive)}$$

We will show this by proving the general form of Bayes's Rule.  For any two hypotheses $H_j$ and $H_k$ and any piece of new evidence $e_0$:

$$
\frac{\mathbb P(H_j)}{\mathbb P(H_k)}
\cdot
\frac{\mathbb P(e_0 | H_j)}{\mathbb P(e_0 | H_k)}
=
\frac{\mathbb P(e_0 \wedge H_j)}{\mathbb P(e_0 \wedge H_k)}
=
\frac{\mathbb P(e_0 \wedge H_j)/\mathbb P(e_0)}{\mathbb P(e_0 \wedge H_k)/\mathbb P(e_0)}
=
\frac{\mathbb P(H_j | e_0)}{\mathbb P(H_k | e_0)}
$$

In the Diseasitis example, this corresponds to performing the operations:

$$
\frac{0.20}{0.80}
\cdot
\frac{0.90}{0.30}
=
\frac{0.18}{0.24}
=
\frac{0.18/0.42}{0.24/0.42}
=
\frac{0.43}{0.57}
$$

Using red for sick, blue for healthy, grey for a mix of sick and healthy patients, and + signs for positive test results, the proof above can be visualized as follows:

![bayes venn](https://i.imgur.com/YBc2nYo.png?0)

%todo: less red in first circle (top left).  in general, don't have prior proportions equal posterior proportions graphically!%

## Bayes' theorem

An alternative form, sometimes called "Bayes' theorem" to distinguish it from "Bayes' rule" (although not everyone follows this convention), uses absolute probabilities instead of ratios.  The [marginal_probability law of marginal probability] states that for any set of [1rd mutually exclusive and exhaustive] possibilities $\{X_1, X_2, ..., X_i\}$ and any proposition $Y$:

$$\mathbb P(Y) = \sum_i \mathbb P(Y \wedge X_i)   \tag*{(law of marginal probability)}$$

Then we can derive an expression for the absolute (non-relative) probability of a proposition $H_k$ after observing evidence $e_0$ as follows:

$$
\mathbb P(H_k | e_0)
= \frac{\mathbb P(H_k \wedge e_0)}{\mathbb P(e_0)}
= \frac{\mathbb P(e_0 \wedge H_k)}{\sum_i P(e_0 \wedge H_i)}
= \frac{\mathbb P(e_0 | X_k) \cdot \mathbb P(X_k)}{\sum_i \mathbb P(e_0 | X_i) \cdot \mathbb P(X_i)}
$$

The equation of the first and last terms above is what you will usually see described as Bayes' theorem.

To see why this decomposition might be useful, note that $\mathbb P(sick | positive)$ is an *inferential* step, a conclusion that we make after observing a new piece of evidence.  $\mathbb P(positive | sick)$ is a piece of *causal* information we are likely to have on hand, for example by testing groups of sick patients to see how many of them turn the tongue depressor black.  $\mathbb P(sick)$ describes our state of belief before making any new observations.  So Bayes' theorem can be seen as taking what we already believe about the world (including our prior belief about how different imaginable states of affairs would generate different observations), plus an actual observation, and outputting a new state of belief about the world.

## Vector and functional generalizations

Since the proof of Bayes' rule holds for *any pair* of hypotheses, it also holds for relative belief in any number of hypotheses.  Furthermore, we can repeatedly multiply by likelihood ratios to chain together any number of pieces of evidence.

Suppose there's a bathtub full of coins:

- Half the coins are "fair" and have a 50% probability of coming up Heads each time they are thrown.
- A third of the coins are biased to produce Heads 25% of the time (Tails 75%).
- The remaining sixth of the coins are biased to produce Heads 75% of the time.

You randomly draw a coin, flip it three times, and get the result **HTH**.  What's the chance this is a fair coin?

We can validly calculate the answer as follows:

$$
\begin{array}{rll}
  & (3 : 2 : 1) & \cong (\frac{1}{2} : \frac{1}{3} : \frac{1}{6}) \\
\times & (2 : 1 : 3) & \cong ( \frac{1}{2} : \frac{1}{4} : \frac{3}{4} ) \\
\times & (2 : 3 : 1) & \cong ( \frac{1}{2} : \frac{3}{4} : \frac{1}{4} ) \\
\times & (2 : 1 : 3) & \\
= & (24 : 6 : 9) & \cong (8 : 2 : 3) \cong (\frac{8}{13} : \frac{2}{13} : \frac{3}{13})
\end{array}
$$

So the posterior probability the coin is fair is 8/13 or ~62%.

This is one reason it's good to know the [1x5 odds form] of Bayes' rule, not just the [554 probability form] in which Bayes' theorem is often given.%%note: Imagine trying to do the above calculation by repeatedly applying the form of the theorem that says: $$\mathbb P(H_k | e_0) = \frac{\mathbb P(e_0 | X_k) \cdot \mathbb P(X_k)}{\sum_i \mathbb P(e_o | X_i) \cdot \mathbb P(X_i)}$$ %%

We can generalize further by writing Bayes' rule in a functional form.  If $\mathbb O(H_i)$ is a relative belief vector or relative belief function on the variable $H,$ and $\mathcal L(e_0 | H_i)$ is the likelihood function giving the relative chance of observing evidence $e_0$ given each possible state of affairs $H_i,$ then relative posterior belief $\mathbb O(H_i | e_0)$ is given by:

$$\mathbb O(H_i | e_0) = \mathcal L(e_0 | H_i) \cdot \mathbb O(H_i)$$

If we [1rk normalize] the relative odds $\mathbb O$ into absolute probabilities $\mathbb P$ - that is, divide through $\mathbb O$ by its sum or integral so that the new function sums or integrates to $1$ - then we obtain Bayes' rule for probability functions:

$$\mathbb P(H_i | e_0) \propto \mathcal L(e_0 | H_i) \cdot \mathbb P(H_i)  \tag*{(functional form of Bayes' rule)}$$

# Applications of Bayesian reasoning

This general Bayesian framework - prior belief, evidence, posterior belief - is a lens through which we can view a *lot* of formal and informal reasoning plus a large amount of entirely nonverbal cognitive-ish phenomena.%%note:This broad statement is widely agreed.  Exactly *which* phenomena are good to view through a Bayesian lens is sometimes disputed.%%

Examples of people who might want to study Bayesian reasoning include:

- Professionals who use statistics, such as scientists or medical doctors.
- Computer programmers working in the field of machine learning.
- Human beings trying to think.

The third application is probably of the widest general interest.

## Example human applications of Bayesian reasoning

Philip Tetlock found when studying "superforecasters", people who were especially good at predicting future events:

"The superforecasters are a numerate bunch: many know about Bayes' theorem and could deploy it if they felt it was worth the trouble. But they rarely crunch the numbers so explicitly. What matters far more to the superforecasters than Bayes' theorem is Bayes' core insight of gradually getting closer to the truth by constantly updating in proportion to the weight of the evidence." — Philip Tetlock and Dan Gardner, [https://en.wikipedia.org/wiki/Superforecasting _Superforecasting_]

This is some evidence that *knowing about* Bayes' rule and understanding its *qualitative* implications is a factor in delivering better-than-average intuitive human reasoning.  This pattern is illustrated in the next couple of examples.

### The OKCupid date.

One realistic example of Bayesian reasoning was deployed by one of the early test volunteers for a much earlier version of a guide to Bayes' rule.  She had scheduled a date with a 96% OKCupid match, who had then cancelled that date without other explanation.  After spending some mental time bouncing back and forth between "that doesn't seem like a good sign" versus "maybe there was a good reason he canceled", she decided to try looking at the problem using that Bayes thing she'd just learned about.  She estimated:

- A 96% OKCupid match like this one, had prior odds of 2 : 5 for being a desirable versus undesirable date.  (Based on her prior experience with 96% OKCupid matches, and the details of his profile.)
- Men she doesn't want to go out with are 3 times as likely as men she might want to go out with to cancel a first date without other explanation.

This implied posterior odds of 2 : 15 that this was an undesirable date, which was unfavorable enough not to pursue him further.%%note: She sent him what might very well have been the first explicitly Bayesian rejection notice in dating history, reasoning that if he wrote back with a Bayesian counterargument, this would promote him to being interesting again.  He didn't write back.%%

The point of looking at the problem this way is not that she knew *exact* probabilities and could calculate that the man had an exactly 88% chance of being undesirable.  Rather, by breaking up the problem in that way, she was able to summarize what she thought she knew in compact form, see what those beliefs already implied, and stop bouncing back and forth between imagined reasons why a good date might cancel versus reasons to protect herself from potential bad dates.  An answer *roughly* in the range of 15/17 made the decision clear.

### Internment of Japanese-Americans during World War II

From Robyn Dawes's [Rational Choice in an Uncertain World](https://amazon.com/Rational-Choice-Uncertain-World-Robyn/dp/0155752154):

> Post-hoc fitting of evidence to hypothesis was involved in a most grievous chapter in United States history: the internment of Japanese-Americans at the beginning of the Second World War.  When California governor Earl Warren testified before a congressional hearing in San Francisco on February 21, 1942, a questioner pointed out that there had been no sabotage or any other type of espionage by the Japanese-Americans up to that time.  Warren responded, "I take the view that this lack [of subversive activity] is the most ominous sign in our whole situation. It convinces me more than perhaps any other factor that the sabotage we are to get, the Fifth Column activities are to get, are timed just like Pearl Harbor was timed... I believe we are just being lulled into a false sense of security."

You might want to take your own shot at guessing what Dawes had to say about a Bayesian view of this situation, before reading further.

%%hidden(Answer):Suppose we put ourselves into the shoes of this congressional hearing, and imagine ourselves trying to set up this problem.

- The [1rm prior] [1rb odds] that there would be a conspiracy of Japanese-American saboteurs.
- The [56t likelihood] of the observation "no visible sabotage or any other type of espionage", *given* that a Fifth Column actually existed.
- The likelihood of the observation "no visible sabotage from Japanese-Americans", in the possible world where there is *no* such conspiracy.

As soon as we set up this problem, we realize that, whatever the probability of "no sabotage" being observed if there is a conspiracy, the likelihood of observing "no sabotage" if there *isn't* a conspiracy must be even higher.  This means that the likelihood ratio:

$$\frac{\mathbb P(\neg \text{sabotage} | \text {conspiracy})}{\mathbb P(\neg \text {sabotage} | \neg \text {conspiracy})}$$

...must be *less than 1,* and accordingly:

$$
\frac{\mathbb P(\text {conspiracy} | \neg \text{sabotage})}{\mathbb P(\neg \text {conspiracy} | \neg \text{sabotage})}
<
\frac{\mathbb P(\text {conspiracy})}{\mathbb P(\neg \text {conspiracy})}
\cdot
\frac{\mathbb P(\neg \text{sabotage} | \text {conspiracy})}{\mathbb P(\neg \text {sabotage} | \neg \text {conspiracy})}
$$

Observing the total absence of any sabotage can only decrease our estimate that there's a Japanese-American Fifth Column, not increase it.  (It definitely shouldn't be "the most ominous" sign that convinces us "more than any other factor" that the Fifth Column exists.)

Again, what matters is not the *exact* likelihood of observing no sabotage given that a Fifth Column actually exists.  As soon as we set up the Bayesian problem, we can see there's something *qualitatively* wrong with Earl Warren's reasoning.%%

# Further reading

This has been a very brief and high-speed presentation of Bayes and Bayesianism.  It should go without saying that a vast literature, nay, a universe of literature, exists on Bayesian statistical methods and Bayesian epistemology and Bayesian algorithms in machine learning.  Staying inside Arbital, you might be interested in moving on to read:

## More on the technical side of Bayes' rule

- A sadly short list of [22w example Bayesian word problems].  Want to add more?  (Hint hint.)
- [1zm __Bayes' rule: Proportional form.__] The fastest way to present a step in Bayesian reasoning in a way that will sound sort of understandable to somebody who's never heard of Bayes.
- [1zh __Bayes' rule: Log-odds form.__] A simple transformation of Bayes' rule reveals tools for measuring degree of belief, and strength of evidence.
- [1zg __The "Naive Bayes" algorithm.__]  (Scroll down to the middle.)  The original simple Bayesian spam filter.
- [1zg __Non-naive multiple updates.__]  (Scroll down past Naive Bayes.)  How to avoid double-counting the evidence, or worse, when considering multiple items of *correlated* evidence.
- [21c __Laplace's Rule of Succession.__]  The classic example of an [21b inductive prior].

## More on intuitive implications of Bayes' rule

- [220 __A Bayesian view of scientific virtues.__] Why is it that science relies on bold, precise, and falsifiable predictions? Because of Bayes' rule, of course.
- [update_by_inches __Update by inches.__] It's virtuous to change your mind in response to overwhelming evidence. It's even more virtuous to shift your beliefs a little bit at a time, in response to *all* evidence (no matter how small).
- [1y6 __Belief revision as probability elimination.__] Update your beliefs by throwing away large chunks of probability mass.
- [552 __Shift towards the hypothesis of least surprise.__] When you see new evidence, ask: which hypothesis is *least surprised?*
- [21v __Extraordinary claims require extraordinary evidence.__] The people who adamantly claim they were abducted by aliens do provide *some* evidence for aliens. They just don't provide quantitatively *enough* evidence.
- [ __Ideal reasoning via Bayes' rule.__] Bayes' rule is to reasoning as the [https://en.wikipedia.org/wiki/Carnot_cycle Carnot cycle] is to engines: Nobody can be a perfect Bayesian, but Bayesian reasoning is still the theoretical ideal.
- [4xx __Likelihoods, p-values, and the replication crisis.__]  Arguably, a large part of the replication crisis can ultimately be traced back to the way journals treat p-values, and a large number of those problems can be summed up as "P-values are not Bayesian."