Probability Distribution Monad (Continued)
In this lesson, we will continue our discussion on probability distribution using monads in C# and implement the weight functionality. We will also highlight the advantages of probability distribution monad.
We'll cover the following...
In the previous lesson, we achieved two significant results in our effort to build better probability tools. First, we demonstrated that the SelectMany
implementation, which applies a likelihood function to a prior probability is the bind operation of the probability monad. Second, we gave an implementation of a wrapper object that implements it. Its action can be summed up as:
- Sample from the prior distribution
- Use the likelihood function to get the conditional distribution
- Sample from the conditional distribution
- Run the projection on the pair of samples to get the result
Implementing the Weight Function
You will probably recall that we did not implement the Weight
function.
It’s a little tricky to do so, for two reasons.
- First, we decided to make weights integers.
- If the weights are fractions between and , you can multiply the weight of the prior sample by the weight of the conditional sample. (And if the weights are logarithms, you can add them.) It’s trickier with integers.
- Second, the projection at the end introduces once again the possibility that there will be “collisions”; the projection could pick non-unique values for unique combinations of the samples, that then have to be weighted as the sum.
That’s all a little abstract, so let’s work an example.
Example
Suppose we have ...