<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.0.1">Jekyll</generator><link href="havu73.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="havu73.github.io/" rel="alternate" type="text/html" /><updated>2025-01-24T01:35:22+00:00</updated><id>havu73.github.io/feed.xml</id><entry><title type="html">Characteristic function– an example use case</title><link href="havu73.github.io/blog/2025/01/20/characteristic_function/" rel="alternate" type="text/html" title="Characteristic function– an example use case" /><published>2025-01-20T00:00:00+00:00</published><updated>2025-01-20T00:00:00+00:00</updated><id>havu73.github.io/blog/2025/01/20/characteristic_function</id><content type="html" xml:base="havu73.github.io/blog/2025/01/20/characteristic_function/"><![CDATA[<h2 id="the-starting-point-a-very-practical-question">The starting point: A very practical question</h2>
<p>The inspiration for this blog post is the paper Tam and Engelhardt, 2025 <a class="citation" href="#edric2024embedCharacteristicScore">(Tam, 2025)</a>, from my postdoc lab under Dr. Barbara Engelhardt. The paper tries to address a highly relevant question in current machine learning research: <strong>Given the rise of all these foundation  models that generate texts, images, etc., are there any theoretically sound ways to evaluate whether data generated by these models are similarly distributed to real data?</strong>. In more plain language: Are the texts/ images generated by GPT-/DallE-type models similar to the texts/ images that we have been generating for years? In more technical language: Are the distributions of the data generated by the foundation models similar to the distributions of the real data?</p>

<p>Now, if our data is simply numerical values derived from some well-defined distributions such as $Normal(\mu, \sigma$), $Beta(\alpha, \beta)$, etc., comparing the two distributions can be done through, most popularly, KL divergence, for which there are analytical formulas if we make assumptions about the distributions. However, when the data becomes high dimension, or even variable dimension as in the case of text data, KL divergence, or any other traditional measure of distance between distributions, becomes unreliable.</p>

<p>The paper itself introduces the concept of <strong>Embedded characteristic score</strong> to measure the discrepancies between two distributions of data (real vs. AI-generated).</p>

<p>The paper itself will explain the theoretical rationale behind their choice of how the embeded characteristic score should be defined (that it is a legitimate measure of distance–satisfying the triangle inequality, that its mean approximation converges to the formal definition of expectation, that it is bounded).</p>
<div class="figure">
    <img src="/image/2025-01-23-characteristic/embed_characteristic_function.png" style="width: 110%; display: block; margin: 0 auto;" />
    <div class="caption">
        <span class="caption-label">Figure 1.</span> Definition of embedded characteristic score.
    </div>
</div>

<p>The paper contains statements for which I am not clear on, and so this blog post is dedicated to helping myself clear up those concepts. Below are the statements I want to understand better:</p>
<ul>
  <li>When the data distribution is heavy-tailed, the higher moments do not exist or do not converge.</li>
  <li>The values of characteristic function of a distribution is always bounded and always exists.</li>
</ul>

<h2 id="when-the-data-distribution-is-heavy-tailed-the-higher-moments-do-not-exist-or-do-not-converge">When the data distribution is heavy-tailed, the higher moments do not exist or do not converge</h2>
<p>When a distribution is ‘heavy-tailed’, the probability that the variable $\mathbf{X}$ is far from the mean is higher than , say, that of a normal distribution. The $k-$moment, is defined as $E[\mathbf{X}^k]$, when $X$ has a higher chance to take extreme values, the $k-$moment may not exist because $P(\mathbf{X}^k = \inf) \neq 0$.</p>

<p>In practice, moments of a distribution are estimated through the data: $E[\mathbf{X}^k] \approx \frac{1}{n}\sum_{i=1}^{n}x_i^k$. The same procedure can be repeated mulitple times, obtaining multiple estimates of the $k-$moments of $\mathbf{X}$. When the data is heavy-tailed, we could imagine the sampled $x_i^k$ to take very extreme values, and the estimated $k-$moments may vary greatly across different rounds of sampling. Hence, we can state that the higher moments do not “converge”, i.e. the estimated $k-$moments are unreliable when $k$ is high, and $X$ follows a heavy-tailed distribution.</p>

<h2 id="the-values-of-characteristic-functions-of-a-distribution-is-always-bounded-and-always-exists">The values of characteristic functions of a distribution is always bounded and always exists</h2>
<h3 id="but-first-moment-generating-functions">But first, moment generating functions</h3>
<p>Before diving in the concept of chracteristic function, we can revisit the concept of moment generating functions (MGF) to understand why characteristic function is different and necessary. 
I previously wrote about MGF in a separate blog post, but will outline the key idea here for completeness:</p>
<ul>
  <li>The MGF of a random variable $X$ is defined as $M_X(t)=E[e^{tX}]$. It is function of $t$.</li>
  <li>Depending on how $X$ is distributed, the MGF may or may not exist. For example, if $X$ is from a normal distribution, the <a href="https://statproofbook.github.io/P/norm-mgf">MGF exists and has a very <em>nice</em> analytical form</a>. If $X$ is from a Cauchy distribution, the MGF does not exist.</li>
  <li>MGF is aptly named, because if we take the $k-$th derivative of $M_X(t)$ and plug in $t=0$, we will get $E[X^k]$. This can be proven by first writing the Taylor expansion of $e^{tX} = 1 + \frac{tX}{1!} + \frac{t^2X^2}{2!} + \frac{t^3X^3}{3!} + …$, and then taking the expectation of this with respect to $X$, and plugging in $t=0$.</li>
  <li>When we define a random variable $X$, we (or at least I) tend to think of its PDF function, which specifies $P(X=x)$. The PDF defines an unique distribution. The MGF is an alterative way to define the distribution of $X$, i.e. if two distribution of variables $X$ and $Y$ have the same MGF, then they are identically distributed and also have the same PDF $P(X=x)=P(Y=y)$.</li>
  <li>When we compare two distributions, we can also compare their moments, i.e. checking whether $E[X^k] = E[Y^k]$ for all or many values of $k$. If the moments are equal, then the two distributions for $X$ and $Y$ are identical. This is an alternative approach to comparing the distributions of $X$ and $Y$ based on their PDFs, because theoretically, two variables are identically distributed if and only if they have the same moments.</li>
  <li><strong>A tangent but slightly related note</strong>: In a separate scenario, when we have data $x_1, x_2, …, x_n$ from a distribution $X$ parameterized by $\theta$ <em>based on our assumption</em>, we tend to first think of <strong>Maximum likelihood estimation</strong> to find the optimal value of parameters $\theta$ that would maximize the likelihood of observing $P(x_1,…x_p| \theta)$. A different way to find $\theta$ is to estimate the moments of the variable $X$ based on data, i.e. $\hat{X}^k \approx \frac{1}{n}\sum_{i=1}^{n}x_i^k$ for different values of $k$. Then, if our <em>assumed</em> distribution of $X$ allows us to have a analytical form of the moment generating function, then we can find the optimal values for parameter $\theta$ by fitting the values of analytical formula for $k-$moment ($E(X^k)$ based on the MGF) with the approximated values of $\hat{X}^k$. This approach is called the <strong>method of moments</strong>, which is just a terminology that scared me for the longest time, just because I dont understand what it is until I do.</li>
</ul>

<p>As you can see, the moment generating function is very useful, but a limitation of MGF is that it does not always exist, as explained above. Tam and Engelhardt, 2025 <a class="citation" href="#edric2024embedCharacteristicScore">(Tam, 2025)</a> paper proposes that instead of comparing the moments of two distributions, we can use a related concept called the characteristic function.</p>

<h3 id="characteristic-function-definition">Characteristic function definition</h3>
<p>The characteristic function of a random variable $X$ is defined as $\phi_X(t) = E[e^{itX}]$, where $i$ is the imaginary unit. The characteristic function is a function of $t$.
Similarly to MGF, the characteristic function is a way to define the distribution of $X$, i.e. if two distribution of variables $X$ and $Y$ have the same characteristic function, then they are identically distributed. This is proven in <a class="citation" href="#Lukacs1970">(Lukacs, 1970)</a>.</p>

<h3 id="why-is-the-characteristic-function-always-bounded">Why is the characteristic function always bounded?</h3>
<ul>
  <li>First, let’s review how a complex number is written in the form of $a+bi$, where $a$ and $b$ are real numbers, and $i$ is the imaginary unit. The magnitude of a complex number $z=a+bi$ is defined as $|z| = \sqrt{a^2+b^2}$.</li>
</ul>

<div class="figure">
    <img src="/image/2025-01-23-characteristic/imaginary_number.png" style="width: 70%; display: block; margin: 0 auto;" />
    <div class="caption">
        <span class="caption-label">Figure 2.</span> Imaginary number plane.
    </div>
</div>

<ul>
  <li>The characteristic function is bounded because $|e^{itX}| = 1$ for any values of $t$ and $X$. This is because: 
\(\[
\begin{aligned}
e^{itX} 
&amp;= \, (itX)^0 \;+\; \frac{itX}{1!} \;+\; \frac{(itX)^2}{2!} \;+\; \frac{(itX)^3}{3!} \;+\; \cdots 
 &amp;&amp; \quad \text{(Taylor expansion of $e^{itX}$)}\\[6pt]
&amp;= \,\Bigl(1 \;-\; \frac{t^2X^2}{2!} \;+\; \frac{t^4X^4}{4!} \;-\; \cdots\Bigr) 
 \;+\; i\Bigl(\frac{tX}{1!} \;-\; \frac{t^3X^3}{3!} \;+\; \cdots\Bigr) 
 &amp;&amp; \quad \text{}\\[6pt]
&amp;= \,\cos(tX) \;+\; i\,\sin(tX) 
 &amp;&amp; \quad \text{(Taylor exansion of $\cos$ and $\sin$)}\\[6pt]
\end{aligned}
\]\)</li>
</ul>

<p>Hence, $|e^{itX}| = \sqrt{\cos^2(tX) + \sin^2(tX)} = 1$. Therefore, we can always say that the value of $e^{itX}$ lies on the unit circle in the complex plane, and hence the values of the real or complex part of the characteristic function is bounded.</p>

<h2 id="concluding-remarks">Concluding remarks</h2>
<ul>
  <li>Just like the PDF, the MGF, the characteristic function is another way to define the distribution of a random variable.</li>
  <li>It is always bounded, and hence, it can be used as a more stable measure of the tail behavior of a distribution.</li>
  <li>This is a very primitive treatment of the characteristic function and how it can be used in understanding the behaviors of a distribution. It is my naive attempt to understand this topic. I encourage you to read the paper <a class="citation" href="#edric2024embedCharacteristicScore">(Tam, 2025)</a>, especially section 3, for a more detailed explanaion of why it is a more stable measurement of the distance between two distributions. Writing this blog made me reread the paper, and I now have a lot more questions than I originally had (such as how the characteristic function is related to the Fourier transform, and more importantly, what exactly is Fourier Transform that I hear a lot about). I will save those for another blog post.</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[The starting point: A very practical question The inspiration for this blog post is the paper Tam and Engelhardt, 2025 (Tam, 2025), from my postdoc lab under Dr. Barbara Engelhardt. The paper tries to address a highly relevant question in current machine learning research: Given the rise of all these foundation models that generate texts, images, etc., are there any theoretically sound ways to evaluate whether data generated by these models are similarly distributed to real data?. In more plain language: Are the texts/ images generated by GPT-/DallE-type models similar to the texts/ images that we have been generating for years? In more technical language: Are the distributions of the data generated by the foundation models similar to the distributions of the real data?]]></summary></entry><entry><title type="html">Moment generating function–an example with Gaussian distribution</title><link href="havu73.github.io/blog/2024/08/31/mgf_normal/" rel="alternate" type="text/html" title="Moment generating function–an example with Gaussian distribution" /><published>2024-08-31T00:00:00+00:00</published><updated>2024-08-31T00:00:00+00:00</updated><id>havu73.github.io/blog/2024/08/31/mgf_normal</id><content type="html" xml:base="havu73.github.io/blog/2024/08/31/mgf_normal/"><![CDATA[<p>Let’s examine the following two statements:</p>
<ul>
  <li>$Beta(1, N-1)$ converges to $Exponential(\frac{1}{N})$ when $N\rightarrow \infty$.</li>
  <li>Let $Y_{n \cdot d}$ be a matrix of data that is generated such that $Y_{n \cdot d}=X_{n \cdot k} \cdot W_{k \cdot d} + \epsilon_{n \cdot d}$, and each row of $X_{n \cdot k}$, $\mathbf{x_i} \sim N(\mathbf{0}, \sigma_x^2I_{k})$ is drawn from $N(0,1)$, and each row of $\epsilon$, $\epsilon_i\sim N(0, \sigma_{\epsilon}^2I_{d})$. Then, $Y_{n \cdot d}$ is drawn from $N(0,)$.</li>
</ul>

<p>In general, my first instinct in proving statements about the equivalence between two distributions is to derive the PDF or CDF of the random variable in question. However, I had a hard time proving them through that route this time. Instead, we have to take an alternative approach: Method of moments. Let’s dive in.</p>

<h2 id="the-moment-of-a-random-variable">The moment of a random variable</h2>
<p>If we have a random variable $X$, the $n$-th moment of $X$ is defined as $E[X^n]$. The first moment is the mean, and the second moment is the variance, those are the ones I care to remember, beyond that, we can call them $n$-th moment. In proving that two distributions are equivalent ($Beta(1, N-1)$ and $Exponential(\frac{1}{N})$ in the first opening statement), or that a particular random variable is from a particular distribution $Y\sim Normal(0, \Sigma_{W,\sigma_{\epsilon}, \sigma_{x}})$ in the second opening statement, we can instead show that their moments are equal. <strong>If $E[X^n]=E[Y^n]$ for all $n$, then $X$ and $Y$ are identically distributed, or asympotically equally distributed</strong>.</p>

<h2 id="moment-generating-function">Moment generating function</h2>
<p>The moment generating function (MGF) of a random variable $X$ is defined as $M_X(t)=E[e^{tX}]$. Here is why MGF takes this particular definition:</p>
<ul>
  <li>Back in Calculus II (which, imho, is the most omnipresent form of math, backed <a href="https://www.goodreads.com/book/show/40796176-infinite-powers?ac=1&amp;from_search=true&amp;qid=SEuA2XkCdx&amp;rank=1" target="_blank">here</a>), we learned about the Taylor series to estimate the value of a function $f(x)$ for any value of $x$: $f(x)=f(0)+f’(0)x+\frac{f''(0)}{2!}x^2+\cdots$. At the same time, the beauty in $e^x$ function is that its derivative is itself, $e^x$, and $e^{0}=1$ (so when $f(x)=e^x$, $f’(0)= f''(0)= …= 1$). The Taylor series of $e^x$, therefore, is $e^x=1+x+\frac{x^2}{2!}+\frac{x^3}{3!}\cdots$. It’s beautiful!</li>
  <li>Replacing $x$ with $tX$, we get $e^{tX}=1+tX+\frac{(tX)^2}{2!}+\frac{(tX)^3}{3!}\cdots$. Taking the expectation of this with respect to the random variable $X$, we get $M_X(t)=E[e^{tX}]=1+tE[X]+\frac{t^2E[X^2]}{2!}+\frac{t^3E[X^3]}{3!}\cdots$. This is, again, the Taylor expansion of the definition of MGF for random variable $X$, $M_X(t)$.</li>
  <li>The moment generating function is aptly named because if we take the $n-$the derivative of $M_X(t)$ and plug in $t=0$ to the resulting function, we will get $E[X^n]$. For example, $M_X’(0)=E[X]$, $M_X''(0)=E[X^2]$, and so on.</li>
  <li>Not all distributions have a closed-form MGF. However, for most of the distributions that we usually do <em>not</em> avoid, MGF is conveniently written in its Wikipedia page. And if you are ever curious about how the MGF for your distribution of interest is derived but cannot derive it yourself, peek at the <a href="https://statproofbook.github.io/I/ToC.html"> solutions manual</a>.</li>
</ul>

<h2 id="example-of-using-mgf-for-a-sensible-proof">Example of using MGF for a sensible proof</h2>
<p>Let’s prove the second opening statement. Here it is in bullet points:</p>
<ul>
  <li>$\mathbf{Y_{n \cdot d}}$: a matrix of observed data, each row represents a data point, each column corresponds to a feature.</li>
  <li>$n$ is the number of data points, $d$ is the number of features, and $k$ is the number of latent features.</li>
  <li>Generative model assumption: $\mathbf{Y_{n \cdot d}}=\mathbf{X_{n \cdot k}} \cdot \mathbf{W_{k \cdot d}} + \mathbf{\epsilon_{n \cdot d}}$, where $\mathbf{X_{n \cdot k}}$ is the latent feature matrix, $\mathbf{W_{k \cdot d}}$ is the weight matrix (another name is ‘loadings’), and $\epsilon_{n \cdot d}$ represents noise.<br />
\(\begin{aligned}
\begin{pmatrix}
\mathbf{y}_1 \\
\mathbf{y}_2 \\
\vdots \\
\mathbf{y}_n
\end{pmatrix} = \begin{pmatrix}
\mathbf{x}_1 \\
\mathbf{x}_2 \\
\vdots \\
\mathbf{x}_n
\end{pmatrix} \cdot \mathbf{W} + \begin{pmatrix}
\mathbf{\epsilon}_1 \\
\mathbf{\epsilon}_2 \\
\vdots \\
\mathbf{\epsilon}_n
\end{pmatrix}
\end{aligned}\)</li>
  <li>Further, we assume that: $\mathbf{x_i} \sim N(\mathbf{0}, \sigma_x^2I_{k})$, and $\mathbf{\epsilon_i} \sim N(\mathbf{0}, \sigma_{\epsilon}^2I_{d})$.</li>
  <li>This, by the way, is the generative model for probabilistic Principal Component Analysis (PPCA), if we have space for another jargon.</li>
</ul>

<p>The question is: What is the marginal distribution of $\mathbf{Y}$? We can try: $\mathbf{y_i} = \int_{\mathbf{x_i}} \mathbf{x_i}\cdot \mathbf{W}+ \mathbf{\epsilon_i} d{\mathbf{x_i}}$. I was not that good at this maneuver, so instead, we try:</p>

<ul>
  <li>For the sake of notation simplicity, let’s work through one row of $\mathbf{Y}$ and $\mathbf{X}$ and $\mathbf{\epsilon}$, denoted as $y$ and $x$ $\mathbf{\epsilon}$, respectively, representing one of $N$ independent datapoints. We have $\mathbf{y=x\cdot W + \epsilon}$.</li>
  <li>Now, in a separate scenario for a random $\mathbf{x}$, if $\mathbf{x} \sim Normal(\mathbf{\mu}, \mathbf{\Sigma})$, then $M_{\mathbf{x}}(\mathbf{t}) = exp(\mathbf{t}^T\mathbf{\mu} + \frac{1}{2}\mathbf{t}^T \mathbf{\Sigma} \mathbf{t})$. This is the analytical solution to the MGF of a multivariate normal distribution for $\mathbf{x}$.</li>
  <li>So, when $\mathbf{y=x\cdot W + \epsilon}$, based on the definition of MGF:</li>
</ul>

\[\begin{aligned}
M_{\mathbf{y}}(\mathbf{t}) &amp;= E_{\mathbf{x}, \mathbf{\epsilon}}[exp(\mathbf{t}^T\mathbf{y})] \\
&amp;= E_{\mathbf{x}, \mathbf{\epsilon}}[exp(\mathbf{t}^T\mathbf{x}\mathbf{W} + \mathbf{t}^T\mathbf{\epsilon})]\\
&amp;= E_{\mathbf{x}}[exp(\underbrace{\mathbf{t}^T}_{1\cdot d}\underbrace{(\mathbf{x}\mathbf{W})}_{(1k),(kd)})] \underbrace{E_{\mathbf{\epsilon}}[exp(\mathbf{t}^T\mathbf{\epsilon})]}_{M_\mathbf{\epsilon}(\mathbf{t}), \text{def.}}\\
&amp;= \underbrace{E_{\mathbf{x}}[exp(\mathbf{x}\mathbf{W}\mathbf{t})]}_{M_\mathbf{x}(\mathbf{W}\mathbf{t})} M_\mathbf{\epsilon}(\mathbf{t})\\

&amp;\overset{\mathbf{x}\sim N, \mathbf{\epsilon}\sim N}{=} exp(\mathbf{t}^T\mathbf{W}^T\mathbf{0} + \frac{1}{2}\mathbf{t}^T\mathbf{W}^T \sigma_x^2\mathbf{I}_k \mathbf{W}\mathbf{t}) \cdot exp(\mathbf{t}^T\mathbf{0} + \frac{1}{2}\mathbf{t}^T \sigma_{\epsilon}^2\mathbf{I}_d \mathbf{t})\\
&amp;= exp(\mathbf{t}^T\mathbf{0}+ \frac{1}{2}\mathbf{t}^T(\mathbf{W}^T \sigma_x^2\mathbf{I}_k \mathbf{W} + \sigma_{\epsilon}^2\mathbf{I}_d) \mathbf{t})\\
\end{aligned}\]

<p>Because of the final form of the MGF of $\mathbf{y}$, we can conclude that $\mathbf{y} \sim N(\mathbf{0} , \mathbf{W}^T \sigma_x^2\mathbf{I}_k \mathbf{W}) $.</p>

<h2 id="extending-marginal-distribution-to-inference-in-ppca">Extending marginal distribution to inference in PPCA</h2>
<p>As mentioned above, our assumption $\mathbf{y=x\cdot W + \epsilon}$ is the generative model for PPCA. In PPCA, we care about infering values of $\mathbf{x}$ and $\mathbf{W}$ given $\mathbf{y}$ as observed data. In theory, based on the above proof, we write the data likelihood $P(\mathbf{y})$ as a function of model parameters $\mathbf{W}, \sigma_x^2, \sigma_{\epsilon}^2$. We can, in theory, take the derivative of the data likelihood with respect to parameters to infer the values of parameters, i.e., setting $\frac{\partial P(\mathbf{Y})}{\partial \mathbf{W}}=\mathbf{0}$ and solve for $\mathbf{W}$. In <a class="citation" href="#lawrenceGPLVM_2005">(Lawrence, 2005)</a>, the introduction mentions that ‘PPCA and other latent variables, such as factor analysis or independent component analysis, requires a marginalization of the latent variables and optimization of the parameters’. This turns out to be a lot more challenging, I dont think we can actually find a closed form solution for $\mathbf{W}$ this way (I therefore found what is stated in <a class="citation" href="#lawrenceGPLVM_2005">(Lawrence, 2005)</a> misleading). Instead, we use the Expectation-Maximization (EM) algorithm to iteratively infer the values of $\mathbf{X}$ and $\mathbf{W}, \sigma_x^2, \sigma_{\epsilon}^2$ that maximize the likelihood of the observed data. I know of <a class="citation" href="#chiuPPCA_2020">(Chiu, 2020)</a> that shows an explanation and implementation of the EM algorithm for PPCA, specfically applied to genetics data in which the input matrix only contains ${0,1,2}$.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Let’s examine the following two statements: $Beta(1, N-1)$ converges to $Exponential(\frac{1}{N})$ when $N\rightarrow \infty$. Let $Y_{n \cdot d}$ be a matrix of data that is generated such that $Y_{n \cdot d}=X_{n \cdot k} \cdot W_{k \cdot d} + \epsilon_{n \cdot d}$, and each row of $X_{n \cdot k}$, $\mathbf{x_i} \sim N(\mathbf{0}, \sigma_x^2I_{k})$ is drawn from $N(0,1)$, and each row of $\epsilon$, $\epsilon_i\sim N(0, \sigma_{\epsilon}^2I_{d})$. Then, $Y_{n \cdot d}$ is drawn from $N(0,)$.]]></summary></entry><entry><title type="html">Variational Inference and Reparameterization– step-by-step</title><link href="havu73.github.io/blog/2024/08/31/reparameterization/" rel="alternate" type="text/html" title="Variational Inference and Reparameterization– step-by-step" /><published>2024-08-31T00:00:00+00:00</published><updated>2024-08-31T00:00:00+00:00</updated><id>havu73.github.io/blog/2024/08/31/reparameterization</id><content type="html" xml:base="havu73.github.io/blog/2024/08/31/reparameterization/"><![CDATA[<h2 id="refreshers-on-some-well-known-models">Refreshers on some well-known models</h2>
<p>In a general framework for inference, we observe some data $X$, which we assume to be generated by some latent variables and/or parameters $\theta$, such that we can write down a formula for $P(\mathbf{X}|\theta)$. $theta$ itself is generated from certain distribution, $P(\theta)$. Whether $\theta$ involve some latent variable or parameters, it is usually the goal of our inference to find the distribution or the values of $\theta$ given the observed data $P(\theta|X)$. Example cases of this framework are:</p>
<ul>
  <li>In a linear regression framework: $y = X\beta + \epsilon$, where $y$ and $X$ are observed. We also assume that $\beta \sim Normal(\mathbf{0}, \sigma^2*\mathbf{I})$.</li>
  <li>In a Variational auto encoder, we assume that our observed data $\mathbf{X}$ of $d$ dimensions is generated, usually through a series of fully connected layers of neural networks $f$, from some latent variables $\mathbf{Z}$, i.e. $\mathbf{X} = f(\mathbf{Z})$. We may also assume that $\mathbf{Z} \sim Normal(\mathbf{0}, \sigma^2*\mathbf{I})$.</li>
  <li>In a Gaussian Mixture Model framework, we observe data $\mathbf{X_i}$’s. We assume that each $X_i$ may be generated from one of the $K$ Gaussian distributions, i.e. $X_i|Z_i=k \sim N(\mu_k, \Sigma_k)$. Here, $Z_i$ is the latent group-indicator variable that we want to infer, $\mu_k$ and $\Sigma_k$ are parameters that we also want to learn. There can be more layers of prior distributions for $Z_i$, $\mu_k$ and $\Sigma_k$, but the backbone of the model model is as indicated above.</li>
</ul>

<h2 id="four-general-approaches-to-inference">Four general approaches to inference</h2>
<p>What these models share in common is that it involves observed data $X$ and one or more layers of latent variables $\theta$. What I learned, over the years of connecting the dots (sometimes, very inefficiently, as part of my interdisciplinary training) is that we will almost will always use one of the following approaches for inference (i.e. finding $P(\theta|X)$).</p>
<ul>
  <li>
    <p>Can we write down the formula for $P(\theta|X) = \frac{P(\mathbf{X}|\theta)P(\theta)}{\int_{\theta}P(\mathbf{X}|\theta)P(\theta)d\theta}$ into a closed-form formula? If yes then do it and find the value of $\theta$ that maximizes this probability potentially through taking the derivatives $\frac{\partial P(\theta|X)}{\partial \theta}$. This is what is called <em>Maximum A Posterori (MAP)</em> estimation. This case <em>usually</em> happens when we design (assume) $P(\theta)$ to be conjugate prior to the likelihood $P(\mathbf{X}|\theta)$. If you don’t know what <em>conjugate prior</em> is, don’t worry, it won’t be mentioned again in this post.</p>
  </li>
  <li>If the model is simple enough such that we can clearly break down $\theta$ to <em>latent variables</em> and <em>parameters</em>, then we can potentially use <em>Expectation Maximization (EM)</em> algorithm to find both the expected values of latent variables and the parameters that maximize the <em>likelihood $P(\mathbf{X}|\theta)$</em> of the observed data. Usually, the first criteria for “simple enough” is that the model has only 1 layer of latent variables, i.e. $\theta \rightarrow \mathbf{X}$ and not $\theta \rightarrow \mathbf{Z} \rightarrow \cdots \rightarrow \mathbf{X}$.</li>
  <li>If the model is such that for each $\theta_i$ as a single latent variable/parameter in $\theta$, we can write down an analytical solution (i.e. usually, a nice known distribution PDF) for $P(\theta_i|X, \theta_{-i})$, then we can use <em>Gibbs sampling</em> to sequentially and repeatedly sample $\theta_i$ from $P(\theta_i|X, \theta_{-i})$ until the values of $\theta_i$’s all converge. This case, again, requires us to analytically explore the closed-form formula for $P(\theta_i|X, \theta_{-i})$ for each $\theta_i$. If you don’t know what <em>Gibbs sampling</em> is, don’t worry, it won’t be mentioned again in this post.</li>
  <li>Lastly, the most versatile approach, in my opinion, is to use <em>Variational Inference</em>. We will zoom into this in the following section.</li>
</ul>

<p>Note that though the general approaches can act like recipes, our job as practitioners of the discipline is to refine these approaches based on the data at hand, hence requires the arts of implementation, trial and error. Identifying when to use which approach, unfortunately, has to be handled on a case-by-case basis. An important observation is that at each iteration, all of these approaches try to reframe the inference problem into one that can be solved by calculus, i.e. designing a loss function where the model’s latent variables and parameters $\theta$ are variables, taking derivatives, setting to $0$ and solve for $\theta$.</p>

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

<p>Let’s try to understand Variational Inference (VI) and where reparameterization trick comes in through a good old example:</p>
<ul>
  <li>Observed data: $\underbrace{\mathbf{A}}_{N \cdot D}$ and $\underbrace{\mathbf{b}}_{N \cdot 1}$. $N$: sample size,  $D$: data dimension.</li>
  <li>Model: $\underbrace{\mathbf{A}}_{N \cdot D} \cdot \underbrace{\mathbf{\beta}}_{D \cdot 1} + \underbrace{\mathbf{\epsilon}}_{N \cdot 1} = \underbrace{\mathbf{b}}_{N \cdot 1}$, where $\mathbf{\beta} \sim Normal(\mathbf{0}, \sigma^2\cdot\mathbf{I_D})$ is the prior distribution that we assume $\mathbf{\beta}$ follows. $\mathbf{\epsilon_i} \sim Normal(\mathbf{0}, \sigma_0^2)$ is the noise term. Assume that $\sigma_0^2$ and $\sigma^2$ are known and fixed.</li>
  <li>Goal: Find $\mathbf{\beta}$ that fit the data, which is usually defined as the $\beta$ that maximize $P(\mathbf{\beta}| \mathbf{A}, \mathbf{b})$.</li>
</ul>

<p>As mentioned above, there are genrally 4 approaches to this problem and in this case, it can be solved by all 4 approaches. Here, we focus on using Variational Inference (VI).</p>

<h3 id="step-1-identifying-observed-data-latent-variables-and-write-down-the-posterior">Step 1: Identifying observed data, latent variables and write down the posterior.</h3>
<p>\(\begin{array}{|c|c|c|}
\hline
 &amp; \textbf{General framework} &amp; \textbf{Example} \\
\hline
Obs.data &amp; \mathbf{X} &amp; \mathbf{A}, \mathbf{b} \\
Latent &amp; \mathbf{\theta} &amp; \mathbf{\beta} \\
Prior &amp; P(\theta) &amp; P(\beta) = Normal(\mathbf{0}, \sigma^2\cdot\mathbf{I_D}) \\
Likelihood &amp; P(\mathbf{X}\|\theta) &amp; P(\mathbf{A}, \mathbf{b}\|\beta) = P(\mathbf{b}-\mathbf{A}\beta) = Normal(\mathbf{0}, \sigma_0^2) \\
Posterior &amp; P(\mathbf{\theta}\|\mathbf{X}) &amp; P(\beta\|\mathbf{A}, \mathbf{b})=\frac{P(\mathbf{A}, \mathbf{b}\|\beta) P{\mathbf{\beta}}}{\int_{\mathbf{\beta}} P(\mathbf{A}, \mathbf{b}\|\beta) P(\mathbf{\beta}) d\mathbf{\beta}} = Normal(\mathbf{\mu}, \mathbf{\Sigma})\\
\hline
\end{array}\)
<br />
In the table above, $\mathbf{\mu}$ and $\mathbf{\Sigma}$ has a closed-form solution (due to the fact that we designed both the likelihood and the prior to be Normal). However, most of the time, in the general cases, $P(\mathbf{\theta}|\mathbf{X})$ cannot be derived analysitically. This is when I say to myself: ‘Let's try Variational Inference’.</p>

<h3 id="step-2-write-down-the-elbo-for-the-general-case">Step 2: Write down the ELBO for the general case</h3>
<p>If this is your first time hearing about the ELBO, just think of it as a fancy word for the loss function for variational inference, which we want to minimize. I have to rederive the ELBO for the general case each time I plan to work with variational inference, just so I can remember what to do next.</p>
<div class="figure">
    <img src="/image/2024-09-04-reparameterization/ELBO.png" style="width: 90%; display: block; margin: 0 auto;" />
    <div class="caption">
        <span class="caption-label">Figure 1.</span> Derivation of the ELBO.
    </div>
</div>

<p>Clearly, we want to find $\theta$ that maximizes $P(X)$. What we are saying, in variational inference, is: we cannot find the exact posterior $P(\theta|\mathbf{X})$, and also cannot find the optimal $\theta$ for $P(X)$ since it is not a tractable problem. Instead, we try to generate $\theta$ from a distribution of our design $q(\theta)$ (called the <em>variational distribution</em>), such that we can easily sample from (i.e. some known distributions that we can easily sample by a call to functions in <code class="highlighter-rouge">numpy</code>, <code class="highlighter-rouge">scipy</code> or <code class="highlighter-rouge">pytorch</code>). The distribution $q$ has some parameters (such as mean $\mu$ for a Normal distribution) that we will optimize so that the ELBO is maximized. The ELBO, as shown above, is a lower bound for the log-likelihood $log(P(X))$.</p>

<p>Another amazing advantage of variational infernece is that we potentially ignore the hierarchical dependency between latent variables. If our model is such that $\theta \rightarrow \mathbf{Z} \rightarrow \cdots \rightarrow \mathbf{X}$ where besides $\mathbf{X}$, all other variables are latent and one generates another, then <em>mean-field</em> variational inference allows us to assume that $\theta \sim q_{\theta}$, $Z \sim q_{Z}$, etc., and $q(\text{latent vars.}) = q(\theta)\cdot q(Z) \cdots$. We just have to find the optimal parameters for each $q$ to maximize the ELBO.</p>

<h3 id="step-3-write-down-the-elbo-for-the-case-at-hand">Step 3: Write down the ELBO for the case at hand</h3>
<p>In the example case, we can <em>choose</em> $q(\mathbf{\beta})$ to be $Normal(\underbrace{\mathbf{\mu}}_{D\cdot 1}, \mathbf{\Sigma})$, where</p>

<p>\(\begin{aligned}
\underbrace{\mathbf{\Sigma}}_{D \cdot D}= \begin{bmatrix}
\sigma_1^2 &amp; 0 &amp; \cdots &amp; 0 \\
0 &amp; \sigma_2^2 &amp; \cdots &amp; 0 \\
\vdots &amp; \vdots &amp; \ddots &amp; \vdots \\
0 &amp; 0 &amp; \cdots &amp; \sigma_D^2
\end{bmatrix}
\end{aligned}\) <br />
The parameters for which we will optimize for, therefore, include $\mathbf{\mu}$ and $\sigma_1^2, \sigma_2^2, \cdots, \sigma_D^2$. The ELBO for this case, based on Fig. 1, is:<br />
\(\begin{align}
\text{ELBO} &amp;= \mathbb{E}_{q(\mathbf{\beta})} \left[ \log P(\mathbf{A}, \mathbf{b} \mid \mathbf{\beta}) \right] - KL(q(\mathbf{\beta}) \| P(\mathbf{\beta})) \\
&amp;= \sum_{t=1}^{T} \sum_{i=1}^{N} \underbrace{\log P(\mathbf{b}_i - \mathbf{A}_i \mathbf{\beta_t})}_{N(0,\sigma_0^2)} - \underbrace{KL(q(\mathbf{\beta}) \| P(\mathbf{\beta}))}_{KL(N(\mu, \Sigma), N(\mathbf{0}, \sigma^2\cdot \mathbf{I_D}))}\\
\end{align}\)</p>

<h3 id="step-4-reparameterize-the-sampled-latent-variables-from-q">Step 4: Reparameterize the sampled latent variables from $q$</h3>
<p>Note that the parameters for the variational inferences include $\mu$ and $\sigma_1^2, \sigma_2^2, \cdots, \sigma_D^2$. The goal of variational inference, again, is to find the optimal values of these parameters to maximize ELBO. There usually exists an analytical fomular for $KL(q(\mathbf{\beta}) | P(\mathbf{\beta}))$ (which, in our example is the KL divergence between two MultivariateNormal distributions with different means and diagonal covariance matrices). Therefore, we can easily calculate $\frac{\partial KL(q | P)}{\partial \mu_i}$ and $\frac{\partial KL(q | P)}{\partial \sigma_i}$ for each $i \in {1,…D}$. That is one part of $\frac{\partial \text{ELBO}}{\partial \mu_i}$ and $\frac{\partial \text{ELBO}}{\partial \sigma_i}$.</p>

<p>However, due to the fact that we sample $\beta_t \sim N(\mu, \Sigma)$ ($t$ is index of $\beta$ samples) to construct the first part of the ELBO, we cannot easily calculate $\frac{\partial  \log P(\mathbf{A}, \mathbf{b} \mid \mathbf{\beta_t}) }{\partial \mu_i}$ and $\frac{\partial  \log P(\mathbf{A}, \mathbf{b} \mid \mathbf{\beta_t}) }{\partial \sigma_i}$ for each $i \in {1,…D}$. This is where the reparameterization trick comes in: Instead of directly sample $\beta_t \sim N(\mu, \Sigma)$, we sample $\epsilon_t \sim N(\mathbf{0}, \mathbf{I})$ and calculate $\beta_t = \mu + \Sigma^{1/2} \zeta_t$ where $\zeta_t \sim N(\mathbf{0}, \mathbf{I})$. This way, we can easily calculate $\frac{\partial  \log P(\mathbf{A}, \mathbf{b} \mid \mathbf{\beta_t}) }{\partial \mu_i}$ and $\frac{\partial  \log P(\mathbf{A}, \mathbf{b} \mid \mathbf{\beta_t}) }{\partial \sigma_i}$ because the randomness in sampling $beta_t$ got attributed to $\zeta_t$, $mu_i$ and $\sigma_i$ are included as an deterministic part of the ELBO.</p>

<h3 id="step-5-optimize-the-elbo">Step 5: Optimize the ELBO</h3>
<p>By this step, we have turned the problem into an optimization problem, and in turn tossing the ball into the court of optimization researchers. We, of course, need to know all the criteria for framing a solvable optimization task (such as making sure all the functions are differentiable, and the loss function should be convex if possible, etc.), but that is a topic for another post. Here, I want to provide a snapshot code for constructing the model for the example case, and the training loop.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="n">nn</span>
<span class="kn">import</span> <span class="nn">torch.optim</span> <span class="k">as</span> <span class="n">optim</span>
<span class="kn">import</span> <span class="nn">torch.distributions</span> <span class="k">as</span> <span class="n">dist</span>
<span class="k">class</span> <span class="nc">model</span><span class="p">(</span><span class="n">nn</span><span class="p">.</span><span class="n">Module</span><span class="p">):</span> 
    <span class="s">'''
    The model module specify the generative process to get to the predicted value of b given input A. 
    Required functions: __init__ and forward
    '''</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">D</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">).</span><span class="n">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="p">.</span><span class="n">D</span> <span class="o">=</span> <span class="n">D</span>
        <span class="c1"># set up the parameters for the variational distribution. 
</span>        <span class="c1"># use nn.Parameter to declare model parameters, so pytorch can keep track of the gradients
</span>        <span class="bp">self</span><span class="p">.</span><span class="n">mu</span> <span class="o">=</span> <span class="n">nn</span><span class="p">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="p">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">D</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>  <span class="c1"># vector of mu_1, ..., mu_D
</span>        <span class="bp">self</span><span class="p">.</span><span class="n">sigma</span> <span class="o">=</span> <span class="n">nn</span><span class="p">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="p">.</span><span class="n">ones</span><span class="p">(</span><span class="n">D</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>  <span class="c1"># vector of sigma_1, ..., sigma_D
</span>
    <span class="o">@</span><span class="nb">staticmethod</span>
    <span class="k">def</span> <span class="nf">reparameterize</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">):</span>
       <span class="n">eps</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">randn_like</span><span class="p">(</span><span class="n">sigma</span><span class="p">)</span>
       <span class="k">return</span> <span class="n">mu</span> <span class="o">+</span> <span class="n">sigma</span> <span class="o">*</span> <span class="n">eps</span>
    
    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">A</span><span class="p">):</span>
        <span class="n">beta</span> <span class="o">=</span> <span class="bp">self</span><span class="p">.</span><span class="n">reparameterize</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">mu</span><span class="p">,</span> <span class="bp">self</span><span class="p">.</span><span class="n">sigma</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">torch</span><span class="p">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>

<span class="c1">#### Generate data ####
</span><span class="n">N</span> <span class="o">=</span> <span class="mi">1000</span>
<span class="n">D</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">true_beta</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">tensor</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">]).</span><span class="n">reshape</span><span class="p">(</span><span class="n">D</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">randn</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">D</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">true_beta</span><span class="p">)</span> <span class="o">+</span> <span class="n">torch</span><span class="p">.</span><span class="n">randn</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="c1">#### Loss function ####
</span><span class="k">def</span> <span class="nf">neg_elbo</span><span class="p">(</span><span class="n">b_pred</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">mu_prior</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">sigma_prior</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="n">epsilon</span> <span class="o">=</span> <span class="n">dist</span><span class="p">.</span><span class="n">Normal</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">log_llh</span> <span class="o">=</span> <span class="n">epsilon</span><span class="p">.</span><span class="n">log_prob</span><span class="p">(</span><span class="n">b</span><span class="o">-</span><span class="n">b_pred</span><span class="p">).</span><span class="nb">sum</span><span class="p">()</span>
    <span class="n">model</span><span class="p">.</span><span class="nb">eval</span><span class="p">()</span>
    <span class="n">kl</span> <span class="o">=</span> <span class="n">dist</span><span class="p">.</span><span class="n">kl_divergence</span><span class="p">(</span><span class="n">dist</span><span class="p">.</span><span class="n">Normal</span><span class="p">(</span><span class="n">model</span><span class="p">.</span><span class="n">mu</span><span class="p">,</span> <span class="n">model</span><span class="p">.</span><span class="n">sigma</span><span class="p">),</span> <span class="n">dist</span><span class="p">.</span><span class="n">Normal</span><span class="p">(</span><span class="n">mu_prior</span><span class="p">,</span> <span class="n">sigma_prior</span><span class="p">)).</span><span class="nb">sum</span><span class="p">()</span>
    <span class="k">return</span> <span class="o">-</span><span class="n">log_llh</span> <span class="o">+</span> <span class="n">kl</span>  <span class="c1"># minimize the negative of ELBO
</span>
<span class="c1">#### Training loop ######
</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="n">D</span><span class="p">)</span>
<span class="n">optimizer</span> <span class="o">=</span> <span class="n">optim</span><span class="p">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">model</span><span class="p">.</span><span class="n">parameters</span><span class="p">(),</span> <span class="n">lr</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>  
<span class="c1"># the optimize registers parameters in the model for which it needs to keep track of the gradients
</span><span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">):</span>
    <span class="n">optimizer</span><span class="p">.</span><span class="n">zero_grad</span><span class="p">()</span>
    <span class="n">b_pred</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
    <span class="n">loss</span> <span class="o">=</span> <span class="n">neg_elbo</span><span class="p">(</span><span class="n">b_pred</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
    <span class="n">loss</span><span class="p">.</span><span class="n">backward</span><span class="p">()</span>
    <span class="n">optimizer</span><span class="p">.</span><span class="n">step</span><span class="p">()</span>

<span class="c1">#### Print the estimated beta ####
</span><span class="k">print</span><span class="p">(</span><span class="n">model</span><span class="p">.</span><span class="n">mu</span><span class="p">,</span> <span class="n">model</span><span class="p">.</span><span class="n">sigma</span><span class="p">)</span>  <span class="c1"># model.mu can be used as the estimated beta,
# model.mu and model.sigma combined defines the estimated posterior P(beta|A, b)
</span></code></pre></div></div>
<h2 id="generalizing-the-model-for-other-cases">Generalizing the model for other cases</h2>
<p>So far, I have outlined the steps that I usually take when I try to apply variational inference, with an example in which the variational distribution $q$ is picked to be Normal (and hence, a reparameterization trick for normal distribution). In many cases, due to the nature of the data (categorical, continuous, non-genative, etc.), the choice of the variational distribution $q$ needs to be adapted. For example:</p>
<ul>
  <li>In the case of Gaussian Mixture Model (presented above, first section), varitational distribution for $Z$, i.e. $q(Z_i)$ should return samples that are one-hot encoded, i.e. $Z_{i}\sim Multinomial(\pi_{q,i})$  so $Z_{i, \text{sampled}} = [0, 0, 1, 0, 0]$ for $K=5$, but the parameters for $q(Z_i)$ should be continuous, i.e. $\pi_{q,i} = [0.1, 0.2, 0.5, 0.1, 0.1]$ for $K=5$. The reparameterization trick should find a way to transform from the continuous parameters $\pi_{q,i}$ to the one-hot encoded $Z_{i, \text{sampled}}$ in a way that $\pi_{q,i}$ is still present in the ELBO. The <em>Gumbel-Softmax</em> trick is one way to do this <a class="citation" href="#Jang2016_gumbell">(Jang, 2016)</a>.</li>
  <li>In my recent application, I need the latent varible to be non-negative and continuous, so I picked $q(Z_i) = LogNormal(\mu_i, \sigma_i^2)$. There is, of course, a reparameterization trick for this distribution, $Z_i = \exp(\mu_i + \sigma_i \zeta_i)$ where $\zeta_i \sim N(0, 1)$.</li>
</ul>

<h1 id="conclusion">Conclusion</h1>
<ul>
  <li>Usually, when faced with a problem of inference, I usually try to work through four general approaches in increasing levels of problem complexity: MAP estimation, EM algorithm, Gibbs sampling, and Variational Inference.</li>
  <li>Variational inference is usually the most versatile approach, overcoming lots of complications in the hierarchical dependency of latent variables. If we understand the nature of the data requirements, we can pick variational distributions that are both suitable for the data, but also easy to sample from and easy to derive the KL divergence from the prior.</li>
  <li>I always rederive the ELBO for the general case as a separate step in my inference (Fig. 1). It guides me to understand why and what we are doing with varitional inference.</li>
  <li>The reparameterization trick is needed to sample the latent variables $Z$ from variational distribution $q(Z)$, such that the parameters of $q$ are still present in the ELBO, and hence can be optimized. Different variational distributions require different reparameterization tricks, choice of variational inference is dependent on data requirements.</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[Refreshers on some well-known models In a general framework for inference, we observe some data $X$, which we assume to be generated by some latent variables and/or parameters $\theta$, such that we can write down a formula for $P(\mathbf{X}|\theta)$. $theta$ itself is generated from certain distribution, $P(\theta)$. Whether $\theta$ involve some latent variable or parameters, it is usually the goal of our inference to find the distribution or the values of $\theta$ given the observed data $P(\theta|X)$. Example cases of this framework are: In a linear regression framework: $y = X\beta + \epsilon$, where $y$ and $X$ are observed. We also assume that $\beta \sim Normal(\mathbf{0}, \sigma^2*\mathbf{I})$. In a Variational auto encoder, we assume that our observed data $\mathbf{X}$ of $d$ dimensions is generated, usually through a series of fully connected layers of neural networks $f$, from some latent variables $\mathbf{Z}$, i.e. $\mathbf{X} = f(\mathbf{Z})$. We may also assume that $\mathbf{Z} \sim Normal(\mathbf{0}, \sigma^2*\mathbf{I})$. In a Gaussian Mixture Model framework, we observe data $\mathbf{X_i}$’s. We assume that each $X_i$ may be generated from one of the $K$ Gaussian distributions, i.e. $X_i|Z_i=k \sim N(\mu_k, \Sigma_k)$. Here, $Z_i$ is the latent group-indicator variable that we want to infer, $\mu_k$ and $\Sigma_k$ are parameters that we also want to learn. There can be more layers of prior distributions for $Z_i$, $\mu_k$ and $\Sigma_k$, but the backbone of the model model is as indicated above.]]></summary></entry><entry><title type="html">Beta distribution– from first principles</title><link href="havu73.github.io/blog/2024/08/28/beta_and_unif/" rel="alternate" type="text/html" title="Beta distribution– from first principles" /><published>2024-08-28T00:00:00+00:00</published><updated>2024-08-28T00:00:00+00:00</updated><id>havu73.github.io/blog/2024/08/28/beta_and_unif</id><content type="html" xml:base="havu73.github.io/blog/2024/08/28/beta_and_unif/"><![CDATA[<p>In an introduction to probability course, we learned to construct certain probability distributions in very intuitive ways, i.e. we first got introduced to the physical events that gave rise to those distributions and then derived the formulas for the distributions based on first principles. For example:</p>
<ul>
  <li>Uniform distribution  $Unif(a,b)$: randomly pick a number between $a$ and $b$.</li>
  <li>Bernoulli distribution  $Bern(p)$: flip a coin with probability of head being $p$.</li>
  <li>Binomial distribution  $Bin(n,p)$: flip a coin $n$ times, where each time we flip the coin, it tuns up head with probability of $p$.</li>
</ul>

<p>Beyond these simple distributions, I remembered getting extremely confused about why the rest of the distributions are defined like they are, i.e. how did we come up with the specific form of the $PDF$ functions for these distributions?. Eventually, after seeing them enough, I started to take them as doctrines, to be used and not to be questioned. This post, however, is one of my attempts in breaking that curse. I derive the Beta distribution, linking it to the Dirichlet distribution and eventually reveal how the ideas for this whole post came to be, which is an application of these probability distributions in simulation of sequencing experiments in genomics.</p>

<h1 id="1-beta-distribution-and-the-breaking-of-a-stick">1. Beta distribution and the breaking of a stick</h1>
<p>Let’s say I have a stick of length 1, and I want to randomly break it into two pieces. I can do this by randomly picking a number between 0 and 1, $x$ ~ Unif(0,1), and the two lengths of the two pieces will be $x$ and $1-x$. Now, imagine I have to uniformly randomly break the stick into $N$ pieces, where $N$&gt;2 is an integer. How should I do this, i.e. how should I simulate this on the computer?</p>

<p>Here:</p>
<pre><code class="language-angular2html">import numpy as np
N = 3
us = np.random.uniform(0,1,N-1)  # ex: N=3, us = [0.3,0.6]
us = np.append(us, 0) # ex: us = [0.3,0.6,0]
us = np.append(us, 1)  # ex: us = [0.3,0.6,0,1]
us.sort() # ex: us = [0,0.3,0.6,1]
xs = np.diff(us) # ex: xs = [0.3,0.3,0.4]
</code></pre>

<p>Based on the snippet of code above, you can see that we simulate the uniform fragmentation of $1$-length stick by first drawing  $N-1$ numbers from the Uniform distribution, sorting them along with  $0,1$, and getting their differences.</p>

<p>The question that follows is: What is the probability distribution of the length of one of the $N$ fragments, denoted $X$? The answer, unsurprisingly given the topic of this post, is $\textbf{Beta}$ distribution. In particular, $\mathbf{Beta(1, N-1)}$. Here is why:</p>

<ul>
  <li>First, the PDF of $X\sim\mathbf{Beta(\alpha, \beta)}$ is given by: $P(X=x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{\mathbf{B}(\alpha, \beta)}$, where $\mathbf{B}(\alpha, \beta) = \frac{\Gamma(\alpha)\Gamma(\beta)}{\Gamma(\alpha+\beta)}$ is the Beta function and  $\mathbf{G}(\alpha, \beta)$ is the Gamma function, which has its own googlable definition. Therefore, the PDF of $X\sim\mathbf{Beta(1, N-1)}$ is given by: $P(X=x) = \frac{(1-x)^{N-2}}{\mathbf{B}(1, N-1)}$.</li>
  <li>Note that by definitions of the Gamma and Beta functions, we have:
\(\begin{align*}
\mathbf{B}(1, N-1) &amp;= \frac{\mathbf{\Gamma}(1)\mathbf{\Gamma}(N-1)}{\mathbf{\Gamma}(N)} \\ 
&amp;= \frac{0!\cdot(N-2)!}{(N-1)!} \\
\end{align*}\)</li>
</ul>

<p>So,
$
\frac{1}{\mathbf{B}(1, N-1)}= N-1
$. Here, Beta function is included in the PDF of the Beta distribution to ensure that $\int_{x=0}^{1}P(\mathbf{X}=x) = 1$.</p>

<ul>
  <li>Separately, If we denote $\mathbf{Y}$ as the random variable representing the length of one of the $N$ fragments, we just argued above (using the logic of simulation) that $\mathbf{Y}$ can be characterized as the <em>minimum</em> of $N-1$ numbers $\mathbf{u_1, u_2, …, u_{N-1}}$ drawn $i.i.d$ from the Uniform distribution. Now:</li>
</ul>

\[\begin{align}
P(\mathbf{Y}=u) &amp;\propto P(\min \{u_1, ..., u_{N-1}\} = u) \\
 &amp;\propto P(u_{(1)}=u, u_{(2)} &gt;= u, u_{(N-1)}&gt;=u) \\
 &amp;\propto P(u_{(1)}=u)P(u_{(2)} &gt;= u)P(u_{(N-1)}&gt;=u) \\
 &amp;\propto \frac{1}{1-0}(1-u) \cdots (1-u) \\
 &amp;\propto u^0(1-u)^{N-2} 
\end{align}\]

<ul>
  <li>The reason why we used $\propto$ instead of $=$ in the above equations is because in order for $P(\mathbf{Y})$ to be a valid probability distribution, just as in the case for $\mathbf(X)\sim Beta(1, N-1)$, we need $\int_{u=0}^{1}P(\mathbf{Y}=u) = 1$. So we need to find $\mathbf{C}$ such that $\int_{u=0}^{1} \frac{u^0(1-u)^{N-2}}{\mathbf{C}} =  1$. Therefore, $\mathbf{C} = \frac{1}{N-1} = \mathbf{B}(1,N-1)$.</li>
  <li>Overall, we just showed that if $\mathbf{Y}$ is the $\mathbf{r.v}$ of the length of one of the $N$ fragments, then $P(\mathbf{Y}=u)=\frac{u^0(1-u)^{N-2}}{\mathbf{B}(1,N-1)}$, which is the same as the PDF of $\mathbf{X}\sim\mathbf{Beta(1, N-1)} \qquad \blacksquare$.</li>
</ul>

<p>We can generalize this idea of uniform fragmentation of a $1-$length stick to describe $\mathbf{Beta}(\alpha, \beta)$ as the probability distribution of the $\alpha-$th fragment lengths in ascending order, when we break the stick into $\alpha+\beta$ fragments. Call this variable $\mathbf{Y}_{(\alpha)}$, then:</p>

\[\\
P(\mathbf{Y}_{(\alpha)}=u) = 
\underbrace{\frac{1}{\mathbf{B}(\alpha, \beta)}}_{\text{Normalizing const.}} 
\cdot
\overbrace{u^{\alpha-1}}^{P(\mathbf{Y}_{(1)...(\alpha-1)}&lt;=u)} 
\cdot 
\underbrace{\frac{1}{1-0}}_{P(\mathbf{Y}_{(\alpha)}=u)} 
\cdot
\overbrace{(1-u)^{\beta-1}}^{P(\mathbf{Y}_{(\alpha+1)...(\alpha+\beta)}&gt;=u)} 
\\\]

<h1 id="2-some-extended-properties-of-the-beta-distribution">2. Some extended properties of the Beta distribution:</h1>
<p>In uniform fragmentation of the $1-$length stick into $N$ fragments as described above, if $N\rightarrow \infty$, then $\mathbf{Beta}(1, N-1)$ becomes $\mathbf{Exponential}(\frac{1}{N})$ <a class="citation" href="#tenchov1985_beta">(Tenchov, 1985)</a>. I have not been able to fully prove this statement yet, but what I know is possible is that we can prove that when $N\rightarrow \infty$, the first and second moments of the two distributions converge to the same values.</p>
<div class="figure">
    <img src="/image/2024-08-28-beta_and_unif/beta_exp.png" style="width: 60%; display: block; margin: 0 auto;" />
    <div class="caption">
        <span class="caption-label">Figure 1.</span> When $N\rightarrow \infty$, the PDF functions of $Beta(1, N-1)$ distribution perfectly overlaps with the PDF function of the $Exponential(1/N)$ distribution.
    </div>
</div>
<p>The Dirichlet distribution is the probability distribution for a vector of $N$ non-negative numbers that sum to $1$, $P(\mathbf{\vec{X}}= (x_1, …x_N))$. Now, if $\mathbf{\vec{X}} \sim \textbf{Dirichlet}(1,1,…,1)$, then we can see the parallel between the marignal distribution of $x_i$, i.e. an individual entry of $\mathbf{\vec{X}}$, will follow $\mathbf{Beta}(1, N-1)$.</p>

<h1 id="3-how-the-beta-distribution-and-its-variants-used-in-genomics">3. How the beta distribution and its variants used in genomics?</h1>
<p>So far we have walked through the various properties of the Beta distributions, its origins (at least in my mind), and its connection to the other distributions. If you are still curious and have the bandwidth, please read on. If not, you already got the gist of the post.</p>

<p>In genomics, we measure gene expression, epigentic expression through sequencing experiment in which a particular stretch of DNA sequence (let’s call it a transcript without lack of generalizability) is broken into fragments. I was studying about how to properly simulate the result of such an experiment, and an essential step in this pipeline, of course, is to simulate the breaking of a trascript such that each fragment is realistically integers between 200-300 bp in length (which is common in sequencing experiments). There are several ways we can do this:</p>
<ul>
  <li>Uniform fragmentations: Treat the fragments like a $1-$length stick and break it into $N$ fragments using uniform fragmentation, then multiply the fragments by the total length $L$, and turn the resulting fragment lengths into integers.</li>
  <li>Poission distribution generation: In a lot of bioinformatics applications, Poisson distribution is used because it is a discrete distribution which fits the nature of our data (what else? I don’t know, maybe people also use it out of convenience? but that’s probably too big of an accussation from my part). In this application, we can continually simulate fragment length from a Poission distribution with mean, say, 250bp, until the total length of the fragments is equal to $L$.</li>
  <li>More sophisticated procedures.</li>
</ul>

<p>What the literature can tell me, based on <a class="citation" href="#pai2017_splice">(Pai AA, 2017)</a> and <a class="citation" href="#fluxSimulator_2012">(Griebel T, 2012)</a>, is that the fragment length from a transcript of length $L$, such that the fragment length should be between 200-300 bp, can be simulated using the <strong>Weibull</strong> distribution with scale $\alpha=200$ and shape $k = log_{10}(L)$. The papers also stated that empirical evidence from doing sequencing experiments, and collecting the data show that this Weibull distribution-based simulation fit the observed fragment length.</p>

<div class="figure">
    <img src="/image/2024-08-28-beta_and_unif/weibull_vs_uniform.png" style="width: 60%; display: block; margin: 0 auto;" />
    <div class="caption">
        <span class="caption-label">Figure 2.</span> The Weibull distribution of fragment length is wildly different from what we get by uniformly breaking the $1-$length strick and multiplying the fragments by the total length $L$.
    </div>
</div>
<p>I was intriqued. How on earth did people come up with using the Weibull distribution for this application in the first place? Or, more likely, how did we invent this distribution in the first place? Unfortunately, I do not have all the answers, but I can show you a few points during my explorations that may justify the choice of the Weibull distribution for this particular application:</p>

<p>One big advantage of modeling the fragment size from the Weibull distribution with scale $\alpha=200$ and shape $k = log_{10}(L)$ is that despite the wide range of possible transcript length $L$ (different gene lengths, and different progress of the transcript down the length of the gene), the distributions of fragment lengths will stay quite similar. This is reflective of what we see in practice: in a sequencing experiment, regardless of the gene length, we would expect to see similar fragment lengths being generated in the experiment.</p>

<div class="figure">
    <img src="/image/2024-08-28-beta_and_unif/weibull_transcript_length.png" style="width: 60%; display: block; margin: 0 auto;" />
    <div class="caption">
        <span class="caption-label">Figure 3.</span> Each line is showing the PDF of weibull distribution with scale $\alpha=200$ and shape $k = log_{10}(L)$ for varying values of $L$ as the transcript length. The dotted line is showing the mean of the distributions
    </div>
</div>

<p>Finally, if we follow the uniform fragmentation procedure and generate $\mathbf{Y_i} \sim Beta(1, N-1)$, or equivalently $\mathbf{\vec{Y}}=(y_1, … y_N) \sim Dirichlet(1, 1,….,1 )$, and we transform the <em>r.v</em> such that $w_i = y_i^{k}$, where $k= log_{10}(L)$, then the $w_i$ will follow the Weibull distribution with scale $\alpha=1$ and shape $k = log_{10}(L)$. I tried to prove this statement, but I have not succeeded yet, I just know that it is true!</p>

<h1 id="4-conclusion">4. Conclusion</h1>
<ul>
  <li>$Beta(\alpha, \beta)$ is the distribution showing the probability of the $\alpha-$th number in ascending order in a pool of $\alpha+\beta$ numbers generated from $Unif(0,1)$.</li>
  <li>$Beta(1, N-1)$ is the distribution of the length of one of the $N$ fragments when breaking a $1-$length stick into $N$ fragments.</li>
  <li>$Beta(1, N-1)$, thererfore, is the marginal distribution of $y_i$ when $\vec{y} \sim Dirichlet(1,1,…,1)$.</li>
  <li>$Beta(1, N-1)$ converges to $Exponential(\frac{1}{N})$ when $N\rightarrow \infty$.</li>
  <li>When we transform $y_i$ from $y_i \sim Beta(1, N-1)$ to $w_i = y_i^{k}$ with $k=log_{10}(L)$ for some number $L$, then the resulting $w_i$ will follow the Weibull distribution with scale $\alpha=1$ and shape $k = log_{10}(L)$.</li>
  <li>Weibull distirbution is used in genomics to simulate the fragment length of a transcript in a sequencing experiment.</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[In an introduction to probability course, we learned to construct certain probability distributions in very intuitive ways, i.e. we first got introduced to the physical events that gave rise to those distributions and then derived the formulas for the distributions based on first principles. For example: Uniform distribution $Unif(a,b)$: randomly pick a number between $a$ and $b$. Bernoulli distribution $Bern(p)$: flip a coin with probability of head being $p$. Binomial distribution $Bin(n,p)$: flip a coin $n$ times, where each time we flip the coin, it tuns up head with probability of $p$.]]></summary></entry></feed>