Post · 10 min read· pp. 108–111

Transforming between Distributions

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 diagram. 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 diagram drawn from some PDF diagram with CDF diagram. Given a function diagram with diagram, if we compute diagram, we would like to find the distribution of the new random variable diagram. In this case, the function diagram must be a one-to-one transformation; if multiple values of diagram mapped to the same diagram value, then it would be impossible to unambiguously describe the probability density of a particular diagram value. A direct consequence of diagram 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 diagram,

diagram

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

diagram

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

diagram

and so

diagram

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

diagram

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

diagram

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 diagram drawn from some diagram and would like to compute diagram from some distribution diagram. What transformation should we use? All we need is for the CDFs to be equal, or diagram, which immediately gives the transformation

diagram

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

2.4.1 Transformation in Multiple Dimensions

In the general diagram-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 diagram-dimensional random variable diagram with density function diagram. Now let diagram, where diagram is a bijection. In this case, the densities are related by

diagram

(2.21)

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

diagram

where subscripts index dimensions of diagram and diagram.

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

diagram

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

diagram

and the determinant is diagram. So, diagram. 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

diagram

(2.22)

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

diagram

(2.23)

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

2.4.2 Sampling with Multidimensional Transformations

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

diagram

and random variables diagram can be found by independently sampling diagram from diagram and diagram from diagram. 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 diagram is obtained by “integrating out” one of the dimensions:

diagram

(2.24)

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

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

diagram

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

diagram

(2.25)

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

diagram

<>+=

Float BilinearPDF(Point2f p, pstd::span w) { if (p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) return 0; if (w[0] + w[1] + w[2] + w[3] == 0) return 1; return 4 * ((1 - p[0]) * (1 - p[1]) * w[0] + p[0] * (1 - p[1]) * w[1] + (1 - p[0]) * p[1] * w[2] + p[0] * p[1] * w[3]) / (w[0] + w[1] + w[2] + w[3]); }

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

<>+=

Point2f SampleBilinear(Point2f u, pstd::span w) { Point2f p; <<Sample diagram for bilinear marginal distribution>> 

p.y = SampleLinear(u[1], w[0] + w[1], w[2] + w[3]);

<<Sample diagram for bilinear conditional distribution>> 

p.x = SampleLinear(u[0], Lerp(p.y, w[0], w[2]), Lerp(p.y, w[1], w[3]));

return p; }

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

diagram

diagram 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 diagram for bilinear marginal distribution>>=

p.y = SampleLinear(u[1], w[0] + w[1], w[2] + w[3]);

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

diagram

which can also be sampled in diagram using SampleLinear().

<<Sample diagram for bilinear conditional distribution>>=

p.x = SampleLinear(u[0], Lerp(p.y, w[0], w[2]), Lerp(p.y, w[1], w[3]));

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.

<>+=

Point2f InvertBilinearSample(Point2f p, pstd::span w) { return {InvertLinearSample(p.x, Lerp(p.y, w[0], w[2]), Lerp(p.y, w[1], w[3])), InvertLinearSample(p.y, w[0] + w[1], w[2] + w[3])}; }

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.