Dimension Reduction - SVD Decomposition

Overview


SVD Decomposition is a technique that can be used to reduce the number of dimensions in a dataset.

Centering


Most often in principal components analysis, the data is preprocessed to be centered at the origin. This can easily be accomplised by using the center function of the moments library.


let mt = await import('/lib/statistics/moments/v1.0.0/moments.mjs');
let data = [
	[1,2,3],
	[2,5,3],
	[6,3,4],
	[3,3,3],
	[1,4,2],
	[7,8,4],
];
let centered = mt.center(data);
					
Try it!

Covariance


The next step of the principal components analysis is to compute the covariance matrix of the dataset.



let mt = await import('/lib/statistics/moments/v1.0.0/moments.mjs');
let data = [
	[1,2,3],
	[2,5,3],
	[6,3,4],
	[3,3,3],
	[1,4,2],
	[7,8,4],
];
let centered = mt.center(data);
let covariance = mt.covariance(centered);
					
Try it!

Eigen Values and Eigen Vectors



let la= await import('/lib/linear-algebra/v1.0.0/linear-algebra.mjs');
let mt = await import('/lib/statistics/moments/v1.0.0/moments.mjs');
let data = [
	[1,2,3],
	[2,5,3],
	[6,3,4],
	[3,3,3],
	[1,4,2],
	[7,8,4],
];
let centered = mt.center(data);
let covariance = mt.covariance(centered);
let eig = la.eigenvectors(covariance);
let eigenvalues = eig.lambda.x;
let vectors = eig.E.x;

//run test

//get the first vector
let v1 = vectors.map(p=>p[0]);

//the covariance times the first vector should equal first vector times the first eigenvalue
let test = numeric.dot(covariance, v1);
let test2 = [];
for(let i=0;i<test.length;i++){
	//divide by the first eigenvalue
	test2.push(test[i]/eigenvalues[0]);
}
//test2 should equal v1;
Try it!


Contents