ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
principal.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// Principal Components Analysis (PCA) example
4 ///
5 /// Example of using TPrincipal as a stand alone class.
6 ///
7 /// We create n-dimensional data points, where c = trunc(n / 5) + 1
8 /// are correlated with the rest n - c randomly distributed variables.
9 ///
10 /// \macro_output
11 /// \macro_code
12 ///
13 /// \authors Rene Brun, Christian Holm Christensen
14 
15 #include "TPrincipal.h"
16 
17 void principal(Int_t n=10, Int_t m=10000)
18 {
19  Int_t c = n / 5 + 1;
20 
21  cout << "*************************************************" << endl;
22  cout << "* Principal Component Analysis *" << endl;
23  cout << "* *" << endl;
24  cout << "* Number of variables: " << setw(4) << n
25  << " *" << endl;
26  cout << "* Number of data points: " << setw(8) << m
27  << " *" << endl;
28  cout << "* Number of dependent variables: " << setw(4) << c
29  << " *" << endl;
30  cout << "* *" << endl;
31  cout << "*************************************************" << endl;
32 
33 
34  // Initilase the TPrincipal object. Use the empty string for the
35  // final argument, if you don't wan't the covariance
36  // matrix. Normalising the covariance matrix is a good idea if your
37  // variables have different orders of magnitude.
38  TPrincipal* principal = new TPrincipal(n,"ND");
39 
40  // Use a pseudo-random number generator
41  TRandom* random = new TRandom;
42 
43  // Make the m data-points
44  // Make a variable to hold our data
45  // Allocate memory for the data point
46  Double_t* data = new Double_t[n];
47  for (Int_t i = 0; i < m; i++) {
48 
49  // First we create the un-correlated, random variables, according
50  // to one of three distributions
51  for (Int_t j = 0; j < n - c; j++) {
52  if (j % 3 == 0) data[j] = random->Gaus(5,1);
53  else if (j % 3 == 1) data[j] = random->Poisson(8);
54  else data[j] = random->Exp(2);
55  }
56 
57  // Then we create the correlated variables
58  for (Int_t j = 0 ; j < c; j++) {
59  data[n - c + j] = 0;
60  for (Int_t k = 0; k < n - c - j; k++) data[n - c + j] += data[k];
61  }
62 
63  // Finally we're ready to add this datapoint to the PCA
64  principal->AddRow(data);
65  }
66 
67  // We delete the data after use, since TPrincipal got it by now.
68  delete [] data;
69 
70  // Do the actual analysis
71  principal->MakePrincipals();
72 
73  // Print out the result on
74  principal->Print();
75 
76  // Test the PCA
77  principal->Test();
78 
79  // Make some histograms of the orginal, principal, residue, etc data
80  principal->MakeHistograms();
81 
82  // Make two functions to map between feature and pattern space
83  principal->MakeCode();
84 
85  // Start a browser, so that we may browse the histograms generated
86  // above
87  TBrowser* b = new TBrowser("principalBrowser", principal);
88 }
Principal Components Analysis (PCA)
Definition: TPrincipal.h:28
return c
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
virtual void Print(Option_t *opt="MSE") const
Print the statistics Options are.
int Int_t
Definition: RtypesCore.h:41
void Test(Option_t *option="")
Test the PCA, bye calculating the sum square of residuals (see method SumOfSquareResiduals), and display the histogram.
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual void MakeHistograms(const char *name="pca", Option_t *option="epsdx")
Make histograms of the result of the analysis.
Definition: TPrincipal.cxx:569
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
virtual void AddRow(const Double_t *x)
Add a data point and update the covariance matrix.
Definition: TPrincipal.cxx:410
TMarker * m
Definition: textangle.C:8
virtual void MakePrincipals()
Perform the principal components analysis.
Definition: TPrincipal.cxx:862
double Double_t
Definition: RtypesCore.h:55
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 .cxx or .C.
Definition: TPrincipal.cxx:544
virtual Int_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition: TRandom.cxx:362
const Int_t n
Definition: legend1.C:16
virtual Double_t Exp(Double_t tau)
Returns an exponential deviate.
Definition: TRandom.cxx:212