# Wrong calculation of the balance heuristic weights in PBRT

Please consider section 16.3.4 of the PBR book, where the authors describe how the balance heuristic weights for the multiple importance sampling estimator in bidrectional path tracing can be computed.

Fix a path length $$k\in\mathbb N$$ and let $$q_i$$ denote the probability density (with respect to the area product measure) corresponding to the $$(i,k-i+1)$$-connection strategy for $$i\in\{0,\ldots,k\}$$ (excluding the strategy with no camera vertex).

Usually, $$q_i$$ arises by sequentially sampling a path of length $$k-i+1$$ starting from the camera, sequentially sampling a path of length $$i$$ starting from a light and deterministically connecting both paths.

However, as noted in section 16.3.3, PBR breaks this rule whenever one of these subpaths would consist of a single vertex. In that case, they sample the single vertex conditional on the last vertex of the other subpath.

So, $$q_1$$ (the strategy with a single light vertex) is of the form $$q_1(x)=q^{(E)}_0(x_0)q^{(E)}_1(x_0,x_1)\prod_{i=2}^{k-1}q((x_{i-2},x_{i-1}),x_i)\tilde q^{(E)}_0(x_{k-1},x_k),\tag1$$ where $$\tilde q^{(E)}$$ is precisely the density of the conditional sampling mentioned before. Analogously, $$q_k$$ (the strategy with a single camera vertex) is of the form $$q_k(x)=q^{(L)}_0(x_k)q^{(L)}_1(x_k,x_{k-1})\prod_{i=3}^kq((x_i,x_{i-1}),x_{i-2})\tilde q^{(E)}_0(x_1,x_0).\tag2$$

Fix $$s\in\{0,\ldots,k\}$$. Now, as explained in section 16.3.4, the weight $$w_s(x)$$ can be written as $$w_s(x)\left(\sum_{i=0}^{s-1}\frac{q_i}{q_s}(x)+1+\sum_{i=s+1}^k\frac{q_i}{q_s}(x)\right)^{-1}\tag3$$ and if the define $$r_i:=\frac{q_i}{q_s}$$, we may write $$r_i=\begin{cases}\displaystyle r_{i+1}\frac{q_i}{q_{i+1}}&\text{, if }is\end{cases}.$$

Consider the case $$k=2$$, $$s=0$$ and the computation of $$\frac{q_2}{q_1}$$. Following the PBR code, they compute $$\frac{q_2}{q_1}(x)=\frac{x_1.\text{pdfRev}}{x_1.\text{pdfFwd}}\tag4.$$ In a pre-computation step, the code sets $$x_1.\text{pdfRev}$$ to $$x_2.\text{PdfLight}(x_1)=q^{(L)}_1(x_2,x_1)$$, while $$x_1.\text{pdfFwd}$$ was set to $$q_1^{(E)}(x_0,x_1)$$ during the random walk. However, from $$(1)$$ and $$(2)$$ we see that we actually should have $$\frac{q_2}{q_1}(x)=\frac{q^{(L)}_0(x_2)q^{(L)}_1(x_2,x_1)\tilde q^{(E)}_0(x_1,x_0)}{q_0^{(E)}q^{(E)}_1(x_0,x_1)\tilde q^{(L)}_0(x_1,x_2)}\tag5.$$ So, it seems like they ignore (or forgotten) that they don't follow the usual connection strategy for $$q_1$$ and $$q_2$$ but use the conditional sampling for these strategies instead. The problematic thing is that, unlike most of the other cases, $$q_2$$ and $$q_1$$ do not only differ by the sampling of a single vertex. So, am I missing something or is the code incorrect?