## 2.4 Transforming between Distributions

In describing the inversion method, we introduced a technique that generates samples according to some distribution by transforming canonical uniform random variables in a particular manner. Here, we will investigate the more general question of which distribution results when we transform samples from an arbitrary distribution to some other distribution with a function . Understanding the effect of such transformations is useful for a few reasons, though here we will focus on how they allow us to derive multidimensional sampling algorithms.

Suppose we are given a random variable drawn from some PDF with CDF . Given a function with , if we compute , we would like to find the distribution of the new random variable . In this case, the function must be a one-to-one transformation; if multiple values of mapped to the same value, then it would be impossible to unambiguously describe the probability density of a particular value. A direct consequence of being one-to-one is that its derivative must either be strictly greater than 0 or strictly less than 0, which implies that for a given ,

From the definition of the CDF, Equation (2.3), we can see that

This relationship between CDFs leads directly to the relationship between their PDFs. If we assume that ’s derivative is greater than 0, differentiating gives

and so

In general, ’s derivative is either strictly positive or strictly negative, and the relationship between the densities is

How can we use this formula? Suppose that over the domain , and let . What is the PDF of the random variable ? Because we know that ,

This procedure may seem backward—usually we have some PDF that we want to sample from, not a given transformation. For example, we might have drawn from some and would like to compute from some distribution . What transformation should we use? All we need is for the CDFs to be equal, or , which immediately gives the transformation

This is a generalization of the inversion method, since if were uniformly distributed over then , and we have the same procedure as was introduced previously.

### 2.4.1 Transformation in Multiple Dimensions

In the general -dimensional case, a similar derivation gives the analogous relationship between different densities. We will not show the derivation here; it follows the same form as the 1D case. Suppose we have a -dimensional random variable with density function . Now let , where is a bijection. In this case, the densities are related by

where is the absolute value of the determinant of ’s Jacobian matrix, which is

where subscripts index dimensions of and .

For a 2D example of the use of Equation (2.21), the polar transformation relates Cartesian coordinates to a polar radius and angle,

Suppose we draw samples from some density . What is the corresponding density ? The Jacobian of this transformation is

and the determinant is . So, . Of course, this is backward from what we usually want—typically we start with a sampling strategy in Cartesian coordinates and want to transform it to one in polar coordinates. In that case, we would have

In 3D, given the spherical coordinate representation of directions, Equation (3.7), the Jacobian of this transformation has determinant , so the corresponding density function is

This transformation is important since it helps us represent directions as points on the unit sphere.

### 2.4.2 Sampling with Multidimensional Transformations

Suppose we have a 2D joint density function that we wish to draw samples from. If the densities are independent, they can be expressed as the product of 1D densities

and random variables can be found by independently sampling from and from . Many useful densities are not separable, however, so we will introduce the theory of how to sample from multidimensional distributions in the general case.

Given a 2D density function, the marginal density function is obtained by “integrating out” one of the dimensions:

This can be thought of as the density function for alone. More precisely, it is the average density for a particular over all possible values.

If we can draw a sample , then—using Equation (2.1)—we can see that in order to sample , we need to sample from the conditional probability density, , which is given by:

Sampling from higher-dimensional distributions can be performed in a similar fashion, integrating out all but one of the dimensions, sampling that one, and then applying the same technique to the remaining conditional distribution, which has one fewer dimension.

#### Sampling the Bilinear Function

The bilinear function

interpolates between four values at the four corners of . ( is at , is at , at , and at .) After integration and normalization, we can find that its PDF is

<<Sampling Inline Functions>>+=
Float BilinearPDF(Point2f p, pstd::span<const Float> w) { if (p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) return 0; if (w + w + w + w == 0) return 1; return 4 * ((1 - p) * (1 - p) * w + p * (1 - p) * w + (1 - p) * p * w + p * p * w) / (w + w + w + w); }

The two dimensions of this function are not independent, so the sampling method samples a marginal distribution before sampling the resulting conditional distribution.

<<Sampling Inline Functions>>+=
Point2f SampleBilinear(Point2f u, pstd::span<const Float> w) { Point2f p; <<Sample for bilinear marginal distribution>>
p.y = SampleLinear(u, w + w, w + w);
<<Sample for bilinear conditional distribution>>
p.x = SampleLinear(u, Lerp(p.y, w, w), Lerp(p.y, w, w));
return p; }

We can choose either or to be the marginal distribution. If we choose and integrate out , we find that

performs linear interpolation between two constant values, and so we can use SampleLinear() to sample from the simplified proportional function since it normalizes the associated PDF.

<<Sample for bilinear marginal distribution>>=
p.y = SampleLinear(u, w + w, w + w);

Applying Equation (2.1) and again canceling out common factors, we have

which can also be sampled in using SampleLinear().

<<Sample for bilinear conditional distribution>>=
p.x = SampleLinear(u, Lerp(p.y, w, w), Lerp(p.y, w, w));

Because the bilinear sampling routine is based on the composition of two 1D linear sampling operations, it can be inverted by applying the inverses of those two operations in reverse order.

<<Sampling Inline Functions>>+=
Point2f InvertBilinearSample(Point2f p, pstd::span<const Float> w) { return {InvertLinearSample(p.x, Lerp(p.y, w, w), Lerp(p.y, w, w)), InvertLinearSample(p.y, w + w, w + w)}; }

See Section A.5 for further examples of multidimensional sampling algorithms, including techniques for sampling directions on the unit sphere and hemisphere, sampling unit disks, and other useful distributions for rendering.