Random variable monads


haskell monad probability

In this note we give a mathematical translation of the monadic treatment to random variables in this amazing stackexchange answer, which is my (figuratively) sixth attempt to understanding monads. We show the meanings of return, bind / (>>=), join, fmap / (<$>) and ap / (<*>) and the monad laws in this concrete example. See Hackage for a clear and concise definition of monads.

For notational consistency, we use capital letters for random variables in Haskell code, as is the convention in probability theory. We also use == to stand for derivation in Haskell code.

For any state space \(S\), let \(R(S)\) be the space of random variables on \(S\). In the following \(S, T, P\) are notations of state spaces, and \(X, Y, Z\) random variables.

In the stackexchange answer (we ignore the psuedorandomness and pretend everything is truly random),

Deriving fmap / (<$>):

As for ap / (<*>):

Now we translate the monad laws: