CS229 • Factor Analysis
 Overview
 Restrictions of \(\Sigma\)
 Marginals and conditionals of Gaussians
 The Factor analysis model
 EM for factor analysis
 References
 Citation
Overview

When we have data \(x^{(i)} \in \mathbb{R}^{n}\) that comes from a mixture of several Gaussians, the EM algorithm can be applied to fit a mixture model. In this setting, we usually imagine problems where we have sufficient data to be able to discern the multipleGaussian structure in the data. For instance, this would be the case if our training set size \(m\) was significantly larger than the dimension \(n\) of the data.

Now, consider a setting in which \(n \gg m\). In such a problem, it might be difficult to model the data even with a single Gaussian, much less a mixture of Gaussian. Specifically, since the \(m\) data points span only a lowdimensional subspace of \(\mathbb{R}^{n}\), if we model the data as Gaussian, and estimate the mean and covariance using the usual maximum likelihood estimators,
 we would find that the matrix \(\Sigma\) is singular. This means that \(\Sigma^{1}\) does not exist, and \(\frac{1}{\Sigma^{1/2}}=\frac{1}{0}=\infty\). But both of these terms are needed in computing the usual density of a multivariate Gaussian distribution. Another way of stating this difficulty is that maximum likelihood estimates of the parameters result in a Gaussian that places all of its probability in the affine space spanned by the data, and this corresponds to a singular covariance matrix.
 The data is the set of points \(x\) satisfying \(x=\sum_{i=1}^{m} \alpha_{i} x^{(i)}\), for some \(\alpha_{i}\)’s so that \(\sum_{i=1}^{m} \alpha_{1}=1\).
 More generally, unless \(m\) exceeds \(n\) by some reasonable amount, the maximum likelihood estimates of the mean and covariance may be quite poor. Nonetheless, we would still like to be able to fit a reasonable Gaussian model to the data, and perhaps capture some interesting covariance structure in the data. How can we do this?
 In the next section, we begin by reviewing two possible restrictions on \(\Sigma\), ones that allow us to fit \(\Sigma\) with small amounts of data but neither of which will give a satisfactory solution to our problem. We next discuss some properties of Gaussians that will be needed later; specifically, how to find marginal and conditional distributions of Gaussians. Finally, we present the factor analysis model, and EM for it.
Restrictions of \(\Sigma\)
 If we do not have sufficient data to fit a full covariance matrix, we may place some restrictions on the space of matrices \(\Sigma\) that we will consider. For instance, we may choose to fit a covariance matrix \(\Sigma\) that is diagonal. In this setting, the reader may easily verify that the maximum likelihood estimate of the covariance matrix is given by the diagonal matrix \(\Sigma\) satisfying,
 Thus, \(\Sigma_{j j}\) is just the empirical estimate of the variance of the \(j\)th coordinate of the data.
 Recall that the contours of a Gaussian density are ellipses. A diagonal \(\Sigma\) corresponds to a Gaussian where the major axes of these ellipses are axis aligned.
 Sometimes, we may place a further restriction on the covariance matrix that not only must it be diagonal, but its diagonal entries must all be equal. In this setting, we have \(\Sigma=\sigma^{2} I\), where \(\sigma^{2}\) is the parameter under our control. The maximum likelihood estimate of \(\sigma^{2}\) can be found to be:
 This model corresponds to using Gaussians whose densities have contours that are circles (in 2 dimensions; or spheres/hyperspheres in higher dimensions).
 If we were fitting a full, unconstrained, covariance matrix \(\Sigma\) to data, it was necessary that \(m \geq n+1\) in order for the maximum likelihood estimate of \(\Sigma\) not to be singular. Under either of the two restrictions above, we may obtain nonsingular \(\Sigma\) when \(m \geq 2\).
 However, restricting \(\Sigma\) to be diagonal also means modeling the different coordinates \(x_{i}, x_{j}\) of the data as being uncorrelated and independent. Often, it would be nice to be able to capture some interesting correlation structure in the data. If we were to use either of the restrictions on \(\Sigma\) described above, we would therefore fail to do so. In this section, we will describe the factor analysis model, which uses more parameters than the diagonal \(\Sigma\) and captures some correlations in the data, but also without having to fit a full covariance matrix.
Marginals and conditionals of Gaussians

Before describing factor analysis, we digress to talk about how to find conditional and marginal distributions of random variables with a joint multivariate Gaussian distribution. Suppose we have a vectorvalued random variable,
\[x=\left[\begin{array}{l} x_{1} \\ x_{2} \end{array}\right]\] where \(x_{1} \in \mathbb{R}^{r}, x_{2} \in \mathbb{R}^{s}\), and \(x \in \mathbb{R}^{r+s}\). Suppose \(x \sim \mathcal{N}(\mu, \Sigma)\), where,
 Here, \(\mu_{1} \in \mathbb{R}^{r}, \mu_{2} \in \mathbb{R}^{s}, \Sigma_{11} \in \mathbb{R}^{r \times r}, \Sigma_{12} \in \mathbb{R}^{r \times s}\), and so on. Note that since covariance matrices are symmetric, \(\Sigma_{12}=\Sigma_{21}^{T}\).
 Under our assumptions, \(x_{1}\) and \(x_{2}\) are jointly multivariate Gaussian. What is the marginal distribution of \(x_{1}\)? It is not hard to see that \(\mathrm{E}\left[x_{1}\right]=\mu_{1}\) and that \(\operatorname{Cov}\left(x_{1}\right)=\mathrm{E}\left[\left(x_{1}\mu_{1}\right)\left(x_{1}\mu_{1}\right)\right]=\Sigma_{11}\). To see that the latter is true, note that by definition of the joint covariance of \(x_{1}\) and \(x_{2}\), we have that,
 Matching the upperleft subblocks in the matrices in the second and the last lines above gives the result. since marginal distributions of Gaussians are themselves Gaussian, we therefore have that the marginal distribution of \(x_{1}\) is given by \(x_{1} \sim \mathcal{N}\left(\mu_{1}, \Sigma_{11}\right)\). Also, we can ask, what is the conditional distribution of \(x_{1}\) given \(x_{2}\)? By referring to the definition of the multivariate Gaussian distribution, it can be shown that \(x_{1} \mid x_{2} \sim \mathcal{N}\left(\mu_{1 \mid 2}, \Sigma_{1 \mid 2}\right)\), where,
 When working with the factor analysis model in the next section, these formulas for finding conditional and marginal distributions of Gaussians will be very useful.
The Factor analysis model
 In the factor analysis model, we posit a joint distribution on \((x, z)\) as follows, where \(z \in \mathbb{R}^{k}\) is a latent random variable:
 Here, the parameters of our model are the vector \(\mu \in \mathbb{R}^{n}\), the matrix \(\Lambda \in \mathbb{R}^{n \times k}\), and the diagonal matrix \(\Psi \in \mathbb{R}^{n \times n}\). The value of \(k\) is usually chosen to be smaller than \(n\).

Thus, we imagine that each datapoint \(x^{(i)}\) is generated by sampling a \(k\) dimension multivariate Gaussian \(z^{(i)}\). Then, it is mapped to a \(k\)dimensional affine space of \(\mathbb{R}^{n}\) by computing \(\mu+\Lambda z^{(i)}\). Lastly, \(x^{(i)}\) is generated by adding covariance \(\Psi\) noise to \(\mu+\Lambda z^{(i)}\).

Equivalently, we can therefore also define the factor analysis model according to,
\[\begin{array}{l} z \sim \mathcal{N}(0, I) \\ \epsilon \sim \mathcal{N}(0, \Psi) \\ x=\mu+\Lambda z+\epsilon \end{array}\] where \(\epsilon\) and \(z\) are independent. Let’s work out exactly what distribution our model defines. Our random variables \(z\) and \(x\) have a joint Gaussian distribution.
 We will now find \(\mu_{z x}\) and \(\Sigma\). We know that \(\mathrm{E}[z]=0\), from the fact that \(z \sim \mathcal{N}(0, I)\). Also, we have that,

Putting these together, we obtain \(\mu_{z x}=\left[\begin{array}{l} \overrightarrow{0} \\ \mu \end{array}\right]\)

Next, to find, \(\Sigma\), we need to calculate \(\Sigma_{z z}=\mathrm{E}\left[(z\mathrm{E}[z])(z\mathrm{E}[z])^{T}\right]\) (the upperleft block of \(\Sigma\)), \(\Sigma_{z x}=\mathrm{E}\left[(z\mathrm{E}[z])(x\mathrm{E}[x])^{T}\right]\) (upperright block), and \(\Sigma_{x x}=\mathrm{E}\left[(x\mathrm{E}[x])(x\mathrm{E}[x])^{T}\right]\) (lowerright block). Now, since \(z \sim \mathcal{N}(0, I)\), we easily find that \(\Sigma_{z z}=\operatorname{Cov}(z)=I\). Also,
 In the last step, we used the fact that \(\mathrm{E}\left[z z^{T}\right]=\operatorname{Cov}(z)\) (since \(z\) has zero \(\operatorname{mean})\), and \(\mathrm{E}\left[z \epsilon^{T}\right]=\mathrm{E}[z] \mathrm{E}\left[\epsilon^{T}\right]=0\) (since \(z\) and \(\epsilon\) are independent, and hence the expectation of their product is the product of their expectations). Similarly, we can find \(\Sigma_{x x}\) as follows:
 Putting everything together, we therefore have that,
 Hence, we also see that the marginal distribution of \(x\) is given by \(x \sim\) \(\mathcal{N}\left(\mu, \Lambda \Lambda^{T}+\Psi\right)\). Thus, given a training set \(\left\{x^{(i)} ; i=1, \ldots, m\right\}\), we can write down the log likelihood of the parameters:
 To perform maximum likelihood estimation, we would like to maximize this quantity with respect to the parameters. But maximizing this formula explicitly is hard (try it yourself), and we are aware of no algorithm that does so in closedform. So, we will instead use to the EM algorithm. In the next section, we derive EM for factor analysis.
EM for factor analysis
 The derivation for the Estep is easy. We need to compute \(Q_{i}\left(z^{(i)}\right)=\) \(P\left(z^{(i)} \mid x^{(i)} ; \mu, \Lambda, \Psi\right)\). By substituting the distribution given in Equation \((3)\) into the formulas \((12)\) used for finding the conditional distribution of a Gaussian, we find that \(z^{(i)} \mid x^{(i)} ; \mu, \Lambda, \Psi \sim \mathcal{N}\left(\mu_{z^{(i)} \mid x^{(i)}}, \Sigma_{z^{(i)} \mid x^{(i)}}\right)\), where,
 So, using these definitions for \(\mu_{z^{(i)} \mid x^{(i)}}\) and \(\Sigma_{z^{(i)} \mid x^{(i)}}\), we have,
 Let’s now work out the Mstep. Here, we need to maximize,
 with respect to the parameters \(\mu, \Lambda, \Psi\). We will work out only the optimization with respect to \(\Lambda\), and leave the derivations of the updates for \(\mu\) and \(\Psi\) as an exercise to the reader. We can simplify Equation \((4)\) as follows:
 Here, the “ \(z^{(i)} \sim Q_{i}\) “ subscript indicates that the expectation is with respect to \(z^{(i)}\) drawn from \(Q_{i}\). In the subsequent development, we will omit this subscript when there is no risk of ambiguity. Dropping terms that do not depend on the parameters, we find that we need to maximize:
 Let’s maximize this with respect to \(\Lambda\). Only the last term above depends on \(\Lambda\). Taking derivatives, and using the facts that \(\operatorname{tr} a=a\) (for \(a \in \mathbb{R}\)), \(\operatorname{tr} A B=\operatorname{tr} B A\), and \(\nabla_{A} \operatorname{tr} A B A^{T} C=C A B+C^{T} A B\), we get:
 Setting this to zero and simplifying, we get:
 Hence, solving for \(\Lambda\), we obtain
 It is interesting to note the close relationship between this equation and the normal equation that we’d derived for least squares regression,
 The analogy is that here, the \(x\)’s are a linear function of the \(z\)’s (plus noise). Given the “guesses” for \(z\) that the Estep has found, we will now try to estimate the unknown linearity \(\Lambda\) relating the \(x\)’s and \(z\)’s. It is therefore no surprise that we obtain something similar to the normal equation. There is, however, one important difference between this and an algorithm that performs least squares using just the “best guesses” of the \(z\)’s; we will see this difference shortly.
 To complete our Mstep update, let’s work out the values of the expectations in Equation \((7)\). From our definition of \(Q_{i}\) being Gaussian with mean \(\mu_{z^{(i)} \mid x^{(i)}}\) and covariance \(\Sigma_{z^{(i)} \mid x^{(i)}}\), we easily find,
 The latter comes from the fact that, for a random variable \(Y, \operatorname{Cov}(Y)=\) \(\mathrm{E}\left[Y Y^{T}\right]\mathrm{E}[Y] \mathrm{E}[Y]^{T}\), and hence \(\mathrm{E}\left[Y Y^{T}\right]=\mathrm{E}[Y] \mathrm{E}[Y]^{T}+\operatorname{Cov}(Y)\). Substituting this back into Equation \((7)\), we get the Mstep update for \(\Lambda\):
 It is important to note the presence of the \(\Sigma_{z^{(i)} \mid x^{(i)}}\) on the right hand side of this equation. This is the covariance in the posterior distribution \(P\left(z^{(i)} \mid x^{(i)}\right)\) of \(z^{(i)}\) give \(x^{(i)}\), and the Mstep must take into account this uncertainty about \(z^{(i)}\) in the posterior. A common mistake in deriving EM is to assume that in the Estep, we need to calculate only expectation \(E[z]\) of the latent random variable \(z\), and then plug that into the optimization in the Mstep everywhere \(z\) occurs. While this worked for simple problems such as the mixture of Gaussians, in our derivation for factor analysis, we needed \(E\left[z z^{T}\right]\) as well \(\mathrm{E}[z]\); and as we saw, \(E\left[z z^{T}\right]\) and \(\mathrm{E}[z] \mathrm{E}[z]^{T}\) differ by the quantity \(\Sigma_{z \mid x}\). Thus, the Mstep update must take into account the covariance of \(z\) in the posterior distribution \(P\left(z^{(i)} \mid x^{(i)}\right)\).
 Lastly, we can also find the Mstep optimizations for the parameters \(\mu\) and \(\Psi\). It is not hard to show that the first is given by,
 since this doesn’t change as the parameters are varied (i.e., unlike the update for \(\Lambda\), the right hand side does not depend on \(Q_{i}\left(z^{(i)}\right)=P\left(z^{(i)} \mid x^{(i)} ; \mu, \Lambda, \Psi\right)\) which in turn depends on the parameters), this can be calculated just once and needs not be further updated as the algorithm is run. Similarly, the diagonal \(\Psi\) can be found by calculating,
 and setting \(\Psi_{i i}=\Phi_{i i}\) (i.e., letting \(\Psi\) be the diagonal matrix containing only the diagonal entries of \(\Phi\)).
References
Citation
If you found our work useful, please cite it as:
@article{Chadha2020DistilledFactorAnalysis,
title = {Factor Analysis},
author = {Chadha, Aman},
journal = {Distilled Notes for Stanford CS229: Machine Learning},
year = {2020},
note = {\url{https://aman.ai}}
}