<blockquote>
  <p>During the implementation, the way rays are scattered does not actually change and remains random.</p>
</blockquote>

<p>Actually the way rays are scattered does change, specifically when you sample a light. In chapter 8 he makes a mixture pdf in order to sample either the light or the bsdf.</p>

<blockquote>
  <p>What changes looks to be the contribution of each ray.</p>
</blockquote>

<p>This does change, but not specifically due to the sampling of the lights but rather due to importance sampling. But that was already introduced in chapter 4.</p>

<p>Basically you have the following:
<span class="math-container">$$I = \int_{\Omega}f(x)\,dx = \int_{\Omega}\frac{f(x)}{p(x)}p(x)\,dx = E\left[\frac{f(X)}{p(X)}\right] = \frac{1}{N}\sum_{i=1}^{N}E\left[\frac{f(X_i)}{p(X_i)}\right] \approx \frac{1}{N}\sum_{i=1}^{N}\frac{f(x_i)}{p(x_i)}$$</span>
Where <span class="math-container">$X$</span> and <span class="math-container">$X_i$</span> are distributed according to the probability density <span class="math-container">$p$</span>.
As evident the main difference to the Monte Carlo formulation you are probably used to is the <span class="math-container">$p(x)$</span> factor, and the fact that <span class="math-container">$x_i$</span> are not uniformly sampled but rather sampled according to <span class="math-container">$p$</span>. Note that for uniform sampling <span class="math-container">$p(x)=1$</span> and the equality still holds. Thus this is simply a generalization.</p>

CC BY-SA 4.0
2019-03-21T10:32:44.743Z
Thank you for your answer. Two comments: the way rays are scattered changes when he makes a mixture pdf in chapter 8, which I haven't implemented yet, but before that (when we are playing around with different pdf) this doesn't seem to change. Second, so the contribution of each ray changes both 1) for the sampling, which makes rays with lower theta more likely to be sampled, 2) and for the multiplication term which depends on the probability. Is this correct?
maurock
CC BY-SA 4.0
2019-03-21T14:36:30.120Z
@MauroComi If you're talking about chapter 7 only (which you should have mentioned explicitly) then he doesn't do brdf sampling at all, as obvious by the code and the images. You can see that he returns the contribution after only one bounce. In chapter 8 brdf and light sampling are combined. The contribution changes since you're not sampling uniformly anymore - I have derived why this is necessary in my answer above, and in this case - yes, directions closer to the normal are more likely to be sampled, the pdf weight 'compensates' for this. I don't get what mult term are you talking about.
lightxbulb
CC BY-SA 4.0
2019-03-15T22:06:21.530Z
2019-03-15T22:06:21.530Z
lightxbulb
<p>I am following the <b>"Ray Tracing - the Rest of your Life"</b> book by Peter Shirley and I am facing some troubles in the implementation of Monte Carlo importance sampling, and  Direct light sampling.<br>
Monte Carlo theory is clear to me. During the implementation, the way rays are scattered does not actually change and remains random. What changes looks to be the contribution of each ray.<br>
So my first question is:<br><br>
<i>Do I understand this correctly? Rays, after hitting a surface, are still scattered randomly, but the value of the color is adjusted based on the probability of that direction?</i></p>

<p><b>For those familiar with the book</b> <br>
When the author implements the Direct light sampling, he modifies the function <i>color()</i>, while we previously modified <i>scatter()</i> and <i>scattering_pdf()</i>. This way, <i>scatter()</i> and <i>scattering_pdf()</i> are sampling based on a specific pdf - proportional to cos(theta) - while <i>color()</i> introduces a new pdf related to the direction of the light. It is not clear to me how these two things coexist, and if I am supposed to disregard the previous changes to <i>scatter()</i> and <i>scattering_pdf()</i>.</p>

<raytracing><c++><monte-carlo>
Monte Carlo Importance Sampling
maurock
# Monte Carlo Importance Sampling

(Asked by: username_0 on 2019-03-14)

I am following the **"Ray Tracing - the Rest of your Life"** book by Peter Shirley and I am facing some troubles in the implementation of Monte Carlo importance sampling, and Direct light sampling.  
Monte Carlo theory is clear to me. During the implementation, the way rays are scattered does not actually change and remains random. What changes looks to be the contribution of each ray.  
So my first question is:  
  
*Do I understand this correctly? Rays, after hitting a surface, are still scattered randomly, but the value of the color is adjusted based on the probability of that direction?*

**For those familiar with the book**   
When the author implements the Direct light sampling, he modifies the function *color()*, while we previously modified *scatter()* and *scattering\_pdf()*. This way, *scatter()* and *scattering\_pdf()* are sampling based on a specific pdf - proportional to cos(theta) - while *color()* introduces a new pdf related to the direction of the light. It is not clear to me how these two things coexist, and if I am supposed to disregard the previous changes to *scatter()* and *scattering\_pdf()*.

**Tags:** raytracing, c++, monte-carlo


---

## 1 answers

### Answer by username_1 on 2019-03-15. Score: 1 | Accepted answer

> During the implementation, the way rays are scattered does not actually change and remains random.

Actually the way rays are scattered does change, specifically when you sample a light. In chapter 8 he makes a mixture pdf in order to sample either the light or the bsdf.

> What changes looks to be the contribution of each ray.

This does change, but not specifically due to the sampling of the lights but rather due to importance sampling. But that was already introduced in chapter 4.

Basically you have the following:
$$I = \int\_{\Omega}f(x)\,dx = \int\_{\Omega}\frac{f(x)}{p(x)}p(x)\,dx = E\left[\frac{f(X)}{p(X)}\right] = \frac{1}{N}\sum\_{i=1}^{N}E\left[\frac{f(X\_i)}{p(X\_i)}\right] \approx \frac{1}{N}\sum\_{i=1}^{N}\frac{f(x\_i)}{p(x\_i)}$$
Where $X$ and $X\_i$ are distributed according to the probability density $p$.
As evident the main difference to the Monte Carlo formulation you are probably used to is the $p(x)$ factor, and the fact that $x\_i$ are not uniformly sampled but rather sampled according to $p$. Note that for uniform sampling $p(x)=1$ and the equality still holds. Thus this is simply a generalization.

> **Comments:**
> **username_0**: Thank you for your answer. Two comments: the way rays are scattered changes when he makes a mixture pdf in chapter 8, which I haven't implemented yet, but before that (when we are playing around with different pdf) this doesn't seem to change. Second, so the contribution of each ray changes both 1) for the sampling, which makes rays with lower theta more likely to be sampled, 2) and for the multiplication term which depends on the probability. Is this correct?  
> **username_1**: @MauroComi If you're talking about chapter 7 only (which you should have mentioned explicitly) then he doesn't do brdf sampling at all, as obvious by the code and the images. You can see that he returns the contribution after only one bounce. In chapter 8 brdf and light sampling are combined. The contribution changes since you're not sampling uniformly anymore - I have derived why this is necessary in my answer above, and in this case - yes, directions closer to the normal are more likely to be sampled, the pdf weight 'compensates' for this. I don't get what mult term are you talking about.  

---

