<p>I've noticed that I consistently get worse results in path-tracing when I try to do monte-carlo correctly compared to when I don't, and I think this might be because of a fundamental misunderstanding.</p>
<p>My assumption has been that you apply MC in path-tracing by calculating PDFs for each vertex and dividing the bounce contributions by their probability, then dividing the complete path by the number of bounces. This feels wrong, since it isn't what happens in e.g. <a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/global-illumination-path-tracing/global-illumination-path-tracing-practical-implementation" rel="nofollow noreferrer">the Scratchapixel tutorial</a>.</p>
<p>The correct method seems like multisampling the scattering function at each vertex, and integrating <em>those</em> samples using MC. So path contributions from each vertex are multiplied together normally, but each vertex computes <code>sum(samples / pdfs) / num_samples</code>.</p>
<p>In pseudocode, my current understanding is this:</p>
<pre><code>for each bounce
    color *= BRDF(origin, direction, etc) / pdf;
color / num_bounces;
</code></pre>
<p>But I think it's really more like:</p>
<pre><code>for each bounce
    for each sample
        contribution += BRDF(origin, direction, etc) / pdf;
    color *= contribution / num_samples;
</code></pre>
<p>Am I on the right track? I've been driving myself crazy with this and the algorithm being per-bounce instead of per-path feels like the only way everything would make sense.</p>

CC BY-SA 4.0
2022-07-18T17:01:50.900Z
Why would you want to divide by the number of bounces? You are sampling one vertex after the other sequentially, therefore the PDF of the light path should be the joint conditional probability along vertices $\mathbf{x}_i$ (i.e. $p(\mathbf{x}_0)\prod_{i=1}^{N-1}p(\mathbf{x}_{i}\,|\,\mathbf{x}_{i-1})$.)
Hubble
CC BY-SA 4.0
2022-07-18T18:16:11.567Z
What you should have is `color += emission * throughput` and `throughput *= brdf * cos / pdf` at each bounce. If you have multiple samples you divide by `num_samples`. What you are integrating with MC are actually the terms $T^kL_e$ where $T$ is the light transport operator and $L_e$ is the emission function.
lightxbulb
CC BY-SA 4.0
2022-07-19T03:29:48.647Z
@Hubble cargo cult math . I saw the 1/N and I was already applying the PDF to each bounce so I thought "maybe N is the number of bounces". You're right it doesn't make sense at all, I realized last night after I'd posted the question.
Kate
<pathtracing><monte-carlo><theory>
Applying Monte-Carlo methods in path tracing
Kate
# Applying Monte-Carlo methods in path tracing

(Asked by: username_0 on 2022-07-18)

I've noticed that I consistently get worse results in path-tracing when I try to do monte-carlo correctly compared to when I don't, and I think this might be because of a fundamental misunderstanding.

My assumption has been that you apply MC in path-tracing by calculating PDFs for each vertex and dividing the bounce contributions by their probability, then dividing the complete path by the number of bounces. This feels wrong, since it isn't what happens in e.g. [the Scratchapixel tutorial](https://www.scratchapixel.com/lessons/3d-basic-rendering/global-illumination-path-tracing/global-illumination-path-tracing-practical-implementation).

The correct method seems like multisampling the scattering function at each vertex, and integrating *those* samples using MC. So path contributions from each vertex are multiplied together normally, but each vertex computes `sum(samples / pdfs) / num_samples`.

In pseudocode, my current understanding is this:

```
for each bounce
    color *= BRDF(origin, direction, etc) / pdf;
color / num_bounces;

```

But I think it's really more like:

```
for each bounce
    for each sample
        contribution += BRDF(origin, direction, etc) / pdf;
    color *= contribution / num_samples;

```

Am I on the right track? I've been driving myself crazy with this and the algorithm being per-bounce instead of per-path feels like the only way everything would make sense.

**Tags:** pathtracing, monte-carlo, theory

> **Comments:**
> **username_1**: Why would you want to divide by the number of bounces? You are sampling one vertex after the other sequentially, therefore the PDF of the light path should be the joint conditional probability along vertices $\mathbf{x}_i$ (i.e. $p(\mathbf{x}_0)\prod_{i=1}^{N-1}p(\mathbf{x}_{i}\,|\,\mathbf{x}_{i-1})$.)  
> **username_2**: What you should have is `color += emission * throughput` and `throughput *= brdf * cos / pdf` at each bounce. If you have multiple samples you divide by `num_samples`. What you are integrating with MC are actually the terms $T^kL_e$ where $T$ is the light transport operator and $L_e$ is the emission function.  
> **username_0**: @username_1 cargo cult math . I saw the 1/N and I was already applying the PDF to each bounce so I thought "maybe N is the number of bounces". You're right it doesn't make sense at all, I realized last night after I'd posted the question.  

---

## 0 answers

