flavour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
MCMC.h
Go to the documentation of this file.
1 #ifndef MCMC_H
2 #define MCMC_H
3 
4 #include "model.h"
5 #include "Math/GSLMinimizer.h"
6 #include "Math/Functor.h"
7 
8 using namespace std;
9 
11 class Peak {
12 public:
13  Peak(const Model * m,int maxx=0): model(m), pr(m->generateparameters(maxx)), max(maxx){
14  lmax=model->likelihood(pr);
15  }
16 
17  void findPeak(){
18  llmax=model->loglike(pr,1,max);
19  area=1;
20  uint fixed=1e2;
21  uint f=fixed;
22  double d=1;
23  //cout<<"f "<<f<<"llmax "<<llmax<<endl;
24  for(uint i=1e5;i;i--){
25  parameters p1(pr);
26  p1.next(model->r,d);
27 
28  double l1=llmax;
29  if(!model->veto(p1,max)){l1=model->loglike(p1,1,max);
30  }
31  if(l1>llmax){pr=p1; llmax=l1; f=fixed;}
32  else {f--; if(!f) {d/=100; f=fixed; if(d<1e-2) break;}}
33  }
34  cout<<"d "<<d<<"llmax "<<llmax<<endl;
35  if(llmax<-1000) lmax=0;
36  else{lmax=exp(llmax);
37  area=pr.area();
38  larea=area*lmax;
39  }
40 
41  }
42  /*
43  void findPeak(){
44  llmax=model->loglike(pr);
45  area=1;
46  uint fixed=1e2;
47  uint f=fixed;
48  parameters p1(pr);
49 
50  for(uint j=1e4;j;j--){
51  for(uint i=0;i<pr.size(); i++){
52  double s=pr[i].step/1e3;
53  pr[i].value+=s;
54  if(pr[i].value>pr[i].max){ s*=-1; pr[i].value+=2*s;}
55  double x=(model->loglike(pr)-llmax)*pr[i].step/1e2;
56  pr[i].value-=s;
57  if(fabs(x)>pr[i].step) x*=pr[i].step/fabs(x);
58  p1[i].value=pr[i].value+x;
59  if(p1[i].value>pr[i].max) p1[i].value=pr[i].max;
60  else if(p1[i].value<pr[i].min) p1[i].value=pr[i].min;
61  }
62  if(pr.dist(p1)<1e-6) {pr.setvalues(p1); f=0; break;}
63  cout<<"Loglike "<<llmax<<" "<<pr.dist(p1)<<endl;
64  pr.setvalues(p1);
65  llmax=model->loglike(pr);
66  }
67 
68  if(f || llmax<-20) lmax=0;
69  else{
70  lmax=exp(llmax);
71  area=pr.area();
72  larea=area*lmax;
73  }
74 
75  }
76 
77  double RosenBrock(const double *xx )
78  { parameters p=model->generateparameters();
79  for(uint i=0; i<p.size();i++) p[i].value=xx[i];
80 
81  return -model->loglike(pr);
82  }
83 
84  void findPeak3(){
85  llmax=model->loglike(pr);
86  area=1;
87  uint fixed=1e2;
88  uint f=fixed;
89  parameters p1(pr);
90  ROOT::Math::GSLMinimizer min( ROOT::Math::kVectorBFGS );
91 
92  min.SetMaxFunctionCalls(1000000);
93  min.SetMaxIterations(100000);
94  min.SetTolerance(0.001);
95 
96  ROOT::Math::Functor f(&RosenBrock,pr.size());
97  double step[2] = {0.01,0.01};
98  double variable[2] = { -1.,1.2};
99  char s[3]="x0";
100 
101  min.SetFunction(f);
102  fo
103  // Set the free variables to be minimized!
104  min.SetVariable(0,"x",variable[0], step[0]);
105  min.SetVariable(1,"y",variable[1], step[1]);
106 
107  min.Minimize();
108 
109  for(uint j=1e4;j;j--){
110  for(uint i=0;i<pr.size(); i++){
111  double s=pr[i].step/1e3;
112  pr[i].value+=s;
113  if(pr[i].value>pr[i].max){ s*=-1; pr[i].value+=2*s;}
114  double x=(model->loglike(pr)-llmax)*pr[i].step/1e2;
115  pr[i].value-=s;
116  if(fabs(x)>pr[i].step) x*=pr[i].step/fabs(x);
117  p1[i].value=pr[i].value+x;
118  if(p1[i].value>pr[i].max) p1[i].value=pr[i].max;
119  else if(p1[i].value<pr[i].min) p1[i].value=pr[i].min;
120  }
121  if(pr.dist(p1)<1e-6) {pr.setvalues(p1); f=0; break;}
122  cout<<"Loglike "<<llmax<<" "<<pr.dist(p1)<<endl;
123  pr.setvalues(p1);
124  llmax=model->loglike(pr);
125  }
126 
127  if(f || llmax<-20) lmax=0;
128  else{
129  lmax=exp(llmax);
130  area=pr.area();
131  larea=area*lmax;
132  }
133 
134  }
135  */
136  bool adjuststeps(){
137  parameters p1(pr);
138  for(uint i=0;i<pr.size(); i++){
139  double s=p1[i].step;
140  p1[i].value+=s;
141  double x=(lmax-model->likelihood(p1))*2/lmax/s/s;
142  double x0=std::pow(2/(pr[i].max-pr[i].min),2);
143  if(x<x0) return 0;
144  // cout<<"X "<<x<<endl;
145  pr[i].step=1/sqrt(x);
146  p1[i].value-=s;
147  }
148  area=pr.area();
149  larea=area*lmax;
150  return 1;
151  }
152 
153  const Model * model;
155  double lmax, llmax;
156  double area;
157  double larea;
158  bool max;
159 };
160 
162 class Proposal{
163 public:
164 
165 Proposal(const Model * m): model(m), floatPeak(m),proposal(m){}
166 
167 void findPeaks(uint ns=1, int max=0) {
168  //float pmin=-100, pmax=100, s=0.1;
169  //floatPeak.s=s;
170  //floatPeak.lmax=0;
171  //int imax=-1;
172  floatPeak=Peak(model,max);
173  floatPeak.lmax=0;
174  floatPeak.llmax=-1000;
175  cout<<"started"<<endl;
176  //for(uint i=5e1;i;i--){
177  for(uint i=ns;i;i--){
178  Peak pp(model,max);
179  pp.findPeak();
180  if(pp.llmax>-15){
181  //for(uint j=0; j< pp.pr.size();j++){
182  //cout<<j<<" "<<pp.pr[j].value<<endl;
183  //}
184  //lst l=model->getlist(pp.pr);
185  //for(uint j=0; j< model->size();j++){
186  // double mean=model->at(j).calculate(l);
187  //cout<<j<<" "<<mean<<" "<<sqrt(2*model->at(j).o->loglikelihood(mean))<<endl;
188  //}
189  }
190  if(pp.lmax>floatPeak.lmax){
191  cout<<i<<" "<<pp.lmax<<endl;
192  floatPeak.lmax=pp.lmax;
193  floatPeak.pr=pp.pr;
194  }
195  }
196  floatPeak.area=floatPeak.pr.area();
197 
198 }
199 
200 void getProposal(){
201  if(model->r->Rndm()<=0.9) {
202  proposal.pr=floatPeak.pr;
203  proposal.pr.next(model->r);
204  return;
205  }
206 
207  proposal.pr=model->generateparameters();
208 }
209 
211  getProposal();
212  double l1=0;
213  l1=model->likelihood(proposal.pr);
214  if(model->r->Rndm()<=l1/floatPeak.lmax){
215  floatPeak.lmax=l1;
216  floatPeak.pr.setvalues(proposal.pr);
217  }
218 }
219 
220 const Model * model;
221 
222 vector<Peak> vPeak;
223 Peak floatPeak, proposal;
224 double total;
225 };
226 
227 #endif
Peak(const Model *m, int maxx=0)
Definition: MCMC.h:13
void findPeaks(uint ns=1, int max=0)
Definition: MCMC.h:167
void getProposal()
Definition: MCMC.h:200
double larea
Definition: MCMC.h:157
Definition: multivector.h:4
double area() const
Definition: model.h:215
bool adjuststeps()
Definition: MCMC.h:136
double total
Definition: MCMC.h:224
void findPeak()
Definition: MCMC.h:17
unsigned int uint
Definition: script.cpp:4
const Model * model
Definition: MCMC.h:153
vector of parameters
Definition: model.h:177
Proposal(const Model *m)
Definition: MCMC.h:165
vector< Peak > vPeak
Definition: MCMC.h:222
void getNextPoint()
Definition: MCMC.h:210
Abstract class for a model.
Definition: model.h:361
bool max
Definition: MCMC.h:158
A class containing the parameters of a maximum of the likelihood function.
Definition: MCMC.h:11
double area
Definition: MCMC.h:156
Peak proposal
Definition: MCMC.h:223
const Model * model
Definition: MCMC.h:220
double lmax
Definition: MCMC.h:155
void next(TRandom3 *r, double f=1)
changes randomly the value of the parameters
Definition: model.h:182
double llmax
Definition: MCMC.h:155
A class containing the parameters of a proposal for the next step in the Markov Chain.
Definition: MCMC.h:162
parameters pr
Definition: MCMC.h:154