<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://derfrost1861.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://derfrost1861.github.io/" rel="alternate" type="text/html" /><updated>2025-02-23T18:02:01-08:00</updated><id>https://derfrost1861.github.io/feed.xml</id><title type="html">Derek Frost</title><author><name>Derek Frost</name><email>derfrost1861@gmail.com</email></author><entry><title type="html">DDPMs: Statistical Foundations</title><link href="https://derfrost1861.github.io/posts/2024/12/blog-post-DDPM-Stat-Founds/" rel="alternate" type="text/html" title="DDPMs: Statistical Foundations" /><published>2025-02-12T00:00:00-08:00</published><updated>2025-02-12T00:00:00-08:00</updated><id>https://derfrost1861.github.io/posts/2024/12/DDPM-Stat-Founds</id><content type="html" xml:base="https://derfrost1861.github.io/posts/2024/12/blog-post-DDPM-Stat-Founds/"><![CDATA[<p>Learning data distribution involves identifying ‘key factors’, which inherently ‘control’
the characteristics of our observations. The controlling relationship can either be statistical,
or based on physical laws, where we understand precisely how these factors manifest in data.
We discuss these ‘key factors’, commonly referred to as <strong>latent variables</strong>, in detail below.
First, we discuss their estimation from data using Variational Inference (VI). Then, we describe the opposite
direction - data reconstruction and generation from latent variables.
<!-- Learning data distribution involves finding 'key factors', which inherently 'control' characteristics of our observations.
The 'control' can be surrogate (perhaps, we just know the map from factors to observations from statistics) or based on a physical law
(we know exactly how factors manifest themselves in data).
We discuss the 'key factors', commonly referred to as latent variables, below: first, how to estimate them, and then,
how to use them in data distribution learning, including new sample generation. --></p>

<h1 id="latent-space">Latent Space</h1>

<p>Diffusion models are latent variable models (<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>; page 2).
It is not only true when diffusion happens in the latent space of a Variational Autoencoder
(for instance, in the Latent Diffusion case
(<a href="https://arxiv.org/abs/2112.10752" target="_blank">Rombach et al., 2022</a>))
but in general for Denoising Diffusion Probabilistic models.
Let’s denote our training (or observed) data as \(\mathbf{x}_0\)
(in Figure 1 in the <a href="/posts/2024/12/blog-post-DDPM-Intro/">Introduction chapter</a>
those would be noise-free cute ca… sorry… schematic Swiss-roll images at \(t=0\))
and its noised versions as \(\mathbf{z}_t\) with \(t=1:T\). In particular, \(\mathbf{z}_t\) corresponds
to inputs \(\mathbf{x}_0\) with added noise, which comes from \(t\) Brownian jumps
(see the full notation list in <a href="/posts/2024/12/blog-post-DDPM-AppendixA/">Appendix A</a>).
These noised versions make up the latent space of a diffusion model
(<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>; page 2).
Some authors instead of \(\mathbf{z}_t\) use \(\mathbf{x}_t\) to highlight a connection with input,
but here we highlight \(\mathbf{z}_t\) being a latent space variable. We further call a sequence of latent spaces,
or noised images, \(\mathbf{z}=(\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T)\) an hierarchy.
But for now, without the loss of generality, let’s abstract from a particular form of the latent space and “simply” denote it
\(\mathbf{z}\).</p>

<p>What is a latent space? I, personally, really like the explanation provided in this
video - <a href="https://www.youtube.com/watch?v=3G5hWM6jqPk&gt;">MIT 6.S191: Deep Generative Modeling</a> (7:53 time stamp).
The analogy for latent space variables would be true objects (or true hidden factors), which cast shadows on the wall (or describe a physical process, which we register with some sort of sensors; Figure 1). In this case, the shadows would correspond to our observed data. Typically, latent variable would have less dimensions than the data we would like to describe. If we can estimate latent variables, we then can model complex distributions of observed data samples: if you know the object, you can predict its shadow.</p>

<blockquote>
<p><br /><b>
Latent Variables
</b>
are unobserved lower dimensional representations of higher dimensional inputs. They can be thought of as
underlying factors responsible for the physical phenomenon we observe. For instance, the intensity of an earthquake
at a particular location depends on such factors as:
1) the distance from the hypocenter
2) the ground you are at and
3) how large was the original displacement (not a full list). In machine learning, however, physical meaning
of latents is not rigid but rather speculative and is subject to interpretation.
<br /><br /></p>
</blockquote>

<p><img src="/images/posts/stableDiffusionBite/simpleObjectsAndTheirShadows.png" alt="" />
<em>Figure 1: If you know true object shapes (latent variables) you can predict its shadows (observed data).</em></p>

<h1 id="variational-inference">Variational Inference</h1>

<p>To have an effective image generator, we need to find “true hidden” factors describing a variety of
Swiss-roll schematics \(\mathbf{x}_0\) we have in our possession. We have a state of our hidden factors \(\mathbf{z}\)
before and after seeing the data. Before, latent space \(\mathbf{z}\) naturally does not incorporate any
knowledge about \(\mathbf{x}_0\). After seeing the data and estimating the latents,
we hope \(\mathbf{z}\) becomes a lower-dimensional compressed representation
of data. Besides searching for hidden factors, we can also search for the model, i.e.,
equation/precise law of physics/approximate relationship based on correlation/regression, which connects them to observations.</p>

<p>Formally, to obtain the latents and the model, the problem is posed in a Bayesian inference framework
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 2):</p>

\[\begin{align}
  p(\mathbf{z},\mathbf{x}_0) = p(\mathbf{z})p(\mathbf{x}|\mathbf{z})
\end{align}\]

<p>So what do we actually see here? \(p\) stands for probability and \(p(\mathbf{z},\mathbf{x}_0)\)
is <strong>joint probability density</strong> between latents and observed data. Physically, the latter describes
how likely a certain combination of latents and observables can occur.
A Bayesian model draws samples from the prior \(p(\mathbf{z})\)
and then relates them to the observations through the <strong>likelihood</strong> \(p(\mathbf{x}_{0} | \mathbf{z})\)
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 2):
the probability density of \(\mathbf{x}_0\) given a particular set of \(\mathbf{z}\).
Imagine you have an operator, which matches latents to observations: the better match you achieve, which can come from both
more accurate latents and an operator, the higher the likelihood will be.
The prior in its turn, corresponds to our knowledge about latents before the inference, e.g.,
in the case of diffusion the hierarchy of latents \(\mathbf{z}=(\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T)\) is described by Gaussian transitions.
Inference in a Bayesian framework is conducted through computing the posterior
(equation 2 in <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 5)</p>

\[\begin{equation}
p(\mathbf{z} | \mathbf{x}_0)\ =\ \frac{p(\mathbf{z}, \mathbf{x}_{0})}{p(\mathbf{x}_0)}
\end{equation}\]

<p>which quantifies what we know about $\mathbf{z}$ after seeing the data
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>),
and can obviously be derived from
the definition of the conditional probability. The better our estimated latents explain the observations, the higher the posterior probability
(of latents for our set of observations) is. Finally, \(p(\mathbf{x}_0)\) is the <strong>marginal density of the observations</strong> or <strong>evidence</strong>,
which is computed by ‘marginalizing out’ the latent variable from the joint density \(p(\mathbf{x}_0) = \int p(\mathbf{z},\mathbf{x}_0) d\mathbf{z}\).
In the Generative Model section, we discuss how $p(\mathbf{x}_0)$ is an estimated data distribution.</p>

<!--<details class="expandable-box">
    <summary>Bayesian Inference components for the earthquake example</summary>
    
<p>For our earthquake example, the <strong>joint probability</strong> is a probability of a combination of intensity vs latents (distance, type of ground, displacement).
For instance, for very far away earthquakes is high intensity likely? Not really. But low intensity at our location would be likely.
In other words, imagine considering a collection of latent values (not only distance as above) vs intensities.
Now, if we model the intensity of an earthquake with a particular set of parameters, e.g., with a 100 feet displacement along the fault, which
is 100 miles away and 50 miles deep, and now we are on sand, the difference with an observed intensity would correspond to the <strong>likelihood</strong>.
The reverse calculation of latents after we observed the intensities would be the <strong>posterior</strong>.
For instance, finding the earthquake hypocenter given observations on seismometers.
<strong>Prior</strong> knowledge could be, for instance,
limiting the distances to a certain location: perhaps we know there is a particularly active tectonic zone. The <strong>evidence</strong> would be
probability of all intensities if we consider the whole possible range of latents. Do not confuse with the joint, which measures how likely the ‘combinations’ are.
Here, we consider a probability of the intensity given all possible latents.</p>

</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Bayesian Inference components for the earthquake example</span>
    </summary>
    <div class="expandable-content">
      
<p>For our earthquake example, the <strong>joint probability</strong> is a probability of a combination of intensity vs latents (distance, type of ground, displacement).
For instance, for very far away earthquakes is high intensity likely? Not really. But low intensity at our location would be likely.
In other words, imagine considering a collection of latent values (not only distance as above) vs intensities.
Now, if we model the intensity of an earthquake with a particular set of parameters, e.g., with a 100 feet displacement along the fault, which
is 100 miles away and 50 miles deep, and now we are on sand, the difference with an observed intensity would correspond to the <strong>likelihood</strong>.
The reverse calculation of latents after we observed the intensities would be the <strong>posterior</strong>.
For instance, finding the earthquake hypocenter given observations on seismometers.
<strong>Prior</strong> knowledge could be, for instance,
limiting the distances to a certain location: perhaps we know there is a particularly active tectonic zone. The <strong>evidence</strong> would be
probability of all intensities if we consider the whole possible range of latents. Do not confuse with the joint, which measures how likely the ‘combinations’ are.
Here, we consider a probability of the intensity given all possible latents.</p>

    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<p>The above recipe (of computing posterior from the Bayes’ rule) is very simple conceptually (<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>),
but is not feasible in practice. For many models, “evidence” \(p(\mathbf{x}_0)\) is unavailable in closed form or requires exponential time to compute (<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>).
Consider a simple 1-hidden layer neural network with nonlinear activation \(p(\mathbf{x}_0|\mathbf{z})\)
with a latent distribution \(p(\mathbf{z})\). Even for a simple Gaussian prior \(p(\mathbf{z})\), which can be integrated analytically on its own, the evidence integral</p>

\[\begin{align}
p(\mathbf{x}_0) = \int p(\mathbf{x}_0|\mathbf{z}) p(\mathbf{z}) d\mathbf{z}
\end{align}\]

<p>is intractable
(<a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: page 2, ‘Intractability’ paragraph).
It does not have an analytical expression (the integrand now is a product of a Gaussian with a non-linear activation),
and would require prohibitively expensive number of evaluations to span the space of latents \(\mathbf{z}\).</p>

<p>This is true for our case of diffusion as well: it can’t be simply computed through marginalization (<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>)
\(\int p(\mathbf{x}_0, \mathbf{z}) d\mathbf{z}=\int p(\mathbf{x}_0,\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T) d\mathbf{z}_1 d\mathbf{z}_2 ...d\mathbf{z}_T\)
(<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>: page 2, paragraph 4).
There is no simple expression of the integrand, while computing it numerically would require evaluation over all possible \(\mathbf{z}\) values
(<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023</a>: page 4, left column, paragraph 5), which are not scalars,
but vectors of sizes \(n = {\mathbf{x}_0}_{height} \cdot {\mathbf{x}_0}_{width}\) (where \(\mathbf{x}_0\) is input image). Sizes would be smaller for <em>Stable Diffusion</em>,
but still you would need to consider every possible \(\mathbf{z}=(\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T)\) vector.</p>

<blockquote>
<p><br /><b>
We cannot directly get latents from data:
</b>
due to evidence integral intractability, estimation of latent variable given observations using Bayes' rule is
computationally infeasible in practice. Hence, we resort to approximate methods: Markov chain Monte Carlo (MCMC) and Variational Inference.
MCMC produces probable samples of latent variables, a set of which characterizes their posterior distribution given the data. Variational Inference, on the other hand, approximates the relationship itself, mapping data to latent variables. This approximation is then used to generate samples of latent variables. The latter is a better choice for DDPMs, given their large model size and typically large datasets.
<br /><br /></p>
</blockquote>

<p>There are two most popular methods for posterior estimation in practice: MCMC (Markov chain Monte Carlo) and Variational Inference
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>). MCMC
<em>simulates samples</em> from the target density and can be exact, but, at the same time, is more computationally demanding.
Variational inference, in its turn, is faster in most cases and <em>approximates</em> the target posterior distribution itself
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>;
<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>).
The relative accuracy of variational inference and MCMC is still unknown.
According to <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., (2018)</a>,
MCMC is more applicable for a small but expensive (in terms of collection) dataset,
for which the inference model is relatively well known.
Variational inference would be used, for instance, to fit a probabilistic model of text to one billion text documents.
We could explore different text models and benefit from higher computational efficiency highly useful for such a large amount of data.
Obviously, diffusion modeling belongs to the second case
(in fact, <a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, (2022)</a> refers to DDPMs as variational diffusion models).</p>

<p>If you are already familiar with the topic, please feel free to skip the expandable boxes below.
Otherwise, I believe it’s instructive to start with an example not directly related to MCMC and VI.
The example shows how latents can be estimated given observations
if a forward modeling operator is known, which is the case in classical Inverse Theory. Then, I proceed
with MCMC estimation of latents, which allows for their probabilistic characterization.
Then I discuss linear regression using MCMC, and finally arrive at Variational Inference -
all using the same linear operator example.</p>

<!--<details class="expandable-box">
    <summary>Linear operator example: Inversion for estimation of latents</summary>
    
<p>Imagine, there is a linear relationship between latents \(z\) (one-dimensional, for simplicity) and data \(x_0\) (one-dimensional as well), and
we have a collection of observations \(\{x_0^i\}_{i=1}^N\), where $N$ denotes a number of observations and \(i\) is
a particular sample instance. As shown below, one latent gives rise to one data point, hence, \(i\) denotes
a sample in a latent space as well. Similarly, a set of latents can be denoted as \(\{z^i\}_{i=1}^N\). Finally,
for brevity, I sometimes use \(z\) and \(x_0\) to denote \(z^i\) and \(x_0^i\) correspondingly.
Those notations are local to the linear operator example. For Diffusion Model notations see Appendix A.
Now, we can express the linear relationship as:</p>

\[\begin{align}
  x_0 = k \cdot z + c
\end{align}\]

<p>where \(k\) and \(c\) are a coefficient and a bias respectively.</p>

<p>How would you find latents that are responsible for our data? We can search for values \(z\),
which minimize the least-squares difference between predicted and observed values:</p>

\[\begin{align}
\text{MSE} = \sum_i (x_0^i - (k \cdot z^i + c))^2
\end{align}\]

<p>The sum of squares above is often referred to as \(l_2\) misfit or mean squared error (MSE). This function is quadratic
and its derivative should be zero at its minimum. The derivative needs to be taken with respect to each value \(z^i\) -
we are searching for a set of latents minimizing the sum altogether. Let’s consider the MSE derivative with respect to
a variable \(z^{i=j}\), which is responsible for a data sample \(i=j\):</p>

\[\begin{align}
\frac{\partial \text{MSE}}{\partial z^j} = -2k(x_0^j - (k \cdot z^j + c))
\end{align}\]

<p>For the derivative to be zero, we obviously need:</p>

\[\begin{align}
z^j = \frac{1}{k}(x_0^j - c)
\end{align}\]

<p>and this condition should be true for all \(z^i\)`s to achieve a minimum. In other words, we are searching for latent values, which
describe our observation as accurately as possible. It so happens in our example that each observation has its own independent latent.
A vector of values \(z^i\), which minimize the MSE, is also referred to as a least-squares solution.</p>

<p>To be honest, as goldfish Emma suggests, we all could have guessed the obvious solution</p>

\[\begin{align}
z = \frac{1}{k}(x_0 - c)
\end{align}\]

<p>right away. But the main point here is to familiarize ourselves with the Inverse Theory framework.</p>

<p>As you can see, Inverse Theory framework is pretty straightforward and provides a single best (in terms of MSE) solution.
However, for the analysis of the posterior probabilities, a single solution might not be enough. In fact,
since we would like to learn the data probability, our goal is to probabilistically characterize potential latent variable
values responsible for the observations.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Linear operator example: Inversion for estimation of latents</span>
    </summary>
    <div class="expandable-content">
      
<p>Imagine, there is a linear relationship between latents \(z\) (one-dimensional, for simplicity) and data \(x_0\) (one-dimensional as well), and
we have a collection of observations \(\{x_0^i\}_{i=1}^N\), where $N$ denotes a number of observations and \(i\) is
a particular sample instance. As shown below, one latent gives rise to one data point, hence, \(i\) denotes
a sample in a latent space as well. Similarly, a set of latents can be denoted as \(\{z^i\}_{i=1}^N\). Finally,
for brevity, I sometimes use \(z\) and \(x_0\) to denote \(z^i\) and \(x_0^i\) correspondingly.
Those notations are local to the linear operator example. For Diffusion Model notations see Appendix A.
Now, we can express the linear relationship as:</p>

\[\begin{align}
  x_0 = k \cdot z + c
\end{align}\]

<p>where \(k\) and \(c\) are a coefficient and a bias respectively.</p>

<p>How would you find latents that are responsible for our data? We can search for values \(z\),
which minimize the least-squares difference between predicted and observed values:</p>

\[\begin{align}
\text{MSE} = \sum_i (x_0^i - (k \cdot z^i + c))^2
\end{align}\]

<p>The sum of squares above is often referred to as \(l_2\) misfit or mean squared error (MSE). This function is quadratic
and its derivative should be zero at its minimum. The derivative needs to be taken with respect to each value \(z^i\) -
we are searching for a set of latents minimizing the sum altogether. Let’s consider the MSE derivative with respect to
a variable \(z^{i=j}\), which is responsible for a data sample \(i=j\):</p>

\[\begin{align}
\frac{\partial \text{MSE}}{\partial z^j} = -2k(x_0^j - (k \cdot z^j + c))
\end{align}\]

<p>For the derivative to be zero, we obviously need:</p>

\[\begin{align}
z^j = \frac{1}{k}(x_0^j - c)
\end{align}\]

<p>and this condition should be true for all \(z^i\)`s to achieve a minimum. In other words, we are searching for latent values, which
describe our observation as accurately as possible. It so happens in our example that each observation has its own independent latent.
A vector of values \(z^i\), which minimize the MSE, is also referred to as a least-squares solution.</p>

<p>To be honest, as goldfish Emma suggests, we all could have guessed the obvious solution</p>

\[\begin{align}
z = \frac{1}{k}(x_0 - c)
\end{align}\]

<p>right away. But the main point here is to familiarize ourselves with the Inverse Theory framework.</p>

<p>As you can see, Inverse Theory framework is pretty straightforward and provides a single best (in terms of MSE) solution.
However, for the analysis of the posterior probabilities, a single solution might not be enough. In fact,
since we would like to learn the data probability, our goal is to probabilistically characterize potential latent variable
values responsible for the observations.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<!--<details class="expandable-box">
    <summary>Linear operator example: MCMC for estimation of latents</summary>
    
<p>Let’s assume we have the same linear operator with known parameters \(k,c\).
In other words, we have high confidence in our inference model, which is consistent with the MCMC use case above.
As opposed to a single deterministic least-squares solution, we now would like to characterize latent variables
probabilistically. Let’s start with the Bayes’ rule for the posterior
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>):</p>

\[\begin{align}
  p(z|x_0) = \frac{p(z)\ p(x_0|z)}{p(x_0)}
\end{align}\]

<p>In MCMC, we typically ignore the density of observations \(p(x_0)\) since it does not depend on \(z\)
(<a href="https://www.oden.utexas.edu/media/reports/2012/1218.pdf" target="_blank">Bui-Thanh, 2012</a>: page 13),
and describe the posterior as a quantity proportional
to a product of a likelihood \(p(x_0|z)\) and a prior \(p(z)\).</p>

<p>The likelihood can be defined to be proportional to a Gaussian (<a href="https://www.oden.utexas.edu/media/reports/2012/1218.pdf" target="_blank">Bui-Thanh, 2012</a>: page 20)
with a mean \(\mu(z) = k \cdot z + c\) and a variance \(\sigma^2\), which reflects noise levels in the data
(e.g., physical sensor imperfections: for instance, 5% deviation between the same data point measurements).
For a given observation \(x_0^i\) modeled from a latent \(z^i\), we have:</p>

\[\begin{align}
  p(x_0|z) \propto e^{-\frac{(x_0 - \mu(z))^2}{2\sigma^2}} = e^{-\frac{(x_0 - (k \cdot z + c))^2}{2\sigma^2}}
\end{align}\]

<p>The prior or <em>a priori</em> knowledge describes what we know about the latents before observing the data. Let’s assume
we know \(z\) comes from a unit-variance zero-mean Gaussian distribution:</p>

\[\begin{align}
\frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}}
\end{align}\]

<p>According to Bayes’ rule (now, with no denominator \(p(x_0)\)), the posterior is proportional to:</p>

\[\begin{align}
  p(z|x_0) \propto \ &amp;p(z)\ p(x_0|z) = \\
  &amp;\frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}}\ e^{\big( -\frac{1}{2\sigma^2}\big( x_0 - (k \cdot z + c) \big)^2 \big)}
\end{align}\]

<p>The better the latents describe the data, the smaller the misfit is.
The closer the latents resemble the Gaussian, the higher the prior term is.
Obviously, the highest posterior is thus achieved for an optimal combination of both: minimum misfit (maximum likelihood)
and the highest prior. OK… So, how do we search for the latents describing our data?</p>

<p>MCMC methods subsequently apply a stochastic transition operator to the random draw of \(z\)
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>).
To put simply, you randomly walk in the latent space but only step towards values, which result in a higher likelihood and prior.
After sampling for a ‘long enough’ time (you can, in fact, visit the same ‘likely’ latents multiple times), your random draws of latents (aka your random walk steps)
converge to the true latent posterior. How long should you be sampling, latent acceptance or rejection criterion, and the random draw probability distribution
are all essential MCMC parameters.</p>

<p>Where do you think our ‘best’ latent draws would lie? Along the line we derived in the example above
(assuming no noise and excluding zero slope (\(k \neq 0\))):</p>

\[\begin{align}
  z = \frac{1}{k} (x_0 - c)
\end{align}\]

<p>Let me emphasize: <em>we never explicitly search for the inverse relationship above</em> (we do in Variational Inference). We sample \(z\)’s along this line.
In other words, if our MCMC random walk has been properly set up, we would be sampling the latents, which lie along this line (of slope \(\frac{1}{k}\) and bias \(-\frac{c}{k}\)). While walking, we gather a set of probable latents - a set of samples, which serves as
our approximation to the posterior probability distribution. For each single observation sample \(x_0^i\),
we have several \(z^{i,l}\)’s, which follow a Gaussian distribution. \(l\), here, is another index denoting latent
value \(z^i\) variation per data example \(x_0^i\).
This is where the stochasticity kicks in: instead of a single least-squares solution, we now have a collection
of latent samples, which are probable for a given data observation point.
There will be a sample of maximum posterior probability (often referred to as MAP - Maximum A Posteriori point (<a href="https://www.oden.utexas.edu/media/reports/2012/1218.pdf" target="_blank">Bui-Thanh, 2012</a>)) and a bunch of others, by looking at which we can get a feel for how a posterior looks like.</p>

<p>Obviously, we would like to find the ‘combined’ posterior of latents given all the observations.
Under the <em>mean-field assumption</em> (<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: equation 15)
we can factorize the posterior over the individual ones:</p>

\[\begin{align}
&amp;p(z^{i=1},z^{i=2},...,z^{i=N}|x_0^{i=1},x_0^{i=2},...,x_0^{i=N}) =\\
&amp;\prod_i p(z^i|x_0^i) =\\
&amp;\prod_i p(z^i)\ p(x_0^i|z^i) \propto \\
&amp;\prod_i e^{-\frac{(z^i)^2}{2}}\ e^{ -\frac{1}{2\sigma^2}\big( x_0^i - (k \cdot z^i + c) \big)^2 } =\\
&amp;e^{-\frac{\sum_i(z^i)^2}{2}}\ e^{ -\frac{\sum_i\big( x_0^i - (k \cdot z^i + c) \big)^2}{2\sigma^2} }
\end{align}\]

<p>The same assumption can be used in VAE’s encoders
(<a href="https://arxiv.org/pdf/1711.05597" target="_blank">Zhang et al., 2018</a>: page 14, left column, paragraph 7)
when ‘no covariance’ (in other words, diagonal covariance matrix is used) between latents is involved
(as we see further, for diffusion models the process of encoding has ‘no covariance’ as well).
We also assume individual observed data points are picked independently from each other
(e.g., see <a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: page 3, paragraph 3).</p>

<p>In plain language the assumptions mean: each data sample, randomly subselected (i.e., without any particular grouping, which could cause biases)
from a dataset, gets independently mapped to a latent variable. While each mapping is independent,
a single latent space (of all possible latents) gets organized to represent the factors guiding the data generation. Independence of maps -
latent posteriors given a data sample - allows for ‘combined’ latent posterior factorization through multiplication.
The latter is a well-known property of independent random variables.
Obviously, we satisfy both of the assumptions perfectly with our simple linear example: \(x_0^i\)’s are independent
and so are \(z^i\)’s, which are obtained from each observation by subtraction of a scalar \(c\) and multiplication by a factor of \(1/k\).</p>

<p>What does it mean for us in practice?
If we take the log of the expression above, we end up with the same MSE objective as in the Inverse Theory example. Keep in mind, however,
now we are not constrained by a single least-squares solution anymore.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Linear operator example: MCMC for estimation of latents</span>
    </summary>
    <div class="expandable-content">
      
<p>Let’s assume we have the same linear operator with known parameters \(k,c\).
In other words, we have high confidence in our inference model, which is consistent with the MCMC use case above.
As opposed to a single deterministic least-squares solution, we now would like to characterize latent variables
probabilistically. Let’s start with the Bayes’ rule for the posterior
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>):</p>

\[\begin{align}
  p(z|x_0) = \frac{p(z)\ p(x_0|z)}{p(x_0)}
\end{align}\]

<p>In MCMC, we typically ignore the density of observations \(p(x_0)\) since it does not depend on \(z\)
(<a href="https://www.oden.utexas.edu/media/reports/2012/1218.pdf" target="_blank">Bui-Thanh, 2012</a>: page 13),
and describe the posterior as a quantity proportional
to a product of a likelihood \(p(x_0|z)\) and a prior \(p(z)\).</p>

<p>The likelihood can be defined to be proportional to a Gaussian (<a href="https://www.oden.utexas.edu/media/reports/2012/1218.pdf" target="_blank">Bui-Thanh, 2012</a>: page 20)
with a mean \(\mu(z) = k \cdot z + c\) and a variance \(\sigma^2\), which reflects noise levels in the data
(e.g., physical sensor imperfections: for instance, 5% deviation between the same data point measurements).
For a given observation \(x_0^i\) modeled from a latent \(z^i\), we have:</p>

\[\begin{align}
  p(x_0|z) \propto e^{-\frac{(x_0 - \mu(z))^2}{2\sigma^2}} = e^{-\frac{(x_0 - (k \cdot z + c))^2}{2\sigma^2}}
\end{align}\]

<p>The prior or <em>a priori</em> knowledge describes what we know about the latents before observing the data. Let’s assume
we know \(z\) comes from a unit-variance zero-mean Gaussian distribution:</p>

\[\begin{align}
\frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}}
\end{align}\]

<p>According to Bayes’ rule (now, with no denominator \(p(x_0)\)), the posterior is proportional to:</p>

\[\begin{align}
  p(z|x_0) \propto \ &amp;p(z)\ p(x_0|z) = \\
  &amp;\frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}}\ e^{\big( -\frac{1}{2\sigma^2}\big( x_0 - (k \cdot z + c) \big)^2 \big)}
\end{align}\]

<p>The better the latents describe the data, the smaller the misfit is.
The closer the latents resemble the Gaussian, the higher the prior term is.
Obviously, the highest posterior is thus achieved for an optimal combination of both: minimum misfit (maximum likelihood)
and the highest prior. OK… So, how do we search for the latents describing our data?</p>

<p>MCMC methods subsequently apply a stochastic transition operator to the random draw of \(z\)
(<a href="https://proceedings.mlr.press/v37/salimans15.pdf" target="_blank">Salimans et al., 2015</a>).
To put simply, you randomly walk in the latent space but only step towards values, which result in a higher likelihood and prior.
After sampling for a ‘long enough’ time (you can, in fact, visit the same ‘likely’ latents multiple times), your random draws of latents (aka your random walk steps)
converge to the true latent posterior. How long should you be sampling, latent acceptance or rejection criterion, and the random draw probability distribution
are all essential MCMC parameters.</p>

<p>Where do you think our ‘best’ latent draws would lie? Along the line we derived in the example above
(assuming no noise and excluding zero slope (\(k \neq 0\))):</p>

\[\begin{align}
  z = \frac{1}{k} (x_0 - c)
\end{align}\]

<p>Let me emphasize: <em>we never explicitly search for the inverse relationship above</em> (we do in Variational Inference). We sample \(z\)’s along this line.
In other words, if our MCMC random walk has been properly set up, we would be sampling the latents, which lie along this line (of slope \(\frac{1}{k}\) and bias \(-\frac{c}{k}\)). While walking, we gather a set of probable latents - a set of samples, which serves as
our approximation to the posterior probability distribution. For each single observation sample \(x_0^i\),
we have several \(z^{i,l}\)’s, which follow a Gaussian distribution. \(l\), here, is another index denoting latent
value \(z^i\) variation per data example \(x_0^i\).
This is where the stochasticity kicks in: instead of a single least-squares solution, we now have a collection
of latent samples, which are probable for a given data observation point.
There will be a sample of maximum posterior probability (often referred to as MAP - Maximum A Posteriori point (<a href="https://www.oden.utexas.edu/media/reports/2012/1218.pdf" target="_blank">Bui-Thanh, 2012</a>)) and a bunch of others, by looking at which we can get a feel for how a posterior looks like.</p>

<p>Obviously, we would like to find the ‘combined’ posterior of latents given all the observations.
Under the <em>mean-field assumption</em> (<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: equation 15)
we can factorize the posterior over the individual ones:</p>

\[\begin{align}
&amp;p(z^{i=1},z^{i=2},...,z^{i=N}|x_0^{i=1},x_0^{i=2},...,x_0^{i=N}) =\\
&amp;\prod_i p(z^i|x_0^i) =\\
&amp;\prod_i p(z^i)\ p(x_0^i|z^i) \propto \\
&amp;\prod_i e^{-\frac{(z^i)^2}{2}}\ e^{ -\frac{1}{2\sigma^2}\big( x_0^i - (k \cdot z^i + c) \big)^2 } =\\
&amp;e^{-\frac{\sum_i(z^i)^2}{2}}\ e^{ -\frac{\sum_i\big( x_0^i - (k \cdot z^i + c) \big)^2}{2\sigma^2} }
\end{align}\]

<p>The same assumption can be used in VAE’s encoders
(<a href="https://arxiv.org/pdf/1711.05597" target="_blank">Zhang et al., 2018</a>: page 14, left column, paragraph 7)
when ‘no covariance’ (in other words, diagonal covariance matrix is used) between latents is involved
(as we see further, for diffusion models the process of encoding has ‘no covariance’ as well).
We also assume individual observed data points are picked independently from each other
(e.g., see <a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: page 3, paragraph 3).</p>

<p>In plain language the assumptions mean: each data sample, randomly subselected (i.e., without any particular grouping, which could cause biases)
from a dataset, gets independently mapped to a latent variable. While each mapping is independent,
a single latent space (of all possible latents) gets organized to represent the factors guiding the data generation. Independence of maps -
latent posteriors given a data sample - allows for ‘combined’ latent posterior factorization through multiplication.
The latter is a well-known property of independent random variables.
Obviously, we satisfy both of the assumptions perfectly with our simple linear example: \(x_0^i\)’s are independent
and so are \(z^i\)’s, which are obtained from each observation by subtraction of a scalar \(c\) and multiplication by a factor of \(1/k\).</p>

<p>What does it mean for us in practice?
If we take the log of the expression above, we end up with the same MSE objective as in the Inverse Theory example. Keep in mind, however,
now we are not constrained by a single least-squares solution anymore.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<!--<details class="expandable-box">
    <summary>Linear operator example: MCMC for linear regression</summary>
    
<p>We could also search for \(k\) and \(c\) and solve a different problem - linear regression
(<a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., 2016</a>: chapter 5, page 135;
<a href="https://ieeexplore.ieee.org/document/10530647" target="_blank">Chandra and Simmons, 2024</a>).
Please keep in mind that in this case we are approximating the posterior of \(k\) and \(c\)
given the data observations \(x_0\), i.e., \(p(\theta \vert x_0)\).
\(\theta\) in this case corresponds to \((k,c)\) with a linear regression operator \(r(\theta=(k,c), z) = k\cdot z + c\).
Linear regression, in its turn, is used to compute the mean of a Gaussian (Gaussian,
which describes the likelihood of latents given the data):</p>

\[\begin{align}
\mu(z)=\mu_\theta(z)=r(\theta=(k,c), z) = k\cdot z + c
\end{align}\]

<p>In other words: first we maximize the posterior of \(\theta\) given the data;
then we use \(\theta\) to compute latent’s likelihood given the data.
The likelihood of latents given the data samples with the coefficients from the linear regression thus becomes:</p>

\[\begin{align}
p(x^i_0|z^i) &amp; \propto &amp; &amp;\frac{1}{\sqrt{2\pi}}e^{-\frac{\sum_i(z^i)^2}{2}}\ e^{ -\frac{\sum_i\big( x_0^i - \mu_\theta(z^i) \big)^2}{2\sigma^2} }\\
&amp; = &amp; &amp;\frac{1}{\sqrt{2\pi}}e^{-\frac{\sum_i(z^i)^2}{2}}\ e^{ -\frac{\sum_i\big( x_0^i - (k \cdot z^i + c) \big)^2}{2\sigma^2} }
\end{align}\]

<p>However, MCMC has been facing challenges in being adapted to larger models and big data problems
(which is the case for the diffusion; <a href="https://ieeexplore.ieee.org/document/10530647" target="_blank">Chandra and Simmons, (2024)</a>), which naturally brings us to variational inference framework.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Linear operator example: MCMC for linear regression</span>
    </summary>
    <div class="expandable-content">
      
<p>We could also search for \(k\) and \(c\) and solve a different problem - linear regression
(<a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., 2016</a>: chapter 5, page 135;
<a href="https://ieeexplore.ieee.org/document/10530647" target="_blank">Chandra and Simmons, 2024</a>).
Please keep in mind that in this case we are approximating the posterior of \(k\) and \(c\)
given the data observations \(x_0\), i.e., \(p(\theta \vert x_0)\).
\(\theta\) in this case corresponds to \((k,c)\) with a linear regression operator \(r(\theta=(k,c), z) = k\cdot z + c\).
Linear regression, in its turn, is used to compute the mean of a Gaussian (Gaussian,
which describes the likelihood of latents given the data):</p>

\[\begin{align}
\mu(z)=\mu_\theta(z)=r(\theta=(k,c), z) = k\cdot z + c
\end{align}\]

<p>In other words: first we maximize the posterior of \(\theta\) given the data;
then we use \(\theta\) to compute latent’s likelihood given the data.
The likelihood of latents given the data samples with the coefficients from the linear regression thus becomes:</p>

\[\begin{align}
p(x^i_0|z^i) &amp; \propto &amp; &amp;\frac{1}{\sqrt{2\pi}}e^{-\frac{\sum_i(z^i)^2}{2}}\ e^{ -\frac{\sum_i\big( x_0^i - \mu_\theta(z^i) \big)^2}{2\sigma^2} }\\
&amp; = &amp; &amp;\frac{1}{\sqrt{2\pi}}e^{-\frac{\sum_i(z^i)^2}{2}}\ e^{ -\frac{\sum_i\big( x_0^i - (k \cdot z^i + c) \big)^2}{2\sigma^2} }
\end{align}\]

<p>However, MCMC has been facing challenges in being adapted to larger models and big data problems
(which is the case for the diffusion; <a href="https://ieeexplore.ieee.org/document/10530647" target="_blank">Chandra and Simmons, (2024)</a>), which naturally brings us to variational inference framework.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<p>In variational inference, we search for such a function of latent variables \(g^*(\mathbf{z})\),
which approximates the intractable posterior \(p(\mathbf{z}|\mathbf{x}_0)\)
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: equation 1 and 10):</p>

\[\begin{equation}
g^*(\mathbf{z})=\underset{q(\mathbf{z}) \in \mathcal{L}}{\text{argmin}}\bigg(D_{\text{KL}}(g(\mathbf{z})||p(\mathbf{z}|\mathbf{x}_0))\bigg)
\end{equation}\]

<p>where \(g^*(\mathbf{z})\) is the best approximation of the conditional, within the family \(\mathcal{L}\).
A family, here, means a specific form of functions: we seek to optimize functions of the form$^*$
\(q_{\phi}(\mathbf{z}|\mathbf{x}_0)\) with trainable parameters \(\phi\)
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>: page 3, paragraph 1).
We train a neural network \(q_\phi(\mathbf{z}|\mathbf{x}_0)\)
and learn weights \(\phi\) to approximate the posterior distribution \(p(\mathbf{z}|\mathbf{x}_0)\) of \(\mathbf{z}\)
given observed data \(\mathbf{x}_0\). The specific form of functions corresponds to a neural net.
I further refer to \(q_\phi(\mathbf{z} \vert \mathbf{x}_0)\) as approximate posterior distribution of latents
given data observations (see <a href="/posts/2024/12/blog-post-DDPM-AppendixA/">Appendix A</a> for the full list of notations).</p>

<p><em>$^*$ You may have noticed, first, functions are denoted as \(g(\mathbf{z})\). In addition to choosing the parameterization
in the form of a neural network, the argument becomes conditional on data samples - \(q_\phi(\mathbf{z}|\mathbf{x}_0)\). There is no hidden meaning here: \(g(\mathbf{z})\) is the aggregate posterior approximation for all data sample altogether,
whereas \(q_\phi(\mathbf{z}|\mathbf{x}_0)\) is a per sample metric.
To compute the aggregate, we take the average of \(q_\phi(\mathbf{z}|\mathbf{x}_0)\) across samples
(see expectation refresher below and VAE section for an example and an illustration of aggregate estimation).</em></p>

<p>Let’s now insert neural network \(q_\phi(\mathbf{z}|\mathbf{x}_0)\) into KL-divergence
instead of \(g(\mathbf{z})\) and expand:</p>

\[\begin{align}
&amp; D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}_0)||p(\mathbf{z|\mathbf{x}_0}))\ =\\
&amp; \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log \frac{q_\phi(\mathbf{z}|\mathbf{x}_0)}{p(\mathbf{z|x_0})}\ =\ 
\int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(q_\phi(\mathbf{z}|\mathbf{x}_0)) - \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(p(\mathbf{z|x_0})) =\\
&amp; \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(q_\phi(\mathbf{z}|\mathbf{x}_0)) - \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(p(\mathbf{z},\mathbf{x}_0)) + \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(p(\mathbf{x}_0)) =\\
\bigg\{ &amp; \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0)=\frac{\int d\mathbf{z}\ q_\phi(\mathbf{z},\mathbf{x}_0)}{q_\phi(\mathbf{x}_0)}=q_\phi(\mathbf{x}_0)/q_\phi(\mathbf{x}_0)=1\ \ (\text{Luo, 2022; equation 9}) \bigg\}\\
&amp; \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(q_\phi(\mathbf{z}|\mathbf{x}_0)) - \int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(p(\mathbf{z},\mathbf{x}_0))
+ \log(p(\mathbf{x}_0))
\end{align}\]

<p>By definition \(\int d\mathbf{z}\ q_\phi(\mathbf{z} \vert \mathbf{x}_0) \log(p(\mathbf{z},\mathbf{x}_0))\)
is expectation (see expectation refresher below)
of \(\log(p(\mathbf{z},\mathbf{x}_0))\) with respect to \(q_\phi(\mathbf{z} \vert \mathbf{x}_0)\)
and is denoted by \(\mathbb{E}_{q_{\phi}}[\log(p(\mathbf{z},\mathbf{x}_0))]\). Similarly,
\(\int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(q_\phi(\mathbf{z}|\mathbf{x}_0))\) becomes
\(\mathbb{E}_{q_{\phi}}[\log(q_\phi(\mathbf{z}|\mathbf{x}_0))]\). Now, the above equation takes the form:</p>

\[\begin{align}
D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}_0)||p(\mathbf{z|\mathbf{x}_0})) = \mathbb{E}_{q_{\phi}}[\log(q_\phi(\mathbf{z|x_0}))]\ - \mathbb{E}_{q_{\phi}}[\log(p(\mathbf{z},\mathbf{x}_0))] + \log(p(\mathbf{x}_0))
\end{align}\]

<p>Dependence on \(\log(p(\mathbf{x}_0))\), however, still remains in place (<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>).</p>

<!--<details class="expandable-box">
    <summary>Mathematical expectation refresher</summary>
    
<p>By definition, the expected value of a function \(g\) of a random variable \(\mathbf{z}\) is given by the integral
\(\mathbb{E}[g(\mathbf{Z})]=\int d\mathbf{z} f(\mathbf{z}) g(\mathbf{z})\), where \(f(\mathbf{z})\)
is a probability density function of \(\mathbf{z} \in \mathbf{Z}\)
(<a href="https://bookdown.org/pkaldunn/DistTheory/Expectation.html#ExpectationFunction" target="_blank">Expectation of a function of a random variable</a>). Capital \(\mathbf{Z}\) denotes a space (or set) of all possible values variable \(\mathbf{z}\)
can take. \(f(\mathbf{z})\) describes a probability of each value.
In other words, we sample values from their probability density \(\mathbf{z} \sim f(\mathbf{z})\).
To avoid confusion, let’s stick to the notation of \(\mathbb{E}[g(\mathbf{z})]\) (ignoring capital \(\mathbf{Z}\)),
but keep in mind we sample \(\mathbf{z}\) from \(f(\mathbf{z})\), which describes probabilities of all possible values \(\mathbf{Z}\).
Another way to denote expectation is \(\mathbb{E}_{\mathbf{z} \sim f(\mathbf{z})}[g(\mathbf{z})]\),
which emphasizes the previous statement.</p>

<p>Notice, our functions
\(\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))\) and \(\log(p(\mathbf{z},\mathbf{x}_0))\) are functions of ‘only’ \(\mathbf{z}\) if you ‘ignore’ (fix and not vary) \(\mathbf{x}_0\). Let’s denote them accordingly:
\(f(\mathbf{z})=\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))\) and \(g(\mathbf{z})=\log(p(\mathbf{z},\mathbf{x}_0))\).
While \(q_\phi(\cdot)\) is not a true probability but is a neural-net-based approximation of the posterior,
we can still sample from it. Hence, the following expectations take place
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>: equation 11; <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: equation 11):</p>

\[\begin{align}
&amp;\int d\mathbf{z}\ q_\phi(\mathbf{z} \vert \mathbf{x}_0) \log(p(\mathbf{z},\mathbf{x}_0)) &amp;=&amp;
&amp;\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \vert \mathbf{x}_0)} [\log(p(\mathbf{z},\mathbf{x}_0))] \\
&amp;\int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(q_\phi(\mathbf{z} \vert \mathbf{x}_0)) &amp;=&amp;
&amp;\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \vert \mathbf{x}_0)}[\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))]
\end{align}\]

<p>You have just seen how notation \(\mathbb{E}_{\mathbf{z} \sim f(\mathbf{z})}[\cdot]\)
transforms when we use \(\log{q(\cdot)}\) and \(\log{p(\cdot)}\) instead of \(f(\cdot)\) and \(g(\cdot)\).
Variations of \(\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \vert \mathbf{x}_0)}[\cdot]\)
include \(\mathbb{E}_{q_\phi(\mathbf{z} \vert \mathbf{x}_0)}[\cdot]\), \(\mathbb{E}_{q_\phi}[\cdot]\) and etc.,
which all denote the same quantity. When expectation is taken with respect to the dataset samples at our hand,
the notation is \(\mathbb{E}_{\mathbf{x}_0}[\cdot]\) or \(\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}[\cdot]\)
(\(q(\mathbf{x}_0)\) denotes true unknown data distribution (see Appendix A
for the full list of notations)).</p>

<p>In terms of the <strong>obvious intuition</strong>, expectation corresponds to the integral of a function \(g(\mathbf{z})\),
weighted by the probability density function \(f(\mathbf{z})\) of its input arguments \(\mathbf{z}\).
Each value \(\mathbf{z}\) input to \(g(\mathbf{z})\) brings its corresponding probability-based weight. In other words, the focus is on values, which have higher probabilities. In contrast, if \(f(\mathbf{z})\) were equal to $1$ for all values,
we would simply average \(g(\mathbf{z})\) values.</p>

<p>In regards to posterior approximation, the better our posterior estimates are, the higher the expectations in our objective function will be.
Hence, we not only focus on highly probable \(\mathbf{z}\) but also make sure the approximate posterior itself
\(\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))\) finds approximately correct latent factors for observations \(\mathbf{x}_0\)
(see ‘Variational Inference for posterior approximation: linear operator example’ and the following sections).</p>

<p><strong>Expectation in practice</strong></p>

<p>While above we define the expectation in terms of the integrals, in practice, an average across several samples from the
distribution is computed. This is especially true for the data distribution \(q(\mathbf{x}_0)\), which we do not know.
We only know samples from it \(\mathbf{x}_0 \sim q(\mathbf{x}_0)\), which comprise our dataset.
We won’t be able to compute the integral \(\mathbb{E}_{\mathbf{x}_0}[\cdot] = \int d\mathbf{x}_0\ q(\mathbf{x}_0)\ [\cdot]\).
The same applies to the posterior: we take several samples of \(\mathbf{z} \sim q(\mathbf{z} \vert \mathbf{x}_0)\) and average
expectation argument across them.
By the Law of Large Numbers, as we increase the number of samples, this average converges to the true expectation.
Even though \(q_\phi(\mathbf{z} \vert \mathbf{x}_0)\) can have an analytical
form, the expectation integral is not explicitly taken when computing the objective functions below.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Mathematical expectation refresher</span>
    </summary>
    <div class="expandable-content">
      
<p>By definition, the expected value of a function \(g\) of a random variable \(\mathbf{z}\) is given by the integral
\(\mathbb{E}[g(\mathbf{Z})]=\int d\mathbf{z} f(\mathbf{z}) g(\mathbf{z})\), where \(f(\mathbf{z})\)
is a probability density function of \(\mathbf{z} \in \mathbf{Z}\)
(<a href="https://bookdown.org/pkaldunn/DistTheory/Expectation.html#ExpectationFunction" target="_blank">Expectation of a function of a random variable</a>). Capital \(\mathbf{Z}\) denotes a space (or set) of all possible values variable \(\mathbf{z}\)
can take. \(f(\mathbf{z})\) describes a probability of each value.
In other words, we sample values from their probability density \(\mathbf{z} \sim f(\mathbf{z})\).
To avoid confusion, let’s stick to the notation of \(\mathbb{E}[g(\mathbf{z})]\) (ignoring capital \(\mathbf{Z}\)),
but keep in mind we sample \(\mathbf{z}\) from \(f(\mathbf{z})\), which describes probabilities of all possible values \(\mathbf{Z}\).
Another way to denote expectation is \(\mathbb{E}_{\mathbf{z} \sim f(\mathbf{z})}[g(\mathbf{z})]\),
which emphasizes the previous statement.</p>

<p>Notice, our functions
\(\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))\) and \(\log(p(\mathbf{z},\mathbf{x}_0))\) are functions of ‘only’ \(\mathbf{z}\) if you ‘ignore’ (fix and not vary) \(\mathbf{x}_0\). Let’s denote them accordingly:
\(f(\mathbf{z})=\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))\) and \(g(\mathbf{z})=\log(p(\mathbf{z},\mathbf{x}_0))\).
While \(q_\phi(\cdot)\) is not a true probability but is a neural-net-based approximation of the posterior,
we can still sample from it. Hence, the following expectations take place
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>: equation 11; <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: equation 11):</p>

\[\begin{align}
&amp;\int d\mathbf{z}\ q_\phi(\mathbf{z} \vert \mathbf{x}_0) \log(p(\mathbf{z},\mathbf{x}_0)) &amp;=&amp;
&amp;\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \vert \mathbf{x}_0)} [\log(p(\mathbf{z},\mathbf{x}_0))] \\
&amp;\int d\mathbf{z}\ q_\phi(\mathbf{z}|\mathbf{x}_0) \log(q_\phi(\mathbf{z} \vert \mathbf{x}_0)) &amp;=&amp;
&amp;\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \vert \mathbf{x}_0)}[\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))]
\end{align}\]

<p>You have just seen how notation \(\mathbb{E}_{\mathbf{z} \sim f(\mathbf{z})}[\cdot]\)
transforms when we use \(\log{q(\cdot)}\) and \(\log{p(\cdot)}\) instead of \(f(\cdot)\) and \(g(\cdot)\).
Variations of \(\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \vert \mathbf{x}_0)}[\cdot]\)
include \(\mathbb{E}_{q_\phi(\mathbf{z} \vert \mathbf{x}_0)}[\cdot]\), \(\mathbb{E}_{q_\phi}[\cdot]\) and etc.,
which all denote the same quantity. When expectation is taken with respect to the dataset samples at our hand,
the notation is \(\mathbb{E}_{\mathbf{x}_0}[\cdot]\) or \(\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}[\cdot]\)
(\(q(\mathbf{x}_0)\) denotes true unknown data distribution (see Appendix A
for the full list of notations)).</p>

<p>In terms of the <strong>obvious intuition</strong>, expectation corresponds to the integral of a function \(g(\mathbf{z})\),
weighted by the probability density function \(f(\mathbf{z})\) of its input arguments \(\mathbf{z}\).
Each value \(\mathbf{z}\) input to \(g(\mathbf{z})\) brings its corresponding probability-based weight. In other words, the focus is on values, which have higher probabilities. In contrast, if \(f(\mathbf{z})\) were equal to $1$ for all values,
we would simply average \(g(\mathbf{z})\) values.</p>

<p>In regards to posterior approximation, the better our posterior estimates are, the higher the expectations in our objective function will be.
Hence, we not only focus on highly probable \(\mathbf{z}\) but also make sure the approximate posterior itself
\(\log(q_\phi(\mathbf{z} \vert \mathbf{x}_0))\) finds approximately correct latent factors for observations \(\mathbf{x}_0\)
(see ‘Variational Inference for posterior approximation: linear operator example’ and the following sections).</p>

<p><strong>Expectation in practice</strong></p>

<p>While above we define the expectation in terms of the integrals, in practice, an average across several samples from the
distribution is computed. This is especially true for the data distribution \(q(\mathbf{x}_0)\), which we do not know.
We only know samples from it \(\mathbf{x}_0 \sim q(\mathbf{x}_0)\), which comprise our dataset.
We won’t be able to compute the integral \(\mathbb{E}_{\mathbf{x}_0}[\cdot] = \int d\mathbf{x}_0\ q(\mathbf{x}_0)\ [\cdot]\).
The same applies to the posterior: we take several samples of \(\mathbf{z} \sim q(\mathbf{z} \vert \mathbf{x}_0)\) and average
expectation argument across them.
By the Law of Large Numbers, as we increase the number of samples, this average converges to the true expectation.
Even though \(q_\phi(\mathbf{z} \vert \mathbf{x}_0)\) can have an analytical
form, the expectation integral is not explicitly taken when computing the objective functions below.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<!-- The same notation pattern can be found in https://www.youtube.com/watch?v=1bpQ0QDPGuI&t=1646s in support to the above -->

<p>Since KL-divergence cannot be computed due to the \(\log(p(\mathbf{x}_0))\) term, we optimize an alternative function: Evidence Lower Bound (ELBO) (<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>; equation 13). ELBO equals negative KL-divergence plus \(\log(p(\mathbf{x}_0))\), which is independent from \(q_{\phi}(\cdot)\). The latter is important since
\(\log(p(\mathbf{x}_0))\) can be treated as a constant during our search for an optimal posterior approximator \(q_{\phi}(\cdot)\).
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 6). Let’s add the terms together (<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>; equation 4):</p>

\[\begin{align}
  &amp; \text{ELBO}(q_\phi(\mathbf{z}|\mathbf{x}_0)) = - D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}_0)||p(\mathbf{z|\mathbf{x}_0})) + \log(p(\mathbf{x}_0)) = \\
- &amp; \mathbb{E}_{q_{\phi}}[\log(q_\phi(\mathbf{z|x_0}))]\ + \mathbb{E}_{q_{\phi}}[\log(p(\mathbf{z},\mathbf{x}_0))] =\\
  &amp; \mathbb{E}_{q_{\phi}}[\log{\frac{p(\mathbf{z},\mathbf{x}_0)}{q_\phi(\mathbf{z|x_0})}}] \leq \log(p(\mathbf{x}_0))
\end{align}\]

<p>The same result can be derived through Jensen’s inequality (see below).</p>

<!--<details class="expandable-box">
    <summary>ELBO derivation through Jensen's inequality</summary>
    
<p>An alternative way to arrive at ELBO, also used in <a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., (2015)</a>,
is through Jensen’s inequality
(see, for instance, <a href="https://lips.cs.princeton.edu/the-elbo-without-jensen-or-kl/" target="_blank">‘The ELBO without Jensen, Kullback, or Leibler’ by Ryan Adams, 2020</a>). Quick Wikipedia search usually gives the following quantity:</p>

\[\begin{align}
f(\mathbb{E}_{q_{\phi}}[\mathbf{Z}]) \leq \mathbb{E}_{q_{\phi}}[f(\mathbf{Z})]
\end{align}\]

<p>which, unfortunately, is not directly applicable in our case: our function \(f\)
is \(\log\), which is concave and not convex - requirement for the inequality.
\(\mathbf{Z}\) denotes a space of value a random variable can take (see ‘Mathematical expectation refresher’;
here, I re-introduced capitalized notation for consistency with references).
However, for concave function the following holds true
(<a href="https://www.youtube.com/watch?v=1bpQ0QDPGuI&amp;t=1646s" target="_blank">UC Berkeley Deep Reinforcement Learning</a>;
<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023: Appendix A</a>):</p>

\[\begin{align}
f(\mathbb{E}_{q_{\phi}}[\mathbf{Z}]) \geq \mathbb{E}_{q_{\phi}}[f(\mathbf{Z})]
\end{align}\]

<p>In fact, in <a href="https://www.youtube.com/watch?v=1bpQ0QDPGuI&amp;t=1646s" target="_blank">UC Berkeley Deep Reinforcement Learning</a>
the rule is explicitly formulated for \(\log\)’s:
\(\log{\mathbb{E}_{q_{\phi}}[\mathbf{Z}]} \geq \mathbb{E}_{q_{\phi}}[\log{\mathbf{Z}}]\)</p>

<p>Now, let’s use a clever trick of multiplication by 1. Yes, you read it right, however I should clarify
that it is not just a trivial math operation but rather multiplication by</p>

\[\begin{align}
1 = \frac{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}
\end{align}\]

<p>which is a pattern commonly used in derivations. By putting together Jensen’s inequality and multiplication by 1
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a> equations (5)-(8);
<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023</a> Appendix A (be careful with a typo in line 3 of equation 15:
first term under the integral should be conditioned on \(\mathbf{x}_0\))):</p>

\[\begin{align}
\log{p(\mathbf{x}_0)}
&amp;= \log{ \int d\mathbf{z}\ p(\mathbf{z},\mathbf{x}_0)\ \cdot 1} \\
&amp;= \log{ \int d\mathbf{z}\ p(\mathbf{z},\mathbf{x}_0)\ \frac{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}} \\
&amp;= \log{ \int d\mathbf{z}\ q_{\phi}(\mathbf{z}|\mathbf{x}_0)\ \frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}} \\
&amp;= \log{ \mathbb{E}_{q_{\phi}}[ \frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)} ] } \\
&amp;\geq \mathbb{E}_{q_{\phi}}[\log{\frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}}]
\end{align}\]

<p>we arrive at the same expression as for the ELBO before. Now, if we see either derivations in the literature,
we would know they are equivalent.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">ELBO derivation through Jensen's inequality</span>
    </summary>
    <div class="expandable-content">
      
<p>An alternative way to arrive at ELBO, also used in <a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., (2015)</a>,
is through Jensen’s inequality
(see, for instance, <a href="https://lips.cs.princeton.edu/the-elbo-without-jensen-or-kl/" target="_blank">‘The ELBO without Jensen, Kullback, or Leibler’ by Ryan Adams, 2020</a>). Quick Wikipedia search usually gives the following quantity:</p>

\[\begin{align}
f(\mathbb{E}_{q_{\phi}}[\mathbf{Z}]) \leq \mathbb{E}_{q_{\phi}}[f(\mathbf{Z})]
\end{align}\]

<p>which, unfortunately, is not directly applicable in our case: our function \(f\)
is \(\log\), which is concave and not convex - requirement for the inequality.
\(\mathbf{Z}\) denotes a space of value a random variable can take (see ‘Mathematical expectation refresher’;
here, I re-introduced capitalized notation for consistency with references).
However, for concave function the following holds true
(<a href="https://www.youtube.com/watch?v=1bpQ0QDPGuI&amp;t=1646s" target="_blank">UC Berkeley Deep Reinforcement Learning</a>;
<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023: Appendix A</a>):</p>

\[\begin{align}
f(\mathbb{E}_{q_{\phi}}[\mathbf{Z}]) \geq \mathbb{E}_{q_{\phi}}[f(\mathbf{Z})]
\end{align}\]

<p>In fact, in <a href="https://www.youtube.com/watch?v=1bpQ0QDPGuI&amp;t=1646s" target="_blank">UC Berkeley Deep Reinforcement Learning</a>
the rule is explicitly formulated for \(\log\)’s:
\(\log{\mathbb{E}_{q_{\phi}}[\mathbf{Z}]} \geq \mathbb{E}_{q_{\phi}}[\log{\mathbf{Z}}]\)</p>

<p>Now, let’s use a clever trick of multiplication by 1. Yes, you read it right, however I should clarify
that it is not just a trivial math operation but rather multiplication by</p>

\[\begin{align}
1 = \frac{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}
\end{align}\]

<p>which is a pattern commonly used in derivations. By putting together Jensen’s inequality and multiplication by 1
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a> equations (5)-(8);
<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023</a> Appendix A (be careful with a typo in line 3 of equation 15:
first term under the integral should be conditioned on \(\mathbf{x}_0\))):</p>

\[\begin{align}
\log{p(\mathbf{x}_0)}
&amp;= \log{ \int d\mathbf{z}\ p(\mathbf{z},\mathbf{x}_0)\ \cdot 1} \\
&amp;= \log{ \int d\mathbf{z}\ p(\mathbf{z},\mathbf{x}_0)\ \frac{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}} \\
&amp;= \log{ \int d\mathbf{z}\ q_{\phi}(\mathbf{z}|\mathbf{x}_0)\ \frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}} \\
&amp;= \log{ \mathbb{E}_{q_{\phi}}[ \frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)} ] } \\
&amp;\geq \mathbb{E}_{q_{\phi}}[\log{\frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}}]
\end{align}\]

<p>we arrive at the same expression as for the ELBO before. Now, if we see either derivations in the literature,
we would know they are equivalent.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<p>To get intuition about optimal variational density, <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., (2018)</a>
(page 6, last paragraph) re-writes ELBO as:</p>

\[\begin{align}
  &amp;\mathbb{E}_{q_{\phi}}\big[\log{\frac{p(\mathbf{z},\mathbf{x}_0)}{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}}\big] =\\
  &amp;\mathbb{E}_{q_{\phi}}[\log{p(\mathbf{x}_0|\mathbf{z})\ p(\mathbf{z})}]&amp; &amp;-&amp; &amp;\mathbb{E}_{q_{\phi}}[\log{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}] =\\
  &amp;\mathbb{E}_{q_{\phi}}[\log{p(\mathbf{x}_0|\mathbf{z})}]&amp; &amp;-&amp; &amp;\mathbb{E}_{q_{\phi}}\big[\log{\frac{q_{\phi}(\mathbf{z}|\mathbf{x}_0)}{p(\mathbf{z})}}\big] =\\
  &amp;\mathbb{E}_{q_{\phi}}[\log{p(\mathbf{x}_0|\mathbf{z})}]&amp; &amp;-&amp; &amp;D_{\text{KL}}(q_{\phi}(\mathbf{z}|\mathbf{x}_0) || p(\mathbf{z}))
\end{align}\]

<p>In fact, this expression is nothing but the objective function for the VAE (see next chapter for more details).
The first term is an expected likelihood. The second term measures dissimilarity between the estimated posterior and a prior
knowledge about it. Thus, the variational objective mirrors the typical balance between likelihood, which encourages latent variable configurations
explaining the observed data well, and prior, which encourages the densities close to the prior 
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>).</p>

<p>Moreover, ELBO does not involve the intractable term \(\log(p(\mathbf{x}_0))\)
and its maximization corresponds to minimization of KL-divergence \(D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}_0)||p(\mathbf{z|\mathbf{x}_0}))\)
between approximate and true posterior probabilities. To maximize ELBO we need
to maximize the likelihood and minimize the prior term: the better we match the prior, the less we subtract
(KL-divergence is non-negative). And thus, when we search for neural network
parameters \(\phi\) maximizing the ELBO, we, at the same time, obtain a better match to the true posterior. While
we do not have access to the true posterior, otherwise the problem would have already been solved, in the sections below
we will see how ELBO can be simplified given a particular model choice, e.g., VAE and a toy example below.</p>

<!--<details class="expandable-box">
    <summary>Variational Inference for posterior approximation: linear operator example</summary>
    
<p>Unlike MCMC, which constructs the posterior by the random walk, variational inference
directly estimates the approximate posterior through optimization of ELBO.
Here, we use the same likelihood operator mapping latents to data with known parameters (\(k\), \(c\)) as in the MCMC example above.
This Bayesian inference model is too simple for practical VI applications, but serves
as a toy example to illustrate the Variational Inference framework.</p>

<p>As opposed to randomly walking in the latent space,
our objective is to find an approximate latent posterior \(q_\phi(z|x_0)\), which maximizes the ELBO
(same as above, except \(x_0\) and \(z\) are one-dimensional and not bolded):</p>

\[\begin{align}
  &amp;\mathbb{E}_{q_{\phi}}\big[\log{\frac{p(z,{x}_0)}{q_{\phi}({z}|{x}_0)}}\big] =\\
  &amp;\mathbb{E}_{q_{\phi}}[\log{p({x}_0|{z})}]&amp; &amp;-&amp; &amp;D_{\text{KL}}(q_{\phi}({z}|{x}_0) || p({z}))
\end{align}\]

<p>We want to find this posterior for all observations and latents \(i\). Given that we draw
the data samples independently, we can use the factorization of marginal likelihoods
(e.g., see <a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: page 3, paragraph 3):</p>

\[\begin{align}
\log{p(x_0^{i=1},x_0^{i=2},...,x_0^{i=N})} = \sum_{i=1}^N \log(p(x_0^i))
\end{align}\]

<p>and, obviously, each of the logs in the sum is bounded by ‘its own’ ELBO:</p>

\[\begin{align}
\sum_{i=1}^N \log(p(x_0^i)) \geq \sum_i \mathbb{E}_{z \sim q_{\phi}({z}|{x}_0^i)}[\log{p({x}_0^i|{z})}] - D_{\text{KL}}(q_{\phi}({z}|{x}_0^i) || p({z}))
\end{align}\]

<p>Let’s, for now, focus on a single data observation point \(x_0 = x_0^i\):</p>

\[\begin{align}
\log(p(x_0)) \geq \mathbb{E}_{z \sim q_{\phi}({z}|{x}_0)}[\log{p({x}_0|{z})}] - D_{\text{KL}}(q_{\phi}({z}|{x}_0) || p({z}))
\end{align}\]

<p>How does \(q_\phi(z \vert x_0)\) enter the ELBO objective? Besides being explicit argument to the second term,
the average of both terms is taken (see expectation in front of the likelihood; KL-divergence includes expectation
with respect to \(q_\phi(\cdot)\) by definition)
with respect to samples from our approximate posterior. We search for such parameters \(\phi\), which, given a dataset \(x_0\), result in the latent samples maximizing the likelihood and the prior matching terms in the variational objective.
In our case, there are two parameters to estimate: a slope and a bias.</p>

<p>Recall, the data likelihood in our MCMC example takes the form:</p>

\[\begin{align}
  p(x_0|z) \propto e^{ -\frac{1}{2\sigma^2} \big(x_0 - (k \cdot z + c) \big)^2 }
\end{align}\]

<p>Let me, for illustration purposes, drop the prior term from ELBO. I briefly summarize its influence on the posterior towards the end of the subsection.</p>

<p>What would optimal parameters \(\phi\) be? If we take the log of the likelihood term and ignore the scaling factor
(let’s assume fixed data variance \(\sigma\) corresponding to 5% measurement error) we get:</p>

\[\begin{align}
  -\mathbb{E}_{q_{\phi}}[\log{p(x_0|z)}] \propto \mathbb{E}_{q_{\phi}}[\big(x_0 - (k \cdot z + c) \big)^2]
\end{align}\]

<p>I have also flipped the sign in front of the likelihood: recall its maximization is equivalent to minimization of the negative. 
We get a very similar expression to the ones we have in the Inverse Theory example. Except now, we are searching
for a map from data to latents - posterior \(q_\phi(\cdot)\). Common practice is to parameterize it as a Gaussian
(<a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>):</p>

\[\begin{align}
  q_{\phi}(z|x_0) \propto e^{-\frac{(z - \mu_\phi(x_0))^2}{2\sigma_\phi(x_0)^2}}
\end{align}\]

<p>with mean \(\mu_\phi(x_0)\) and variance \(\sigma_\phi(x_0)\) as neural networks.
The variance allows for variability of \(z^{i,l}\) for each data sample \(x_0^i\). Recall, in MCMC estimation for latents example, \(l\) denotes
denotes a particular draw of \(z^i\). Equivalently (and we discuss it in detail in the following sections), a Gaussian can be expressed as:</p>

\[\begin{align}
z^{i,l} = \mu_\phi(x_0^i) + \sigma_\phi(x_0^i) \cdot \epsilon^l
\end{align}\]

<p>where \(\mu_\phi(\cdot)\) and \(\sigma_\phi(\cdot)\) remain the mean and the variance of the original Gaussian, while \(\epsilon\) denotes
a unit-variance zero-mean Gaussian distribution. Notice, randomness in \(z\) now comes from sampling \(\epsilon\).
We predict the mean and deviations around it, extent of which is defined by the variance.
The above expression has a name of ‘reparameterization’ trick.</p>

<p>For further discussion, it turns out easier to set the variance \(\sigma_\phi(x_0)\) to zero, i.e., collapse our posterior
to the purely deterministic case. This is not something, which is done in practice as we loose
the desired probabilistic character of latents. I use it here to make things easier to understand.
In this case, our Gaussian collapses to a spike centered at the mean value and all draws \(l\) result in the same \(z^i\).
We can now express the likelihood term as:</p>

\[\begin{align}
\mathbb{E}_{z \sim q_\phi(x_0)}[(x_0 - (k \cdot z + c))^2] = (x_0 - (k \cdot \mu_\phi(x_0) + c))^2
\end{align}\]

<p>where, with zero variance, expectation collapses to a single value (mean \(\mu_\phi(x_0)\)) since all the \(z^{i,l}\) samples are equal.</p>

<p>Since the forward modeling operator is linear, let’s assume the reverse one (our mean \(z\) predictor from \(x_0\) - \(\mu_\phi(x_0)\)) is too:</p>

\[\begin{align}
z = \mu_\phi(x_0) = \kappa_1 x_0 + \kappa_2
\end{align}\]

<p>where \(\phi = (\kappa_1, \kappa_2)\) we search for, has two parameters: slope \(\kappa_1\) and bias \(\kappa_2\).
Let’s combine all our observations together (recall marginal likelihood factorization at the beginning of the subsection),
and get the following sum of likelihoods:</p>

\[\begin{align}
\text{MSE} = \sum_i \big(x_0^i - (k \cdot (\kappa_1 x_0^i + \kappa_2) + c)\big)^2
\end{align}\]

<p>Goldfish Emma tells me this setup is very similar to the Inverse Theory section. You can see the familiar MSE expression,
which we solve in a least-squares sense in a sec.</p>

<p><em>Let’s pause for a moment. Simplification to the expression above would not be possible without the assumptions we’ve used - no variance and mean linearity.
In real life, we would search for a neural network (an actual neural network with nonlinear activations and not only two coefficients)
with weights \(\phi\) minimizing the following likelihood:</em></p>

\[\begin{align}
\sum_i \big( \frac{1}{L} \sum_{l=1}^L\big(x_0^i - (k \cdot (\mu_{\phi}(x_0^i) + \sigma_\phi^2(x_0^i)\cdot \epsilon) + c)\big)^2 \big)
\end{align}\]

<p><em>which also corresponds to MSE (MSE is a popular cost function and is not only used in inversion).
Multiple latent draws can be used in MSE evaluation while learning the posterior, so I add averaging over \(l\)
(\(L\) denotes total number of draws).</em></p>

<p><em>In the ELBO objective, we should also minimize a prior matching term (ignored so far), which shapes \(q_\phi(\cdot)\)
towards desired a priori characteristics. For instance, if both the posterior and the prior are Gaussian, KL-divergence
has an analytical expression measuring the difference between their means and variances. And the closer \(\mu_\theta(\cdot)\)
and \(\sigma_\theta(\cdot)\) get to the mean and the variance of the a priori Gaussian, e.g., unit-variance zero-mean Gaussian, the higher the ELBO gets.
The unit-variance constraint helps create a more well-behaved latent space,
where all the latent values are not spread all over but
are rather concentrated. This concentration ensures there are no information gaps
and there is smooth transition between latents.</em></p>

<p><em>Unlike our deterministic zero-variance case, we search for a probabilistic posterior</em></p>

\[\begin{align}
q(z|x_0) \sim e^{\frac{(z - \mu_\phi(x_0))^2}{2\sigma_\phi^2(x_0)}}
\end{align}\]

<p><em>which describes the latent variable distribution. And instead of explicitly finding a least-squares solution
(typically, we are not solving a simple linear problem and won’t be able to get the optimal coefficients \(\phi\) analytically
so simple as below), stochastic gradient descent (SGD) or coordinate ascent variational inference
(CAVI; see <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 9)
is used for optimization.</em></p>

<p>With that cleared, let’s take the derivatives of our toy-example MSE with respect to \(\kappa_1\) and \(\kappa_2\)
and search, which \(\phi=(\kappa_1,\kappa_2)\) set them to zero (similarly to the case of inversion, where we search for optimal latents):</p>

\[\begin{align}
&amp;\frac{\partial \text{MSE}}{\partial \kappa_1} &amp;=&amp; -2 k\ x_0 \sum_i \big( x_0^i - (k \cdot (\kappa_1 x_0^i + \kappa_2) + c) \big) &amp; = &amp; &amp;0\\
&amp;\frac{\partial \text{MSE}}{\partial \kappa_2} &amp;=&amp; -2 k \sum_i \big( x_0^i - (k \cdot (\kappa_1 x_0^i + \kappa_2) + c) \big) &amp; = &amp; &amp;0
\end{align}\]

<p>If we squint at the problem, we are basically trying to find coefficients to match scaled
(\(k \cdot \kappa_1 \cdot x_0^i\) plus some constants) and unscaled data \(x_0\) - sort of a linear regression
on the data itself. We can guess the trivial solution \(\phi=(\frac{1}{k},-\frac{c}{k})\), which
sets derivatives to zero and is the optimal pair of values we are looking for.
We, thus, get our perfect encoder \(q_\phi(\cdot)\), which transforms input data samples into latents.</p>

<p>In reality, likelihood operator is not known perfectly, e.g., comes from an empirical relationship.
Data has noise. And we have more equations than unknowns (given more than two observations).
Thus, if we were to solve the MSE minimization in the least-squares sense, we would be looking for such a posterior,
which ‘on average’ maximizes the likelihood for all the observations (we talk more about ‘classic’ linear regression
in ‘Variational Inference for likelihood approximation: linear operator example’).
By ‘on average’ I mean such a posterior, which is equivalently good (or bad) for all the observation points
at the same time in terms of MSE. This is especially true, since the likelihood operator, in reality,
would not fit all the data deviations induced by noise but would somewhat outline the trend.</p>

<p>To add back the stochastic character of the posterior, let’s re-introduce the variance to \(z\):</p>

\[\begin{align}
z = \kappa_1 x_0 + \kappa_2 + \sigma_\phi (x_0) \epsilon
\end{align}\]

<p>A simple variance parameterization would be taking a fraction of input data magnitude, e.g.,</p>

\[\begin{align}
\sigma_\phi (x_0) = \kappa_3 \lVert x_0 \rVert
\end{align}\]

<p>where \(\lVert x_0 \rVert=\vert x_0 \vert\) since \(x_0\) is 1D.
Why is this a reasonable approach? We use a prior matching term in the objective function
to shape the posterior distribution to be close to a zero-mean unit-variance Gaussian.
In its turn, the variance parameterization
above assigns higher uncertainty to latents from larger magnitude data samples,
which makes sense in practice: the larger the value, the higher the surrounding variation around it
we allow. Think of it as a percent of the amplitude: larger values allow larger variations
without being affected. We thus could obtain more consistent
latent representations across different scales of input data.</p>

<p>Plugging in the variance-dependent latent $z$ and adding back the prior matching term into MSE
modifies our perfect encoder by forcing $\kappa_1$ and $\kappa_2$ to
balance between optimal reconstruction and the zero-mean Gaussian prior condition. $\kappa_3$, in turn, controls
the scale of uncertainty around the mean value $\mu_{(\frac{1}{k},-\frac{c}{k})}(x_0)$, allowing for
probabilistic fluctuations that respect both the data scale and the unit variance prior constraint.</p>

<p>Our demonstration is based on a toy example but all principal steps are in place:
we formulate the ELBO objective, and solve for optimal parameters \(\phi\) of a posterior latent distribution \(q_\phi(\cdot)\).
While we use assumptions limiting the generality for the simplicity of derivation, the principles remain the same -
a certain optimization procedure should be employed to find approximated posterior distribution \(q_\phi(\cdot)\), which, unlike the MCMC case,
is used to sample latent values maximizing the objective function. To solve real-world problems, we need
to model uncertainty, regularize a latent space by a prior matching term, as well as use neural net parameterizations of mean and variance. By using an
advanced numerical and probabilistic solver, we then obtain a probabilistic distribution of the latent variable posterior
given the observed data by maximizing the corresponding ELBO objective.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Variational Inference for posterior approximation: linear operator example</span>
    </summary>
    <div class="expandable-content">
      
<p>Unlike MCMC, which constructs the posterior by the random walk, variational inference
directly estimates the approximate posterior through optimization of ELBO.
Here, we use the same likelihood operator mapping latents to data with known parameters (\(k\), \(c\)) as in the MCMC example above.
This Bayesian inference model is too simple for practical VI applications, but serves
as a toy example to illustrate the Variational Inference framework.</p>

<p>As opposed to randomly walking in the latent space,
our objective is to find an approximate latent posterior \(q_\phi(z|x_0)\), which maximizes the ELBO
(same as above, except \(x_0\) and \(z\) are one-dimensional and not bolded):</p>

\[\begin{align}
  &amp;\mathbb{E}_{q_{\phi}}\big[\log{\frac{p(z,{x}_0)}{q_{\phi}({z}|{x}_0)}}\big] =\\
  &amp;\mathbb{E}_{q_{\phi}}[\log{p({x}_0|{z})}]&amp; &amp;-&amp; &amp;D_{\text{KL}}(q_{\phi}({z}|{x}_0) || p({z}))
\end{align}\]

<p>We want to find this posterior for all observations and latents \(i\). Given that we draw
the data samples independently, we can use the factorization of marginal likelihoods
(e.g., see <a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: page 3, paragraph 3):</p>

\[\begin{align}
\log{p(x_0^{i=1},x_0^{i=2},...,x_0^{i=N})} = \sum_{i=1}^N \log(p(x_0^i))
\end{align}\]

<p>and, obviously, each of the logs in the sum is bounded by ‘its own’ ELBO:</p>

\[\begin{align}
\sum_{i=1}^N \log(p(x_0^i)) \geq \sum_i \mathbb{E}_{z \sim q_{\phi}({z}|{x}_0^i)}[\log{p({x}_0^i|{z})}] - D_{\text{KL}}(q_{\phi}({z}|{x}_0^i) || p({z}))
\end{align}\]

<p>Let’s, for now, focus on a single data observation point \(x_0 = x_0^i\):</p>

\[\begin{align}
\log(p(x_0)) \geq \mathbb{E}_{z \sim q_{\phi}({z}|{x}_0)}[\log{p({x}_0|{z})}] - D_{\text{KL}}(q_{\phi}({z}|{x}_0) || p({z}))
\end{align}\]

<p>How does \(q_\phi(z \vert x_0)\) enter the ELBO objective? Besides being explicit argument to the second term,
the average of both terms is taken (see expectation in front of the likelihood; KL-divergence includes expectation
with respect to \(q_\phi(\cdot)\) by definition)
with respect to samples from our approximate posterior. We search for such parameters \(\phi\), which, given a dataset \(x_0\), result in the latent samples maximizing the likelihood and the prior matching terms in the variational objective.
In our case, there are two parameters to estimate: a slope and a bias.</p>

<p>Recall, the data likelihood in our MCMC example takes the form:</p>

\[\begin{align}
  p(x_0|z) \propto e^{ -\frac{1}{2\sigma^2} \big(x_0 - (k \cdot z + c) \big)^2 }
\end{align}\]

<p>Let me, for illustration purposes, drop the prior term from ELBO. I briefly summarize its influence on the posterior towards the end of the subsection.</p>

<p>What would optimal parameters \(\phi\) be? If we take the log of the likelihood term and ignore the scaling factor
(let’s assume fixed data variance \(\sigma\) corresponding to 5% measurement error) we get:</p>

\[\begin{align}
  -\mathbb{E}_{q_{\phi}}[\log{p(x_0|z)}] \propto \mathbb{E}_{q_{\phi}}[\big(x_0 - (k \cdot z + c) \big)^2]
\end{align}\]

<p>I have also flipped the sign in front of the likelihood: recall its maximization is equivalent to minimization of the negative. 
We get a very similar expression to the ones we have in the Inverse Theory example. Except now, we are searching
for a map from data to latents - posterior \(q_\phi(\cdot)\). Common practice is to parameterize it as a Gaussian
(<a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>):</p>

\[\begin{align}
  q_{\phi}(z|x_0) \propto e^{-\frac{(z - \mu_\phi(x_0))^2}{2\sigma_\phi(x_0)^2}}
\end{align}\]

<p>with mean \(\mu_\phi(x_0)\) and variance \(\sigma_\phi(x_0)\) as neural networks.
The variance allows for variability of \(z^{i,l}\) for each data sample \(x_0^i\). Recall, in MCMC estimation for latents example, \(l\) denotes
denotes a particular draw of \(z^i\). Equivalently (and we discuss it in detail in the following sections), a Gaussian can be expressed as:</p>

\[\begin{align}
z^{i,l} = \mu_\phi(x_0^i) + \sigma_\phi(x_0^i) \cdot \epsilon^l
\end{align}\]

<p>where \(\mu_\phi(\cdot)\) and \(\sigma_\phi(\cdot)\) remain the mean and the variance of the original Gaussian, while \(\epsilon\) denotes
a unit-variance zero-mean Gaussian distribution. Notice, randomness in \(z\) now comes from sampling \(\epsilon\).
We predict the mean and deviations around it, extent of which is defined by the variance.
The above expression has a name of ‘reparameterization’ trick.</p>

<p>For further discussion, it turns out easier to set the variance \(\sigma_\phi(x_0)\) to zero, i.e., collapse our posterior
to the purely deterministic case. This is not something, which is done in practice as we loose
the desired probabilistic character of latents. I use it here to make things easier to understand.
In this case, our Gaussian collapses to a spike centered at the mean value and all draws \(l\) result in the same \(z^i\).
We can now express the likelihood term as:</p>

\[\begin{align}
\mathbb{E}_{z \sim q_\phi(x_0)}[(x_0 - (k \cdot z + c))^2] = (x_0 - (k \cdot \mu_\phi(x_0) + c))^2
\end{align}\]

<p>where, with zero variance, expectation collapses to a single value (mean \(\mu_\phi(x_0)\)) since all the \(z^{i,l}\) samples are equal.</p>

<p>Since the forward modeling operator is linear, let’s assume the reverse one (our mean \(z\) predictor from \(x_0\) - \(\mu_\phi(x_0)\)) is too:</p>

\[\begin{align}
z = \mu_\phi(x_0) = \kappa_1 x_0 + \kappa_2
\end{align}\]

<p>where \(\phi = (\kappa_1, \kappa_2)\) we search for, has two parameters: slope \(\kappa_1\) and bias \(\kappa_2\).
Let’s combine all our observations together (recall marginal likelihood factorization at the beginning of the subsection),
and get the following sum of likelihoods:</p>

\[\begin{align}
\text{MSE} = \sum_i \big(x_0^i - (k \cdot (\kappa_1 x_0^i + \kappa_2) + c)\big)^2
\end{align}\]

<p>Goldfish Emma tells me this setup is very similar to the Inverse Theory section. You can see the familiar MSE expression,
which we solve in a least-squares sense in a sec.</p>

<p><em>Let’s pause for a moment. Simplification to the expression above would not be possible without the assumptions we’ve used - no variance and mean linearity.
In real life, we would search for a neural network (an actual neural network with nonlinear activations and not only two coefficients)
with weights \(\phi\) minimizing the following likelihood:</em></p>

\[\begin{align}
\sum_i \big( \frac{1}{L} \sum_{l=1}^L\big(x_0^i - (k \cdot (\mu_{\phi}(x_0^i) + \sigma_\phi^2(x_0^i)\cdot \epsilon) + c)\big)^2 \big)
\end{align}\]

<p><em>which also corresponds to MSE (MSE is a popular cost function and is not only used in inversion).
Multiple latent draws can be used in MSE evaluation while learning the posterior, so I add averaging over \(l\)
(\(L\) denotes total number of draws).</em></p>

<p><em>In the ELBO objective, we should also minimize a prior matching term (ignored so far), which shapes \(q_\phi(\cdot)\)
towards desired a priori characteristics. For instance, if both the posterior and the prior are Gaussian, KL-divergence
has an analytical expression measuring the difference between their means and variances. And the closer \(\mu_\theta(\cdot)\)
and \(\sigma_\theta(\cdot)\) get to the mean and the variance of the a priori Gaussian, e.g., unit-variance zero-mean Gaussian, the higher the ELBO gets.
The unit-variance constraint helps create a more well-behaved latent space,
where all the latent values are not spread all over but
are rather concentrated. This concentration ensures there are no information gaps
and there is smooth transition between latents.</em></p>

<p><em>Unlike our deterministic zero-variance case, we search for a probabilistic posterior</em></p>

\[\begin{align}
q(z|x_0) \sim e^{\frac{(z - \mu_\phi(x_0))^2}{2\sigma_\phi^2(x_0)}}
\end{align}\]

<p><em>which describes the latent variable distribution. And instead of explicitly finding a least-squares solution
(typically, we are not solving a simple linear problem and won’t be able to get the optimal coefficients \(\phi\) analytically
so simple as below), stochastic gradient descent (SGD) or coordinate ascent variational inference
(CAVI; see <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 9)
is used for optimization.</em></p>

<p>With that cleared, let’s take the derivatives of our toy-example MSE with respect to \(\kappa_1\) and \(\kappa_2\)
and search, which \(\phi=(\kappa_1,\kappa_2)\) set them to zero (similarly to the case of inversion, where we search for optimal latents):</p>

\[\begin{align}
&amp;\frac{\partial \text{MSE}}{\partial \kappa_1} &amp;=&amp; -2 k\ x_0 \sum_i \big( x_0^i - (k \cdot (\kappa_1 x_0^i + \kappa_2) + c) \big) &amp; = &amp; &amp;0\\
&amp;\frac{\partial \text{MSE}}{\partial \kappa_2} &amp;=&amp; -2 k \sum_i \big( x_0^i - (k \cdot (\kappa_1 x_0^i + \kappa_2) + c) \big) &amp; = &amp; &amp;0
\end{align}\]

<p>If we squint at the problem, we are basically trying to find coefficients to match scaled
(\(k \cdot \kappa_1 \cdot x_0^i\) plus some constants) and unscaled data \(x_0\) - sort of a linear regression
on the data itself. We can guess the trivial solution \(\phi=(\frac{1}{k},-\frac{c}{k})\), which
sets derivatives to zero and is the optimal pair of values we are looking for.
We, thus, get our perfect encoder \(q_\phi(\cdot)\), which transforms input data samples into latents.</p>

<p>In reality, likelihood operator is not known perfectly, e.g., comes from an empirical relationship.
Data has noise. And we have more equations than unknowns (given more than two observations).
Thus, if we were to solve the MSE minimization in the least-squares sense, we would be looking for such a posterior,
which ‘on average’ maximizes the likelihood for all the observations (we talk more about ‘classic’ linear regression
in ‘Variational Inference for likelihood approximation: linear operator example’).
By ‘on average’ I mean such a posterior, which is equivalently good (or bad) for all the observation points
at the same time in terms of MSE. This is especially true, since the likelihood operator, in reality,
would not fit all the data deviations induced by noise but would somewhat outline the trend.</p>

<p>To add back the stochastic character of the posterior, let’s re-introduce the variance to \(z\):</p>

\[\begin{align}
z = \kappa_1 x_0 + \kappa_2 + \sigma_\phi (x_0) \epsilon
\end{align}\]

<p>A simple variance parameterization would be taking a fraction of input data magnitude, e.g.,</p>

\[\begin{align}
\sigma_\phi (x_0) = \kappa_3 \lVert x_0 \rVert
\end{align}\]

<p>where \(\lVert x_0 \rVert=\vert x_0 \vert\) since \(x_0\) is 1D.
Why is this a reasonable approach? We use a prior matching term in the objective function
to shape the posterior distribution to be close to a zero-mean unit-variance Gaussian.
In its turn, the variance parameterization
above assigns higher uncertainty to latents from larger magnitude data samples,
which makes sense in practice: the larger the value, the higher the surrounding variation around it
we allow. Think of it as a percent of the amplitude: larger values allow larger variations
without being affected. We thus could obtain more consistent
latent representations across different scales of input data.</p>

<p>Plugging in the variance-dependent latent $z$ and adding back the prior matching term into MSE
modifies our perfect encoder by forcing $\kappa_1$ and $\kappa_2$ to
balance between optimal reconstruction and the zero-mean Gaussian prior condition. $\kappa_3$, in turn, controls
the scale of uncertainty around the mean value $\mu_{(\frac{1}{k},-\frac{c}{k})}(x_0)$, allowing for
probabilistic fluctuations that respect both the data scale and the unit variance prior constraint.</p>

<p>Our demonstration is based on a toy example but all principal steps are in place:
we formulate the ELBO objective, and solve for optimal parameters \(\phi\) of a posterior latent distribution \(q_\phi(\cdot)\).
While we use assumptions limiting the generality for the simplicity of derivation, the principles remain the same -
a certain optimization procedure should be employed to find approximated posterior distribution \(q_\phi(\cdot)\), which, unlike the MCMC case,
is used to sample latent values maximizing the objective function. To solve real-world problems, we need
to model uncertainty, regularize a latent space by a prior matching term, as well as use neural net parameterizations of mean and variance. By using an
advanced numerical and probabilistic solver, we then obtain a probabilistic distribution of the latent variable posterior
given the observed data by maximizing the corresponding ELBO objective.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<p>You can see where the name ELBO comes from: we bound the evidence from below (recall KL is non-negative) with
equality (meaning ELBO equals the evidence it attempts to bound) achieved when approximated posterior \(q_\phi(\mathbf{z}|\mathbf{x}_0)\)
matches the true one - \(p(\mathbf{z|\mathbf{x}_0})\).
By maximizing the ELBO we minimize KL-divergence and, due to bounding, maximize the evidence. 
While ELBO bounds log likelihood from below, negative ELBO bounds negative log likelihood from above.
The latter is often referred to as Variational Lower Bound (VLB) and is used frequently in diffusion model cost functions
(<a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., 2015</a>;
<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>;
<a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/#:~:text=The%20data%20sample,isotropic%20Gaussian%20distribution." target="_blank">Lilian Weng blog</a>) alongside with ELBO.</p>

<p>So far we have covered how latents can be estimated from data using Variational Inference. How does it help in new sample generation?
According to <a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, (2022)</a>: ‘… as we increase the lower bound by tuning the
parameters \(\phi\) to maximize the ELBO, we gain access to components that can be used to model the true
data distribution and sample from it, thus learning a generative model’. In other words,
the more effectively our latents capture the properties of the data, the better we can
reconstruct/generate new observations. For instance, in our linear operator example for Variational Inference
we have estimated the coefficients transforming data to latents - \(\phi=(\frac{1}{k},-c)\).
We can perfectly reconstruct existing observations by applying our forward modeling operator
to their encoded versions, while creating a new observation is simply a matter of choosing a latent value from the region we learned through 
\(\phi\) and applying our forward model.</p>

<p>The opposite direction of converting latents to data can be learnt as well. For instance, this is the case
for our MCMC example for linear regression, with the likelihood function \(p_\theta(x_0 \vert z)\), where \(\theta=(k,c)\).
As we will see when exploring VAEs, both \(\phi\) and \(\theta\) can be optimized for, when maximizing the ELBO.
In fact, learning the reverse transform (from latents to data) is crucial for generative modeling
(we typically do not know our forward model and it is much more complex than above). It allows for
new data creation by first choosing a latent sample and converting it to a data space, thus resulting in an observation,
which we may have never registered in the first place.</p>

<!--<details class="expandable-box">
    <summary>Variational Inference for likelihood approximation: linear operator example</summary>
    
<p>Recall, we have already estimated likelihood parameters in the MCMC linear regression example.
However, this approach becomes suboptimal for complex models with many parameters.
Instead, we can optimize \(p_\theta(x_0 \vert z)\) through ELBO maximization, just as we do for \(q_\phi(z \vert x_0)\).
This allows us to simultaneously optimize both distributions: the approximate posterior learns useful latent features while
the likelihood optimizes data reconstruction.</p>

<p>Similarly to the posterior, the likelihood can be parameterized as a Gaussian
(<a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: Appendix C.2):</p>

\[\begin{align}
p_\theta(x_0|z) \propto e^{-\frac{(x_0 - \mu_\theta(z))^2}{2\sigma_\theta(z)^2}}
\end{align}\]

<p>If we take a log and use marginal posterior factorization across different data observations,
as we do in the posterior estimation section, we get the following sum of squared errors:</p>

\[\begin{align}
\text{MSE} = \sum_i \frac{\big(x_0^i - \mu_\theta(z^i)\big)^2}{2\sigma_\theta(z^i)^2}
\end{align}\]

<p>The sign has been flipped to minimize MSE instead of equivalently maximizing the likelihood.
I have ignored the prior terms, since they do not depend on neither mean \(\mu_\theta(\cdot)\)
nor variance \(\sigma_\theta(\cdot)\). \(i\), as always, denotes an observation sample number.
Its corresponding latent \(z^i\) comes from the approximate posterior, encoding the same observation</p>

\[\begin{align}
z^i \sim q_\phi(z \vert x_0^i)
\end{align}\]

<p>Let’s use a linear mean parameterization:</p>

\[\begin{align}
\mu_\theta(z_0) = \kappa_4 z_i + \kappa_5
\end{align}\]

<p>and set variance to a constant \(\sigma_\theta(z_i)=\sigma_0\), where \(\sigma_0\),
for instance, equals 5% of the average data norm.</p>

<p>The MSE takes the following form:</p>

\[\begin{align}
\text{MSE} = \frac{1}{2\sigma_0^2}\sum_i \big(x_0^i - (\kappa_4 z_i + \kappa_5)\big)^2
\end{align}\]

<p>We have arrived at a classical linear regression problem, where, as before, we would set the
derivatives with respect to a slope \(\kappa_4\) and a bias \(\kappa_5\) to zero.
See more details in <a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., (2016)</a>:
Chapter 5, page 130, equation (5.59) regarding the least-squares solution.
The regression now aims to find a relationship between
estimated latents \(z \sim q_\phi(z \vert x_0)\) and data observations \(x_0\). Once again,
in reality, as we discuss in the posterior approximation example, \(p_\theta(\cdot)\)
would be far more complex than a linear model, and we would need to use SGD or CAVI to optimize the ELBO.</p>

<p>Unlike simple regression, where we predict a value of one variable from another, here, we model the variance of the data.
It’s easier to see when predicted \(x_0\) is expressed through the reparameterization trick:</p>

\[\begin{align}
x_0^i = \kappa_4 z_i + \kappa_5 + \sigma_\theta(z_i) \epsilon
\end{align}\]

<p>The stochasticity in a recovered data sample now depends on the variance
\(\sigma_\theta(z_i)\), which takes a latent value as an input.
We have set a variance to be constant. In the previous example for the posterior estimation
we set the variance of \(z_i\) as \(\sigma_\phi(x_0) = \kappa_3 \lVert x_0 \rVert\).
Let’s parameterize the linear variance of \(p_\theta(x_0^i \vert z)\) similarly,
using already estimated posterior distribution variance \(\sigma_\phi(\cdot)\):</p>

\[\begin{align}
\sigma_\theta(z_i) = \kappa_6 \sigma_\phi(x_0) = \kappa_6 \kappa_3 \lVert x_0 \rVert
\end{align}\]

<p>where scalar \(\kappa_6\) intends to recover the data variance from the latent space variance.
The optimal variation in the latent space can, obviously, be different from the data space.
We can now describe the data probabilistically. We do reconstruct the original
value - obviously, the goal of maximizing the likelihood \(p_\theta(x_0^i \vert z)\)
for an observed data sample \(x_0^i\) is in place -
but also get an estimate of the confidence interval around it. Probabilistic
data description allows for accounting for noise and other uncertainties, for instance,
incurred by the non-perfect likelihood or posterior operators.</p>

<p>Once again, while this is a toy example,
principal steps have been described. For a more realistic example, refer to the VAE section.</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">Variational Inference for likelihood approximation: linear operator example</span>
    </summary>
    <div class="expandable-content">
      
<p>Recall, we have already estimated likelihood parameters in the MCMC linear regression example.
However, this approach becomes suboptimal for complex models with many parameters.
Instead, we can optimize \(p_\theta(x_0 \vert z)\) through ELBO maximization, just as we do for \(q_\phi(z \vert x_0)\).
This allows us to simultaneously optimize both distributions: the approximate posterior learns useful latent features while
the likelihood optimizes data reconstruction.</p>

<p>Similarly to the posterior, the likelihood can be parameterized as a Gaussian
(<a href="https://arxiv.org/pdf/1312.6114" target="blank">Kingma and Welling, 2013</a>: Appendix C.2):</p>

\[\begin{align}
p_\theta(x_0|z) \propto e^{-\frac{(x_0 - \mu_\theta(z))^2}{2\sigma_\theta(z)^2}}
\end{align}\]

<p>If we take a log and use marginal posterior factorization across different data observations,
as we do in the posterior estimation section, we get the following sum of squared errors:</p>

\[\begin{align}
\text{MSE} = \sum_i \frac{\big(x_0^i - \mu_\theta(z^i)\big)^2}{2\sigma_\theta(z^i)^2}
\end{align}\]

<p>The sign has been flipped to minimize MSE instead of equivalently maximizing the likelihood.
I have ignored the prior terms, since they do not depend on neither mean \(\mu_\theta(\cdot)\)
nor variance \(\sigma_\theta(\cdot)\). \(i\), as always, denotes an observation sample number.
Its corresponding latent \(z^i\) comes from the approximate posterior, encoding the same observation</p>

\[\begin{align}
z^i \sim q_\phi(z \vert x_0^i)
\end{align}\]

<p>Let’s use a linear mean parameterization:</p>

\[\begin{align}
\mu_\theta(z_0) = \kappa_4 z_i + \kappa_5
\end{align}\]

<p>and set variance to a constant \(\sigma_\theta(z_i)=\sigma_0\), where \(\sigma_0\),
for instance, equals 5% of the average data norm.</p>

<p>The MSE takes the following form:</p>

\[\begin{align}
\text{MSE} = \frac{1}{2\sigma_0^2}\sum_i \big(x_0^i - (\kappa_4 z_i + \kappa_5)\big)^2
\end{align}\]

<p>We have arrived at a classical linear regression problem, where, as before, we would set the
derivatives with respect to a slope \(\kappa_4\) and a bias \(\kappa_5\) to zero.
See more details in <a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., (2016)</a>:
Chapter 5, page 130, equation (5.59) regarding the least-squares solution.
The regression now aims to find a relationship between
estimated latents \(z \sim q_\phi(z \vert x_0)\) and data observations \(x_0\). Once again,
in reality, as we discuss in the posterior approximation example, \(p_\theta(\cdot)\)
would be far more complex than a linear model, and we would need to use SGD or CAVI to optimize the ELBO.</p>

<p>Unlike simple regression, where we predict a value of one variable from another, here, we model the variance of the data.
It’s easier to see when predicted \(x_0\) is expressed through the reparameterization trick:</p>

\[\begin{align}
x_0^i = \kappa_4 z_i + \kappa_5 + \sigma_\theta(z_i) \epsilon
\end{align}\]

<p>The stochasticity in a recovered data sample now depends on the variance
\(\sigma_\theta(z_i)\), which takes a latent value as an input.
We have set a variance to be constant. In the previous example for the posterior estimation
we set the variance of \(z_i\) as \(\sigma_\phi(x_0) = \kappa_3 \lVert x_0 \rVert\).
Let’s parameterize the linear variance of \(p_\theta(x_0^i \vert z)\) similarly,
using already estimated posterior distribution variance \(\sigma_\phi(\cdot)\):</p>

\[\begin{align}
\sigma_\theta(z_i) = \kappa_6 \sigma_\phi(x_0) = \kappa_6 \kappa_3 \lVert x_0 \rVert
\end{align}\]

<p>where scalar \(\kappa_6\) intends to recover the data variance from the latent space variance.
The optimal variation in the latent space can, obviously, be different from the data space.
We can now describe the data probabilistically. We do reconstruct the original
value - obviously, the goal of maximizing the likelihood \(p_\theta(x_0^i \vert z)\)
for an observed data sample \(x_0^i\) is in place -
but also get an estimate of the confidence interval around it. Probabilistic
data description allows for accounting for noise and other uncertainties, for instance,
incurred by the non-perfect likelihood or posterior operators.</p>

<p>Once again, while this is a toy example,
principal steps have been described. For a more realistic example, refer to the VAE section.</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<p>We have covered how to encode the data into latents (learn \(q_\phi(z \vert x_0)\))
and how to decode the latents back to the data (learn \(p_\theta(x_0 \vert z)\)).
Both can be learned simultaneously.
As we see further, all generative models aim to predict the density \(p_\theta(\mathbf{x}_0)\) in one or another way
(<a href="https://books.google.com/books/about/Generative_Deep_Learning.html?id=Bkq8EAAAQBAJ&amp;redir_esc=y" target="_blank">Foster, Generative Deep Learning, 2022</a>: page 18). \(p_\theta(\mathbf{x}_0)\) corresponds to evidence or marginal likelihood, where \(\theta\) denotes that the reverse transformation
from latents to data has been learnt. It comes from the following marginalization:</p>

\[\begin{align}
p_\theta(\mathbf{x}_0) = \int d\mathbf{z}\ p_\theta(\mathbf{x}_0,\mathbf{z}) = \int d\mathbf{z}\ p_\theta(\mathbf{x}_0 \vert \mathbf{z})\ p(\mathbf{z})
\end{align}\]

<p>Recall our example of a neural network, which has a single hidden layer with non-linear activations?
It shows intractability of evidence \(p(\mathbf{x}_0)\). \(p_\theta(\mathbf{x}_0)\)
is, obviously, intractable as well, since the only difference is in \(\theta\)-notation.</p>

<p>We do not know the true data distribution
\(q(\mathbf{x}_0)\) (or \(p(\mathbf{x}_0)\): see <a href="/posts/2024/12/blog-post-DDPM-AppendixA/">Appendix A</a> for the full list of notations)
and ‘only’ have samples from it - our dataset.
However, we can maximize the probabilities \(p_\theta(\mathbf{x}_0)\), which our model assings to dataset samples, through ELBO maximization.
The better we estimate the latents (learn \(\phi\)) and do the reconstruction (learn \(\theta\)), the higher the
evidence will be for the samples of our dataset at hand.
The objective of maximizing the probabilities, which our model (\(\phi\) and \(\theta\))
assigns to registered observations \(p_\theta(\mathbf{x}_0)\), has a very well-known name - Maximum Likelihood Estimation
(MLE; <a href="https://arxiv.org/pdf/1906.02691" target="_blank">Kingma and Welling, 2019</a>: page 5, paragraph 1).</p>

<!--<details class="expandable-box">
    <summary>A note on why evidence in ELBO corresponds to likelihood in MLE</summary>
    
<p><a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., (2016)</a> (Chapter 5, page 130, equation (5.59))
defines Maximum Likelihood Estimation as:</p>

\[\begin{align}
\mathbf{\theta}_{\text{ML}} = \arg \max_{\mathbf{\theta}} \mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)} [ \log p_{\mathbf{\theta}}(\mathbf{x}_0) ] = \arg \max_{\mathbf{\theta}} \int d\mathbf{x}_0 \ q(\mathbf{x}_0)\ [ \log p_{\mathbf{\theta}}(\mathbf{x}_0) ]
\end{align}\]

<p>where the expectation is taken with respect to observed data samples \(\mathbf{x}_0 \sim q(\mathbf{x}_0)\)
(see ‘Mathematical expectation refresher’ box).
In plain language, we search for such model weights \(\theta\), which maximizes the probability our model assigns to data observations.
One way to interpret MLE is as minimizing the KL divergence between modeled and observed data distributions
(<a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., 2016</a>):</p>

\[\begin{align}
D_{\text{KL}}(q(\mathbf{x}_0) || p_{\mathbf{\theta}}(\mathbf{x}_0))=\mathbb{E}_{\mathbf{x}_0} [\log{q(\mathbf{x}_0)}-\log{p_{\mathbf{\theta}}(\mathbf{x}_0)}]
\end{align}\]

<p>Once again, true data distribution \(q(\mathbf{x}_0)\) is not known. But given the observed data distribution does not depend on the neural network weights \(\mathbf{\theta}\), our objective becomes minimization of the negative log-likelihood \(\mathbb{E}_{\mathbf{x}_0} [-\log{p_{\mathbf{\theta}}(\mathbf{x}_0)}]\). Negative log-likelihood minimization is referred to as NLL. Equivalently,
if we flip the sign before the log, we can seek the maximization objective. The latter is referred to MLE,
which we have introduced using \(\arg \max\) argument above. 
Do not get confused by the expectation over the data samples:
in the later sections ELBO objective functions are also averaged across observations.</p>

<p>We have so far established that MLE (or NLL) objectives correspond to maximization (or minimization) of
the positive (or negative) log of evidence \(\log[p_\theta(\cdot)]\). While evidence is intractable (see earlier),
we can use our bounds - lower (ELBO) for MLE
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>) or upper (VLB) for NLL
(<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023</a>;
<a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/" target="_blank">Lilian Weng blog</a>) - when solving for optimal
model parameters (\(\phi\) and \(\theta\)).</p>

<p>Let me do some language clarifications. In the case of ELBO, ‘the evidence is quantified … as the log likelihood of the observed data’
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>). Evidence corresponds to data likelihood? Yes, indeed.
Evidence \(p_\theta(\mathbf{x}_0)\) can also be referred to as marginal likelihood
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 7, paragraph 3;
<a href="https://arxiv.org/pdf/1906.02691" target="_blank">Kingma and Welling, 2019</a>: equation 1.13).
In this sense, we do indeed maximize the likelihood - marginal likelihood. However, let’s take a deeper look.</p>

<p>Bayesian Inference components, we have introduced in the beginning,
are defined with respect to the latents \(\mathbf{z}\). In other words, the goal is to infer the latents from the data. Usually in Machine Learning, we search for the neural networks weights \(\theta\) (typically denotes neural networks operating in the latent to data direction, or \(\phi\), which denotes latent estimation from data) - operator itself, e.g., <a href="https://ieeexplore.ieee.org/document/10530647" target="_blank">Chandra and Simmons, (2024)</a>. And the inference metrics are defined with respect to \(\theta\), i.e., we estimate the posterior distribution \(p(\theta|\mathbf{x}_0)\) of the model parameters
given the observations \(\mathbf{x}_0\). Throughout this blog series, by default, Bayesian Inference components are defined with respect to the latents, even when the operator is learnt too. While we search for the optimal weights \(\phi\) of the operator \(q_\phi(\mathbf{z}|\mathbf{x}_0)\), it approximates the true latent posterior distribution
\(p(\mathbf{z} \vert \mathbf{x}_0)\). Hence, the naming convention (see Appendix A for the full list of notations).</p>


</details>-->
<details class="expandable-box">
    <summary>
      <span class="math-title">A note on why evidence in ELBO corresponds to likelihood in MLE</span>
    </summary>
    <div class="expandable-content">
      
<p><a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., (2016)</a> (Chapter 5, page 130, equation (5.59))
defines Maximum Likelihood Estimation as:</p>

\[\begin{align}
\mathbf{\theta}_{\text{ML}} = \arg \max_{\mathbf{\theta}} \mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)} [ \log p_{\mathbf{\theta}}(\mathbf{x}_0) ] = \arg \max_{\mathbf{\theta}} \int d\mathbf{x}_0 \ q(\mathbf{x}_0)\ [ \log p_{\mathbf{\theta}}(\mathbf{x}_0) ]
\end{align}\]

<p>where the expectation is taken with respect to observed data samples \(\mathbf{x}_0 \sim q(\mathbf{x}_0)\)
(see ‘Mathematical expectation refresher’ box).
In plain language, we search for such model weights \(\theta\), which maximizes the probability our model assigns to data observations.
One way to interpret MLE is as minimizing the KL divergence between modeled and observed data distributions
(<a href="https://www.deeplearningbook.org/" target="_blank">Goodfellow et al., 2016</a>):</p>

\[\begin{align}
D_{\text{KL}}(q(\mathbf{x}_0) || p_{\mathbf{\theta}}(\mathbf{x}_0))=\mathbb{E}_{\mathbf{x}_0} [\log{q(\mathbf{x}_0)}-\log{p_{\mathbf{\theta}}(\mathbf{x}_0)}]
\end{align}\]

<p>Once again, true data distribution \(q(\mathbf{x}_0)\) is not known. But given the observed data distribution does not depend on the neural network weights \(\mathbf{\theta}\), our objective becomes minimization of the negative log-likelihood \(\mathbb{E}_{\mathbf{x}_0} [-\log{p_{\mathbf{\theta}}(\mathbf{x}_0)}]\). Negative log-likelihood minimization is referred to as NLL. Equivalently,
if we flip the sign before the log, we can seek the maximization objective. The latter is referred to MLE,
which we have introduced using \(\arg \max\) argument above. 
Do not get confused by the expectation over the data samples:
in the later sections ELBO objective functions are also averaged across observations.</p>

<p>We have so far established that MLE (or NLL) objectives correspond to maximization (or minimization) of
the positive (or negative) log of evidence \(\log[p_\theta(\cdot)]\). While evidence is intractable (see earlier),
we can use our bounds - lower (ELBO) for MLE
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>) or upper (VLB) for NLL
(<a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023</a>;
<a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/" target="_blank">Lilian Weng blog</a>) - when solving for optimal
model parameters (\(\phi\) and \(\theta\)).</p>

<p>Let me do some language clarifications. In the case of ELBO, ‘the evidence is quantified … as the log likelihood of the observed data’
(<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>). Evidence corresponds to data likelihood? Yes, indeed.
Evidence \(p_\theta(\mathbf{x}_0)\) can also be referred to as marginal likelihood
(<a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>: page 7, paragraph 3;
<a href="https://arxiv.org/pdf/1906.02691" target="_blank">Kingma and Welling, 2019</a>: equation 1.13).
In this sense, we do indeed maximize the likelihood - marginal likelihood. However, let’s take a deeper look.</p>

<p>Bayesian Inference components, we have introduced in the beginning,
are defined with respect to the latents \(\mathbf{z}\). In other words, the goal is to infer the latents from the data. Usually in Machine Learning, we search for the neural networks weights \(\theta\) (typically denotes neural networks operating in the latent to data direction, or \(\phi\), which denotes latent estimation from data) - operator itself, e.g., <a href="https://ieeexplore.ieee.org/document/10530647" target="_blank">Chandra and Simmons, (2024)</a>. And the inference metrics are defined with respect to \(\theta\), i.e., we estimate the posterior distribution \(p(\theta|\mathbf{x}_0)\) of the model parameters
given the observations \(\mathbf{x}_0\). Throughout this blog series, by default, Bayesian Inference components are defined with respect to the latents, even when the operator is learnt too. While we search for the optimal weights \(\phi\) of the operator \(q_\phi(\mathbf{z}|\mathbf{x}_0)\), it approximates the true latent posterior distribution
\(p(\mathbf{z} \vert \mathbf{x}_0)\). Hence, the naming convention (see Appendix A for the full list of notations).</p>


    </div>
  </details>

<script>
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.querySelector('.math-title')]);
</script>

<p>We have covered what latent variables are and how to estimate them in practice. Using a toy linear relationship example,
we pose the problem in different frameworks: starting with Inverse Theory, which gives a single deterministic solution;
continuing with MCMC for latent estimation, allowing for probabilistic description of the estimates; and finally deriving the ELBO - the key objective of
the Variational Inference framework - to approximate the posterior distribution of the latents.
We then learn the reverse procedure of data reconstruction/generation.</p>

<p>Interestingly enough, in diffusion modeling we do not optimize for latents and do not search for an optimal data-to-latent encoder. There is
a hierarchy of latent spaces, each level of which corresponds to an input image with a certain amount of
noise. The deepest latent space corresponds to Gaussian noise, and thus does not have any particular
physical meaning. The procedure of moving in the opposite direction, however, carries the burden of data
reconstruction or new sample generation from seemingly meaningless noisy samples.</p>

<p>In the next section, we formally discuss the generative models, and, in particular, VAEs, references to which I have already been making all along.
As such, VAE encoder, typically denoted as \(q_\phi(\cdot)\), corresponds to the approximate latent posterior and describes data translation into the latent space. The decoder in its turn, is denoted as \(p_\theta(\cdot)\), which in our context corresponds to likelihood, and describes data reconstruction/generation from the latent space.</p>

<p>In essence, we have two directions of information processing: from data to latents (forward) and from latents to data (reverse). We learn both transition operators and optimal hidden data characteristics. Even in diffusion models, with their hierarchy of latents, we move information back and forth while preserving its most relevant parts to learn the underlying statistics of our dataset.</p>]]></content><author><name>Derek Frost</name><email>derfrost1861@gmail.com</email></author><category term="DDPM" /><summary type="html"><![CDATA[Learning data distribution involves identifying ‘key factors’, which inherently ‘control’ the characteristics of our observations. The controlling relationship can either be statistical, or based on physical laws, where we understand precisely how these factors manifest in data. We discuss these ‘key factors’, commonly referred to as latent variables, in detail below. First, we discuss their estimation from data using Variational Inference (VI). Then, we describe the opposite direction - data reconstruction and generation from latent variables.]]></summary></entry><entry><title type="html">DDPMs: Introduction</title><link href="https://derfrost1861.github.io/posts/2024/12/blog-post-DDPM-Intro/" rel="alternate" type="text/html" title="DDPMs: Introduction" /><published>2025-01-01T00:00:00-08:00</published><updated>2025-01-01T00:00:00-08:00</updated><id>https://derfrost1861.github.io/posts/2024/12/DDPM-Intro</id><content type="html" xml:base="https://derfrost1861.github.io/posts/2024/12/blog-post-DDPM-Intro/"><![CDATA[<p>By this point, you most probably have heard that <em>Stable Diffusion</em> gradually noises the image until it becomes not recognizable.
After that, you reverse the process and create images. Let’s say, you are interested in creating schematic Swiss-roll images
(I mean… everyone likes cute cats but how about a variety?). In this case, forward and reverse diffusion processes could look like below:</p>

<p><img src="/images/posts/stableDiffusionBite/forwardNreverseDiffustionPPT.001.png" alt="" />
<em>Figure 1: adapted after <a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., (2015)</a>: shows forward (noising) and reverse (de-noising) diffusion trajectories. Forward gradually adds noise, whereas reverse sequentially removes it, recovering data structure. \(t\) is “time” of diffusion with \(t=T\)
corresponding to Gaussian noise (think of it as completed diffusion process) and \(t=0\) - to input/recovered data (diffusion has not started or is “reverted”).</em></p>

<p>While there are many wonderful tutorials online, I attempt to provide an explanation of the process based on a perhaps
<strong>obvious intuition</strong>, which, at the same time, helped me clear my personal understanding of the subject.
<strong>Obvious intuition</strong> is a term I use to refer to a simple plain language explanation of a concept.
While sometimes it can be overly simplified, it is how I would “explain like I am five”
(well… I am definitely older than five, and the explanation is a bit more complex too).
Detailed mathematical expressions are provided as well, with complexity increasing towards the end.</p>

<p>There are two main goals for this blog series. First, provide an intermediate level explanation
<strong>without</strong> skipping any steps. Often details are omitted because they are either too ‘trivial’ for experts
or too ‘hard’ to explain in the bird-eye-view reviews. 
Second, describe diffusion-related concepts within the same context. Often, I would be referred
to different resources to remind myself about VAEs (Variational Autoencoders),
for instance. Here, diffusion building blocks are gathered in one place.
I do hope these goals are met at least partially.</p>

<p>Among many giants there are two, on whose shoulders the narration stands:
the papers by <a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., (2015)</a>
and <a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., (2020)</a>.
The first paper draws inspiration from thermodynamics to introduce diffusion models,
the second simplifies their training framework and introduces Denoising Diffusion Probabilistic Models (DDPMs),
which are the focus in this blog series. While DDPMs are not that hyped on their own, they are fundamental to 
Latent Diffusion Models (LDM), where they operate in the compressed image space, i.e., so-called latent space,
and not in the input image pixel space directly
(<a href="https://arxiv.org/abs/2112.10752" target="_blank">Rombach et al., 2022</a>).
In its turn, a much more ‘famous’ <em>Stable Diffusion</em> model follows the LDM’s latent space setup
(<a href="https://arxiv.org/pdf/2403.03206" target="_blank">Esser et al., 2024</a>: page 4, second 4, paragraph 2).</p>

<p>The <em>Stable Diffusion</em> model consists of three construction blocks: obviously, DDPM
(based on a denoising neural network (<a href="https://en.wikipedia.org/wiki/U-Net" target="_blank">UNet</a>)), but also a text encoder,
and an autoencoder (<a href="https://openreview.net/forum?id=zzboa1TtNI&amp;noteId=7MmUrGHtQp" target="_blank">Perez et al., 2023</a>).
Autoencoder performs above-mentioned image compression
(<a href="https://arxiv.org/abs/2112.10752" target="_blank">Rombach et al., 2022</a>):
it reduces original image dimensions and allows for significant computational cost
reduction since diffusion is now run in a smaller domain.
After diffusion completes, original image dimensions are recovered by a decoder.
A text encoder, in its turn, allows for conditioning
on a text prompt, which guides reverse diffusion to produce image compositions as requested by the user.
<em>Stable Diffusion</em> model without the autoencoder and with no text conditioning
(e.g., null text prompt (empty string, for instance) is given, which makes it an unconditional diffusion model)
basically corresponds to DDPM applied in the pixel space
(keep in mind of course, <em>Stable Diffusion</em> is trained to operate in the latent space).
Below, I predominantly focus on Denoising Diffusion Probabilistic Model,
and both image compression and conditioning on text are out of scope of this blog series.</p>

<p>I start with a brief introduction (below), where I draw an <strong>obvious</strong> connection with a physical diffusion process. We then
proceed with <a href="/posts/2024/12/blog-post-DDPM-Stat-Founds/">Statistical Foundations</a> chapter, where
latent variables - key hidden factors controlling our data characteristics - are introduced and searched for
using Variational Inference (VI)\(^*\). Within the VI framework, we optimize for approximate probabilities,
which statistically relate data and latents, thus enabling data generation/reconstruction by generative models, discussed
in the following section. We discuss VAEs, and through Hierarchical VAEs (HVAEs) work our way up to
Markovian HVAEs, which are a close DDPM sibling with encoders and decoders
operating on latent hierarchies with dependence on the previous latent level only.
With all the building blocks described, we proceed with DDPMs.
First, we focus on a single forward (noising) and reverse (de-noising) diffusion operators.
We then formulate an objective function to optimize the reverse process as a whole.
Finally, we condense the objective function to a ‘simple’ noise estimation problem.
Brief summary of the series follows. Notations are provided in <a href="/posts/2024/12/blog-post-DDPM-AppendixA/">Appendix A</a>.</p>

<p><em>\(^*\)as of February 2025 this is when the published material ends.</em></p>

<p>Let me introduce someone special before proceeding - meet ink-tolerant Gold Fish Emma. She greatly helps us
in the ‘Basic Intuition’ section, where without her we would lose track of the potential reverse trajectories.
Emma also accompanies us throughout the blog series, asks for clarifications, where necessary
and, overall, holds me accountable. Regarding her presence in a tank with ink - no worries, she is ink tolerant
and in fact enjoys changing colors temporarily. Also the diffusion processes we consider are slow,
which means the water temperature is very comfortable for her (see the relation between diffusion speed and temperature below).</p>

<p><img src="/images/posts/stableDiffusionBite/editedGoldFishEmma.png" alt="" />
<em>Figure with no number: an artistic portrait of ink-tolerant Gold Fish Emma using DALL⋅E 2.
No real photographs are used for the protection of privacy.</em></p>

<h1 id="introduction">Introduction</h1>

<p>The importance of diffusion models comes from their ability to overcome limitations of GANs and VAEs.
In particular, generation process in the latter two models may regress towards the “average” image/sample of the dataset
(so-called mode collapse) resulting in a low variety of produced outputs
(<a href="https://www.youtube.com/watch?v=FHeCmnNe0P8&amp;t=3076s&gt;." target="_blank">IntroToDeepLearning.com</a>).
<a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., (2015)</a>
formulate advantages of diffusion models as: “Most existing density estimation techniques must sacrifice
modeling power in order to stay tractable and efficient,” while diffusion models, given the number
of steps is made large, can “learn a fit to any data distribution” and remain
“tractable to train, exactly sample from, and evaluate.”
My <strong>obvious intuition</strong> is as follows: it can be easier to approximate a complex data distribution
by a set of small steps as opposed to learning a complex yet single approximator.</p>

<h1 id="basic-intuition">Basic Intuition</h1>

<p>The approach is proposed by <a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., (2015)</a>
and referred to as Diffusion Probabilistic Models. The authors propose a two-step process: first systematically
and slowly destroying structure in input data, then learning a reverse process to restore this structure.
The method uses a Markov chain, which, among many other applications, models Brownian motion.
For Brownian motion (and Markov chains), the future state of the particle (event)
depends only on the current state of the event, not on the history of previous states.
A change of state is controlled by transition probabilities,
which can be described by Gaussians. Brownian motion is responsible for the phenomenon of diffusion
(<a href="https://galton.uchicago.edu/~lalley/Courses/313/WienerProcess.pdf" target="_blank">University of Chicago Statistics Course</a>).</p>

<p>The systematic and slow process of destroying structure in data, referred to as the forward diffusion process,
corresponds to a set of Gaussian transitions describing Brownian motion. The forward process is a Markov chain
that gradually adds Gaussian noise to the data (<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>).
To continue the <strong>obvious analogy</strong>, think of the input image as initial concentration of paint or ink.
With time proceeding, initial concentration (by the means of Brownian motion) evens out into a uniform
distribution of pixels corresponding to a normal distribution.</p>

<p>A single Brownian step of the <em>forward</em> process can be mathematically described as a Gaussian transition
(<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>), where the result
depends on the previous state of Brownian motion. In other words, <strong>obviously</strong>, how ink is distributed
at a given diffusion time depends on how it was distributed a time step before. The distribution is described
by a Gaussian function with a mean depending on a previous step. Notice, it only depends on a previous time
step and does not use information from previous steps (Markov chain property). So, it is not like we “just”
add some random noise to images: we model physical diffusion process by mathematically reproducing a set of Brownian motion steps.
Well… We end up noising the images, but using Gaussian transitions, each of which takes a previous noisy version as an input.</p>

<p>If we can reverse the above process, we can generate an image from a Gaussian noise input
(<a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/#:~:text=The%20data%20sample,isotropic%20Gaussian%20distribution." target="_blank">Lilian Weng blog</a>).
Here, the <strong>obvious analogy</strong> corresponds to the recovery of the initial ink concentration
after it has diluted in water over time. How would we construct such a <em>reverse</em> process?
If forward diffusion is happening “slowly” (an <strong>obvious</strong> analogy would be
ink diffusion in relatively cold water as opposed to boiling) or Brownian jumps are small or, equivalently,
you do not noise an image too much at each step, then you could backtrack or <em>revert</em> a Brownian step and
find out which previous state (less noisy image) caused current state (noisier image). More formally,
if a variance is small enough, reverse diffusion process has the identical functional form of the forward process
(<a href="https://arxiv.org/abs/1503.03585" target="_blank">Sohl-Dickstein et al., 2015</a>).
This means that the reverse process of recovering data structure corresponds to Brownian motion with
a Gaussian transition probability as well.</p>

<p>So, we need to revert a Brownian step… Unfortunately, unlike the forward case,
in which we add noise to a previous less noisy image, this is, in general, an intractable problem.
Recovering a previous state of the system based on the current observation is an ill-posed problem with multiple solutions:
multiple Brownian steps from multiple previous states could all produce the same state of the system at an observed diffusion
time of \(t\) (see schematic in Figure 2, where alongside a true particle distribution at \(t-1\),
Brownian steps from two other hypothetical concentrations can result in the distribution at time \(t\)).</p>

<p>This challenge has two aspects: first, the stochasticity of transitions, each of which has a certain probability and hence the uncertainty.
Second, the systematic direction of denoising: if we don’t know what is behind noise, how would we know the correct reverse transition?
However, if a noise-free image, on which our forward diffusion has operated, is known, then there is a closed-form expression
for a reverse Brownian step (see <a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>, equation 7).
While, in general, reverting diffusion steps is intractable: there are just too many system states you could arrive at the
current step from (see <a href="https://arxiv.org/abs/2209.04747" target="_blank">Croitoru et al., 2023</a>, page 4,
second to last paragraph), - when you know the original input, you know what noising trajectory took place and hence
we get a particular reverse Brownian jump. Once again, the reverse process becomes tractable when conditioned
on input images (<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>).</p>

<p><img src="/images/posts/stableDiffusionBite/forwardAndReverseTrajs.001.png" alt="" />
<em>Figure 2: A schematic showing reverse process intractability:
multiple previous steps could result in the current observed state of diffusion at time \(t\).
Goldfish Emma sees the forward noising process of a particular input image.
Thus, Emma can help us find the reverse Brownian jump, which, amongst many other possible system states at
\(t-1\), corresponds to a given image she saw during noising. In this sense, if we check with her,
we can collapse the manifold of all reverse diffusion steps to the one, which takes the place when we noise our given input image.
Formally, “forward process posteriors… are tractable when conditioned on”
input data (<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>;
think of forward process posteriors as reverse diffusion steps).
Emma uses strict mathematical logic when helping us to choose correct reverse Brownian steps, which I share further down the text.</em></p>

<p>Interestingly enough, we have just briefly described how diffusion models are trained
under a ‘simple’ de-noising objective. We have all the ingredients to train a de-noising neural network and then predict images from noise.
We know the form of the forward noising process and can create de-noised training labels from the reverse process,
conditioned on input images (<a href="https://arxiv.org/abs/2208.11970" target="_blank">Luo, 2022</a>). Concisely,
to tackle general reverse process intractability, you train on a tractable reverse process, conditioned on input data,
and learn to approximate the distribution of input images. The trained network is then applied in a number of steps to revert the diffusion process and create an image from noise: step by step we reverse the noising process until a Swiss-roll image shows up. You now have sampled an image from the learnt distribution.</p>

<p>I hope this high-level sketch is illustrative and suggest to proceed with the <a href="/posts/2024/12/blog-post-DDPM-Stat-Founds/">Statistical Foundations</a>
chapter, where we start formulating diffusion model building blocks with more mathematical rigor.</p>]]></content><author><name>Derek Frost</name><email>derfrost1861@gmail.com</email></author><category term="Stable Diffusion" /><category term="Diffusion model" /><category term="Denoising Diffusion Probabilistic Model" /><summary type="html"><![CDATA[By this point, you most probably have heard that Stable Diffusion gradually noises the image until it becomes not recognizable. After that, you reverse the process and create images. Let’s say, you are interested in creating schematic Swiss-roll images (I mean… everyone likes cute cats but how about a variety?). In this case, forward and reverse diffusion processes could look like below:]]></summary></entry><entry><title type="html">DDPMs: Appendix A - Notations</title><link href="https://derfrost1861.github.io/posts/2024/12/blog-post-DDPM-AppendixA/" rel="alternate" type="text/html" title="DDPMs: Appendix A - Notations" /><published>2024-12-31T00:00:00-08:00</published><updated>2024-12-31T00:00:00-08:00</updated><id>https://derfrost1861.github.io/posts/2024/12/DDPM-AppendixA</id><content type="html" xml:base="https://derfrost1861.github.io/posts/2024/12/blog-post-DDPM-AppendixA/"><![CDATA[<p>Here, notations used in the DDPM blog series are defined. Please keep in mind, some notations are introduced later in text and it’s OK to not understand them all right away
(e.g., when you just start reading).</p>

<hr />

<p><strong>Notation for input and latents</strong></p>

<ul>
  <li>
    <p>\(\mathbf{x}_0\) - noise-free input images.
Together input images form a dataset \(\mathbf{x}_0=\{\mathbf{x}_0^i\}_{i=0:N-1}\) - a collection of observation points \(\mathbf{x}_0^i\) (one point - one image) 
with \(i=0:N-1\) denoting a sample number. $N$ is how many samples or observations (e.g., images) we have overall. Input images have three color channels (e.g., RGB) and a two-dimensional pixel space (e.g., \(\mathbf{x}_0^i=\{x\}_{jk}^{i}\), where \(j=0:\text{height}-1\) and \(k=0:\text{width}-1\));</p>
  </li>
  <li>$\mathbf{z}$ - latent variables; depending on the context, can denote both: single-level latent (e.g., in VAE) and an hierarchy;</li>
  <li>$\mathbf{z}_i$ - single latent level: for models with hierarchies of latents index $i$ denotes the level of the latent ($i=0:T$);
    <ul>
      <li>$\mathbf{z}_T$ - deepest level latent corresponding to last diffusion time $T$ ($\mathbf{z}_i$ with $i=T$);</li>
      <li>$\mathbf{z}_0$ - shallowest level latent corresponding to diffusion input ($\mathbf{z}_i$ with $i=0$): $\mathbf{z}_0=\mathbf{x}_0$; typically, only $\mathbf{x}_0$-notation is used;</li>
    </ul>
  </li>
  <li>$\mathbf{z}_{1:T}$ - hierarchy of latents $(\mathbf{z}_1,\mathbf{z}_2,…,\mathbf{z}_T)$. The deepest latent of the hierarchy can also correspond to $i&lt;T$, e.g.,
$(\mathbf{z}_1,\mathbf{z}_2,\mathbf{z}_3)$ hierarchy;</li>
</ul>

<p>Hierarchical latents can be denoted in various ways. In variational inference we use \(\mathbf{z}\) to denote latents regardless of a particular form (hierarchical, Markovian or not…). While typically this notation does denote only a single latent, as in a regular VAE (\(\mathbf{z}=\mathbf{z}_1\)), it can also represent a collection or an hierarchy \(\mathbf{z}=(\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_N)\) used in joint and conditional probabilities of, for instance, MHVAEs. In diffusion, we always assume a hierarchy of latents and use the colon notation above. For instance, for illustration purposes, let’s write the conditional probability of latents given a data sample:</p>

\[\begin{align}
p(\mathbf{z}_{1:T}|\mathbf{x}_0)=\frac{p(\mathbf{z}_{1:T},\mathbf{x}_0)}{p(\mathbf{x}_0)}
\end{align}\]

<p>with evidence \(p(\mathbf{x}_0)=\int d\mathbf{z}_{1:T}\ p(\mathbf{z}_{1:T},\mathbf{x}_0) = \int d\mathbf{z}_{1}...d\mathbf{z}_T\ p(\mathbf{z}_{1},...,\mathbf{z}_{T},\mathbf{x}_0)\).</p>

<hr />

<p><strong>Notation for probabilities</strong></p>

<p>Probabilities are denoted by \(p(\cdot)\) and \(q(\cdot)\). Both \(p(\cdot)\) and \(q(\cdot)\) can represent different quantities depending on their arguments and indices.
In particular:</p>

<ul>
  <li>
    <p>$p(\mathbf{z} \vert \mathbf{x}_0)$ - true latent posterior density given observations $\mathbf{x}_0$. Used in Variational Inference introduction. Same quantity as $q(\mathbf{z} \vert \mathbf{x}_0)$;</p>
  </li>
  <li>\(q(\mathbf{z} \vert \mathbf{x}_0)\) - true latent posterior density given observations $\mathbf{x}_0$. Used in forward diffusion process. Same quantity as $p(\mathbf{z} \vert \mathbf{x}_0)$;
    <ul>
      <li>\(q(\mathbf{z}_{i+1} \vert \mathbf{z}_{i})\) - same as above but denotes the posterior density of the next level ($i+1$) latent given the previous level ($i$) latent; denotes a single Gaussian transition (aka a single Brownian jump) between above mentioned latent levels in the forward diffusion process; corresponds to a single noising step in DDPM; has an analytical expression (unlike its approximate counterpart
$q_\phi(\mathbf{z}_{i+1} \vert \mathbf{z}_i)$ used in MHVAE (see below));</li>
    </ul>
  </li>
  <li>\(q(\mathbf{z}_{i} \vert \mathbf{z}_{i+1})\) - true diffusion forward process posterior (denoising); denotes true forward diffusion process,
which is turned back in time, hence the transition from latent level $i+1$ to $i$, and is intractable (see Reverse Diffusion Process section (working progress and not published as of Feb 2025));
    <ul>
      <li>\(q(\mathbf{z}_{i} \vert \mathbf{z}_{i+1}, \mathbf{x}_0)\) - same as above except conditioned on an input data sample $\mathbf{x}_0$;
conditioning on input makes the quantity tractable (see Reverse Diffusion Process section (working progress and not published as of Feb 2025)); referred to as forward process posterior in the text;</li>
    </ul>
  </li>
  <li>$q_\phi(\mathbf{z} \vert \mathbf{x}_0)$ - approximate latent posterior density given observations $\mathbf{x}_0$. Corresponds to a neural network with weights $\phi$; denotes VAE’s encoder;
    <ul>
      <li>\(q_\phi(\mathbf{z}_{i+1} \vert \mathbf{z}_{i})\) - same as above, except denoting the posterior density of the next level ($i+1$) latent given the previous level ($i$) latent; used in MHVAE’s encoder;</li>
    </ul>
  </li>
  <li>$p_\theta(\mathbf{x}_0 \vert \mathbf{z})$ - approximate likelihood given latents $\mathbf{z}$. Corresponds to a neural network with weights $\theta$; denotes VAE’s decoder;
    <ul>
      <li>\(p_\theta(\mathbf{z}_{i} \vert \mathbf{z}_{i+1})\) - same as above, except denoting the likelihood of the previous level ($i$) latent given the higher level ($i+1$) latent; denotes MHVAE’s decoder;</li>
    </ul>
  </li>
  <li>
    <p>$g(\mathbf{z})$ - aggregate approximate posterior latent distribution; aims to reproduce $p(\mathbf{z} \vert \mathbf{x}_0)$; optimal approximation is denoted by $^*$; only used once in <a href="/posts/2024/12/blog-post-DDPM-Stat-Founds/">Statistical Foundations</a> chapter (changed from $q(\mathbf{z})$ (as in <a href="https://arxiv.org/abs/1601.00670" target="_blank">Blei et al., 2018</a>) to $g(\mathbf{z})$ for consistency);</p>
  </li>
  <li>$p(\mathbf{z})$ - prior probability density of the latent variable $\mathbf{z}$; typically, corresponds to a zero-mean unit-variance Gaussian; used in VAE;
    <ul>
      <li>$p(\mathbf{z}_T)$ - same as above but a probability distribution of the deepest level latent (typically, Gaussian); used in DDPM and MHVAE;</li>
    </ul>
  </li>
  <li>
    <p>$q(\mathbf{x}_0)$ - true data probability density (notation by <a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a>); the quantity we aim to learn (it is unknown to us); our dataset $\mathbf{x}_0$ corresponds to a finite set of its samples $\mathbf{x}_0 \sim q(\mathbf{x}_0)$;</p>
  </li>
  <li>
    <p>$p(\mathbf{x}_0)$ - true data probability density as $q(\mathbf{x}_0)$, but infers latents’ marginalization $\int d\mathbf{z}\ p(\mathbf{x}_0,\mathbf{z})$ and, thus, corresponds to evidence; intractable due to the high-dimensional integration over all possible values of all possible latent variables (see <a href="/posts/2024/12/blog-post-DDPM-Stat-Founds/">Statistical Foundations</a> chapter);</p>
  </li>
  <li>$q(\mathbf{z}_1, \mathbf{z}_2, … , \mathbf{z}_T \vert \mathbf{x}_0)$ - forward diffusion process;
    <ul>
      <li>$q_\phi(\mathbf{z}_1, \mathbf{z}_2, … , \mathbf{z}_T \vert \mathbf{x}_0)$ - data encoding process in MHVAE;</li>
    </ul>
  </li>
  <li>
    <p>$p_\theta(\mathbf{z}_1, \mathbf{z}_2, … , \mathbf{z}_T, \mathbf{x}_0)$ - joint distribution between latents and data, which corresponds to a generative model aka process in DDPM and MHVAE (see Generative Model section (working progress and not published as of Feb 2025));</p>
  </li>
  <li>\(p_\theta(\mathbf{x}_0)\) - approximated data distribution; corresponds to the learnt reverse process marginalized over latents
\(p_\theta(\mathbf{x}_0) = \int d\mathbf{z}_{1:T} p_\theta(\mathbf{z}_{1:T}, \mathbf{x}_0)\); evidence with respect to latents $\mathbf{z}$. Intractable as its true counterpart $p(\mathbf{x}_0)$;</li>
</ul>

<h2 id="distinction-between-pcdot-and-qcdot">Distinction between $p(\cdot)$ and $q(\cdot)$</h2>

<ul>
  <li>
    <p>\(q(\cdot)\) - denotes posterior probabilities (with the exception of $q(\mathbf{x}_0)$, which denotes true data distribution$^{*}$) and corresponds to transformations mapping data to latents and shallower latents to deeper latents (except forward process posterior, which is still related to forward process except turned back in time); denotes quantities associated with the forward diffusion process;</p>
  </li>
  <li>
    <p>\(p(\cdot)\) - denotes likelihoods (with the exception of $p(\mathbf{z}|\mathbf{x}_0)$, $p(\mathbf{x}_0)$
(see $^{*}$) and $p(\mathbf{z})$, $p(\mathbf{z}_T)$) associated with a generative model $p(\mathbf{z},\mathbf{x}_0)$
and corresponds to transformations mapping latents to data and deeper latents to shallower latents; denotes quantities associated with the reverse diffusion process</p>
  </li>
</ul>

<p>In other words, $q(\cdot)$ (and associated neural net weights $\phi$,
e.g., VAE’s and MHVAE’s encoders)
denotes quantities corresponding to operations in the data-to-latent direction;
$p(\cdot)$ (and associated neural net weights $\theta$, e.g., decoders in VAE, MHVAE and reverse diffusion process) denotes quantities
corresponding to operations in the latent-to-data direction.</p>

<p>Obviously, we do not use $p(\cdot)$ and $q(\cdot)$ interchangeably,
and expand the corresponding quantities using a consistent notation
e.g.,</p>

\[\begin{align}
q(z|x_0)=p(z,x_0)/q(x_0)
\end{align}\]

<p>is wrong and should be corrected to</p>

\[\begin{align}
q(z|x_0)=q(z,x_0)/q(x_0)
\end{align}\]

<p><em>$^{*}$ Our input data \(\mathbf{x}_0\) distribution is denoted by \(\mathbf{x}_0 \sim q(\mathbf{x}_0)\)
(<a href="https://arxiv.org/abs/2006.11239" target="_blank">Ho et al., 2020</a> page 2). While it essentially
describes the same quantity as \(p(\mathbf{x}_0)\), the latter comes from marginalization of latent variables</em></p>

\[\begin{align}
\int d\mathbf{z}_{1:T}\ p(\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T,\mathbf{x}_{0}) = p(\mathbf{x}_0)
\end{align}\]

<p><em>Hence, while both \(p(\mathbf{x}_0)\) and \(q(\mathbf{x}_0)\) describe data distribution, \(p(\mathbf{x}_0)\)
does it in the context of image recovery/generation from latents and represents model’s learned approximation of data.
\(q(\mathbf{x}_0)\) pertains to raw input data: we, in general, do not know its distribution besides a set of samples in our hands.
Since samples are input in the forward noising process, notation of $q(\cdot)$ is used.</em></p>

<h2 id="bayesian-inference-notations-in-the-context-of-mle">Bayesian Inference notations in the context of MLE</h2>
<p>As mentioned in ‘A note on why evidence in ELBO corresponds to likelihood in MLE’
in <a href="/posts/2024/12/blog-post-DDPM-Stat-Founds/">Statistical Foundations</a> chapter, Bayesian Inference notations above are defined with respect to latents \(\mathbf{z}\). In particular, \(p(\mathbf{x}_0)\) and its learnt counterpart \(p_\theta(\mathbf{x}_0)\) correspond to evidence:
result of marginalization over latents \(\int d\mathbf{z}\ p(\mathbf{x}_0,\mathbf{z})\) and
\(\int d\mathbf{z}\ p_\theta(\mathbf{x}_0,\mathbf{z})\) correspondingly. At the same time, with respect to learnt parameters \(\theta\),
\(p_\theta(\mathbf{x}_0)\) is likelihood: data probability given a set of neural network parameters. By
maximizing the Evidence Lower Bound (ELBO), we obviously maximize evidence (with respect to latents),
which with respect to \(\theta\) is likelihood - hence, the MLE objective (see ‘A note on why evidence in ELBO corresponds to likelihood in MLE’).</p>

<h2 id="additional-notation-clarification-for-vae-mhvae-and-ddpm">Additional notation clarification for VAE, MHVAE and DDPM</h2>

<p>With many notations introduced and interchangeably used in different contexts, let me additionally discuss their utilization in different models: VAE, MHVAE, and, most importantly, diffusion.</p>

<hr />

<p><strong>Notations related to <em>forward</em> process (VAE, MHVAE encoders)</strong></p>

<p>\(q(\cdot)\) denotes forward-process-related probabilities: mapping the input \(\mathbf{x}_0\) to a latent space \(\mathbf{z}\).
It typically takes the form of \(q(\mathbf{z}|\mathbf{x}_0)\) and approximates true but intractable posterior distribution \(p(\mathbf{z}|\mathbf{x}_0)\)
of latents \(\mathbf{z}\) given a dataset \(\mathbf{x}_0\).
For VAE, a posterior \(q_{\phi}(\mathbf{z}|\mathbf{x}_0)\) is learnt, with \(\phi\) denoting neural network weights. For MHVAE, it takes the form</p>

\[\begin{align}
q_\phi(\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T|\mathbf{x}_{0}) = q_\phi(\mathbf{z}_1 \vert \mathbf{x}_{0}) \prod^T_{t=2} q_\phi(\mathbf{z}_{t} \vert \mathbf{z}_{t-1})
\end{align}\]

<p>For diffusion, as mentioned above and shown below, encoding the input into a latent space is not learnt - thus, \(q(\cdot)\) with no \(\phi\) subscripts. Moreover, we can <em>reverse</em> the <em>forward process</em> analytically and generate training examples for the actual <em>reverse process</em>. Yes, I myself find the latter sentence very confusing but, hopefully, diffusion section sheds some light to show us the exit from the cave. For now, simply note that for two neighbour levels of latents \(\mathbf{z}_i\) and \(\mathbf{z}_{i+1}\), we can analytically revert \(q(\mathbf{z}_{i+1}|\mathbf{z}_i)\)
into \(q(\mathbf{z}_{i}|\mathbf{z}_{i+1},\mathbf{x}_0)\)
if we know the input \(\mathbf{x}_0\). And obviously, since \(q(\mathbf{z}_{i}|\mathbf{z}_{i+1},\mathbf{x}_0)\)
is a reverse of the forward it is still denoted by \(q(\cdot)\).</p>

<hr />

<p><strong>Notations related to <em>reverse</em> and <em>generative</em> processes (VAE, MHVAE decoders)</strong></p>

<p>\(p(\cdot)\) denotes reverse-process-related and generative modeling probabilities. For mapping latents \(\mathbf{z}\) back to the input domain \(\mathbf{x}_0\)
it typically takes the form of \(p_{\theta}(\mathbf{x}_0|\mathbf{z})\). When denoting a joint probability, and thus a generative model, it typically takes the form \(p_\theta(\mathbf{x}_0,\mathbf{z})\) where subscript \(\theta\) denotes learnt neural network weights (see, for instance, decoder in the VAE context). Since a generative model is a combination of a decoder with a prior (sampling aggregate posterior) it takes the form: \(p_\theta(\mathbf{x}_0,\mathbf{z})=p_\theta(\mathbf{x}_0|\mathbf{z})p(\mathbf{z})\). For MHVAE we have the following generative model:</p>

\[\begin{align}
p_\theta(\mathbf{x}_{0},\mathbf{z}_1,\mathbf{z}_2,...,\mathbf{z}_T) = p(\mathbf{z}_T) p_\theta(\mathbf{x}_{0} \vert \mathbf{z}_1) \prod^T_{t=2} p_\theta(\mathbf{z}_{t-1} \vert \mathbf{z}_t)
\end{align}\]

<p>DDPM’s generative model has the same form. The only difference is \(p(\mathbf{z}_T)\),
which is Gaussian for DDPM. In MHVAE, the deepest latent \(\mathbf{z}_T\)
is sampled from \(q_\phi(\mathbf{z}_T \vert \mathbf{z}_{T-1})\)
(output of the encoder applied to a previous-to-the-deepest latent $\mathbf{z}_{T-1}$),
which serves as the prior for the generative process.</p>]]></content><author><name>Derek Frost</name><email>derfrost1861@gmail.com</email></author><category term="DDPM" /><summary type="html"><![CDATA[Here, notations used in the DDPM blog series are defined. Please keep in mind, some notations are introduced later in text and it’s OK to not understand them all right away (e.g., when you just start reading).]]></summary></entry></feed>