# 2D sampling with multidimensional transformations

I'm currently learning mathemetical concepts of distribution and the way to use them in a ray tracer with the book "Physically Based Rendering".

Let's start by uniformly sampling an hemisphere:

As you probably know, a way to generate the uniformly distributed direction is to use the inversion method.

Let us denote by $p$ our uniform probability density function:

$p(\omega) = \cfrac{1}{2\pi}$ and so $p(\theta, \phi) = \sin(\theta)p(\omega)$.

Then you compute $p(\theta)$, $p(\phi | \theta)$, you integrate your cumulative distribution function and you invert the function.

My questions are:

• What does $p(\theta, \phi)$ really mean?

• What is the transformation between $p(\omega)$ and $p(\theta, \phi)$?

• In the book, to find $p(\theta,\phi)$ they state that $p(\theta, \phi) d\theta d\phi = p(\omega)d\omega$, but why?

I know that for $p(\omega)$, our random variable is a given $\omega$ (a direction), so the function represents a relative probability for this direction (so a solid angle, because the relative term implies a direction and a delta area around this direction).

But for $p(\theta,\phi)$, our random variable is now the couple $(\theta,\phi)$. To what extent is it different from a direction?

• @DanHulme Do you have any answer for this post ? – Yoo Jun 24 '17 at 11:08
• Note that the @username notifications only work for users who have already commented on this particular post. It's just coincidence that the person mentioned happens to have posted an answer... – trichoplax Jun 25 '17 at 18:43
• @trichoplax It's not a complete coincidence. I think I did get notified, because I'd previously edited the question, and the comment reminded me that I was intending to come back and post an answer when I got more time. – Dan Hulme Jun 28 '17 at 10:55
• Interesting. I didn't realise that edits did that. It makes sense though... Thanks for letting me know. – trichoplax Jun 28 '17 at 21:43
• @trichoplax I just tried my luck – Yoo Jun 29 '17 at 21:27

I'm not sure I've correctly understood the question, but here goes.

You're trying to sample directions uniformly, so you've got $p(\omega)$, which is the probability of getting a particular direction. But what is a direction? You actually need your probability distribution to produce numbers in some representation, and the easiest representation to deal with is lat-long (i.e. two angles). So the thing you actually need to sample from is the probability distribution of pairs of angles. This is what $p(\theta, \phi)$ is: the joint probability of two variables.

$p(\omega)$ and $p(\theta, \phi)$ mean the same thing geometrically, but the former gives you an abstract direction you can't sample from directly, while the latter more usefully gives you two numbers that represent a direction.

The reason for your third bullet point is to do with the point you've made about how it isn't just a single direction. These aren't really functions: they're distributions. A direction is infinitesimal, so you can't have a probability of just one direction. What you actually need to do is integrate it over the directions you're interested in. $$\int p(\omega)d\omega = \int_0^{2\pi}\int_0^\pi p(\theta, \phi)d\theta d\phi = 1$$

Whichever representation you use, the integral over the hemisphere has to be 1, because it's a probability distribution.

I'm not sure if I need to explain this or if you already understood, but here's the origin of the $sin(\theta)$. When you do the double-integral on the right, you're splitting the problem up into a series of rings, or slices of the unit sphere. Each ring has constant $\theta$ while $\phi$ goes from $0$ to $2\pi$. Also, the area of each individual ring decreases as $\theta$ increases: the ring at the equator is huge, while the last "ring" at the pole is tiny. The area decreases as $sin(\theta)$. Because we want each unit of area of the sphere to have the same probability, we need the smaller rings to get a smaller share of the probability.

As Florian R. explains, you can do that by including the $sin(\theta)$ factor in the integral, or you can put it inside the definition of $p(\theta, \phi)$ like the book does.

• In your equation, you transform from integration in cartesian coordinates to integration in sphere coordinates. Shouldn't you add sin(ϕ) there to account for this coordinate transformation? Alternatively, you might define p(θ, ϕ)=sin(ϕ)p(sin(ϕ)cos(θ), sin(ϕ)sin(θ), cos(ϕ)), but I think it will be more clear if the sin(ϕ) factor is distinct from p(θ, ϕ). – Florian R. Jun 28 '17 at 10:07
• @FlorianR. The question already describes that sin(θ) is inside p(θ, ϕ). The questioner didn't seem to have trouble with it and it's a bit of a side issue, so I didn't really pay any attention to it. – Dan Hulme Jun 28 '17 at 11:32
• @Yoo Rather the transformation from integrating over ω to integrating over θ and ϕ is the transformation from integrating in cartesian to integrating in spherical coordinates. While you already define p(ω) = sin(θ)p(θ,ϕ), I'd rather not change p at all, using ω=(sin(ϕ)cos(θ), sin(ϕ)sin(θ), cos(ϕ)) and using dω = sin(θ)dθdϕ. I think that should make it clear that p stays the same, we are just transforming the coordinate system of the integration. This is, however, subjective and if you redefine p to include sin(θ) like you did, the resulting equation will be the exact same. – Florian R. Jun 30 '17 at 4:41
• @Yoo Ah, I'm sorry. Dan just added an explanation and maybe to help you visualize it, think of a map using latitude/longitude. The south pole appears enormous, and if you were to integrate something along latitude and longitude, the poles would be overrepresented. The sin(θ) factor removes this bias towards the poles. Also, you might want to read up on integration by substitution for the general case. – Florian R. Jun 30 '17 at 12:25
• That wouldn't generally be true, but it is true here because of the extra constraint that the distribution is uniform. Any probability distribution has to integrate to 1, but this one also has the same value everywhere. (Or to put it another way, the two integrals have to be equal for every region you integrate over.) – Dan Hulme Jul 1 '17 at 18:22