forked from amissert/atmFitTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkovTools.cxx
267 lines (232 loc) · 6.34 KB
/
markovTools.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "markovTools.h"
void markovTools::savePath(){
pathTree->Write();
// fout->Close();
// if (filename){
// pathTree->SaveAs(filename);
// }
// else{
// pathTree->SaveAs("mcmcpath.root");
// }
fout->Close();
return;
}
void markovTools::setParVar(int ipar,double value){
varPar[ipar] = value;
//atmPars->setParameter(ipar, value);
cout<<"param "<<ipar<<" variance set to: "<<value<<endl;
return;
}
void markovTools::testAtm(int ntry){
//setup atmFitPars
atmFitPars* fitpars = new atmFitPars(1,1,1,1);
fitpars->setParameter(0,0.);
fitpars->setParameter(1,0.);
fitpars->setSysParUnc(0,1.);
fitpars->setSysParUnc(1,1.);
nPars = fitpars->nTotPars;
atmPars = fitpars;
tuneParameter = 1.0;
int itry = 0;
int iaccept = 0;
double LnL;
htest2D = new TH2D("htest2","htest2",50,-5,5,50,-5,5);
while (itry<ntry){
cout<<"step: "<<itry<<endl;
double par1 = atmPars->getParameter(0);
double par2 = atmPars->getParameter(1);
LnL = par1*par1 + par2*par2;
setL(LnL);
cout<<"L: "<<oldL<<endl;
proposeStep();
par1 = atmPars->getParameter(0);
par2 = atmPars->getParameter(1);
LnL = par1*par1 + par2*par2;
if (acceptStepLnL(LnL)){
iaccept++;
htest2D->Fill(atmPars->getParameter(0),atmPars->getParameter(1));
}
cout<<"accepted: "<<iaccept<<endl;
itry++;
}
htest2D->Draw("colz");
//pathTree->SaveAs("pathTree.root");
return;
}
void markovTools::test(int ntry){
nPars = 1;
double thePar[1] = {0.};
int itry = 0;
int iaccept = 0;
htest = new TH1D("htest","htest",100,-5,5);
while (itry<ntry){
cout<<"step: "<<itry<<endl;
setL(1.*(thePar[0]*thePar[0]));
cout<<"L: "<<oldL<<endl;
proposeStep(thePar);
cout<<"proposal: "<<thePar[0]<<endl;
if (acceptStepLnL(1.*(thePar[0]*thePar[0]),thePar)){
iaccept++;
htest->Fill(thePar[0]);
}
cout<<"accepted: "<<iaccept<<endl;
itry++;
}
htest->Draw();
pathTree->SaveAs("pathTree.root");
return;
}
/////////////////////////////////////////////
//decide if new parameters shoudl be accepted
int markovTools::acceptStepLnL(double newL){
double alpha = (oldL-newL); //< get difference in LnL
double rand = randy->Rndm(); //< throw a random number
int iaccept = 0; //< acceptance flag
/////////////////////////////
//chekc if we should accept
if (alpha>TMath::Log(rand)){
//accepted! new pars are now old
for (int i=0;i<nPars;i++){
oldPars[i]=atmPars->getParameter(i);
}
atmPars->acceptStep();
pathTree->Fill();
iaccept = 1;
std::cout<<iStep<<" accepted, LnL = "<<newL<<std::endl;
} /*
else{
for (int i=0;i<nPars;i++){
//rejected, reset to old parameters
atmPars->setParameter(i,oldPars[i]);
}
} */
iStep++; //< increment global step count
if ((iStep%100)==0) cout<<"step: "<<iStep<<endl;
/////////////////////////////
return iaccept;
}
int markovTools::acceptStepLnL(double newL,double* par){
double alpha = (oldL-newL);
double rand = randy->Rndm();
int iaccept = 0;
//cout<<"delta L: "<<alpha<<endl;
if (alpha>TMath::Log(rand)){
// pathTree->SetBranchAddress("par",par);
for (int i=0;i<nPars;i++){
oldPars[i]=par[i];
}
atmPars->acceptStep();
pathTree->Fill();
iaccept = 1;
}
else{
for (int i=0;i<nPars;i++){
par[i]=oldPars[i];
}
}
iStep++;
if ((iStep%100)==0) cout<<"step: "<<iStep<<endl;
return iaccept;
}
int markovTools::acceptStep(double newL,double* par){
double alpha = 1.;
double lnOld;
double lnNew;
lnOld = TMath::Log(oldL);
lnNew = TMath::Log(newL);
alpha = (lnNew-lnOld);
double rand = randy->Rndm();
int iaccept = 0;
if (alpha>TMath::Log(rand)){
cout<<"accept"<<endl;
for (int i=0;i<nPars;i++){
oldPars[i]=par[i];
}
pathTree->Fill();
iaccept = 1;
}
else{
cout<<"reject"<<endl;
for (int i=0;i<nPars;i++){
par[i]=oldPars[i];
}
}
iStep++;
return iaccept;
}
/////////////////////////////////////////////////
//takes a poiter to a parameter array and returns
//a new array of proposed parameters
void markovTools::proposeStep(double* par){
atmPars->proposeStep();
for (int i=0;i<nPars;i++){
if (fixPar[i]!=1) par[i] = atmPars->getPropParameter(i);
//oldPars[i] = par[i];
//if (fixPar[i]!=1) par[i] = randy->Gaus(par[i],varPar[i]*tuneParameter);
}
return;
}
void markovTools::setTuneParameter(double value)
{
tuneParameter=value;
atmPars->setStepSize(tuneParameter);
}
/////////////////////////////////////////////////
//takes a poiter to atmFitPars and suggests new parameters
//
void markovTools::proposeStep(){
atmPars->proposeStep();
for (int i = 0; i < nPars; ++i) {
oldPars[i] = atmPars->getParameter(i);
}
/*
for (int i=0;i<nPars;i++){
oldPars[i] = atmPars->getParameter(i);
if (atmPars->fixPar[i]!=1){
// cout<<"old par: "<<oldPars[i]<<endl;
double random = randy->Gaus(oldPars[i],atmPars->parUnc[i]*tuneParameter);
// cout<<"random: "<<random<<endl;
atmPars->setParameter(i,random);
//atmPars->proposeStep();
// cout<<"new par: "<<atmPars->getParameter(i);
}
}
return;
*/
}
/////////////////////////////////////////////
//initialization
void markovTools::Init(int npars){
// set total number of parameters
nPars = npars;
//current step counter
iStep=0;
//branch setup
pathTree->Branch("npars",&nPars,"npars/I");
pathTree->Branch("step",&iStep,"step/I");
pathTree->Branch("par",oldPars,"par[500]/D");
//done
return;
}
/////////////////////////////////////////////
//constructor
markovTools::markovTools(int npars){
fout = new TFile("mcmctree.root","RECREATE"); //< set output file name
pathTree = new TTree("MCMCpath","MCMCpath"); //< initialize new tree for steps
nPars = npars; //< set total # of parameters
iStep = 0; //< counter of current step
//////////////////////////////////////////
//branch setup
pathTree->Branch("npars",&nPars,"npars/I");
pathTree->Branch("step",&iStep,"step/I");
pathTree->Branch("par",oldPars,"par[500]/D");
}
////////////////////////////////////////////
//construct from atmFitPars
markovTools::markovTools(atmFitPars* fitpars){
fout = new TFile("mcmctree.root","RECREATE"); //< set output file name
pathTree = new TTree("MCMCpath","MCMCpath"); //< initialize new tree for steps
atmPars = fitpars;
Init(fitpars->nTotPars);
return;
}