Public Member Functions | Protected Member Functions | Protected Attributes

PCA Class Reference

Performs Pricipal Component Analysis on samples with N variables. More...

List of all members.

Public Member Functions

 PCA (int nrvars)
virtual ~PCA ()
void clearAllSamples ()
template<class IDXABL >
void addSample (const IDXABL &sample)
bool calculate ()
float getEigenValue (int idx) const
template<class IDXABL >
void getEigenVector (int idx, IDXABL &vec) const
void setThreadWorker (Threads::WorkManager *)

Protected Member Functions

bool tqli (float[], float[], int, ObjectSet< float > &)
void tred2 (ObjectSet< float > &, int, float[], float[])

Protected Attributes

const int nrvars
Array2DImpl< float > covariancematrix
ObjectSet< TypeSet< float > > samples
TypeSet< float > samplesums
Threads::WorkManagerthreadworker
ObjectSet< SequentialTasktasks
float * eigenvalues
int * eigenvecindexes

Detailed Description

Performs Pricipal Component Analysis on samples with N variables.

Example of usage:

   //The samples will have three variables
   PCA pca( 3 );                

   //Samples can be added by any object that has a readable [] operator
   const float sample0[] = { 0, 1, 2 };
   pca.addSample( sample0 );

   const float sample1[] = { 4.343, 9.8, 2.72 };
   pca.addSample( sample1 );

   const float sample2[] = { 23.15, 210, -15 };
   pca.addSample( sample2 );

   const float sample3[] = { -0.36, 0.68, 3 };
   pca.addSample( sample3 );

   const float sample4[] = { 4.4, 9,6, 11 };
   pca.addSample( sample4 );

   TypeSet<float> sample5; sample5 += 34.1; sample5 += 8.37; sample5 += -44;
   pca.addSample( sample5 ); 

   pca.calculate();


   //Any object that has a writable [] operator can be used to fetch
   //the resulting vectors:
   TypeSet<float> eigenvec0(3,0);
   float eigenval0 = pca.getEigenValue(0);
   pca.getEigenVector( 0, eigenvec0 );

   float[3] eigenvec1;
   float eigenval1 = pca.getEigenValue(1);
   pca.getEigenVector( 1, eigenvec1 );

   float[3] eigenvec2;
   float eigenval2 = pca.getEigenValue(2);
   pca.getEigenVector( 2, eigenvec2 );

Constructor & Destructor Documentation

PCA::PCA ( int  nrvars  ) 
Parameters:
nrvars The number of variables that the samples have.
virtual PCA::~PCA (  )  [virtual]

Member Function Documentation

template<class IDXABL >
void PCA::addSample ( const IDXABL &  sample  )  [inline]

Adds a sample to the analysis.

Parameters:
sample The sample that should be added. The sample can be of any type that have [] operators.
bool PCA::calculate (  ) 

Computes the pca for all added samples.

void PCA::clearAllSamples (  ) 

Removes all samples so a new analysis can be made (by adding new samples)

float PCA::getEigenValue ( int  idx  )  const
Returns:
an eigenvalue.
Parameters:
idx Determines which eigenvalue to return. The eigenvalues are sorted in descending order, so idx==0 gives the largest eigenvalue.
template<class IDXABL >
void PCA::getEigenVector ( int  idx,
IDXABL &  vec 
) const [inline]

Returns the eigenvector corresponding to the eigenvalue idx.

Parameters:
idx Determines which eigenvector to return. The eigenvectors are sorted in order of descending eigenvalue, so idx==0 gives the eigenvector corresponding to the largest eigenvalue.
vec The object where the to store the eigenvector. Any object that has a writable [] operator can be used, for example float* and TypeSet<T>.
void PCA::setThreadWorker ( Threads::WorkManager  ) 

Enables multi-threaded calculation, which is beneficial when the number of samples and/or number of variables are large. The object does however work without setting the threadworkmanager.

Note:
threadworkmanager is managed (i.e. deleted) by caller, and it can be shared with many other objects (such as other PCAs).
bool PCA::tqli ( float  [],
float  [],
int  ,
ObjectSet< float > &   
) [protected]
void PCA::tred2 ( ObjectSet< float > &  ,
int  ,
float  [],
float  [] 
) [protected]

Member Data Documentation

Array2DImpl<float> PCA::covariancematrix [protected]
float* PCA::eigenvalues [protected]

The negation of the eigenval, to get the sorting right.

int* PCA::eigenvecindexes [protected]
const int PCA::nrvars [protected]
ObjectSet<TypeSet<float> > PCA::samples [protected]
TypeSet<float> PCA::samplesums [protected]