# User:Timothee Flutre/Notebook/Postdoc/2012/08/16

(Difference between revisions)
 Revision as of 15:29, 31 August 2012 (view source) (→Variational Bayes approach for the mixture of Normals: add principle of Variational Bayes)← Previous diff Revision as of 19:11, 2 September 2012 (view source) (→Variational Bayes approach for the mixture of Normals: shorten + add principle variational bayes)Next diff → Line 11: Line 11: - * '''Data''': we have N univariate observations, $y_1, \ldots, y_N$, gathered into the vector $\mathbf{y}$. + * '''Data''': N univariate observations, $y_1, \ldots, y_N$, gathered into the vector $\mathbf{y}$ + * '''Model''': mixture of K Normal distributions - * '''Assumptions''': we assume the observations to be exchangeable and distributed according to a mixture of K Normal distributions. The parameters of this model are the mixture weights ($w_k$), the means ($\mu_k$) and the [http://en.wikipedia.org/wiki/Precision_%28statistics%29 precisions] ($\tau_k$) of each mixture components, all gathered into $\Theta = \{w_1,\ldots,w_K,\mu_1,\ldots,\mu_K,\tau_1,\ldots,\tau_K\}$. There are two constraints: $\sum_{k=1}^K w_k = 1$ and $\forall k \; w_k > 0$. + * '''Parameters''': K mixture weights ($w_k$), K means ($\mu_k$) and K [http://en.wikipedia.org/wiki/Precision_%28statistics%29 precisions] ($\tau_k$), one per mixture component + $\Theta = \{w_1,\ldots,w_K,\mu_1,\ldots,\mu_K,\tau_1,\ldots,\tau_K\}$ - * '''Observed likelihood''': $p(\mathbf{y} | \Theta, K) = \prod_{n=1}^N p(y_n|\Theta,K) = \prod_{n=1}^N \sum_{k=1}^K w_k Normal(y_n;\mu_k,\tau_k^{-1})$ + * '''Constraints''': $\sum_{k=1}^K w_k = 1$ and $\forall k \; w_k > 0$. + * '''Observed likelihood''':  observations assumed exchangeable (independent and identically distributed given the parameters) - * '''Maximizing the observed log-likelihood''': as shown [http://openwetware.org/wiki/User:Timothee_Flutre/Notebook/Postdoc/2011/12/14 here], maximizing the likelihood of a mixture model is like doing a weighted likelihood maximization. However, these weights depend on the parameters we want to estimate! That's why we now switch to the missing-data formulation of the mixture model. + $p(\mathbf{y} | \Theta, K) = \prod_{n=1}^N p(y_n|\Theta,K) = \prod_{n=1}^N \sum_{k=1}^K w_k Normal(y_n;\mu_k,\tau_k^{-1})$ + * '''Latent variables''': N hidden variables, $z_1,\ldots,z_N$, each being a vector of length K with a single 1 indicating the component to which the $n^{th}$ observation belongs, and K-1 zeroes. - * '''Latent variables''': let's introduce N latent variables, $z_1,\ldots,z_N$, gathered into the vector $\mathbf{z}$. Each $z_n$ is a vector of length K with a single 1 indicating the component to which the $n^{th}$ observation belongs, and K-1 zeroes. + * '''Augmented likelihood''': + $p(\mathbf{y},\mathbf{z}|\Theta,K) = \prod_{n=1}^N p(y_n,z_n|\Theta,K) = \prod_{n=1}^N p(z_n|\Theta,K) p(y_n|z_n,\Theta,K) = \prod_{n=1}^N \prod_{k=1}^K w_k^{z_{nk}} Normal(y_n;\mu_k,\tau_k^{-1})^{z_{nk}}$ - * '''Augmented likelihood''': $p(\mathbf{y},\mathbf{z}|\Theta,K) = \prod_{n=1}^N p(y_n,z_n|\Theta,K) = \prod_{n=1}^N p(z_n|\Theta,K) p(y_n|z_n,\Theta,K) = \prod_{n=1}^N \prod_{k=1}^K w_k^{z_{nk}} Normal(y_n;\mu_k,\tau_k^{-1})^{z_{nk}}$ + * '''Maximum-likelihood estimation''': integrate out the latent variables + $\mathrm{ln} \, p(\mathbf{y} | \Theta, K) = \sum_{n=1}^N \mathrm{ln} \, \int_{z_n} \mathrm{d}{z_n} \; p(y_n, z_n | \Theta, K)$ - * '''Priors''': in the Bayesian paradigm, parameters and latent variables are random variables for which we want to infer the posterior distribution. To make the calculations possible, we choose for them prior distributions that are conjuguate with the form of the likelihood. + The latent variables induce dependencies between all the parameters of the model. - ** for the parameters: $\forall k \; \mu_k | \tau_k \sim Normal(\mu_0,(\tau_0 \tau_k)^{-1})$ and $\forall k \; \tau_k \sim Gamma(\alpha,\beta)$ + This makes it difficult to find the parameters that maximize the likelihood. - ** for the latent variables: $\forall n \; z_n \sim Multinomial_K(1,\mathbf{w})$ and $\mathbf{w} \sim Dirichlet(\gamma)$ + An elegant solution is to introduce a variational distribution of parameters and latent variables, which leads to a re-formulation of the classical EM algorithm. + But let's show it directly in the Bayesian paradigm. + * '''Priors''': conjuguate + ** $\forall k \; \mu_k | \tau_k \sim Normal(\mu_0,(\tau_0 \tau_k)^{-1})$ and $\forall k \; \tau_k \sim Gamma(\alpha,\beta)$ + ** $\forall n \; z_n \sim Multinomial_K(1,\mathbf{w})$ and $\mathbf{w} \sim Dirichlet(\gamma)$ - * '''Variational Bayes''': our primary goal here is to calculate the marginal log-likelihood of our data set: + + * '''Variational Bayes''': let's focus here on calculating the marginal log-likelihood of our data set in order to perform model comparison: $\mathrm{ln} \, p(\mathbf{y} | K) = \mathrm{ln} \, \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; p(\mathbf{y}, \mathbf{z}, \Theta | K)$ $\mathrm{ln} \, p(\mathbf{y} | K) = \mathrm{ln} \, \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; p(\mathbf{y}, \mathbf{z}, \Theta | K)$ - - However the fact that there are latent variables induce dependencies between all the parameters of the model. - This makes it difficult to find the parameters that maximize the marginal likelihood. - An elegant solution is to introduce a "variational distribution" $q$ of the parameters and the latent variables $\mathrm{ln} \, p(\mathbf{y} | K) = \mathrm{ln} \, \left( \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \frac{p(\mathbf{y}, \mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} \right) + C_{\mathbf{z}, \Theta}$ $\mathrm{ln} \, p(\mathbf{y} | K) = \mathrm{ln} \, \left( \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \frac{p(\mathbf{y}, \mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} \right) + C_{\mathbf{z}, \Theta}$ Line 46: Line 53: The constant $C$ is here to remind us that $q$ has the constraint of being a distribution, ie. of summing to 1, which can be enforced by a Lagrange multiplier. The constant $C$ is here to remind us that $q$ has the constraint of being a distribution, ie. of summing to 1, which can be enforced by a Lagrange multiplier. - The '''crucial assumption''' is to assume the independence of the parameters and the latent variables: + We can then use the concavity of the logarithm ([http://en.wikipedia.org/wiki/Jensen%27s_inequality Jensen's inequality]) to derive a lower bound of the marginal log-likelihood: + + $\mathrm{ln} \, p(\mathbf{y} | K) \ge \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \mathrm{ln} \, \frac{p(\mathbf{y}, \mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} + C_{\mathbf{z}, \Theta} = \mathcal{F}(q)$ + + Let's call this lower bound $\mathcal{F}(q)$ as it is a [http://en.wikipedia.org/wiki/Functional_%28mathematics%29 functional], ie. a ''function of functions''. To gain some intuition about the impact of introducing $q$, let's expand $\mathcal{F}$: + + $\mathcal{F}(q) = \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \mathrm{ln} \, p(\mathbf{y} | \mathbf{z}, \Theta, K) + \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \mathrm{ln} \, \frac{p(\mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} + C_{\mathbf{z}, \Theta}$ + + $\mathcal{F}(q) = \mathrm{ln} \, p(\mathbf{y} | \mathbf{z}, \Theta, K) - D_{KL}(q || p)$ + + From this, it is clear that $\mathcal{F}$ (ie. a lower-bound of the marginal log-likelihood) is the conditional log-likelihood minus the [http://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence Kullback-Leibler divergence] between the variational distribution $q$ and the joint posterior of latent variables and parameters. + + In practice, we have to make the following crucial assumption of independence on $q$ in order for the calculations to be analytically tractable: + + $q(\mathbf{z}, \Theta) = q_{\mathbf{z}}(\mathbf{z}) q_\Theta(\Theta)$ + + This means that $q_\mathbf{z} q_\Theta$ approximates the joint posterior, and therefore the lower-bound will be tight if and only if this approximation is exact and the KL divergence is zero. + + As we ultimately aim at inferring the parameters and latent variables that maximize the marginal log-likelihood, we will use the [http://en.wikipedia.org/wiki/Calculus_of_variations calculus of variations] to find the functions $q_\mathbf{z}$ and $q_\Theta$ that maximize the functional $\mathcal{F}$. + + $\mathcal{F}(q_\mathbf{z}, q_\Theta) = \int_\Theta \, \mathrm{d}\Theta \; \left( \int_\mathbf{z} \, \mathrm{d}\mathbf{z} \; q(\mathbf{z}) \; \mathrm{ln} \, \frac{p(\mathbf{y}, \mathbf{z} | \Theta, K)}{q(\mathbf{z})} + \mathrm{ln} \, \frac{p(\Theta | K)}{q(\Theta)} \right) + C_{\mathbf{z}} + C_{\Theta}$ + + This naturally leads to a procedure very similar to the EM algorithm where, at the E step, we calculate the expectations of the parameters with respect to the variational distributions $q_\mathbf{z}$ and $q_\Theta$, and, at the M step, we recompute the variational distributions over the parameters. + + + * '''Updates for $q_\mathbf{z}$''': - $q(\mathbf{z}, \Theta) = q(\mathbf{z}) q(\Theta)$ + TODO - We can then use the concavity of the logarithm and Jensen's inequality to optimize a lower bound of the marginal log-likelihood: - $\mathrm{ln} \, p(\mathbf{y} | K) \ge \int_\Theta \, \mathrm{d}\Theta \; \left( \int_\mathbf{z} \, \mathrm{d}\mathbf{z} \; q(\mathbf{z}) \; \mathrm{ln} \, \frac{p(\mathbf{y}, \mathbf{z} | \Theta, K)}{q(\mathbf{z})} + \mathrm{ln} \, \frac{p(\Theta | K)}{q(\Theta)} \right) + C_{\mathbf{z}} + C_{\Theta}$ + * '''Updates for $q_\Theta$''': - Now we have to optimize the right-hand side of the inequality. Let's name it $\mathcal{F}$ as it is a [http://en.wikipedia.org/wiki/Functional_%28mathematics%29 functional], ie. a ''function of functions''. Using the [http://en.wikipedia.org/wiki/Calculus_of_variations calculus of variations], we'll find the function $q$ that maximizes it. + TODO

## Revision as of 19:11, 2 September 2012

Project name Main project page
Previous entry      Next entry

## Variational Bayes approach for the mixture of Normals

• Motivation: I have described on another page the basics of mixture models and the EM algorithm in a frequentist context. It is worth reading before continuing. Here I am interested in the Bayesian approach as well as in a specific variational method (nicknamed "Variational Bayes").

• Data: N univariate observations, $y_1, \ldots, y_N$, gathered into the vector $\mathbf{y}$
• Model: mixture of K Normal distributions
• Parameters: K mixture weights (wk), K means (μk) and K precisions (τk), one per mixture component

$\Theta = \{w_1,\ldots,w_K,\mu_1,\ldots,\mu_K,\tau_1,\ldots,\tau_K\}$

• Constraints: $\sum_{k=1}^K w_k = 1$ and $\forall k \; w_k > 0$.
• Observed likelihood: observations assumed exchangeable (independent and identically distributed given the parameters)

$p(\mathbf{y} | \Theta, K) = \prod_{n=1}^N p(y_n|\Theta,K) = \prod_{n=1}^N \sum_{k=1}^K w_k Normal(y_n;\mu_k,\tau_k^{-1})$

• Latent variables: N hidden variables, $z_1,\ldots,z_N$, each being a vector of length K with a single 1 indicating the component to which the nth observation belongs, and K-1 zeroes.
• Augmented likelihood:

$p(\mathbf{y},\mathbf{z}|\Theta,K) = \prod_{n=1}^N p(y_n,z_n|\Theta,K) = \prod_{n=1}^N p(z_n|\Theta,K) p(y_n|z_n,\Theta,K) = \prod_{n=1}^N \prod_{k=1}^K w_k^{z_{nk}} Normal(y_n;\mu_k,\tau_k^{-1})^{z_{nk}}$

• Maximum-likelihood estimation: integrate out the latent variables

$\mathrm{ln} \, p(\mathbf{y} | \Theta, K) = \sum_{n=1}^N \mathrm{ln} \, \int_{z_n} \mathrm{d}{z_n} \; p(y_n, z_n | \Theta, K)$

The latent variables induce dependencies between all the parameters of the model. This makes it difficult to find the parameters that maximize the likelihood. An elegant solution is to introduce a variational distribution of parameters and latent variables, which leads to a re-formulation of the classical EM algorithm. But let's show it directly in the Bayesian paradigm.

• Priors: conjuguate
• $\forall k \; \mu_k | \tau_k \sim Normal(\mu_0,(\tau_0 \tau_k)^{-1})$ and $\forall k \; \tau_k \sim Gamma(\alpha,\beta)$
• $\forall n \; z_n \sim Multinomial_K(1,\mathbf{w})$ and $\mathbf{w} \sim Dirichlet(\gamma)$

• Variational Bayes: let's focus here on calculating the marginal log-likelihood of our data set in order to perform model comparison:

$\mathrm{ln} \, p(\mathbf{y} | K) = \mathrm{ln} \, \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; p(\mathbf{y}, \mathbf{z}, \Theta | K)$

$\mathrm{ln} \, p(\mathbf{y} | K) = \mathrm{ln} \, \left( \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \frac{p(\mathbf{y}, \mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} \right) + C_{\mathbf{z}, \Theta}$

The constant C is here to remind us that q has the constraint of being a distribution, ie. of summing to 1, which can be enforced by a Lagrange multiplier.

We can then use the concavity of the logarithm (Jensen's inequality) to derive a lower bound of the marginal log-likelihood:

$\mathrm{ln} \, p(\mathbf{y} | K) \ge \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \mathrm{ln} \, \frac{p(\mathbf{y}, \mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} + C_{\mathbf{z}, \Theta} = \mathcal{F}(q)$

Let's call this lower bound $\mathcal{F}(q)$ as it is a functional, ie. a function of functions. To gain some intuition about the impact of introducing q, let's expand $\mathcal{F}$:

$\mathcal{F}(q) = \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \mathrm{ln} \, p(\mathbf{y} | \mathbf{z}, \Theta, K) + \int_\mathbf{z} \int_\Theta \, \mathrm{d}\mathbf{z} \, \mathrm{d}\Theta \; q(\mathbf{z}, \Theta) \; \mathrm{ln} \, \frac{p(\mathbf{z}, \Theta | K)}{q(\mathbf{z}, \Theta)} + C_{\mathbf{z}, \Theta}$

$\mathcal{F}(q) = \mathrm{ln} \, p(\mathbf{y} | \mathbf{z}, \Theta, K) - D_{KL}(q || p)$

From this, it is clear that $\mathcal{F}$ (ie. a lower-bound of the marginal log-likelihood) is the conditional log-likelihood minus the Kullback-Leibler divergence between the variational distribution q and the joint posterior of latent variables and parameters.

In practice, we have to make the following crucial assumption of independence on q in order for the calculations to be analytically tractable:

$q(\mathbf{z}, \Theta) = q_{\mathbf{z}}(\mathbf{z}) q_\Theta(\Theta)$

This means that $q_\mathbf{z} q_\Theta$ approximates the joint posterior, and therefore the lower-bound will be tight if and only if this approximation is exact and the KL divergence is zero.

As we ultimately aim at inferring the parameters and latent variables that maximize the marginal log-likelihood, we will use the calculus of variations to find the functions $q_\mathbf{z}$ and qΘ that maximize the functional $\mathcal{F}$.

$\mathcal{F}(q_\mathbf{z}, q_\Theta) = \int_\Theta \, \mathrm{d}\Theta \; \left( \int_\mathbf{z} \, \mathrm{d}\mathbf{z} \; q(\mathbf{z}) \; \mathrm{ln} \, \frac{p(\mathbf{y}, \mathbf{z} | \Theta, K)}{q(\mathbf{z})} + \mathrm{ln} \, \frac{p(\Theta | K)}{q(\Theta)} \right) + C_{\mathbf{z}} + C_{\Theta}$

This naturally leads to a procedure very similar to the EM algorithm where, at the E step, we calculate the expectations of the parameters with respect to the variational distributions $q_\mathbf{z}$ and qΘ, and, at the M step, we recompute the variational distributions over the parameters.

• Updates for $q_\mathbf{z}$:

TODO