42 #include "../../Matrix/Matrix.h"
43 #include "../../Matrix/MatrixTools.h"
44 #include "../../VectorTools.h"
54 const vector<double>& rowW,
55 const vector<double>& colW,
78 setData(tmpData, rowW, colW, nbAxes, tol, verbose);
97 vector<double> rowW(nRow);
98 vector<double> colW(nCol);
113 scale(tmpData, rowW);
116 setData(tmpData, rowW, colW, nbAxes, tol, verbose);
125 if (nRow != rowW.size())
126 throw Exception(
"PrincipalComponentAnalysis::center. The number of row weigths have to be equal to the number of rows!");
130 vector<double> columnMeans(nCol);
131 for (
unsigned int i = 0; i < nCol; i++)
134 for (
unsigned int j = 0; j < nRow; j++)
136 tmp += matrix(j, i) * rowW[j];
138 columnMeans[i] = tmp / sumRowWeights;
141 for (
unsigned int i = 0; i < nCol; i++)
143 for (
unsigned int j = 0; j < nRow; j++)
145 matrix(j, i) -= columnMeans[i];
156 if (nRow != rowW.size())
157 throw Exception(
"PrincipalComponentAnalysis::scale. The number of row weigths have to be equal to the number of rows!");
161 vector<double> columnSd(nCol);
162 for (
size_t i = 0; i < nCol; i++)
165 for (
unsigned int j = 0; j < nRow; j++)
167 tmp += pow(matrix(j, i), 2) * rowW[j];
169 columnSd[i] = sqrt(tmp / sumRowWeights);
172 for (
size_t i = 0; i < nCol; i++)
174 for (
unsigned int j = 0; j < nRow; j++)
176 if (columnSd[i] == 0.)
179 matrix(j, i) /= columnSd[i];
The core class of a multivariate analysis.
void setData(const Matrix< double > &matrix, const std::vector< double > &rowWeights, const std::vector< double > &colWeights, unsigned int nbAxes, double tol=0.0000001, bool verbose=true)
Set the data and perform computations.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
The matrix template interface.
virtual size_t getNumberOfColumns() const =0
virtual size_t getNumberOfRows() const =0
static void scale(Matrix< double > &matrix, const std::vector< double > &rowW)
This function allows to center an input matrix from its column means.
static void center(Matrix< double > &matrix, const std::vector< double > &rowW)
This function allows to center an input matrix from its column means.
PrincipalComponentAnalysis(const Matrix< double > &data, unsigned int nbAxes, const std::vector< double > &rowW, const std::vector< double > &colW, bool centered=true, bool scaled=true, double tol=0.0000001, bool verbose=true)
Build a new PrincipalComponentAnalysis object.