Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
principal.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook
4/// Principal Components Analysis (PCA) example.
5///
6/// Example of using TPrincipal as a stand alone class.
7///
8/// We create n-dimensional data points, where c = trunc(n / 5) + 1
9/// are correlated with the rest n - c randomly distributed variables.
10///
11/// \macro_image
12/// \macro_output
13/// \macro_code
14///
15/// \authors Rene Brun, Christian Holm Christensen
16
17#include "TPrincipal.h"
18
19void m=10000)
20{
21 Int_t c = n / 5 + 1;
22
23 cout << "*************************************************" << endl;
24 cout << "* Principal Component Analysis *" << endl;
25 cout << "* *" << endl;
26 cout << "* Number of variables: " << setw(4) << n
27 << " *" << endl;
28 cout << "* Number of data points: " << setw(8) << m
29 << " *" << endl;
30 cout << "* Number of dependent variables: " << setw(4) << c
31 << " *" << endl;
32 cout << "* *" << endl;
33 cout << "*************************************************" << endl;
34
35
36 // Initilase the TPrincipal object. Use the empty string for the
37 // final argument, if you don't wan't the covariance
38 // matrix. Normalising the covariance matrix is a good idea if your
39 // variables have different orders of magnitude.
40 n,"ND");
41
42 // Use a pseudo-random number generator
44
45 // Make the m data-points
46 // Make a variable to hold our data
47 // Allocate memory for the data point
48 Double_t* data = new Double_t[n];
49 for (Int_t i = 0; i < m; i++) {
50
51 // First we create the un-correlated, random variables, according
52 // to one of three distributions
53 for (Int_t j = 0; j < n - c; j++) {
54 if (j % 3 == 0) data[j] = randomNum->Gaus(5,1);
55 else if (j % 3 == 1) data[j] = randomNum->Poisson(8);
56 else data[j] = randomNum->Exp(2);
57 }
58
59 // Then we create the correlated variables
60 for (Int_t j = 0 ; j < c; j++) {
61 data[n - c + j] = 0;
62 for (Int_t k = 0; k < n - c - j; k++) data[n - c + j] += data[k];
63 }
64
65 // Finally we're ready to add this datapoint to the PCA
66 data);
67 }
68
69 // We delete the data after use, since TPrincipal got it by now.
70 delete [] data;
71
72 // Do the actual analysis
73 principal->MakePrincipals();
74
75 // Print out the result on
76 principal->Print();
77
78 // Test the PCA
79 principal->Test();
80
81 // Make some histograms of the original, principal, residue, etc data
82 principal->MakeHistograms();
83
84 // Make two functions to map between feature and pattern space
85 principal->MakeCode();
86
87 // Start a browser, so that we may browse the histograms generated
88 // above
89 principal);
90}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Principal Components Analysis (PCA)
Definition TPrincipal.h:21
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
const Int_t n
Definition legend1.C:16
TMarker m
Definition textangle.C:8