Foundations of the Giry Monad

The Giry monad is the canonical probability monad that operates on the level of measures, which are the abstract constructs that canonically represent probability distributions. It’s sort of the baseline by which all other probability monads can be judged.

In this article I’m going to go through the categorical and measure-theoretic foundations of the Giry monad. In another article, I’ll describe how you can implement it in a very faithful sense in Haskell.

I was putting some notes together for another project and wound up writing up things up in a somewhat blog-friendly style, but this isn’t intended to be a tutorial per se. Really this isn’t the kind of content I’d usually post here, but since I’ve jotted everything up, I figured I may as well. If you like extremely dry mathematics and computer science, you’re in the right place.

I won’t define everything under the sun here - for properties or coherence conditions or other things that I’ve elided details on, check out something like Mac Lane or Aliprantis & Border. I’ll include some references at the end.

This is the game plan we’re working with:

Let’s get started.

Categorical Foundations

A category is a collection of objects and morphisms between them. So if , , , and are objects in , then , , and are examples of morphisms. These morphisms can be composed in the obvious associative way, i.e.

and there exist identity morphisms (or automorphisms) that simply map objects to themselves.

A functor is a mapping between categories (equivalently, it’s a morphism in the category of so-called ‘small’ categories). The functor takes every object in to some object in , and every morphism in to some morphism in , such that the structure of morphism composition is preserved. An endofunctor is a functor from a category to itself, and a bifunctor is a functor from a pair of categories to another category, i.e. .

A natural transformation is a mapping between functors. So for two functors , a natural transformation associates to every object in a morphism in .

A monoidal category is a category with some additional monoidal structure, namely an identity object and a bifunctor called the tensor product, plus several natural isomorphisms that provide the associativity of the tensor product and its right and left identity with the identity object .

A monoid in a monoidal category is an object in together with two morphisms (obeying the standard associativity and identity properties) that make use of the category’s monoidal structure: the associative binary operator , and the identity .

A monad is (infamously) a ‘monoid in the category of endofunctors’. So take the category of endofunctors whose objects are endofunctors and whose morphisms are natural transformations between them. This is a monoidal category; there exists an identity endofunctor for all in , plus a tensor product defined by functor composition such that the required associativity and identity properties hold. is thus a monoidal category, and any specific monoid we construct on it is a specific monad.

Probabilistic Foundations

A measurable space is a set equipped with a topology-like structure called a -algebra that essentially contains every well-behaved subset of in some sense. A measure is a particular kind of set function from the -algebra to the nonnegative real line. A measure just assigns a generalized notion of area or volume to well-behaved subsets of . In particular, if the total possible area or volume of the underlying set is 1 then we’re dealing with a probability measure. A measurable space completed with a measure, e.g. is called a measure space, and a measurable space completed with a probability measure is called a probability space.

There is a lot of overloaded lingo around the word ‘measurable’. A ‘measurable set’ is an element of a -algebra in a measurable space. A measurable mapping is a mapping between measurable spaces. Given a ‘source’ measurable space and ‘target’ measurable space , a measurable mapping is a map with the property that, for any measurable set in the target, the inverse image is measurable in the source. Or, formally, for any in , you have that is in .

The Space of Probability Measures on a Measurable Space

If you consider the collection of all measurable spaces and measurable mappings between them, you get a category. Define to be the category of measurable spaces. So, objects are measurable spaces and morphisms are the measurable mappings between them.

For any specific measurable space in , we can consider the space of all possible probability measures that could be placed on it and denote that . To be clear, is a space of measures - that is, a space in which the points themselves are probability measures.

What’s remarkable about is that it is itself a measurable space. Let me explain.

As a probability measure, any element of is a function from measurable subsets of to the interval in . That is: if is the measurable space , then a point in is a function . For any measurable in , there just naturally exists a sort of ‘evaluation’ mapping I’ll call that takes a measure on and evaluates it on the set . To be explicit: if is a measure in , then simply evaluates . It ‘runs’ the measure in a sense; in Haskell, would be analogous to a function like \f -> f a for some a.

This evaluation map corresponds to an integral. If you have a measurable space , then for any a subset in , for the characteristic or indicator function of (where is if is in , and is otherwise). And we can actually extend to operate over measurable mappings from to , where is a suitable -algebra on . Here we typically use what’s called the Borel -algebra, which takes a topology on the set and then generates a -algebra from the open sets in the topology (for we can just use the ‘usual’ topology generated by the Euclidean metric). For a measurable function, we can define the evaluation mapping as .

We can abuse notation here a bit and just use to refer to ‘duck typed’ mappings that evaluate measures over measurable sets or measurable functions depending on context. If we treat as a function , then has type . If we treat as a function , then has type . I’ll say to refer to the mappings that accept either measurable sets or functions.

In any case. For a measurable space , there exists a topology on called the weak-* topology that makes all the evaluation mappings continuous for any measurable set or measurable function . From there, we can generate the Borel -algebra that makes the evaluation functions measurable. The result is that is itself a measurable space, and thus an object in .

The space actually has all sorts of insane properties that one wouldn’t expect - there are implications on convexity, completeness, compactness and such that carry over from . But I digress.

is a Functor

So: for any an object in , we have that is also an object in . And if you look at like a functor, you notice that it takes objects of to objects of . Indeed, you can define an analogous procedure on morphisms in as follows. Take to be another object (read: measurable space) in and to be a morphism (read: measurable mapping) between them. Now, for any measure in we can define (this is called the image, distribution, or pushforward of under ). For some and , thus takes measurable sets in to a value in the interval - that is, it is a measure on . So we have that:

and so is an endofunctor on .

is a Monad

See where we’re going here? If we can define natural transformations and such that is a monoid in the category of endofunctors, we’ll have defined a monad. We thus need to come up with a suitable monoidal structure, et voilà.

First the identity. We want a natural transformation between the identity functor and the functor such that for any measurable space in . Evaluating the identity functor simplifies things to .

We can define this concretely as follows. Grab a measurable space in and define for any point and any measurable set . is thus a probability measure on - we assign to measurable sets that contain , and 0 to those that don’t. If we peel away another argument, we have that , as required.

So takes points in measurable spaces to probability measures on those spaces. In technical parlance, it takes a point to the Dirac measure at - the probability measure that places the entirety of its mass at .

Now for the other part of the monoidal structure, . I initially found this next part to be a bit of a mind fuck, but let me see what I can do about that.

Recall that the category of endofunctors, , is monoidal, so there exists a tensor product that we can deal with, which here just corresponds to functor composition. We’re looking for a natural transformation:

which is often written as:

Take a measurable space in and then consider the space of probability measures over it, . Then take the space of probability measures over the space of probability measures on , . Since is an endofunctor, this is again a measurable space, and for any measurable subset of we again have a family of mappings that take a probability measure in and evaluate it on . We want to be the thing that turns a measure over measures into a plain old probability measure on .

In the context of probability theory, this kind of semigroup action is a marginalizing operator. We’re taking the ‘uncertainty’ captured in via the probability measure and smearing it into the probability measures in .

Take in and some a measurable subset of . We can define as follows:

Using some lambda calculus notation to see the argument for , we can expand the integrals to get the following gnarly expression:

Notice what’s happening here. For a measurable space, we’re integrating over the space of probability measures on , with respect to the probability measure , which itself is a point in the space of probability measures over probability measures on , . Whew.

The spaces we’re integrating over here are unusual, but is still a probability measure, so when applied to a measurable set in it results in a probability in . So, peeling back an argument, we have that has type . In other words, it’s a probability measure on , and thus is in . And if we peel back another argument, we find that:

so, as required, that

It’s also worth noting that we can overload the notation for in the same way we did for , i.e. to supply measurable functions in addition to measurable sets:

Combining the three components, we get , the canonical Giry monad.

In Haskell, when we’re dealing with monads we typically use the bind operator instead of manually dealing with the functorial structure and (called ‘join’). Bind has the type:

and for illustration, we can define for the Giry monad like so:

Here is in , is in , and is in , so note that we potentially simplify the outermost integral enormously. It now operates over a general measurable space, rather than a space of measures in particular, and this will come in handy when we get to implementation details in the next post.

Wrapping Up

That’s about it for now. It’s worth noting as a kind of footnote here that the existence of the Giry monad also obviously implies the existence of a Giry applicative functor. But the official situation for applicative functors seems kind of weird in this context, and I’m not yet up to the task of dealing with it formally.

Intuitively, one should be able to define the binary applicative operator characteristic of its lax monoidal structure as follows:

But this has some really weird measure-theoretic implications - namely, that it assumes the existence of a space of probability measures over the space of all measurable functions , which is not trivial to define and indeed may not even exist. It seems like some people are looking into this problem as I just happened to stumble on this paper on the arXiv while doing some googling. I notice that some people on e.g. nLab require categories with additional structure beyond for the development of the Giry monad as well, for example the category of Polish (separable, completely metrizable) spaces , so maybe the extra structure there takes care of the quirks.

Anyway. Applicatives are neat here because applicative probability measures are independent probability measures. And the existence of applicativeness means you can do all the things with independent probability measures that you might be used to. Measure convolution and friends are good examples. Given a measurable space that supports some notion of addition and two probability measures and in , we can add measures together via:

where and are both points in . Subtraction and multiplication translate trivially as well.

In another article I’ll detail how the Giry monad can be implemented in Haskell and point out some neat extensions. There are some cool connections to continuations and codensity monads, and seemingly de Finetti’s theorem and exchangeability. That kind of thing. It’d also be worth trying to justify independence of probability measures from a categorical perspective, which seems easier than resolving the nitty-gritty measurability qualms I mentioned above.

‘Til then! Thanks to Jason Forbes for sifting through this stuff and providing some great comments.

References: