Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 39

Inverse Transform Sampling Explained

Recommended resources

Introduction

In our last lesson, we established the statistical pillars of the Monte Carlo method: the Law of Large Numbers, which guarantees convergence to the correct physical quantity, and the Central Limit Theorem, which quantifies the statistical uncertainty of our estimate and its characteristic convergence rate. We now have the "why," but we still need the "how." A Monte Carlo simulation is, at its core, a numerical experiment that mimics the random processes of particle transport. To run this experiment, we must be able to generate random numbers that follow the specific probability distributions governing these physical events.

This lesson introduces our first, and most fundamental, tool for this task. Our objective is to learn how to sample from arbitrary probability distributions using the inverse transform sampling method, with examples for exponential and discrete distributions. We will see how this elegant technique allows us to transform the generic, uniformly distributed random numbers our computers provide into samples from any distribution we desire, provided we know its cumulative distribution function (CDF). This is the key that unlocks our ability to simulate everything from the distance a particle travels to the type of interaction it undergoes.


The Principle of Inverse Transform Sampling

The core problem is this: programming languages typically provide a function that generates pseudo-random numbers uniformly distributed between 0 and 1. Let's call such a number . However, physical processes are rarely uniform. For example, the distance a photon travels in a medium before an interaction is described by an exponential distribution. How can we use our uniform random numbers to generate samples that follow this, or any other, distribution?

The inverse transform method provides a beautiful and direct solution. The key lies in the cumulative distribution function, , which you'll recall gives the probability that a random variable will take on a value less than or equal to . By definition, the range of any CDF is the interval [0, 1]—precisely the same interval from which our uniform random number generator draws its values.

The method proposes that we generate a uniform random number and set it equal to the value of the CDF. Then, we solve for :

The value obtained from this "inverse transform" is a random sample from our target probability distribution. The process is illustrated beautifully in the following plots, which show a CDF and its corresponding inverse. A uniform random number on the y-axis of the CDF maps to the sampled value .

On the right, a cumulative distribution function (CDF), \(p=F(x)\). On the left, its inverse, \(x=F^{-1}(p)\). A uniformly chosen value of \(p\) between 0 and 1 produces a value of \(x\) sampled from the target distribution.

Why does this work? The intuition is that regions where the PDF is high correspond to steep sections of the CDF. When we sample the y-axis uniformly, we are more likely to land in these steep regions, which map to a narrow range of x-values. Conversely, regions where the PDF is low correspond to flat sections of the CDF, which map to a wide range of x-values but are sampled less frequently.

The formal proof is quite direct and confirms this intuition. Your interest in mathematical formalism will make this satisfying.

Inverse transform sampling - Wikipedia

This Wikipedia article provides a concise and formal overview of the method.

First, read the formal statement of the method. Then, study the short proof. The key steps are: Start with the probability we want to find, P(F^{-1}(U) \le x). Apply the (monotonically increasing) function F to both sides of the inequality. Use the definition of the CDF of a uniform random variable, P(U \le y) = y, to complete the proof.


Applying the Method: Two Key Cases

The inverse transform method is powerful because it provides a general algorithm. However, its practical application depends on whether the inverse CDF, , can be easily computed. Let's explore the two most common scenarios you'll encounter in particle transport simulation.

Case 1: The Exponential Distribution

The exponential distribution is of paramount importance in transport theory. It describes the probability distribution of the distance a particle will travel in a uniform medium before its next interaction. Given a total macroscopic cross section , the probability density function (PDF) for the path length is:

To sample from this distribution, we follow the inverse transform method:

  1. Find the CDF: We integrate the PDF from 0 to :

  2. Invert the CDF: We set and solve for , where is our uniform random number.

  3. Simplify: A crucial observation is that if is a random variable uniformly distributed on [0, 1], then is also uniformly distributed on [0, 1]. This means we can replace with for simplicity, yielding the final sampling formula:

This is a very important result that you will use constantly when writing Monte Carlo codes. The following video provides an excellent walkthrough of this exact derivation.

Inverse Transform Sampling : Data Science Concepts

This video from the channel ritvikmath clearly explains the motivation and derives the inverse transform method for the exponential distribution.

Watch the review of the exponential PDF and CDF. Focus on the core derivation where the transformation T is shown to be the inverse of the CDF: the derivation. Finally, see how this is applied to find the explicit formula for sampling from the exponential distribution.

The article below also contains a very clear, step-by-step derivation.

Mathematical Foundations of Monte Carlo Methods

This article from Scratchapixel provides excellent context and derivations for Monte Carlo sampling methods.

Please read the section titled "CDFs of Well-Known PDFs". It walks through the derivation for the exponential distribution, which we will use to sample particle path lengths in the next module.

Case 2: Discrete Distributions

Another common task is to choose one outcome from a set of discrete possibilities, each with a known probability. For example, when a photon interacts, we need to sample whether the interaction is a photoelectric effect, Compton scatter, or pair production, based on their respective partial cross sections.

For a discrete distribution, the CDF is a step function, which isn't analytically invertible. However, we can still apply the same principle algorithmically.

Consider a set of possible outcomes with probabilities , where .

  1. Construct the CDF: We compute the cumulative probabilities, , as a sequence:

    • ...
    • ...
  2. Sample: We generate a uniform random number . We then find the index such that:

    The sampled outcome is .

This process is equivalent to dividing the interval [0, 1] into segments with lengths equal to the probabilities . Our uniform random number will fall into one of these segments, and we simply select the corresponding outcome.

A discrete distribution for four outcomes with probabilities 0.1, 0.2, 0.3, and 0.4. The dashed red lines mark the cumulative probabilities at 0.1, 0.3 (=0.1+0.2), 0.6 (=0.3+0.3), and 1.0 (=0.6+0.4). To sample, you'd generate a random number \(\xi \in [0, 1]\) and see which zone it falls into.

The following video gives a superb and detailed explanation of this process using a dice roll as an example. It also discusses the algorithmic implementation in Python, which will be very clear given your programming background.

Inverse Transform Method for Generating Random Variables

This video by Roman Paolucci provides a deep dive into inverse transform sampling for discrete distributions.

Watch the beginning for a review of PMFs and CDFs for a discrete dice-roll example. This builds the foundation. The core idea of inverting the discrete CDF is explained here: inverting the map. The four steps of the algorithm are summarized concisely here: the steps. Finally, observe how this is implemented in Python. The logic of calculating the cumulative sum and then searching for the correct interval is shown clearly: Python implementation.


Conclusion

In this lesson, we have moved from the theory of why Monte Carlo works to the practice of how it works. We've developed our first essential tool for building a simulation: a method for generating random numbers that follow the laws of physics.

Key Takeaways:

  • The inverse transform sampling method is a general technique to sample from any probability distribution whose cumulative distribution function (CDF), , is known and invertible.
  • The method works by generating a uniform random number and calculating the sample as .
  • For continuous distributions with an analytical inverse CDF, such as the exponential distribution, this method provides a direct formula. The sampling formula for path length is .
  • For discrete distributions, the method is applied algorithmically by constructing the cumulative probability table and finding which interval the uniform random number falls into.

Preview of the Next Lesson:
The inverse transform method is powerful, but it has a major limitation: what if we cannot easily invert the CDF? This is true for many important distributions, including the normal (Gaussian) distribution and, more relevant to us, the Klein-Nishina distribution for Compton scattering angles. In our next lesson, we will explore a different and more flexible technique to handle these cases: rejection sampling.