Logo ROOT   6.08/07
Reference Guide
testSampleQuantiles.cxx
Go to the documentation of this file.
1 // test sample quantiles
2 // function TMath::Quantiles and indirectly also TMath::kOrdStat
3 // compare with results from R (hardcoded in the test)
4 // L.M 9/11/2011
5 
6 #include "TMath.h"
7 
8 #include <iostream>
9 #include <algorithm>
10 using std::cout;
11 using std::endl;
12 
13 bool debug = false;
14 
15 // restults obtained runnnig r for all types from 1 to 9
16 // there are 13 values for each ( size is 13*9 = 121
17 double result[121] = {
18  0.1 , 0.1 , 0.1 , 0.3 , 0.7 , 1 , 1.2 , 1.5 , 1.8 , 2 , 10 , 10 , 10, // type = 1
19  0.1 , 0.1 , 0.2 , 0.5 , 0.85 , 1.1 , 1.35 , 1.65 , 1.9 , 6 , 10 , 10 , 10, // type = 2
20  0.1 , 0.1 , 0.1 , 0.3 , 0.7 , 1 , 1.2 , 1.5 , 1.8 , 2 , 10 , 10 , 10, // type = 3
21  0.1 , 0.1 , 0.1 , 0.3 , 0.7 , 1 , 1.2 , 1.5 , 1.8 , 2 , 10 , 10 , 10, // type = 4
22  0.1 , 0.1 , 0.2 , 0.5 , 0.85 , 1.1 , 1.35 , 1.65 , 1.9 , 6 , 10 , 10 , 10,
23  0.1 , 0.1 , 0.12 , 0.38 , 0.79 , 1.08 , 1.35 , 1.68 , 1.94 , 8.4 , 10 , 10 , 10,
24  0.1 , 0.118 , 0.28 , 0.62 , 0.91 , 1.12 , 1.35 , 1.62 , 1.86 , 3.6 , 10 , 10 , 10,
25  0.1 , 0.1 , 0.1733333 , 0.46 , 0.83 , 1.093333 , 1.35 , 1.66 , 1.913333 , 6.8 , 10 , 10 , 10,
26  0.1 , 0.1 , 0.18 , 0.47 , 0.835 , 1.095 , 1.35 , 1.6575 , 1.91 , 6.6 , 10 , 10 , 10 };
27 
28 
29 bool testQuantiles(int type = 0, bool sorted = true) {
30 
31  const int n = 10;
32  double x[] = {0.1,0.3,0.7,1.,1.2,1.5,1.8,2.,10,10};
33 
34  const int np = 13;
35  double p[] = { 0.,0.01,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.99,1.0 };
36  double quant[np];
37 
38  if (!sorted) {
39  // shuffle the data
40  std::random_shuffle(x, x+10);
41  if (debug) {
42  std::cout << "shuffle data " << std::endl;
43  std::cout << " data = { ";
44  for (int i = 0; i < n; ++i)
45  std::cout << x[i] << " ";
46  std::cout << " }\n";
47  }
48  }
49 
50 
51  if (type >0 && type < 10)
52  TMath::Quantiles(n,np,x,quant,p,sorted,0,type);
53  else
54  TMath::Quantiles(n,np, x,quant,p,sorted);
55 
56  if (debug) {
57  for (int i = 0; i < np; ++i) {
58  printf(" %5.2f ",p[i]);
59  }
60  std::cout << std::endl;
61  for (int i = 0; i < np; ++i) {
62  printf(" %5.3f ",quant[i]);
63  }
64  std::cout << std::endl;
65  }
66 
67  // test if result is OK
68  if (type == 0) type = 7;
69  if (type < 0) type = - type;
70  bool ok = true;
71  std::cout << "Testing for type " << type << " :\t\t";
72  for (int i = 0; i < np; ++i) {
73  double r_result = result[ (type-1)*np + i];
74  if (TMath::AreEqualAbs(quant[i], r_result, 1.E-6) )
75  std::cout << ".";
76  else {
77  std::cout << " Failed for prob = " << p[i] << " - R gives " << r_result << " TMath gives " << quant[i] << std::endl;
78  ok = false;
79  }
80  }
81  if (ok)
82  std::cout << "\t OK !\n";
83  else
84  std::cout << "\nTest Failed for type " << type << std::endl;
85 
86  return ok;
87 }
88 
89 int main(int argc, char *argv[]) {
90 
91  if (argc > 1) {
92  int type = atoi(argv[1]);
93  debug = true;
94  bool ret = testQuantiles(type,true);
95  return (ret) ? 0 : -1;
96  }
97 
98  bool ok = true;
99  std::cout << "Test ordered data ....\n";
100  //itype == 0 is considered the defafult
101  for (int itype = 0; itype < 10; ++itype) {
102  ok &= testQuantiles(itype,true);
103  }
104  std::cout << "\nTest data in random order....\n";
105  for (int itype = 0; itype < 10; ++itype) {
106  ok &= testQuantiles(itype,false);
107  }
108 
109  if (!ok) {
110  std::cout << "Test sample quantiles FAILED " << std::endl;
111  return -1;
112  }
113  return 0;
114 
115 }
bool testQuantiles(int type=0, bool sorted=true)
int main(int argc, char *argv[])
Double_t x[n]
Definition: legend1.C:17
void Quantiles(Int_t n, Int_t nprob, Double_t *x, Double_t *quantiles, Double_t *prob, Bool_t isSorted=kTRUE, Int_t *index=0, Int_t type=7)
Computes sample quantiles, corresponding to the given probabilities Parameters: x -the data sample n ...
Definition: TMath.cxx:1177
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Definition: TMath.h:192
Double_t E()
Definition: TMath.h:54
int type
Definition: TGX11.cxx:120
bool debug
double result[121]
const Int_t n
Definition: legend1.C:16