<p>I'm not sure I've correctly understood the question, but here goes.</p>

<p>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>

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

<p>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 <em>distributions</em>. 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
$$</p>

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

<hr>

<p>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.</p>

<p>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.</p>

CC BY-SA 3.0
2017-06-28T10:07:11.750Z
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.
CC BY-SA 3.0
2017-06-28T11:32:59.357Z
@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
CC BY-SA 3.0
2017-06-29T21:58:27.563Z
@DanHulme Thanks for your answer.
Ok it's good to see p(w) as an abstract representation.
In my question, I didn't understand how to compute $p(\theta, \phi)$. According to your answer, to calculate $p(\theta, \phi)$ you state that _integrals equality_ involves _integrands equality_ so $p(\omega)d\omega$ = $p(\theta, \phi)d\theta d\phi$, am I right ?
But is it still right if in this case integration domains are not the same for the two integrals?
Qzaac
CC BY-SA 3.0
2017-06-29T22:03:19.413Z
@FlorianR do you state that the transformation from $p(\omega)$ to $p(\theta, \phi)$ is the transformation from cartesian coordinates to spherical coordinates ?
Qzaac
CC BY-SA 3.0
2017-06-30T04:41:08.773Z
@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.
CC BY-SA 3.0
2017-06-30T11:40:23.130Z
@FlorianR You are right to precise that p is the same thing no matter its starting set ($\omega$, or {\theta, \phi}). It's the probability density function, used to find the relative probability of a direction, am I right ?

I think you are misunderstanding what I understand and what I don't (my bad, my question can be confusing).
**Even though I state that $p(\omega) = \sin(\theta)p(\theta, \phi)$, i don't know why it is true.** (That's why I asked the third question in my post), and because I don't know why is true, I can't plainly understand what is the transformation.
Qzaac
CC BY-SA 3.0
2017-06-30T12:21:03.047Z
@Yoo Is my edit helpful? At first I thought I had understood what you understand and what you don't, but I'm not so sure now.
Dan Hulme
CC BY-SA 3.0
2017-06-30T12:25:31.683Z
@Yoo Ah, I'm sorry. Dan just added an explanation and maybe to help you visualize it, think of [a map using latitude/longitude](https://en.wikipedia.org/wiki/Equirectangular_projection). 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.
CC BY-SA 3.0
2017-06-30T12:29:44.127Z
@FlorianR. Oh, that's a good way of describing it. I might use that one in future.
Dan Hulme
CC BY-SA 3.0
2017-07-01T14:43:01.353Z
@DanHulme yes your edit is helpful, it is always good to restart from scratch to be sure that everything is understood.
Qzaac
CC BY-SA 3.0
2017-07-01T14:44:09.660Z
@FlorianR.Thanks, it's useful to have a "viewable" way of what the integral means.
Qzaac
CC BY-SA 3.0
2017-07-01T14:56:50.787Z
@DanHulme

However, Florian and you explained why $d(\omega) = \sin(\theta)d(\theta)d(\phi)$, am I right ?

Let me ask the question:
**Why can we state that $p(\omega)d(\omega) = p(\theta,\phi)d(\theta)d(\phi)$ ? An equality that is in the book.** This is the equality I want to prove, because it would infer that $p(\omega) = p(\theta, \phi) \sin(\theta)$, the questions I asked in my post.

**Is it because the _integrals_ (in the Dan's answer) are equals so their _integrands_ are equals ?**
I'm looking for a concrete, and rigorous proof of the equality.
Qzaac
CC BY-SA 3.0
2017-07-01T15:06:02.820Z
@DanHulme To be clear, in th book they state that:

$$p(\omega)d(\omega) = p(\theta, \phi)d(\theta)d(\phi)$$ so 
$$p(\omega)\sin(\theta)d(\theta)d(\phi) = p(\theta, \phi)d(\theta)d(\phi)$$ so
$$p(\omega)\sin(\theta) = p(\theta, \phi)$$.

And I can't rigorously demonstrate the first equality.
Qzaac
CC BY-SA 3.0
2017-07-01T18:22:54.607Z
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
CC BY-SA 3.0
2017-06-25T10:53:36.243Z
2017-06-30T12:20:17.527Z
2017-06-30T12:20:17.527Z
Dan Hulme
<p>I'm currently learning mathemetical concepts of distribution and the way to use them in a ray tracer with the book "Physically Based Rendering".</p>

<p>Let's start by uniformly sampling an hemisphere:</p>

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

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

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

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

<p>My questions are:</p>

<ul>
<li><p><strong>What does $p(\theta, \phi)$ really mean?</strong></p></li>
<li><p><strong>What is the transformation between $p(\omega)$ and $p(\theta, \phi)$?</strong></p></li>
<li><p><strong>In the book, to find $p(\theta,\phi)$ they state that $p(\theta, \phi) d\theta d\phi = p(\omega)d\omega$, but why?</strong></p></li>
</ul>

<p>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).</p>

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

CC BY-SA 3.0
2017-06-24T11:08:27.120Z
@DanHulme Do you have any answer for this post ?
Qzaac
CC BY-SA 3.0
2017-06-25T18:43:13.147Z
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 is on Codidact now
CC BY-SA 3.0
2017-06-28T10:55:16.287Z
@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
CC BY-SA 3.0
2017-06-28T21:43:24.287Z
Interesting. I didn't realise that edits did that. It makes sense though... Thanks for letting me know.
trichoplax is on Codidact now
CC BY-SA 3.0
2017-06-29T21:27:39.423Z
@trichoplax I just tried my luck
Qzaac
<raytracing><mathematics><sampling><distribution>
2D sampling with multidimensional transformations
Qzaac
# 2D sampling with multidimensional transformations

(Asked by: username_0 on 2017-06-21)

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?

**Tags:** raytracing, mathematics, sampling, distribution

> **Comments:**
> **username_0**: @username_2 Do you have any answer for this post ?  
> **username_1**: 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...  
> **username_2**: @username_1 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.  
> **username_1**: Interesting. I didn't realise that edits did that. It makes sense though... Thanks for letting me know.  
> **username_0**: @username_1 I just tried my luck  

---

## 1 answers

### Answer by username_2 on 2017-06-25. Score: 5 | Accepted answer

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 username_3. 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.

> **Comments:**
> **username_3**: 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(θ, ϕ)*.  
> **username_2**: @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.  
> **username_0**: @username_2 Thanks for your answer. Ok it's good to see p(w) as an abstract representation. In my question, I didn't understand how to compute $p(\theta, \phi)$. According to your answer, to calculate $p(\theta, \phi)$ you state that _integrals equality_ involves _integrands equality_ so $p(\omega)d\omega$ = $p(\theta, \phi)d\theta d\phi$, am I right ? But is it still right if in this case integration domains are not the same for the two integrals?  
> **username_0**: @FlorianR do you state that the transformation from $p(\omega)$ to $p(\theta, \phi)$ is the transformation from cartesian coordinates to spherical coordinates ?  
> **username_3**: @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.  
> **username_0**: @FlorianR You are right to precise that p is the same thing no matter its starting set ($\omega$, or {\theta, \phi}). It's the probability density function, used to find the relative probability of a direction, am I right ?  I think you are misunderstanding what I understand and what I don't (my bad, my question can be confusing). **Even though I state that $p(\omega) = \sin(\theta)p(\theta, \phi)$, i don't know why it is true.** (That's why I asked the third question in my post), and because I don't know why is true, I can't plainly understand what is the transformation.  
> **username_2**: @Yoo Is my edit helpful? At first I thought I had understood what you understand and what you don't, but I'm not so sure now.  
> **username_3**: @Yoo Ah, I'm sorry. Dan just added an explanation and maybe to help you visualize it, think of [a map using latitude/longitude](https://en.wikipedia.org/wiki/Equirectangular_projection). 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.  
> **username_2**: @FlorianR. Oh, that's a good way of describing it. I might use that one in future.  
> **username_0**: @username_2 yes your edit is helpful, it is always good to restart from scratch to be sure that everything is understood.  
> **username_0**: @username_3Thanks, it's useful to have a "viewable" way of what the integral means.  
> **username_0**: @username_2  However, username_3 and you explained why $d(\omega) = \sin(\theta)d(\theta)d(\phi)$, am I right ?  Let me ask the question: **Why can we state that $p(\omega)d(\omega) = p(\theta,\phi)d(\theta)d(\phi)$ ? An equality that is in the book.** This is the equality I want to prove, because it would infer that $p(\omega) = p(\theta, \phi) \sin(\theta)$, the questions I asked in my post.  **Is it because the _integrals_ (in the Dan's answer) are equals so their _integrands_ are equals ?** I'm looking for a concrete, and rigorous proof of the equality.  
> **username_0**: @username_2 To be clear, in th book they state that:  $$p(\omega)d(\omega) = p(\theta, \phi)d(\theta)d(\phi)$$ so  $$p(\omega)\sin(\theta)d(\theta)d(\phi) = p(\theta, \phi)d(\theta)d(\phi)$$ so $$p(\omega)\sin(\theta) = p(\theta, \phi)$$.  And I can't rigorously demonstrate the first equality.  
> **username_2**: 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.)  

---

