Solution (source code)

= Solution

It holds generally whenever the posterior is well defined and the expectations exist. Bayes' theorem gives
$$
\log\frac{p(\theta\mid y)}{\pi(\theta)}
=\log L(\theta)-\log Z.
$$
Taking posterior expectations yields
$$
D_{\mathrm{KL}}\{p(\theta\mid y)\Vert\pi\}
=\mathbb E_{\theta\mid y}\log L(\theta)-\log Z,
$$
which rearranges to the claimed evidence decomposition.