ROOTPWA
checkMCMC.C
Go to the documentation of this file.
1 
2 //
3 // Copyright 2009 Sebastian Neubert
4 //
5 // This file is part of rootpwa
6 //
7 // rootpwa is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // rootpwa is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
19 //
21 #include "TTree.h"
22 #include "TGraph.h"
23 #include "TMultiGraph.h"
24 #include <iostream>
25 using namespace std;
26 
27 
28 
29 void checkMCMC(TTree* tree, unsigned int ndim, unsigned int burnin=0, unsigned int step=100){
30  double X[ndim];
31  double Grad[ndim];
32  double Xmean[ndim];
33  double Var2[ndim]; // variances
34  double Var3[ndim]; // sum_k (x_k - x_mean)^3*dL/dx_k
35  double C[ndim]; // Convergence Ratio
36  vector<TGraph*> graphs(ndim);
37  unsigned int nentries=tree->GetEntriesFast();
38  TMultiGraph* mgraph=new TMultiGraph();
39 
40  for(unsigned int ip=0;ip<ndim;++ip){
41  Xmean[ip]=0;
42  Grad[ip]=0;
43  Var2[ip]=0;
44  Var3[ip]=0;
45  graphs[ip]=new TGraph((nentries-burnin)/step);
46  mgraph->Add(graphs[ip]);
47  }// end loop over parameters
48 
49  tree->SetBranchAddress("dL",&Grad);
50  tree->SetBranchAddress("X",&X);
51 
52 
53 
54 
55  for(unsigned int i=burnin; i<nentries; i+=step){
56  // calculate mean
57  for(unsigned int j=burnin; j<=i; ++j){
58  tree->GetEntry(j);
59  for(unsigned int ip=0;ip<ndim;++ip){
60  Xmean[ip]+=X[ip];
61  }// end loop over parameters
62  }
63  for(unsigned int ip=0;ip<ndim;++ip){
64  Xmean[ip]/=(double)(i-burnin+1);
65  //cout << Xmean[ip] << endl;
66  }// end loop over parameters
67  for(unsigned int j=burnin; j<=i; ++j){
68  tree->GetEntry(j);
69  // caluclate variance terms
70  for(unsigned int ip=0;ip<ndim;++ip){
71  double diff=X[ip]-Xmean[ip];
73  Var2[ip]+=diff*diff;
74  Var3[ip]+=diff*diff*diff*Grad[ip];
75  }// end loop over parameters
76  }
77 
78  double Rmin=10000000;
79  double Rmax=-10000000;
80  double Rmean=0;
81  unsigned int Imin, Imax;
82 
83 
84  if(i>burnin){
85  for(unsigned int ip=0;ip<ndim;++ip){
86  if(Var2[ip]==0) continue;
87  double R=Var3[ip]/(3*Var2[ip]);
88  graphs[ip]->SetPoint(i-burnin,i-burnin,R);
89  if(R<Rmin){Rmin=R;Imin=ip;}
90  if(R>Rmax){Rmax=R;Imax=ip;}
91  Rmean+=R;
92 
93  Xmean[ip]=0;
94  Grad[ip]=0;
95  Var2[ip]=0;
96  Var3[ip]=0;
97  }// end loop over parameters
98  Rmean/=(double)ndim;
99  cout << "Rmin("<<Imin<<")="<<Rmin
100  <<" ... Rmean="<<Rmean
101  <<" ... Rmax("<<Imax<<")="<<Rmax<<endl;
102  }
103 
104  }
105 
106  mgraph->Draw("AP");
107 
108 }