Starting from:

$30

CSC411-Homework 5 Gaussian Discriminant Analysis and Categorial Distribution Solved

1.    Gaussian Discriminant Analysis. For this question you will build classifiers to label images of handwritten digits. Each image is 8 by 8 pixels and is represented as a vector of dimension 64 by listing all the pixel values in raster scan order. The images are grayscale and the pixel values are between 0 and 1. The labels y are 0,1,2,··· ,9 corresponding to which character was written in the image. There are 700 training cases and 400 test cases for each digit; they can be found in a2digits.zip.

Starter code is provided to help you load the data (data.py). A skeleton (q1.py) is also provided for each question that you should use to structure your code.

Using maximum likelihood, fit a set of 10 class-conditional Gaussians with a separate, full covariance matrix for each class. Remember that the conditional multivariate Gaussian probability density is given by,

                                                                                                                                     (1)

You should take . You will compute parameters µkj and Σk for k ∈ (0...9),j ∈

(1...64). You should implement the covariance computation yourself (i.e. without the aid of ’np.cov’). Hint: To ensure numerical stability you may have to add a small multiple of the identity to each covariance matrix. For this assignment you should add 0.01I to each matrix.

(a)    Using the parameters you fit on the training set and Bayes rule, compute the average conditional log-likelihood, i.e. )) on both the train and test set and report it.

(b)    Select the most likely posterior class for each training and test data point as your prediction, and report your accuracy on the train and test set.

(c)    Compute the leading eigenvectors (largest eigenvalue) for each class covariance matrix (can use np.linalg.eig) and plot them side by side as 8 by 8 images.

Report your answers to the above questions, and submit your completed Python code for q1.py.

2.    Categorial Distribution. Let’s consider fitting the categorical distribution, which is a discrete distribution over K outcomes, which we’ll number 1 through K. The probability of each category is explicitly represented with parameter θk. For it to be a valid probability distribution, we clearly need θk ≥ 0 and Pk θk = 1. We’ll represent each observation x as a 1-of-K encoding, i.e, a vector where one of the entries is 1 and the rest are 0. Under this model, the probability of an observation can be written in the following form:

K p(x;θ) = Y θkxk.

k=1

Denote the count for outcome k as Nk, and the total number of observations as N. In the previous assignment, you showed that the maximum likelihood estimate for the counts was:

.

Now let’s derive the Bayesian parameter estimate.

(a)    For the prior, we’ll use the Dirichlet distribution, which is defined over the set of probability vectors (i.e. vectors that are nonnegative and whose entries sum to 1). Its PDF is as follows:

.

A useful fact is that if θ ∼ Dirichlet(a1,...,aK), then

.

Determine the posterior distribution p(θ |D), where D is the set of observations. From that, determine the posterior predictive probability that the next outcome will be k.

(b)    Still assuming the Dirichlet prior distribution, determine the MAP estimate of the parameter vector θ. For this question, you may assume each ak 1.

3.    Factor Analysis. This question is about the EM algorithm. Since some of you will have seen EM in more detail than others before reading week, we have decided to give you the 4 points for free. So you don’t need to submit a solution to this part if you don’t want to. But we recommend you make an effort anyway, since you probably know enough to solve it, and it will help you practice the course material.

In lecture, we covered the EM algorithm applied to mixture of Gaussians models. In this question, we’ll look at another interesting example of EM, namely factor analysis. This is a model very similar in spirit to PCA: we have data in a high-dimensional space, and we’d like to summarize it with a lower-dimensional representation. Unlike PCA, we formulate the problem in terms of a probabilistic model. We assume the latent code vector z is drawn from a standard Gaussian distribution N(0,I), and that the observations are drawn from a diagonal covariance Gaussian whose mean is a linear function of z. We’ll consider the slightly simplified case of scalar-valued z. The probabilistic model is given by:

z ∼ N(0,1) x|z ∼ N(zu,Σ),

where Σ = diag( ). Note that the observation model can be written in terms of coordinates: xj |z ∼ N(zuj,σj).

We have a set of observations , and z is a latent variable, analogous to the mixture component in a mixture-of-Gaussians model.

In this question, we’ll derive both the E-step and the M-step for the EM algorithm. If you don’t feel like you understand the EM algorithm yet, don’t worry; we’ll walk you through it, and the question will be mostly mechanical.

(a)    E-step . In this step, our job is to calculate the statistics of the posterior distribution q(z) = p(z |x) which we’ll need for the M-step. In particular, your job is to find formulas for the (univariate) statistics:

m = E[z |x] = s = E[z2 |x] =

Tips:

•    Compare the model here with the linear Gaussian model of the Appendix. Note that z here is a scalar, while the Appendix gives the more general formulation where x and z are both vectors.

•    Determine p(z |x). To help you check your work: p(z |x) is a univariate Gaussian distribution whose mean is a linear function of x, and whose variance does not depend on x.

•    Once you have figured out the mean and variance, that will give you the conditional expectations.

(b)    M-step . In this step, we need to re-estimate the parameters of the model. The parameters are u and Σ = diag( ). For this part, your job is to derive a formula for unew that maximizes the expected log-likelihood, i.e.,

unew ← argmax. u

(Recall that q(z) is the distribution computed in part (a).) This is the new estimate obtained by the EM procedure, and will be used again in the next iteration of the E-step. Your answer should be given in terms of the m(i) and s(i) from the previous part. (I.e., you don’t need to expand out the formulas for m(i) and s(i) in this step, because if you were implementing this algorithm, you’d use the values m(i) and s(i) that you previously computed.)

Tips:

•    Expand logp(z(i),x(i)) to logp(z(i))+logp(x(i) |z(i)) (log is the natural logarithm).

•    Expand out the PDF of the Gaussian distribution.

•    Apply linearity of expectation. You should wind up with terms proportional to Eq(z(i))[z(i)] and Eq(z(i)[[z(i)]2]. Replace these expectations with m(i) and s(i). You should get an equation that does not mention z(i).

•    In order to find the maximum likelihood parameter unew, you need to take the derivative with respect to uj, set it to zero, and solve for unew.

(c)    M-step, cont’d (optional) Find the M-step update for the observation variances . This can be done in a similar way to part (b).


Appendix: Some Properties of Conditional Gaussians
Consider a multivariate Gaussian random variable z with the mean µ and the covariance matrix Λ−1 (Λ is the inverse of the covariance matrix and is called the precision matrix). We denote this by

p(x) = N(z|µ,Λ−1).

Now consider another Gaussian random variable x, whose mean is an affine function of z (in the form to be clear soon), and its covariance L−1 is independent of z. The conditional distribution of x given z is

p(x|z) = N(x|Az + b,L−1).

Here the matrix A and the vector b are of appropriate dimensions.

In some problems, we are interested in knowing the distribution of z given x, or the marginal distribution of x. One can apply Bayes’ rule to find the conditional distribution p(z|x). After some calculations, we can obtain the following useful formulae:



with

C = (Λ + ALA)−1.

More products