Logo coherent WaveBurst  
Library Reference Guide
Logo
PvalueLocal2GlobalBelts.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 // This macro is used to find the correspondence between local and global coverage
19 
20 // N=20 -> 98.950% global = 90.0089% local
21 // N=23 -> 99.032% global = 90.0013% local
22 
23 #define NTRIALS 100000 // trials
24 
25 #define NPVALUES 20 // number of pvalues
26 #define COVERAGE 98.950
27 
28 //#define NPVALUES 23 // number of pvalues
29 //#define COVERAGE 99.032
30 
31 //#define NPVALUES 100 // number of pvalues
32 //#define COVERAGE 99.5
33 
34 #include "TGraph.h"
35 #include "TAxis.h"
36 #include "TMath.h"
37 #include "TRandom.h"
38 #include "TLegend.h"
39 #include "TROOT.h"
40 #include "TStyle.h"
41 #include "TCanvas.h"
42 #include "Math/SpecFuncMathCore.h"
43 #include <iostream>
44 
46 
47 double InverseProbabilityBH(int N, int k, double p);
48 TGraph* DrawProbabilityBH(int N, double p, Color_t color, int style=9, TString mode="ALP");
49 
50 void PvalueLocal2GlobalBelts(double coverage=COVERAGE, bool draw_pvalues_plot=true) {
51 //
52 // input coverage is the local coverage
53 // NOTE: NPVALUES must be setted
54 // this macro printout the global coverage
55 //
56 
57  if(coverage<0 || coverage>100) {cout << "Error: wrong input coverage, must be [0,1]" << endl;exit(1);}
58 
59  for(int i=0;i<NTRIALS;i++) { // loop over NTRIALS
60  double pvalue[NPVALUES];
61  int index[NPVALUES];
62  for(int j=0;j<NPVALUES;j++) pvalue[j] = gRandom->Uniform(0,1); // fill pvalue array with NPVALUES uniform random numbers [0,1]
63  TMath::Sort(NPVALUES,pvalue,index,kFALSE); // sort NPVALUES with increasing order
64  for(int j=0;j<NPVALUES;j++) vpvalue[i][j]=pvalue[index[j]]; // save sorted pvalues into vpvalue
65  }
66 
67  // compute lower/upper belts for coverage
68 
69  double median_belt[NPVALUES]; // median belt array
70  double lower_belt[NPVALUES]; // lower belt array
71  double upper_belt[NPVALUES]; // upper belt array
72 
73  double median = (50.)/100.; // median
74  double lower_tail = ((100.-coverage)/2.)/100.; // lower tail
75  double upper_tail = 1.-lower_tail; // upper tail
76 
77  int imedian = int(NTRIALS*median); if(imedian>=NTRIALS) imedian=NTRIALS-1;
78  int ilower = int(NTRIALS*lower_tail); if(ilower>=NTRIALS) ilower=NTRIALS-1;
79  int iupper = int(NTRIALS*upper_tail); if(iupper>=NTRIALS) iupper=NTRIALS-1;
80 
81  for(int j=0;j<NPVALUES;j++) {
82  int index[NTRIALS];
83  double pvalue[NTRIALS];
84  for(int i=0;i<NTRIALS;i++) pvalue[i]=vpvalue[i][j];
85  TMath::Sort(NTRIALS,pvalue,index,false);
86  median_belt[j] = pvalue[index[imedian]];
87  lower_belt[j] = pvalue[index[ilower]];
88  upper_belt[j] = pvalue[index[iupper]];
89  }
90 
91  // compute the fraction of trials inside coverage
92 
93  int nInside=0;
94  for(int i=0;i<NTRIALS;i++) {
95  bool iSinside=true;
96  for(int j=0;j<NPVALUES;j++) if(vpvalue[i][j]<lower_belt[j] || vpvalue[i][j]>upper_belt[j]) iSinside=false;
97  if(iSinside) nInside++;
98  }
99  double global_coverage = 100.*nInside/double(NTRIALS);
100 
101  cout << endl;
102  cout << "percentage of trials inside coverage " << coverage << "% = " << global_coverage << endl;
103  cout << endl;
104 
105  if(!draw_pvalues_plot) exit(0);
106 
107  // draw numerical median_belt, lower_belt, upper_belt
108 
109  gStyle->SetFrameBorderMode(0); // remove the red box around canvas
110  gROOT->ForceStyle();
111  gStyle->SetTitleFont(12,"D");
112  TCanvas* canvas = new TCanvas("pvalue", "pvalue", 300,40, 1000, 600);
113  canvas->Clear();
114  canvas->SetLogx();
115  canvas->SetLogy();
116  canvas->SetGridx();
117  canvas->SetGridy();
118  canvas->SetFillColor(kWhite);
119 
120  double x[NPVALUES]; for(int i=0;i<NPVALUES;i++) x[i]=i+1;
121 
122  TGraph* gmedian_belt = new TGraph(NPVALUES,x,median_belt);
123  TGraph* glower_belt = new TGraph(NPVALUES,x,lower_belt);
124  TGraph* gupper_belt = new TGraph(NPVALUES,x,upper_belt);
125 
126  glower_belt->Draw("ALP");
127  gmedian_belt->Draw("SAME");
128  gupper_belt->Draw("SAME");
129 
130  // draw analytic belts (NOTE: numerical and analytic belts are compatible !!!)
131 
132  TGraph* gmedian_belt2 = DrawProbabilityBH(NPVALUES, median, kRed, 10,"same");
133  TGraph* glower_belt2 = DrawProbabilityBH(NPVALUES, lower_tail, kBlue,10,"same");
134  TGraph* gupper_belt2 = DrawProbabilityBH(NPVALUES, upper_tail, kBlue,10,"same");
135 
136  // draw analytic local 90% coverage belts
137 
138  double lower_tail3 = ((100.-90.)/2.)/100.; // lower tail
139  double upper_tail3 = 1.-lower_tail3; // upper tail
140 
141  TGraph* glower_belt3 = DrawProbabilityBH(NPVALUES, lower_tail3, kGreen,10,"same");
142  TGraph* gupper_belt3 = DrawProbabilityBH(NPVALUES, upper_tail3, kGreen,10,"same");
143 
144  // draw labels
145  glower_belt->SetTitle(TString::Format("p-values distribution (N = %d)",NPVALUES));
146  glower_belt->GetXaxis()->SetTitle("Cumulative number of events");
147  glower_belt->GetYaxis()->SetTitle("p-value");
148 
149  glower_belt->GetYaxis()->SetRangeUser(0,1);
150  glower_belt->GetXaxis()->SetTitleOffset(0.80);
151  glower_belt->GetYaxis()->SetTitleOffset(0.80);
152  glower_belt->GetXaxis()->CenterTitle(kTRUE);
153  glower_belt->GetYaxis()->CenterTitle(kTRUE);
154  glower_belt->GetXaxis()->SetTitleFont(132);
155  glower_belt->GetXaxis()->SetLabelFont(132);
156  glower_belt->GetYaxis()->SetTitleFont(132);
157  glower_belt->GetYaxis()->SetLabelFont(132);
158  glower_belt->GetXaxis()->SetTitleSize(0.045);
159  glower_belt->GetXaxis()->SetLabelSize(0.045);
160  glower_belt->GetYaxis()->SetTitleSize(0.045);
161  glower_belt->GetYaxis()->SetLabelSize(0.045);
162  glower_belt->GetYaxis()->SetLabelOffset(0.01);
163  glower_belt->GetYaxis()->SetNdivisions(3);
164 
165  // draw legend
166  TLegend *leg = new TLegend(0.4148297,0.1184669,0.8897796,0.3501742,NULL,"brNDC");
167  leg->SetBorderSize(1);
168  leg->SetTextAlign(22);
169  leg->SetTextFont(12);
170  leg->SetLineColor(1);
171  leg->SetLineStyle(1);
172  leg->SetLineWidth(1);
173  leg->SetFillColor(0);
174  leg->SetFillStyle(1001);
175  leg->SetTextSize(0.04);
176  leg->SetLineColor(kBlack);
177  leg->SetFillColor(kWhite);
178  leg->AddEntry(gmedian_belt,"numerical median","lp");
179  leg->AddEntry(glower_belt,TString::Format("numerical %.4g%% confidence region",coverage));
180  leg->AddEntry(gmedian_belt2,"analytical median","lp");
181  leg->AddEntry(glower_belt2,TString::Format("analytical %.4g%% confidence region",coverage));
182  leg->AddEntry(glower_belt3,TString::Format("analytical %.4g%% confidence region",90.));
183  leg->Draw();
184 
185  return;
186 }
187 
188 double InverseProbabilityBH(int N, int k, double p) {
189 
190 #define LMAX 100
191 #define PRECISION 1.e-6
192 
193  int L=100;
194 
195  double x;
196  double xmin=0;
197  double xmax=1;
198 
199  double a=0.;
200  for(int i=0;i<LMAX;i++) {
201  x = gRandom->Uniform(xmin,xmax);
202  //cout << i << "\txmin " << xmin << "\txmax " << xmax << "\tx " << x << endl;
203  a=ROOT::Math::inc_beta(x, k, N-k+1);
204  if(a<p) xmin=x; else xmax=x;
205  if(fabs(a-p)<PRECISION) break;
206  }
207  return x;
208 }
209 
210 TGraph* DrawProbabilityBH(int N, double p, Color_t color, int style, TString mode) {
211 // BENJAMINI-HOCHBERG
212 
213  double* x = new double[N];
214  for(int i=0;i<N;i++) x[i]=i+1;
215 
216  double* prob = new double[N];
217  for(int k=1;k<=N;k++) prob[k-1]=InverseProbabilityBH(N, k, p);
218 
219  TGraph* gprob = new TGraph(N,x,prob);
220  gprob->SetLineColor(color);
221  gprob->SetLineWidth(3);
222  gprob->SetLineStyle(style);
223  gprob->Draw(mode);
224  gprob->GetXaxis()->SetRangeUser(1,N);
225  gprob->GetYaxis()->SetRangeUser(0,1);
226 
227  return gprob;
228 }
229 
#define PRECISION
wavearray< double > a(hp.size())
TString("c")
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1) *itime+ifreq;double time=itime *dT;double freq=(ifreq >0) ? ifreq *dF :dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
double median(std::vector< double > &vec)
Definition: wavegraph.cc:485
return wmap canvas
void PvalueLocal2GlobalBelts(double coverage=COVERAGE, bool draw_pvalues_plot=true)
int j
Definition: cwb_net.C:28
i drho i
#define N
TGraph * DrawProbabilityBH(int N, double p, Color_t color, int style=9, TString mode="ALP")
#define LMAX
size_t mode
#define COVERAGE
i() int(T_cor *100))
int k
double InverseProbabilityBH(int N, int k, double p)
double vpvalue[NTRIALS][NPVALUES]
#define NTRIALS
wavearray< int > index
double fabs(const Complex &x)
Definition: numpy.cc:55
#define NPVALUES
exit(0)