### Principal Component Analysis (PCA)

PCA is a linear model in mapping d-dimensional input features to k-dimensional latent factors (k principal components).

Notation:

Dimension reduction can be used for data compression or visualization.

### Example

For example, here is the $W$ to match a RGB pixel to one of the 4 color palete:

In our example, we have 4 principal components/latent factors. $(R_1, G_1, B_1), (R_2, G_2, B_2), (R_3, G_3, B_3) \text{ and } (R_4, G_4, B_4).$

The $G$ value for $z=(z_1, z_2, z_3, z_4)$ is reconstructed as.

For d = 2 and k = 1, 2D features (purple dots) are projected onto the 1D blue line.

PCA selects a projection that can maximize the variance of their output. Hence, PCA will pick the blue line over the green line if it has a higher variance.

#### Matrix factorization

PCA can formulated as an approximation to the matrix factorization.

### PCA Cost Function

We want to minimize the MSE for $x$ and the corresponding value for the latent variable $z$. $(\hat{x} = w^T_j z_i)$:

which

### Solving W

First, we need to perform feature scaling on input features $x_i$:

$x^i$ is the ith training datapoints.

which $\mu_j \text{ and } \sigma_j$ are the mean and standard deviation for the feature $x_i$. For an image, they are the means and standard deviations of each pixel. For a 100x100x3 image, we will have 30,000 $\mu_j \text{ and } \sigma_j$.

PCA is based on unsupervised learning. We want to optimize the latent factors $W$ and latent variables $Z$ for the cost function $J$

One of the method to solve PCA is to use Gradient descent to optimize the trainable parameters $W$ and $Z$ with the cost function above.

### Alternating minimization:

Alternating minimization is another method to find a solution for PCA.

• Optimize ‘W’ with ‘Z’ fixed
• Optimize ‘Z’ with ‘W’ fixed
• Keep repeating

### Singular value decomposition (SVD)

Both methods above solve the PCA using empirical method. SVD solves the PCA analytically. Before discussing it in details, we discuss the Singular value decomposition first (SVD). SVD decompose a matrix into 3 matrice as:

The matrix $U$ and $V$ is later used to transfrom $x$ to $z$ in PCA.

SVD consists of

• Finding the eigenvalues and eigenvectors of $AA^T$ and $A^TA$
• The eigenvectors of $AA^T$ make up the columns of U
• The eigenvectors of $A^TA$ make up the columns of V
• The singular values in S are square roots of eigenvalues from $AA^T$ or $A^TA$

Let’s go through an example:

The eigenvector $X$ and eigenvalue $\lambda$ of $A$ is defined as:

Now solving:

The eigenvalues are:

We always sort lambda in the descending order. The kth highest eigenvectors will be used for $W$.

For $\lambda_1 = 29.88$

which is the first column of $U$.

For $\lambda_2 = 0.118$

which is the second column of $U$.

We can skip all the eigenvalues = 0. Hence:

Similarly, we calculate $A^TA$ to find $V$ which is:

The singular values in S are square roots of eigenvalues from $AA^T$ or $A^TA$:

The sample above is originated from [http://web.mit.edu/be.400/www/SVD/Singular_Value_Decomposition.htm]

Now we apply SVD to solve PCA. First we compute the covariance matrix with our $m$ n-Dimensional training datapoints $x$.

$\Sigma$ is a nxn matrix

Apply SVD to decompose $\Sigma$ to $U$:

Which $U$ have the dimension of:

We only take the first k columns:

To transform $X$ to $Z$:

Let’s check the dimensionality again:

To convert $z$ to $x$:

### Predicting Z

Given $W$ and testing data $\hat{X}$, $\hat{Z}$ is computed by first subtract the training mean from the features and then calculate $Z$.

### Choosing the number of latent factors k

PCA is about maximizing variance. Say to keep 90% of variance, we set $R = 0.1$.

Alternatively, we can start from k=1 and increment it until say $T \lt 0.01$.

$S$ stores the eigenvalues of the eigenvectors but also reflect how important for a particular latent factors. Hence, we can also determine k by:

### W uniqueness (orthogonal)

The solution for W is not unique. But we can impose a few restrictions to solve this.

• Set the magnitude of the principal component to 1. ($W_{c_i}$: row $c_i$ of $w$)
• Each latent factors are independent of each other (cross product = 0).

We can still have the factors $W_{c_i}$ rotated or label switching $W_{c_i}$ switch to $W_{c_i^{'}}$. To fix this, we can

• First set k = 1, and solve W with the constraint above
• Set k = 2 with the first factor set and solve $W$ for the second factor $W_2$
• Repeat until reaching our target k

The blue line is our first optimized W for $k = 1$. The optimal solution $W$ for $k = 2$ is the red line orthogonal to the blue line.

With these constraints:

Solving Z becomes:

### Eigenfaces

Eigenfaces apply PCA to represent a facial image with latent factors. First we compute the mean image and the top k eigenvectors (Principal components)

The image is encoded with the latent factors:

### Non-negative matrix factorization (NMF):

NMP is similar to the matrix factorization methods above with the same cost functions, except that we add an additional constraint that $W$ and $Z$ are non-negative.

IN NMF,

• $W$ and $Z$ are non-negative instead of orthogonal
• Promote sparsity
• Avoiding postive & negative matrix elements cancelling each other
• Brain seem to use sparse representation
• Energy efficient
• Increase the number of concepts that can memorize
• Learning the parts of objects

In NMF. the latent factors are more close to individual facial features.

Credit: Daniel D. Lee: Learning the parts of objects by non-negative matrix factorization

### Sparse Matrix Factorization

Sparse Matrix Factorization promotes the sparsity of $W$.

Here is the plot of J with an optimized $w$ smaller than 0.

With the constraint of $w>0$, the optimized cost is now at $w=0$ (promote sparsity).

We are going to use Gradient descent to train $W$ and reset the value to 0 if it is negative:

#### L1-regularization

We can also use L1 regularization to control sparsity:

Here is the visualization of latent factors with different techniques:

Credit: Julien Mairal etc… Online Learning for Matrix Factorization and Sparse Coding

### Regularized Matrix Factorization

Instead of forcing orthogonality, we can add a L2 regularization cost to control how $W$ is optimized.

### Latent Factor Model using logistic loss

We can also use logistic loss for our cost function

### Robust PCA

To reduce the effects of outlier, Robust PCA switch to a L1-norm in calculating the errors.