232 fCovarianceMatrix(1,1),
254 : fMeanValues(nVariables),
256 fCovarianceMatrix(nVariables,nVariables),
257 fEigenVectors(nVariables,nVariables),
258 fEigenValues(nVariables),
259 fOffDiagonal(nVariables),
262 if (nVariables <= 1) {
263 Error(
"TPrincipal",
"You can't be serious - nVariables == 1!!!");
274 while (opt && strlen(opt) > 0) {
290 Error(
"TPrincipal",
"Couldn't create vector mean values");
292 Error(
"TPrincipal",
"Couldn't create vector sigmas");
294 Error(
"TPrincipal",
"Couldn't create covariance matrix");
296 Error(
"TPrincipal",
"Couldn't create eigenvector matrix");
298 Error(
"TPrincipal",
"Couldn't create eigenvalue vector");
300 Error(
"TPrincipal",
"Couldn't create offdiagonal vector");
305 Error(
"TPrincipal",
"Couldn't create user data vector");
314 fNumberOfDataPoints(pr.fNumberOfDataPoints),
315 fNumberOfVariables(pr.fNumberOfVariables),
316 fMeanValues(pr.fMeanValues),
318 fCovarianceMatrix(pr.fCovarianceMatrix),
319 fEigenVectors(pr.fEigenVectors),
320 fEigenValues(pr.fEigenValues),
321 fOffDiagonal(pr.fOffDiagonal),
322 fUserData(pr.fUserData),
324 fHistograms(pr.fHistograms),
325 fIsNormalised(pr.fIsNormalised),
326 fStoreData(pr.fStoreData)
431 meanValues[i] *= cor;
432 meanValues[i] +=
p[i] * invnp;
433 const Double_t t1 = (
p[i] - meanValues[i]) * invnpM1;
436 for (j = 0; j < i + 1; j++) {
438 covMatrix[
index] *= cor;
439 covMatrix[
index] +=
t1 * (
p[j] - meanValues[j]);
468 while ((
h = (
TH1*)next()))
476 b->Add(&
fSigmas,
"Sigma value vector");
585 for (i = 0; i <
len; i++) {
612 Warning(
"MakeHistograms",
"Unknown option: %c",opt[i]);
617 if (!makeX && !makeD && !makeP && !makeE && !makeS)
663 hS->SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
688 TString::Format(
"Distance from pattern to feature space, variable %d", i),
716 if (!makeX && !makeP && !makeD && !makeS) {
739 if (makeP||makeD||makeS)
743 if (makeD || makeS) {
755 hS->Fill(j,
d[k]*
d[k]);
759 (hD[k])->Fill(
d[k],j);
803 for (j = 0; j <= i; j++)
811 for (j = 0; j <= i; j++) {
899 const char *cv_qual = isMethod ?
"" :
"static ";
901 prefix.
Form(
"%s::", classname);
903 std::ofstream outFile(
filename,std::ios::out|std::ios::trunc);
905 Error(
"MakeRealCode",
"couldn't open output file '%s'",
filename);
909 std::cout <<
"Writing on file \"" <<
filename <<
"\" ... " << std::flush;
914 outFile <<
"// -*- mode: c++ -*-" << std::endl;
916 outFile <<
"// " << std::endl
918 <<
" generated by TPrincipal::MakeCode" << std::endl;
921 outFile <<
"// on " << date.
AsString() << std::endl;
923 outFile <<
"// ROOT version " <<
gROOT->GetVersion()
924 << std::endl <<
"//" << std::endl;
926 outFile <<
"// This file contains the functions " << std::endl
928 <<
"// void " << prefix
929 <<
"X2P(Double_t *x, Double_t *p); " << std::endl
930 <<
"// void " << prefix
931 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest);"
932 << std::endl <<
"//" << std::endl
933 <<
"// The first for transforming original data x in " << std::endl
934 <<
"// pattern space, to principal components p in " << std::endl
935 <<
"// feature space. The second function is for the" << std::endl
936 <<
"// inverse transformation, but using only nTest" << std::endl
937 <<
"// of the principal components in the expansion" << std::endl
938 <<
"// " << std::endl
939 <<
"// See TPrincipal class documentation for more "
940 <<
"information " << std::endl <<
"// " << std::endl;
942 outFile <<
"#ifndef __CINT__" << std::endl;
945 outFile <<
"#include \"" << classname <<
".h\"" << std::endl;
948 outFile <<
"#include <Rtypes.h> // needed for Double_t etc" << std::endl;
950 outFile <<
"#endif" << std::endl << std::endl;
959 outFile <<
"//" << std::endl
960 <<
"// Static data variables" << std::endl
961 <<
"//" << std::endl;
962 outFile << cv_qual <<
"Int_t " << prefix <<
"gNVariables = "
969 outFile << std::endl <<
"// Assignment of eigenvector matrix." << std::endl
970 <<
"// Elements are stored row-wise, that is" << std::endl
971 <<
"// M[i][j] = e[i * nVariables + j] " << std::endl
972 <<
"// where i and j are zero-based" << std::endl;
973 outFile << cv_qual <<
"Double_t " << prefix
974 <<
"gEigenVectors[] = {" << std::flush;
979 outFile << (
index != 0 ?
"," :
"" ) << std::endl
983 outFile <<
"};" << std::endl << std::endl;
986 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
987 outFile << cv_qual <<
"Double_t " << prefix
988 <<
"gEigenValues[] = {" << std::flush;
990 outFile << (i != 0 ?
"," :
"") << std::endl
992 outFile << std::endl <<
"};" << std::endl << std::endl;
995 outFile <<
"// Assignment to mean value vector. Zero-based." << std::endl;
996 outFile << cv_qual <<
"Double_t " << prefix
997 <<
"gMeanValues[] = {" << std::flush;
999 outFile << (i != 0 ?
"," :
"") << std::endl
1001 outFile << std::endl <<
"};" << std::endl << std::endl;
1004 outFile <<
"// Assignment to sigma value vector. Zero-based." << std::endl;
1005 outFile << cv_qual <<
"Double_t " << prefix
1006 <<
"gSigmaValues[] = {" << std::flush;
1008 outFile << (i != 0 ?
"," :
"") << std::endl
1011 outFile << std::endl <<
"};" << std::endl << std::endl;
1018 outFile <<
"// " << std::endl
1020 << (isMethod ?
"method " :
"function ")
1021 <<
" void " << prefix
1022 <<
"X2P(Double_t *x, Double_t *p)"
1023 << std::endl <<
"// " << std::endl;
1024 outFile <<
"void " << prefix
1025 <<
"X2P(Double_t *x, Double_t *p) {" << std::endl
1026 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1027 <<
" p[i] = 0;" << std::endl
1028 <<
" for (Int_t j = 0; j < gNVariables; j++)" << std::endl
1029 <<
" p[i] += (x[j] - gMeanValues[j]) " << std::endl
1030 <<
" * gEigenVectors[j * gNVariables + i] "
1031 <<
"/ gSigmaValues[j];" << std::endl << std::endl <<
" }"
1032 << std::endl <<
"}" << std::endl << std::endl;
1036 outFile <<
"// " << std::endl <<
"// The "
1037 << (isMethod ?
"method " :
"function ")
1038 <<
" void " << prefix
1039 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest)"
1040 << std::endl <<
"// " << std::endl;
1041 outFile <<
"void " << prefix
1042 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest) {" << std::endl
1043 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1044 <<
" x[i] = gMeanValues[i];" << std::endl
1045 <<
" for (Int_t j = 0; j < nTest; j++)" << std::endl
1046 <<
" x[i] += p[j] * gSigmaValues[i] " << std::endl
1047 <<
" * gEigenVectors[i * gNVariables + j];" << std::endl
1048 <<
" }" << std::endl <<
"}" << std::endl << std::endl;
1051 outFile <<
"// EOF for " <<
filename << std::endl;
1056 std::cout <<
"done" << std::endl;
1069 for (
Int_t j = 0; j < nTest; j++)
1112 Warning(
"Print",
"Unknown option '%c'",opt[i]);
1117 if (printM||printS||printE) {
1118 std::cout <<
" Variable # " << std::flush;
1120 std::cout <<
"| Mean Value " << std::flush;
1122 std::cout <<
"| Sigma " << std::flush;
1124 std::cout <<
"| Eigenvalue" << std::flush;
1125 std::cout << std::endl;
1127 std::cout <<
"-------------" << std::flush;
1129 std::cout <<
"+------------" << std::flush;
1131 std::cout <<
"+------------" << std::flush;
1133 std::cout <<
"+------------" << std::flush;
1134 std::cout << std::endl;
1137 std::cout << std::setw(12) << i <<
" " << std::flush;
1139 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1142 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1143 <<
fSigmas(i) <<
" " << std::flush;
1145 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1147 std::cout << std::endl;
1149 std::cout << std::endl;
1154 std::cout <<
"Eigenvector # " << i << std::flush;
1187 s[i] += (
x[j] - xp[j])*(
x[j] - xp[j]);
1203 TH1 *pca_s =
nullptr;
1206 Warning(
"Test",
"Couldn't get histogram of square residuals");
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
TMatrixTColumn_const< Double_t > TMatrixDColumn_const
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Using a TBrowser one can browse all ROOT objects.
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
const char * AsString() const
Return the date & time as a string (ctime() format).
1-D histogram with a float per channel (see TH1 documentation)}
TH1 is the base class of all histogram classes in ROOT.
virtual void SetXTitle(const char *title)
virtual Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="")
Performs the operation: this = this + c1*f1 if errors are defined (see TH1::Sumw2),...
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
void Draw(Option_t *option="") override
Draw this histogram with options.
virtual void SetYTitle(const char *title)
2-D histogram with a float per channel (see TH1 documentation)}
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
void Add(TObject *obj) override
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
const TVectorD & GetEigenValues() const
const TMatrixD & GetEigenVectors() const
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
const Element * GetMatrixArray() const override
The TNamed class is the base class for all named ROOT classes.
virtual void SetName(const char *name)
Set the name of the TNamed.
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Principal Components Analysis (PCA)
virtual void MakeMethods(const char *classname="PCA", Option_t *option="")
Generate the file <classname>PCA.cxx which contains the implementation of two methods:
virtual void AddRow(const Double_t *x)
Add a data point and update the covariance matrix.
virtual void X2P(const Double_t *x, Double_t *p)
Calculate the principal components from the original data vector x, and return it in p.
Double_t fTrace
Trace of covarience matrix.
const Double_t * GetRow(Int_t row)
Return a row of the user supplied data.
TPrincipal()
Empty constructor. Do not use.
void Clear(Option_t *option="") override
Clear the data in Object.
virtual void MakeHistograms(const char *name="pca", Option_t *option="epsdx")
Make histograms of the result of the analysis.
void Print(Option_t *opt="MSE") const override
Print the statistics Options are.
TVectorD fUserData
Vector of original data points.
virtual void MakeCode(const char *filename="pca", Option_t *option="")
Generates the file <filename>, with .C appended if it does argument doesn't end in ....
TMatrixD fCovarianceMatrix
Covariance matrix.
Int_t fNumberOfVariables
Number of variables.
TVectorD fSigmas
vector of sigmas
TVectorD fOffDiagonal
Elements of the tridiagonal.
TVectorD fEigenValues
Eigenvalue vector of trans.
TVectorD fMeanValues
Mean value over all data points.
TList * fHistograms
List of histograms.
void MakeRealCode(const char *filename, const char *prefix, Option_t *option="")
This is the method that actually generates the code for the transformations to and from feature space...
Bool_t fStoreData
Should we store input data?
TMatrixD fEigenVectors
Eigenvector matrix of trans.
~TPrincipal() override
Destructor.
virtual void P2X(const Double_t *p, Double_t *x, Int_t nTest)
Calculate x as a function of nTest of the most significant principal components p,...
void Browse(TBrowser *b) override
Browse the TPrincipal object in the TBrowser.
Int_t fNumberOfDataPoints
Number of data points.
void MakeNormalised()
Normalize the covariance matrix.
virtual void MakePrincipals()
Perform the principal components analysis.
virtual void SumOfSquareResiduals(const Double_t *x, Double_t *s)
Calculates the sum of the square residuals, that is.
void Test(Option_t *option="")
Test the PCA, bye calculating the sum square of residuals (see method SumOfSquareResiduals),...
Bool_t fIsNormalised
Normalize matrix?
TPrincipal & operator=(const TPrincipal &)
Assignment operator.
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
TVectorT< Element > & Zero()
Set vector elements to zero.
TVectorT< Element > & ResizeTo(Int_t lwb, Int_t upb)
Resize the vector to [lwb:upb] .
Element * GetMatrixArray()
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
#define sym(otri1, otri2)