Difference between revisions of "stat841f14"

From statwiki
Jump to: navigation, search
(Theorem)
(Theorem)
Line 215: Line 215:
 
The Legrangian is defined by:
 
The Legrangian is defined by:
  
<math>\ L(x,y,\lambda)</math>
+
<math>\ L(x,y,\lambda)= f(x,y) - \lambda(g(x,y)-c)</math>
  
  

Revision as of 14:51, 17 September 2014

Editor Sign Up

Data Visualization (Fall 2014)

Principal Components Analysis (PCA) (Lecture: Sep. 10, 2014)

Introduction

Principal Component Analysis (PCA), first invented by Karl Pearson in 1901, is a statistical technique for data analysis. Its main purpose is to reduce the dimensionality of the data.

Suppose there is a set of data points in a d-dimensional space. PCA’s goal is to find a linear subspace with lower dimensionality p (p [math]\leq[/math] d), such that it contains as many as possible of data points. The linear subspace can be specified by d orthogonal vectors, such as u1, u2 ... ud which form a new coordinate system, called 'principle components'.In other words, PCA aims to reduce the dimensionality of the data, while preserving its information (or minimizing the loss of information). Information comes from variation. For example, if all data points have the same value along one dimension, that dimension does not carry any information. So, to preserve information, the subspace need to contain components (or dimensions) along which, data has its most variability. However, finding a linear subspace with lower dimensionality which include all data points is not possible in practical problems and loss of information is inevitable. But, we try to reduce this loss and capture most of the features of data.

Figure below, demonstrates an example of PCA. Data is transformed from original 3D space to 2D coordinate system where each coordiante is a principal component.

PCA Plot example

For example<ref> https://onlinecourses.science.psu.edu/stat857/sites/onlinecourses.science.psu.edu.stat857/files/lesson05/PCA_plot.gif </ref>, in the top left corner of the image below, the point [math] x_1 [/math] showed in two-dimensional and it's coordinates are [math](x_1, 1)[/math] and [math](x_1, 2) [/math]

All the red points in the plot represented by their projected values on the two original coordinators (Feature 1, Feature 2).

In PCA technique, it uses new coordinators (showed in green). As coordinate system changed, the points also are shown by their new pair of coordinate values for example [math](z_1, 1)[/math] and [math](z_1, 2)[/math]. These values are determined by original vector, and the relation between the rotated coordinates and the original ones.

PCA Plot.gif

In the original coordinates the two vectors [math]x_1[/math] and [math]x_2[/math] are not linearly uncorrelated. In other words, after applying PCA, if there are two principal component, and use those as the new coordinates, then it is guaranteed that the data along the two coordinates have correlation = 0

By rotating the coordinates we performed an orthonormal transform on the data. After the transform, the correlation is removed - which is the whole point of PCA.

As an example of an extreme case where all of the points lie on a straight line, which in the original coordinates system it is needed two coordinates to describe these points.

In short, after rotating the coordinates, we need only one coordinate and clearly the value on the other one is always zero. This example shows PCA's dimension reduction in an extreme case while in real world points may not fit exactly on a line, but only approximately.


PCA applications

As mentioned, PCA is a method to reduce data dimension if possible to principal components such that those PCs cover as much data variation as possible. This technique is useful in different type of applications which involve data with a huge dimension like Data pre-processing, neuroscience, Computer graphics, meteorology, oceanography, gene expression, economics, and finance among of all other applications.

Data usually is represented by lots of variables. In data preprocessing, PCA is a technique to select a subset of variables in order to figure our best model for data. In neuroscience, PCA used to identify the specific properties of a stimulus that increase a neuron’s probability of generating an action potential.

Figures below show some example of PCA in real world applications. Click to enlarge.


Mathematical Details

PCA is a transformation from original space to a linear subspace with a new coordinate system. Each coordinate of this subspace is called a Principle Component. First principal component is the coordinate of this system along which the data points has the maximum variation. That is, if we project the data points along this coordinate, maximum variance of data is obtained (compared to any other vector in original space). Second principal component is the coordinate in the direction of the second greatest variance of the data, and so on.

Lets denote the basis of original space by [math] \mathbf{v_1}[/math], [math]\mathbf{v_2}[/math], ... , [math]\mathbf{v_d}[/math]. Our goal is to find the principal components (coordinate of the linear subspace), denoted by [math]\mathbf{u_1}[/math], [math]\mathbf{u_2}[/math], ... , [math]\mathbf{u_p}[/math] in the hope that [math] p \leq d [/math]. First, we would like to obtain the first principal component [math]\mathbf{u_1}[/math] or the components in the direction of maximum variance. This component can be treated as a vector in the original space and so is written as a linear combination of the basis in original space.

[math]\mathbf{u_1}=w_1\mathbf{v_1}+w_2\mathbf{v_2}+...+w_d\mathbf{v_d} [/math]

Vector [math] \mathbf{w} [/math] contains the weight of each basis in this combination.


[math]\mathbf{w}=\begin{bmatrix} w_1\\w_2\\w_3\\...\\w_d \end{bmatrix}[/math]

Suppose we have n data points in the original space. We represent each data points by [math] \mathbf{x_1}[/math], [math] \mathbf{x_2} [/math], ..., [math] \mathbf{x_n} [/math]. Projection of each point [math] \mathbf{x_i} [/math] on the [math] \mathbf{u_1} [/math] is [math] \mathbf{w}^T\mathbf{x_i} [/math].

Let [math] \mathbf{S} [/math] be the covariance matrix of data points in original space. The variance of the projected data points, denoted by [math] \Phi [/math] is

[math] \Phi = Var(\mathbf{w}^T \mathbf{x_i}) = \mathbf{w}^T \mathbf{S} \mathbf{w} [/math]

we would like to maximize [math] \Phi [/math] over set of all vectors [math] \mathbf{w} [/math] in original space. But, this problem is not yet well-defined, because for any choice of [math] \mathbf{w} [/math], we can increase [math] \Phi [/math] by simply multiplying [math] \mathbf{w} [/math] in a positive scalar greater than one. So, we add the following constraint to the problem to bound the length of vector [math] \mathbf{w} [/math]:

[math] max \Phi = \mathbf{w}^T \mathbf{S} \mathbf{w} [/math]

subject to : [math]\mathbf{w}^T \mathbf{w} =1 [/math]

Using Lagrange Multiplier technique we have:

[math] L(\mathbf{w} , \lambda ) = \mathbf{w}^T \mathbf{S} \mathbf{w} - \lambda (\mathbf{w}^T \mathbf{w} - 1 ) [/math]

By taking derivative of [math] L [/math] w.r.t. primary variable [math] \mathbf{w} [/math] we have:


[math]\frac{\partial L}{\partial \mathbf{w}}=( \mathbf{S}^T + \mathbf{S})\mathbf{w} -2\lambda\mathbf{w}= 2\mathbf{S}\mathbf{w} -2\lambda\mathbf{w}= 0 [/math]

Note that [math] \mathbf{S}[/math] is symmetric so [math] \mathbf{S}^T = \mathbf{S} [/math].

From the above equation we have:

[math] \mathbf{S} \mathbf{w} = \lambda\mathbf{w} [/math].

So [math] \mathbf{w} [/math] is the eigenvector and [math] \lambda [/math] is the eigenvalue of the matrix [math] \mathbf{S} [/math] .(Taking derivative of [math] L [/math] w.r.t. [math] \lambda [/math] just regenerate the constraint of the optimization problem.)


By multiplying both sides of the above equation to [math]\mathbf{w}^T[/math] and considering the constraint, we obtain:


[math] \Phi = \mathbf{w}^T \mathbf{S} \mathbf{w} = \mathbf{w}^T \lambda \mathbf{w} = \lambda \mathbf{w}^T \mathbf{w} = \lambda [/math]

The interesting result is that objective function is equal to eigenvalue of the covariance matrix. So, to obtain the first principle component, which maximizes the objective function, we just need the eigenvector corresponding to the largest eigenvalue of [math] \mathbf{S} [/math]. Subsequently, the second principal component is the eigenvector corresponding to the second largest eigenvalue, and so on.

Principal Component Extraction using Singular Value Decomposition

Singular Value Decomposition (SVD), is a well-known way to decompose any kind of matrix [math] \mathbf{A} [/math] (m by n) into three useful matrices.

[math] \mathbf{A} = \mathbf{U}\mathbf{\Sigma}\mathbf{V}^T [/math].


where [math] \mathbf{U}[/math] is m by m unitary matrix, [math] \mathbf{UU^T} = I_m [/math], each column of [math] \mathbf{U}[/math] is an eigenvector of [math] \mathbf{AA^T} [/math]

[math] \mathbf{V}[/math] is n by n unitary matrix, [math] \mathbf{VV^T} = I_n [/math], each column of [math]\mathbf{V}[/math] is an eigenvector of [math] \mathbf{A^TA} [/math]

[math] \mathbf{\Sigma}[/math] is m by n diagonal matrix, non-zero elements of this matrix are square roots of eigenvalues of [math] \mathbf{AA^T} [/math].


Now, comparing the concepts of PCA and SVD, one may find out that we can perform PCA using SVD.

Lets construct a matrix p by n with our n data points such that each column of this matrix represent one data point in p-dimensional space:

[math] \mathbf{X} = [\mathbf{x_1} \mathbf{x_2} .... \mathbf{x_n} ] [/math]


and make another matrix [math] \mathbf{X}^* [/math] simply by subtracting the mean of data points from [math] \mathbf{X} [/math].

[math] \mathbf{X}^* = \mathbf{X} - \mu_X [/math]


Then we will get a zero-mean version of our data points for which [math] \mathbf{X^*} \mathbf{X^{*^T}} [/math] is the covariance matrix.

So, [math] \mathbf{\Sigma}[/math] and [math] \mathbf{U}[/math] give us the eigenvalues and corresponding eigenvectors of covariance matrix, respectively. We can then easily extract the desired principal components.


MATLAB Example

In this example we use different pictures of a man's face with different facial expressions. But, these pictures are noisy. So the face is not easily recognizable. The data set consists of 1965 pictures each 20 by 28. So dimensionality is 20*28= 560.

Our goal is to remove noise using PCA and of course by means of SVD function in matlab. We know that noise is not the main feature that makes these pictures look different, so noise is among least variance components. We first extract the principal components and then remove those which correspond to the least values of eigenvalues of covariance matrix. Here, eigenvalues are sorted in a descending order from column to column of matrix S. For example, the first column of matrix U, which corresponds to the first element in S, is the first principal component.

We then reconstruct the picture using first d principal components. If d is too large, we can not completely remove the noise. If it is too small, we will loose some information from original data, for example we may loose the facial expression (smile, sadness and etc.). We can change d until we achieve a reasonable balance between noise reduction and information loss.

File:noisy-face.jpg
Noise reduced version of a picture in MATLAB example
 >> % loading the noisy data, file "noisy" stores our variable X which contains the pictures 
 >> load noisy;
 >>  
 >> 
 >> % show a sample image in column 1 of matrix X
 >> imagesc(reshape(X(:,1),20,28)')
 >> 
 >> % set the color of image to grayscale
 >> colormap gray
 >>
 >> % perform SVD, if X matrix if full rank, will obtain 560 PCs
 >> [U S V] = svd(X);
 >> 
 >> d= 10;
 >> 
 >> % reconstruct X using only the first d principal components and removing noise
 >> XX = U(:, 1:d)* S(1:d,1:d)*U(:,1:d)' ;
 >> 
 >> % show image in column 1 of XX which is a noise reduced version
 >> imagesc(reshape(XX(:,1),20,28)')

PCA Continued, Lagrange Multipliers, Singular Value Decomposition (SVD) (Lecture: Sep. 15, 2014)

Principal Component Analysis (Continued)

PCA is a method to reduce dimensions in data or to extract features from data.

Given a data point in vector form x the goal of PCA is to map x to y where x [math]\isin[/math] [math]\real[/math]d and y [math]\isin[/math] [math]\real[/math]p such that p is much less than d. For example we could map a two-dimensional data point onto any one-dimensional vector in the original 2D space or we could map a three-dimensional data point onto any 2D plane in the original 3D space.

In the case of many data points xi in d-dimensional space:

Xdxn = [x1 x2 ... xn]

There is any infinite amount of vectors in [math]\real[/math]p to which the points in X can be mapped. When mapping these data points to lower dimensions information will be lost. To preserve as much information as possible the points are mapped to a vector in [math]\real[/math]p which will preserve as much variation in the data as possible. In other words, the data is mapped to the vector in [math]\real[/math]p that will have maximum variance. In the images below, the data points mapped onto u in Mapping 1 have greater variance than in Mapping 2, and thus preserves more information in the data.

To project n data points onto u compute Y = uTX.

The variance of Y = uTX can be calculated easily as Y is a 1xn vector.

In higher dimensional space [math]\real[/math]q where q > 1 the concept is Covariance.

The variance of projected points in the direction of vector u is given by:

Var(uTX) = uTSu where S is the sample covariance matrix of X.

In finding the first principal component the objective is to find the direction u which will have the greatest variance of the points projected onto it. This can be expressed as the following optimization problem:

maxu uTSu
s.t. uTu = 1

The restriction uTu = 1 is to bound the problem. If the length of u were not restricted, uTSu would go to infinity as the magnitude of u goes to infinity. The selection of 1 as the bound is arbitrary.

Lagrange Multiplier Review

The Lagrange multiplier is a method for solving optimization problems. After constraining the principal component analysis output vector to an arbitrary size the task of maximizing variance in lower dimensional spaces becomes an optimization problem.

Theorem

The method of Lagrange multipliers states that given the optimization problem:

   [math]max\ f(x,y)[/math] 
      [math]s.t.\ g(x,y) = c [/math]

The Legrangian is defined by:

[math]\ L(x,y,\lambda)= f(x,y) - \lambda(g(x,y)-c)[/math]


[math]\mathbf{w}=\begin{bmatrix} w_1\\w_2\\w_3\\...\\w_d \end{bmatrix}[/math]


[math]L(x,y,lambda) = f(x,y) - lambda(g(x,y)-c)[/math] => delta f(x,y) = delta g(x,y) == delta f(x,y) - lambda delta g(x,y) = 0

Intuitive explanation

Examples

Objective function: f(x,y) = max x - y s.t. g(x,y) = x^2 + y^2 = 1

calculating the Lagrangian gives: L(x,y,lambda) = x - y- lambda(x^2 + y^2 - 1)

Which gives three partial derivatives: delta L/delta x = 1 - lambda(2x) = 0 delta L/delta y = -1 - 2 lambda y = 0 delta L/delta y = x^2 + y - 1 = 0

This gives two solutions: solution 1: x = sqrt(2)/2, y = -sqrt(2)/2, lambda = 1/sqrt(2) solution 2: x = -sqrt(2)/2, y = sqrt(2)/2, lambda = 1/sqrt(2)

Both solutions give extreme values of our objective function. Because this is an optimization problem we are interested in the solution which gives the maximum output.



The optimization problem created in principal component analysis can be solved by calculating the Lagrange multiplier.

this is useful because after normalizing our output vector

Matlab Example, Dimensionality Reduction Using PCA

The following example shows how principal component analysis can be used to identify between a handwritten 2 and a handwritten 3. In the example the original images are each 64 pixels and thus 64 dimensional. By reducing the dimensionality to 2D the 2's and 3's are visualized on a 2D plot and can it could be estimated as to which digit one was.

 >> % load file containing 200 handwritten 2's and 200 handwritten 3's
 >> load 2_3;
 >>  
 >> who?
 >> % displays that X is 64x400 matrix containing the handwritten digits
 >> 
 >> imagesc(reshape(X(:,1),8,8)')
 >> colormap gray
 >> % displays first image, a handwritten 2
 >>
 >> M = mean(X,2)*ones(1,400);
 >> Xb = X-M
 >> % M is matrix of mean of columns repeated 400 times
 >> % Xb is data centred about mean
 >> 
 >> % Now use SVD to find eigenvectors, eigenvalues of Xb
 >> [U S V] = svd(Xb);
 >> 
 >> % 2D projection on points
 >> Y = U(:,1:2)'*X;
 >> 
 >> % 2D projections of 2's
 >> plot(Y(1,1:200),Y(2,1:200),'.')
 >> 
 >> % 2D projections of 3's
 >> plot(Y(1,201:400),Y(2,201:400),'.')