Introduction
In our previous lesson, we mastered the inverse transform method, a powerful technique for sampling from distributions like the exponential and discrete distributions, whose cumulative distribution functions (CDFs) are readily invertible. However, this method's primary requirement—an invertible CDF—is also its main limitation. Many of the most important probability distributions in physics either do not have a simple analytical CDF or their CDF cannot be inverted. A prime example, which we will tackle soon, is the Klein-Nishina formula for Compton scattering.
This lesson introduces a more versatile and general technique that overcomes this obstacle. Our goal is to learn how to sample from distributions using the rejection method. This clever algorithm allows us to generate samples from a complex "target" distribution even if we only know its functional form up to a constant of proportionality. It does this by using a simpler, easy-to-sample "proposal" distribution as a starting point. This method is a cornerstone of practical Monte Carlo simulation and will be an indispensable tool in our computational toolkit.
The Principle of Rejection Sampling
Imagine you want to sample from a target probability density function (PDF) that is difficult to sample from directly. The core idea of rejection sampling is to find a simpler, "proposal" PDF, , which we can easily sample from (e.g., a uniform or normal distribution). The only strict requirement is that the support of must cover the support of (i.e., if , then must also be greater than 0).
Next, we find a constant large enough such that is greater than or equal to for all values of . This scaled proposal function, , forms an "envelope" that completely encloses our target distribution .
The process then works as follows:
- We sample a random value from the proposal distribution . This gives us a candidate sample.
- We then generate a second random number, , uniformly from the interval . This is geometrically equivalent to picking a random point under the envelope curve at the position .
- If this point also falls under the target curve (i.e., if ), we "accept" the sample .
- If the point falls between the target and the envelope curves (i.e., if ), we "reject" the sample and repeat the process from step 1.
The collection of all accepted samples will be distributed according to the target PDF, .

The beauty of this method is that it works even if we only know up to a normalizing constant. If , where is an unnormalized density that we can evaluate and is an unknown (or difficult to compute) integral, we can simply build our envelope over with and use the acceptance condition . The logic remains the same.
The Algorithm and its Justification
Let's formalize the algorithm and explore why it works. The following video provides an excellent walkthrough, starting from the motivation and then detailing the steps. It also introduces the common and practical scenario where we only know the numerator of the target PDF.
Accept-Reject Sampling : Data Science Concepts
This video from the channel ritvikmath offers a clear and comprehensive introduction to the accept-reject method, which is another name for rejection sampling.
First, watch the section that explains why we need this method and how it handles distributions where the normalizing constant is unknown: the motivation. Then, focus on the formal presentation of the algorithm's steps: the algorithm.
As the video explains, the algorithm can be stated more compactly. After sampling from , we sample a single uniform random number and accept if:
This is mathematically equivalent to the two-step process described earlier and is how the method is typically implemented.
Formal Proof
Your appreciation for mathematical rigor will find the proof for this method satisfying. It elegantly uses conditional probability to show that the distribution of accepted samples is indeed our target distribution .
The core question is: what is the probability distribution of a sample , given that it was accepted? Using Bayes' theorem, we can write this as:
Here, is simply . The conditional probability of acceptance, given , is the ratio of the target height to the envelope height, . The denominator, the overall probability of acceptance, is found by integrating the conditional acceptance probability over all possible .
The following article provides a concise and formal proof.
Sampling: Two Basic Algorithms
This blog post by Gregory Gundersen offers an excellent written explanation of rejection sampling, complete with a formal proof that complements the video's derivation.
First, read the introduction which sets up the problem and shows a helpful diagram: the concept. The three steps of the algorithm are listed clearly here. Then, work through the formal proof, which demonstrates that the distribution of accepted samples is exactly the target distribution.
For a second perspective, the ritvikmath video also contains a very clear mathematical proof and a powerful intuitive explanation.
Accept-Reject Sampling : Data Science Concepts
This video provides both a mathematical and an intuitive justification for why rejection sampling works.
Watch the step-by-step mathematical proof, which uses Bayes' theorem in a very similar way to the article. Then, watch the intuitive explanation. It focuses on the meaning of the ratio p(x)/g(x) and provides a deep conceptual understanding of the process.
Practical Considerations: Efficiency
The main drawback of rejection sampling is its efficiency, which is directly related to how "tight" the envelope is to the target .
The probability of accepting a sample in any given attempt can be shown to be (assuming both and are normalized PDFs). This means, on average, we need to generate candidate samples to get one accepted sample.
- If is close to 1 (meaning is a very good approximation of ), the method is very efficient.
- If is a poor match for , might need to be very large to create a valid envelope, leading to a high rejection rate and a computationally expensive algorithm. This is especially problematic in high-dimensional spaces.

Finding the Optimal M
To maximize efficiency, we need to find the smallest possible value of . This is an optimization problem:
where denotes the supremum, or least upper bound. For most well-behaved functions, this is simply the maximum value. Given your background in physics and mathematics, you can see that this maximum can often be found analytically by taking the derivative of the ratio with respect to , setting it to zero, and solving for the value of that maximizes . Plugging this back into gives the optimal . When this is not feasible, can be found numerically or a safe, larger-than-necessary value can be used at the cost of efficiency.
Conclusion
We have now added a second, more flexible sampling method to our Monte Carlo arsenal. Rejection sampling frees us from the constraints of the inverse transform method and allows us to tackle a much broader class of probability distributions.
Key Takeaways:
- Rejection sampling is a general method for drawing samples from a target distribution by using an easier-to-sample proposal distribution .
- The method requires finding a constant to create an "envelope" that is always greater than or equal to .
- The algorithm works by generating a candidate from and accepting it with a probability equal to the ratio of the target height to the envelope height at that point: .
- The efficiency of the method is . A good choice of proposal distribution that closely matches the shape of is crucial for good performance.
Preview of the Next Lesson:
With the theory of rejection sampling firmly established, we are now ready for a direct and vital application. In the next lesson, we will implement a rejection sampling algorithm in Python to sample from the Klein-Nishina distribution. This distribution governs the change in angle and energy during Compton scattering, a fundamental process in photon transport. Since its CDF is not analytically invertible, rejection sampling is the ideal technique for simulating this interaction.