A Principal Component Analysis is a Singular Value Decomposition of the original data. More...
#include <STK_PcaModel.h>
Inherits STK::SemiLinearAAModel.
Public Member Functions | |
| PcaModel (Matrix const &X=Matrix(), bool ref=false) | |
| PcaModel (Matrix const &X, Vector const &W, bool ref=false) | |
| PcaModel (const PcaModel &mod) | |
| virtual | ~PcaModel () |
| void | clear () |
| virtual void | run () |
| void | step () |
| PcaModel & | operator= (const PcaModel &mod) |
Private Member Functions | |
| void | pca () |
A Principal Component Analysis is a Singular Value Decomposition of the original data.
Definition at line 56 of file STK_PcaModel.h.
Default ctor. Compute the PCA with of the observations stored in the matrix X.
Definition at line 55 of file STK_PcaModel.cpp.
: SemiLinearAAModel(X, ref) { // If there is no data : the PcaModel Model is computed
Misc ctor. Compute the weighted PCA of the observations stored in the matrix X.
Definition at line 67 of file STK_PcaModel.cpp.
: SemiLinearAAModel(X, W, ref) { // copy X_ in comp_ this->comp_ = this->X_; // If there is no data : the PcaModel Model is computed
| STK::PcaModel::PcaModel | ( | const PcaModel & | mod | ) |
Copy ctor.
Definition at line 82 of file STK_PcaModel.cpp.
| STK::PcaModel::~PcaModel | ( | ) | [virtual] |
Virtual destructor
Definition at line 86 of file STK_PcaModel.cpp.
| void STK::PcaModel::clear | ( | ) |
clear all storage.
Definition at line 90 of file STK_PcaModel.cpp.
| void STK::PcaModel::run | ( | ) | [virtual] |
Compute the PCA of X Without effect if compModel_ == true (the computation is already done).
Definition at line 97 of file STK_PcaModel.cpp.
References pca().
Referenced by step().
{
if (!this->compModel_) // model has not been computed
{ pca(); // compute the pca model
this->compModel_ = true; // computation done
| void STK::PcaModel::step | ( | ) | [inline] |
Compute a step of the the PCA of X Without effect if compModel_ == true (the computation is already done).
Definition at line 94 of file STK_PcaModel.h.
References run().
{ run();}
Operator = : overwrite the PcaModel with P.
Definition at line 106 of file STK_PcaModel.cpp.
: overwrite the PcaModel with P. */ PcaModel& PcaModel::operator=(const PcaModel &mod) { SemiLinearAAModel *p1; // pointer on this const SemiLinearAAModel *p2; // pointer on mod p1 = this; // conversion to SemiLinearAAModel class p2 = &mod; // idem for mod (*p1) = (*p2); // copy SemiLinearAAModel part
| void STK::PcaModel::pca | ( | ) | [private] |
compute the pca, calling compSvd(). Construct the biplot : scale U_ in oder to obtain the components.
Definition at line 120 of file STK_PcaModel.cpp.
References STK::abs(), STK::Svd::getD(), STK::Svd::getV(), and STK::Svd::rank().
Referenced by run().
{
// copy X_ in comp_
this->comp_ = this->X_;
// save currrent beginning
Integer cbeg = comp_.firstCol(), rbeg = comp_.firstRow();
// if there is weights, pre-mult of comp_ by W^{1/2}
if (this->weighted_)
for (Integer i=cbeg; i<=comp_.lastCol(); i++)
this->comp_(i) *= sqrt(abs(this->W_.at(i)));
// compute the svd of comp_, the result will be in comp_
// thus ref = true
comp_.shift(); // set newbeg to (1,1) : needed by Svd
// create a svd of comp_
Svd *csvd = new Svd(this->comp_, true);
// get the number of components
this->ncomp_ = csvd->rank();
// delete unecessary cols of comp_
this->comp_.popBackCols(this->nvar_ - this->ncomp_);
// get the std of the Components (the singular values)
this->cstd_.swap(csvd->getD());
this->cstd_.popBack(this->nvar_ - this->ncomp_);
// get the Axis
this->axis_.swap(csvd->getV());
this->axis_.popBackCols(this->nvar_ - this->ncomp_);
// clear csvd
delete csvd;
// restore beginning
comp_.shift(rbeg, cbeg);
cstd_.shift(cbeg);
axis_.shift(rbeg, cbeg);
// if there is weights, post-mult of comp_ by W^{-1/2}
if (this->weighted_)
for (Integer i=rbeg; i<=comp_.lastRow(); i++)
{ Real weight = sqrt(abs(this->W_.at(i)));
// divide the ith row by weight
if (weight) this->comp_(i) /= weight;
}
else
{ // compute the components and its standard deviation
const Real aux = sqrt(this->nobs_);
for (Integer i=cbeg; i<=comp_.lastCol(); i++)
{
comp_[i] *= this->cstd_[i]/ aux;
}
}
// Compute the variance of comp_ = criteria_
this->cvar_.resize(comp_.rangeHo());
this->criteria_.resize(comp_.rangeHo());
for (Integer j=comp_.firstCol(); j<=comp_.lastCol(); j++)
{
this->cvar_[j] = this->cstd_[j] * this->cstd_[j];
this->criteria_[j] = this->cvar_[j];
1.7.1