Discrete Probability Distribution Type as a Monad
Explore the concept of discrete probability distributions as monads in C#. Understand additive monads, zero values, and the implications of defining Where and SelectMany methods. This lesson guides you through implementing empty distributions and addresses the challenges and benefits of these concepts in probability programming.
We'll cover the following...
Before we get going on this lesson, you might want to refresh your memory of what an additive monad is.
Additive Monad
Briefly, an additive monad is a monad where there is a “zero value”; like the number zero, “multiplying” by zero produces a zero, and “adding” a zero is an identity.
For example, the sequence monad, IEnumerable<T>, has a zero: the empty sequence. If we Select or SelectMany from the empty sequence — our analog of “multiplying” — we get an empty sequence. If we concatenate an empty sequence onto another sequence — the sequence analog of “adding” — we get the original sequence.
All additive monads can have a Where function defined on them; if we wanted to implement Where for sequences and didn’t care about performance, we could implement it like this:
public static IEnumerable<T> Single<T>(T t)
{
yield return t;
}
public static IEnumerable<T> ...