# Generative Learning Algorithm

A Chinese version of this section is available. It can be found here. The Chinese version will be synced periodically with English version. If the page is not working, you can check out a back-up link here.

# 1 Discriminative Model

The discriminative model is the model where algorithms try to directly classify a label for input such as logstic regression and perceptron algorithm. The discriminative model does not have a concept of what the object might look like. They just classify. It cannot generate a new image based on the class.

Formally, it is $p(y\lvert x;\theta)$ where p can be any classification model such as logistic regression model.

# 2 Generative Model

On the other hand, the generative model is the models that fisrt try to learn what each object might look like. Then, based on input, it gives a probability of the input being this class. It has the concepts on what the object might look like. It can generate a new image based on the past knowledge.

The classical example is naive Bayes classifier. In this case, we have a class prior. A prior distribution is basically the knowledge that we assume for the data distribution. We just assume that the model we select as prior is a proper model for explaining hidden information in dataset. From dataset, we can know what parameters are the best fit for the model we selected. With calculated class prior model, we can use Bayes rule to calculate the probability of being each class and then take the one with a bigger value. Meanwhile, with a certain prior, we can generate a new sample from prior model and then generate new features based on the chosen prior. This is so-called generative process.

# 3 Gaussian Discriminant Analysis

Gaussian discriminant analysis (GDA) model is a generative model where $p(x\lvert y)$ is a multi-variate Gaussian. So I will start talking about multi-veriate Gaussian.

## 3.1 The Multivariate Normal Distribution

In multivariate normal distribution, a random variable is vector-valued in $\mathbb{R}^n$ where n is the number of dimensionality. Thus, multivariate Gaussian has mean vector $\mu\in \mathbb{R}^n$ and covariance matrix $\Sigma\in\mathbb{R}^{n\times n}$ where $\Sigma$ is sysmetric and postive semi-definite. The density is:

As said, the expectation is $\mu$.

The covariance for a vector-values random variable Z:

An example of plot of density function with zero mean but different covariance can be shwon below. In this example, we have covariance frome left from right:

# 4 GDA and logistic regression

## 4.1 GDA

Let’s talk about binary classification problem again. We can use multivariate Gaussian to model $p(x\lvert y)$. Put all together, we have:

where $\phi, \mu_0,\mu_1,\Sigma$ is the parameters that we want to find out. Note that although we have different mean for different classes, we have shared covariance between different classes.

Why is it a generative model? In short, we have a class prior on y, which is a Bernoulli. The generative process is to (1) sample a class from Bernoulli. (2) Based on the class label, we sample a x from corresponding distribution. This is generative process.

Then, the log likelihood of data is:

In the above equation, we plug in each distribution without specifying a class. We just abstract it as k. Then, we have:

Now, we need to take derivative w.r.t. each parameter and set to zero to find the argmax. Some formules might be useful for the derivation.

$\frac{\partial x^TAx}{\partial x} = 2x^TA$ iff A is symmetric and independent of x

Proof: A is symmetric so $A=A^T$ and assume the dimension is n.

Jacobi’s formula:

Proof:

Proof:

This proof is a bit complicated. You should know Kronecker delta and Frobenius inner product beforehand.

For a matrix X, we can write:

You can think of $\mathcal{H}$ as a identity element for the Frobenius product.

Before starting the proof, let’s prepare to find the derivative of inverse matrix. That is, $\frac{\partial X^{-1}}{\partial X}$.

So we can solve it as:

Then, back to the original:

where F means Frobenius inner product.

Then, plug it back:

Now, we are good to go for finding gradient for each parameter.

For $\phi$:

For $\mu_k$:

For $\Sigma$:

The results can be shown as: Note that we have shared covariance so the shape of two contours are the same but the means are different. On the boundary line, we have probability of 0.5 for each class.

## 4.2 GDA and Logistic Regression

How is GDA is related with logistic regression? We can see that if $P(x\lvert y)$ above is multivariate gaussian with shared covariance, then we can calculate $P(y\lvert x)$ and find out that it follows a logistic function. To see this, we can have:

Since Gaussian is member of Exponential family, we can eventually turn the ratio in the denominator to $exp(\theta^Tx)$ where $\theta$ is a function of $\phi,\mu_0,\mu_1,\Sigma$.

Similarly, if $P(x\lvert y)$ is Possion with different $\lambda$, then $P(y\lvert x)$ also follows a logistic function. It means that GDA requires a strong assumption that data of each class can be modeled with a gaussian with shared covariance. However, GDA will fit better and train faster if this assumption is correct.

On the other side, if assumption cannot be made, logistic regression is less sensitive. So you can directly use logistic regression without touching Gaussian assumption or Possion assumption.

# 5 Naive Bayes

In GDA, random variables are supposed to be continuous-valued. In Naive Bayes, it is for learning discrete valued random variables like text classification. Text classification is to classify text based on the words in it to a binary class. In text classification, a word vector is used for training. A word vector is like a dictionary. The length of the vector is the number of words. A word is represented by a 1 on certain position and elsewhere with 0’s in the vector.

For example, a vector of an email can be:

where first two words might refer to “sport” and “basketball”.

However, this might not work. Say, if we have 50000 words (len(x)=50000) and try to model it as multinomial. Formally, we can model $p(x\lvert y)$ in this case where p is a multinomial. Since each word is either there or not there, which is binary case. For multinomial, we have to model all the possibility, which means the number of class is all the combinations of possible outcomes for an email. In this way, for a given class, each word can be either dependent and independent. It does not matter. We model it into Multinomial. But the dimension of parameter is $2^{50000}-1$, which is too large. Thus, to solve it, we make Naive Bayes Assumption:

Each word is conditionally independent to each other based on given class.

What it means is that if, say, an email is known in the class of sport, the appearance of word “basketball” is independent of that of word “dunk”. In this way, we can model each word indepently since we assume they are independent given a class. We can then model it as Bernoulli. We know this might not be true. That’s why we call it naive. However, based on my experience, this will give you fairly good results. Removing this assumption requires a lot of additional calculations on dependency.

Then, we have:

We apply probability law of chain rule for the first step and naive basyes assumption for the second step.

After finding the max of log joint likelihood, which is:

where $\phi_{j\lvert y=1} = P(x_j = 1 \lvert y = 1)$, $\phi_{j\lvert y=0} = P(x_j = 1 \lvert y = 0)$ and $\phi_y = p(y=1)$. Those are the parameters that we want to learn. All three parameters are the parameters of Bernoulli.

We can find the derivative and solve them:

Now, the number of parameters are around 100000 since 50000 parameters are learned for each given class. This is much much less than before.

To predict for a new sample, we can use Bayes Rule to calculate $P(y=1\lvert x)$ and compare which is higher.

Ext: In this case, we model $P(x_i\lvert y)$ as Bernouli since it is binary valued. That is, it can be either ‘have that word’ or ‘not have that word’. Bernouli takes class label as input and models its probability but it has to binary. To deal with non-binary valued $x_i$, we can model it as Multinomial distribution, which can be parameterized with multiple classes.

Summary: Naive Bayes is for discrete space. GDA is for continuous space. We can alsway discretize our random variable from continuous to discrete space.

# 6 Laplace smoothing

The above shwon example is generally good but will possibly fail where a new word which does not exist in the past training samples appear in the coming email. In such case, it would cause $\phi$ for both classes to become zero because the models never see the word before. The model will fail to make prediction.

This motivates a solution called Laplace Smoothing, which sets each parameter as:

where k is the number of classes. In reality, the Laplace smoothing does not make too much difference since it usually has all the words but it is good to have it here.

Tags: