Principal Component Analysis

Principal component analysis (PCA) is a mathematical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components. This transformation is defined in such a way that the first principal component has the largest possible variance (i.e., accounts for as much of the variability in the data as possible), and each succeeding component in turn has the highest variance possible under the constraint that it be orthogonal to (i.e., uncorrelated with) the preceding components.

See the Technical Background for an introduction to principal component analysis and the implementation notes.

- Training Function
- The training functions have the following formats:
pca_train( source_table, out_table, row_id, components_param, grouping_cols, lanczos_iter, use_correlation, result_summary_table )

andpca_sparse_train( source_table, out_table, row_id, col_id, val_id, row_dim, col_dim, components_param, grouping_cols, lanczos_iter, use_correlation, result_summary_table )

**Arguments**

- source_table
TEXT. Name of the input table containing the data for PCA training. The input data matrix should have rows and columns, where is the number of data points, and is the number of features for each data point.

A dense input table is expected to be in the one of the two standard MADlib dense matrix formats, and a sparse input table should be in the standard MADlib sparse matrix format.

The two standard MADlib dense matrix formats are

{TABLE|VIEW}

*source_table*(*row_id*INTEGER,*row_vec*FLOAT8[], )and

{TABLE|VIEW}

*source_table*(*row_id*INTEGER,*col1*FLOAT8,*col2*FLOAT8, ... )Note that the column name

*row_id*is taken as an input parameter, and should contain a continguous list of row indices (starting at 1) for the input matrix.The input table for sparse PCA is expected to be in the form:

{TABLE|VIEW}

*source_table*( ...*row_id*INTEGER,*col_id*INTEGER,*val_id*FLOAT8, ... )The

*row_id*and*col_id*columns specify which entries in the matrix are nonzero, and the*val_id*column defines the values of the nonzero entries.- out_table
TEXT. The name of the table that will contain the output. The output is divided into three tables.

The primary output table (

*out_table*) encodes the principal components with the*k*highest eigenvalues where*k*is either directly provided by the user or computed according to the proportion of variance. The table has the following columns:row_id Eigenvalue rank in descending order of the eigenvalue size. principal_components Vectors containing elements of the principal components. std_dev The standart deviation of each principal component. proportion The proportion of variance covered by the principal component. The table

*out_table*_mean contains the column means. This table has just one column:column_mean A vector containing the column means for the input matrix. The optional table

*result_summary_table*contains information about the performance of the PCA. The contents of this table are described under the*result_summary_table*argument.- row_id
TEXT. Column name containing the row IDs in the input source table. The column needs to be of type that can be cast to INT and should only contain values between 1 and

*N*. For dense matrix format, it should contain all continguous integers from 1 to*N*.- col_id
TEXT. Column name of containing the col IDS in sparse matrix representation (sparse matrices only). The column should be of type that can be cast to INT and contain values between 1 and

*M*.- val_id
TEXT. Name of 'val_id' column in sparse matrix representation (sparse matrices only).

- row_dim
INTEGER. The number of rows in the sparse matrix (sparse matrices only).

- col_dim
INTEGER. The number of columns in the sparse matrix (sparse matrices only).

- components_param
INTEGER or FLOAT. The parameter to control the number of principal components to calculate from the input data. If 'components_param' is INTEGER, it is used to denote the number of principal components (

*k*) to compute. If 'components_param' is FLOAT, the algorithm will return enough principal vectors so that the ratio of the sum of the eigenvalues collected thus far to the sum of all eigenvalues is greater than this parameter (proportion of variance). The value of 'components_param' must be either a positive INTEGER or a FLOAT in the range (0.0,1.0]- Note
- The difference in interpretation between INTEGER and FLOAT was introduced to maintain backward campatibility after the proportion of variance feature was introduced. A special case to be aware of: 'components_param' = 1 (INTEGER) will return 1 principal component, but 'components_param' = 1.0 (FLOAT) will return all principal components, i.e., proportion of variance of 100%.

- grouping_cols (optional)
TEXT, default: NULL.

- Note
*Not currently implemented. Any non-NULL value is ignored. Grouping support will be added in a future release.*The parameter is planned to be implemented as a comma-separated list of column names, with the source data grouped using the combination of all the columns. An independent PCA model will be computed for each combination of the grouping columns.

- lanczos_iter (optional)
INTEGER, default: minimum of {k+40, smallest matrix dimension}. The number of Lanczos iterations for the SVD calculation. The Lanczos iteration number roughly corresponds to the accuracy of the SVD calculation, and a higher iteration number corresponds to greater accuracy but longer computation time. The number of iterations must be at least as large as the value of

*k*, but no larger than the smallest dimension of the matrix. If the iteration number is given as zero, then the default number of iterations is used.- Note
- If both 'lanczos_iter' and proportion of variance (via the 'components_param' parameter) are defined, 'lanczos_iter' will take precedence in determining the number of principal components (i.e. the number of principal components will not be greater than 'lanczos_iter' even if the target proportion had not been reached).

- use_correlation (optional)
BOOLEAN, default FALSE. Whether to use the correlation matrix for calculating the principal components instead of the covariance matrix. Currently

*use_correlation*is a placeholder for forward compatibility, and this value must be set to false.- result_summary_table (optional)
TEXT, default NULL. Name of the optional summary table. When NULL, no summary table is generated.

This sumary table has the following columns:

rows_used INTEGER. Number of data points in the input. exec_time (ms) FLOAT8. Number of milliseconds for the PCA calculation to run. iter INTEGER. Number of iterations used in the SVD calculation. recon_error FLOAT8. The absolute error in the SVD approximation. relative_recon_error FLOAT8. The relative error in the SVD approximation. use_correlation BOOLEAN. Indicates if the correlation matrix was used.

- Examples

- View online help for the PCA training function.
SELECT madlib.pca_train();

- Create the sample data.
DROP TABLE IF EXISTS mat; CREATE TABLE mat ( row_id integer, row_vec double precision[] ); COPY mat (row_id, row_vec) FROM stdin DELIMITER '|'; 1|{1,2,3} 2|{2,1,2} 3|{3,2,1} \.

- Run the PCA function for a fixed number of components:
DROP TABLE IF EXISTS result_table; DROP TABLE IF EXISTS result_table_mean; SELECT pca_train( 'mat', 'result_table', 'row_id', 3 );

- View the PCA results:
SELECT * FROM result_table;

Resultrow_id | principal_components | std_dev | proportion --------+--------------------------------------------------------------+----------------------+---------------------- 1 | {-0.707106781186547,-1.6306400674182e-16,0.707106781186547} | 1.41421356237309 | 0.857142857142245 2 | {-1.66533453693773e-16,1,5.55111512312578e-17} | 0.577350269189626 | 0.142857142857041 3 | {-0.707106781186548,1.11022302462516e-16,-0.707106781186547} | 1.59506745224211e-16 | 1.09038864737157e-32

- Run the PCA function for a proportion of variance:
DROP TABLE IF EXISTS result_table; DROP TABLE IF EXISTS result_table_mean; SELECT pca_train( 'mat', 'result_table', 'row_id', 0.9 );

- View the PCA results:
SELECT * FROM result_table;

Resultrow_id | principal_components | std_dev | proportion --------+--------------------------------------------------------------+-------------------+------------------- 1 | {-0.707106781186548,-3.46944695195361e-17,0.707106781186548} | 1.4142135623731 | 0.857142857142245 2 | {2.22044604925031e-16,-1,1.11022302462516e-16} | 0.577350269189626 | 0.142857142857041

- Notes

- Table names can be optionally schema qualified (current_schemas() would be searched if a schema name is not provided) and all table and column names should follow case-sensitivity and quoting rules per the database. (For instance, 'mytable' and 'MyTable' both resolve to the same entity, i.e. 'mytable'. If mixed-case or multi-byte characters are desired for entity names then the string should be double-quoted; in this case the input would be '"MyTable"').
- Because of the centering step in PCA (see Technical Background), sparse matrices almost always become dense during the training process. Thus, this implementation automatically densifies sparse matrix input, and there should be no expected performance improvement in using sparse matrix input over dense matrix input.
- For the parameter 'components_param', INTEGER and FLOAT are interpreted differently. A special case to be aware of: 'components_param' = 1 (INTEGER) will return 1 principal component, but 'components_param' = 1.0 (FLOAT) will return all principal components, i.e., proportion of variance of 100%.
- If both 'lanczos_iter' and proportion of variance (via the 'components_param' parameter) are defined, 'lanczos_iter' will take precedence in determining the number of principal components (i.e. the number of principal components will not be greater than 'lanczos_iter' even if the target proportion had not been reached).

- Technical Background

The PCA implemented here uses an SVD decomposition implementation to recover the principal components (as opposed to the directly computing the eigenvectors of the covariance matrix). Let be the data matrix, and let be a vector of the column averages of . PCA computes the matrix as

where is the vector of all ones.

PCA then computes the SVD matrix factorization

where is a diagonal matrix. The eigenvalues are recovered as the entries of , and the principal components are the rows of . The reasoning behind using N − 1 instead of N to calculate the covariance is Bessel's correction.

It is important to note that the PCA implementation assumes that the user will use only the principal components that have non-zero eigenvalues. The SVD calculation is done with the Lanczos method, with does not guarantee correctness for singular vectors with zero-valued eigenvalues. Consequently, principal components with zero-valued eigenvalues are not guaranteed to be correct. Generally, this will not be problem unless the user wants to use the principal components for the entire eigenspectrum.

- Literature

[1] Principal Component Analysis. http://en.wikipedia.org/wiki/Principal_component_analysis

[2] Shlens, Jonathon (2009), A Tutorial on Principal Component Analysis

- Related Topics

File pca.sql_in documenting the SQL functions