Docsity

Factor Analysis Lecture Notes: Mixture of Gaussians and Singular Covariance Matrices, Study notes of Machine Learning

Toggle sidebar
bg1
CS229 Lecture notes
Andrew Ng
Part X
Factor analysis
When we have data x(i)∈Rnthat 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 were the we have sufficient data to be able
to discern the multiple-Gaussian structure in the data. For instance, this
would be the case if our training set size mwas significantly larger than the
dimension nof the data.
Now, consider a setting in which nm. 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 mdata points span only a low-dimensional
subspace of Rn, if we model the data as Gaussian, and estimate the mean
and covariance using the usual maximum likelihood estimators,
µ=1
m
m
X
i=1
x(i)
Σ = 1
m
m
X
i=1
(x(i)−µ)(x(i)−µ)T,
we would find that the matrix Σ is singular. This means that Σ−1does not
exist, and 1/|Σ|1/2= 1/0. 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,1and this corresponds to a singular covariance matrix.
1This is the set of points xsatisfying x=Pm
i=1 αix(i), for some αi’s so that Pm
i=1 α1=
1.
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Factor Analysis Lecture Notes: Mixture of Gaussians and Singular Covariance Matrices and more Study notes Machine Learning in PDF only on Docsity!

CS229 Lecture notes

Andrew Ng

Part X

Factor analysis

When we have data x(i)^ ∈ Rn^ 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 were the we have sufficient data to be able to discern the multiple-Gaussian 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  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 low-dimensional subspace of Rn, if we model the data as Gaussian, and estimate the mean and covariance using the usual maximum likelihood estimators,

μ =

m

∑^ m

i=

x(i)

m

∑^ m

i=

(x(i)^ − μ)(x(i)^ − μ)T^ ,

we would find that the matrix Σ is singular. This means that Σ−^1 does not exist, and 1/|Σ|^1 /^2 = 1/0. 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,^1 and this corresponds to a singular covariance matrix.

(^1) This is the set of points x satisfying x = ∑m i=1 αix (i), for some αi’s so that ∑m i=1 α^1 =

More generally, unless m exceeds n by some reasonable amount, the max- imum 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 Σ, ones that allow us to fit Σ 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 conditonal distributions of Gaussians. Finally, we present the factor analysis model, and EM for it.

1 Restrictions of Σ

If we do not have sufficient data to fit a full covariance matrix, we may place some restrictions on the space of matrices Σ that we will consider. For instance, we may choose to fit a covariance matrix Σ 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 Σ satisfying

Σjj =

m

∑^ m

i=

(x( ji )− μj )^2.

Thus, Σjj 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 Σ 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 Σ = σ^2 I, where σ^2 is the parameter under our control. The maximum likelihood estimate of σ^2 can be found to be:

σ^2 =

mn

∑^ n

j=

∑^ m

i=

(x( ji )− μj )^2.

This model corresponds to using Gaussians whose densities have contours that are circles (in 2 dimesions; or spheres/hyperspheres in higher dimen- sions).

that

Cov(x) = Σ

=

[

]

= E[(x − μ)(x − μ)T^ ]

= E

[(

x 1 − μ 1 x 2 − μ 2

x 1 − μ 1 x 2 − μ 2

)T ]

= E

[

(x 1 − μ 1 )(x 1 − μ 1 )T^ (x 1 − μ 1 )(x 2 − μ 2 )T (x 2 − μ 2 )(x 1 − μ 1 )T^ (x 2 − μ 2 )(x 2 − μ 2 )T

]

Matching the upper-left 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 ∼ N (μ 1 , Σ 11 ). 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 |x 2 ∼ N (μ 1 | 2 , Σ 1 | 2 ), where

μ 1 | 2 = μ 1 + Σ 12 Σ− 221 (x 2 − μ 2 ), (1) Σ 1 | 2 = Σ 11 − Σ 12 Σ− 221 Σ 21. (2)

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.

3 The Factor analysis model

In the factor analysis model, we posit a joint distribution on (x, z) as follows, where z ∈ Rk^ is a latent random variable:

z ∼ N (0, I) x|z ∼ N (μ + Λz, Ψ).

Here, the parameters of our model are the vector μ ∈ Rn, the matrix Λ ∈ Rn×k, and the diagonal matrix Ψ ∈ Rn×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 Rn^ by computing μ + Λz(i). Lastly, x(i)^ is generated by adding covariance Ψ noise to μ + Λz(i). Equivalently (convince yourself that this is the case), we can therefore also define the factor analysis model according to

z ∼ N (0, I)  ∼ N (0, Ψ) x = μ + Λz + .

where  and z are independent. Lets work out exactly what distribution our model defines. Our random variables z and x have a joint Gaussian distribution [ z x

]

∼ N (μzx, Σ).

We will now find μzx and Σ. We know that E[z] = 0, from the fact that z ∼ N (0, I). Also, we have that

E[x] = E[μ + Λz + ] = μ + ΛE[z] + E[] = μ.

Putting these together, we obtain

μzx =

[

μ

]

Next, to find, Σ, we need to calculate Σzz = E[(z − E[z])(z − E[z])T^ ] (the upper-left block of Σ), Σzx = E[(z − E[z])(x − E[x])T^ ] (upper-right block), and Σxx = E[(x − E[x])(x − E[x])T^ ] (lower-right block). Now, since z ∼ N (0, I), we easily find that Σzz = Cov(z) = I. Also,

E[(z − E[z])(x − E[x])T^ ] = E[z(μ + Λz +  − μ)T^ ] = E[zzT^ ]ΛT^ + E[zT^ ] = ΛT^.

In the last step, we used the fact that E[zzT^ ] = Cov(z) (since z has zero mean), and E[zT^ ] = E[z]E[T^ ] = 0 (since z and  are independent, and

Lets now work out the M-step. Here, we need to maximize ∑^ m

i=

z(i)

Qi(z(i)) log

p(x(i), z(i); μ, Λ, Ψ) Qi(z(i))

dz(i)^ (4)

with respect to the parameters μ, Λ, Ψ. We will work out only the optimiza- tion with respect to Λ, and leave the derivations of the updates for μ and Ψ as an exercise to the reader. We can simplify Equation (4) as follows: ∑^ m

i=

z(i)

Qi(z(i))

[

log p(x(i)|z(i); μ, Λ, Ψ) + log p(z(i)) − log Qi(z(i))

]

dz(i)^ (5)

∑^ m

i=

Ez(i)∼Qi

[

log p(x(i)|z(i); μ, Λ, Ψ) + log p(z(i)) − log Qi(z(i))

]

Here, the “z(i)^ ∼ Qi” subscript indicates that the expectation is with respect to z(i)^ drawn from Qi. 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:

∑^ m

i=

E

[

log p(x(i)|z(i); μ, Λ, Ψ)

]

∑^ m

i=

E

[

log

(2π)n/^2 |Ψ|^1 /^2

exp

(x(i)^ − μ − Λz(i))T^ Ψ−^1 (x(i)^ − μ − Λz(i))

)]

∑^ m

i=

E

[

log |Ψ| −

n 2

log(2π) −

(x(i)^ − μ − Λz(i))T^ Ψ−^1 (x(i)^ − μ − Λz(i))

]

Lets maximize this with respect to Λ. Only the last term above depends on Λ. Taking derivatives, and using the facts that tr a = a (for a ∈ R), trAB = trBA, and ∇AtrABAT^ C = CAB + CT^ AB, we get:

∑^ m

i=

−E

[

(x(i)^ − μ − Λz(i))T^ Ψ−^1 (x(i)^ − μ − Λz(i))

]

∑^ m

i=

∇ΛE

[

−tr

z(i)

T ΛT^ Ψ−^1 Λz(i)^ + trz(i)

T ΛT^ Ψ−^1 (x(i)^ − μ)

]

∑^ m

i=

∇ΛE

[

−tr

ΛT^ Ψ−^1 Λz(i)z(i)

T

  • trΛT^ Ψ−^1 (x(i)^ − μ)z(i)

T

]

∑^ m

i=

E

[

−Ψ−^1 Λz(i)z(i)

T

  • Ψ−^1 (x(i)^ − μ)z(i)

T ]

Setting this to zero and simplifying, we get:

∑^ m

i=

ΛEz(i)∼Qi

[

z(i)z(i)

T ]

∑^ m

i=

(x(i)^ − μ)Ez(i)∼Qi

[

z(i)

T ]

Hence, solving for Λ, we obtain

( (^) m ∑

i=

(x(i)^ − μ)Ez(i)∼Qi

[

z(i)

T ]

) ( (^) m ∑

i=

Ez(i)∼Qi

[

z(i)z(i)

T ]

It is interesting to note the close relationship between this equation and the normal equation that we’d derived for least squares regression,

“θT^ = (yT^ X)(XT^ X)−^1 .”

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 E-step has found, we will now try to estimate the unknown linearity Λ 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 M-step update, lets work out the values of the expecta- tions in Equation (7). From our definition of Qi being Gaussian with mean μz(i)|x(i) and covariance Σz(i)|x(i) , we easily find

Ez(i)∼Qi

[

z(i)

T ]

= μTz(i)|x(i)

Ez(i)∼Qi

[

z(i)z(i)

T ]

= μz(i)|x(i) μTz(i)|x(i) + Σz(i)|x(i).

The latter comes from the fact that, for a random variable Y , Cov(Y ) = E[Y Y T^ ] − E[Y ]E[Y ]T^ , and hence E[Y Y T^ ] = E[Y ]E[Y ]T^ + Cov(Y ). Substitut- ing this back into Equation (7), we get the M-step update for Λ:

( (^) m ∑

i=

(x(i)^ − μ)μTz(i)|x(i)

) ( (^) m ∑

i=

μz(i)|x(i) μTz(i)|x(i) + Σz(i)|x(i)

It is important to note the presence of the Σz(i)|x(i) on the right hand side of

this equation. This is the covariance in the posterior distribution p(z(i)|x(i)) of z(i)^ give x(i), and the M-step must take into account this uncertainty