Efficiently Sampling a Conditioned Distribution
In this lesson we will learn how to implement the conditioned distribution from the previous lesson without using rejection sampling; we want to avoid the possibly-long-running loop.
We'll cover the following...
In the previous lesson, we concluded that the predicates and projections used by Where
and Select
on discrete distributions be pure functions:
- They must complete normally, produce no side effects, consume no external state, and never change their behavior.
- They must produce the same result when called with the same argument, every time.
If we make these restrictions then we can get some big performance wins out of Where and Select. Let’s see how.
Dealing With the “Long-Running Loop”
The biggest problem we face is that possibly-long-running loop in the Where
. We are “rejection sampling” the distribution, and we know that can take a long time. Is there a way to directly produce a new distribution that can be efficiently sampled?
Of course, there is. Let’s make a helper method: ...