From $ROOTSYS/tutorials/math/principal.C

#include "TPrincipal.h"

void principal(Int_t n=10, Int_t m=10000)
{
  //
  // Principal Components Analysis (PCA) example
  //
  // Example of using TPrincipal as a stand alone class.
  //
  // We create n-dimensional data points, where c = trunc(n / 5) + 1
  // are  correlated with the rest n - c randomly distributed variables.
  //
  // End_Html
  //Authors: Rene Brun, Christian Holm Christensen

  Int_t c = n / 5 + 1;

  cout << "*************************************************" << endl;
  cout << "*         Principal Component Analysis          *" << endl;
  cout << "*                                               *" << endl;
  cout << "*  Number of variables:           " << setw(4) << n
       << "          *" << endl;
  cout << "*  Number of data points:         " << setw(8) << m
       << "      *" << endl;
  cout << "*  Number of dependent variables: " << setw(4) << c
       << "          *" << endl;
  cout << "*                                               *" << endl;
  cout << "*************************************************" << endl;


  // Initilase the TPrincipal object. Use the empty string for the
  // final argument, if you don't wan't the covariance
  // matrix. Normalising the covariance matrix is a good idea if your
  // variables have different orders of magnitude.
  TPrincipal* principal = new TPrincipal(n,"ND");

  // Use a pseudo-random number generator
  TRandom* random = new TRandom;

  // Make the m data-points
  // Make a variable to hold our data
  // Allocate memory for the data point
  Double_t* data = new Double_t[n];
  for (Int_t i = 0; i < m; i++) {

    // First we create the un-correlated, random variables, according
    // to one of three distributions
    for (Int_t j = 0; j < n - c; j++) {
       if (j % 3 == 0)
          data[j] = random->Gaus(5,1);
       else if (j % 3 == 1)
          data[j] = random->Poisson(8);
       else
          data[j] = random->Exp(2);
    }

    // Then we create the correlated variables
    for (Int_t j = 0 ; j < c; j++) {
       data[n - c + j] = 0;
       for (Int_t k = 0; k < n - c - j; k++)
          data[n - c + j] += data[k];
    }

    // Finally we're ready to add this datapoint to the PCA
    principal->AddRow(data);
  }

  // We delete the data after use, since TPrincipal got it by now.
  delete [] data;

  // Do the actual analysis
  principal->MakePrincipals();

  // Print out the result on
  principal->Print();

  // Test the PCA
  principal->Test();

  // Make some histograms of the orginal, principal, residue, etc data
  principal->MakeHistograms();

  // Make two functions to map between feature and pattern space
  principal->MakeCode();

  // Start a browser, so that we may browse the histograms generated
  // above
  TBrowser* b = new TBrowser("principalBrowser", principal);

}
 principal.C:1
 principal.C:2
 principal.C:3
 principal.C:4
 principal.C:5
 principal.C:6
 principal.C:7
 principal.C:8
 principal.C:9
 principal.C:10
 principal.C:11
 principal.C:12
 principal.C:13
 principal.C:14
 principal.C:15
 principal.C:16
 principal.C:17
 principal.C:18
 principal.C:19
 principal.C:20
 principal.C:21
 principal.C:22
 principal.C:23
 principal.C:24
 principal.C:25
 principal.C:26
 principal.C:27
 principal.C:28
 principal.C:29
 principal.C:30
 principal.C:31
 principal.C:32
 principal.C:33
 principal.C:34
 principal.C:35
 principal.C:36
 principal.C:37
 principal.C:38
 principal.C:39
 principal.C:40
 principal.C:41
 principal.C:42
 principal.C:43
 principal.C:44
 principal.C:45
 principal.C:46
 principal.C:47
 principal.C:48
 principal.C:49
 principal.C:50
 principal.C:51
 principal.C:52
 principal.C:53
 principal.C:54
 principal.C:55
 principal.C:56
 principal.C:57
 principal.C:58
 principal.C:59
 principal.C:60
 principal.C:61
 principal.C:62
 principal.C:63
 principal.C:64
 principal.C:65
 principal.C:66
 principal.C:67
 principal.C:68
 principal.C:69
 principal.C:70
 principal.C:71
 principal.C:72
 principal.C:73
 principal.C:74
 principal.C:75
 principal.C:76
 principal.C:77
 principal.C:78
 principal.C:79
 principal.C:80
 principal.C:81
 principal.C:82
 principal.C:83
 principal.C:84
 principal.C:85
 principal.C:86
 principal.C:87
 principal.C:88
 principal.C:89
 principal.C:90
 principal.C:91
 principal.C:92
 principal.C:93