source: Sophya/trunk/SophyaExt/MinuitAdapt/testminuit.cc@ 2404

Last change on this file since 2404 was 2404, checked in by cmv, 22 years ago

Creation du module (MinuitAdapt) d'adaptation de MINUIT pour Sophya

cmv 11/06/2003

File size: 9.2 KB
Line 
1/*
2 Fit d'une gaussienne par une methode de chi2
3*/
4#include <machdefs.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include <iostream.h>
8#include <math.h>
9#include <string.h>
10
11#include "nbrandom.h"
12#include "minuitadapt.h"
13
14// Ne pas changer NPAR
15#define NPAR 7
16
17const int DIMX = 25;
18const int DIMY = 25;
19const double HAUT=10000.;
20const double X0=0., Y0=0.;
21const double SX=5., SY=5., RHO=0.05;
22const double FOND=100.;
23// vol x0 y0 sx sy rho fond
24const bool FIX[NPAR]={false,false,false,false,false,false,false};
25const double ERR=5.;
26const double ERRMIN=ERR*sqrt(FOND);
27const double nSX=7., nSY=5.;
28const bool USERGRAD=false;
29const bool DOIMPROVE=true;
30const bool DOMINOS=true;
31const bool DOCONT=true;
32const bool DOSCAN=true;
33#define VARAND (drandpm1())
34//#define VARAND 1.
35#define MAXCALL 99999
36
37void fcn(int_4 *,double *,double *,double *,int_4 *,double futils(double *));
38double futils(double *x) {return 0.;}
39double Gauss2D(double x,double y,double *param);
40double dGauss2D(double x,double y,double *param,double *dparam);
41
42double X[DIMX], Y[DIMY], Z[DIMX][DIMY], EZ[DIMX][DIMY];
43int_4 IFLAG[10]={0,0,0,0,0,0,0,0,0,0};
44
45/*==========================================================================*/
46int main(int nargv, char *argv[])
47{
48 /* initialisation */
49 if(DIMX*DIMY<=NPAR) exit(-1);
50
51 int nran=0;
52 if(nargv>1) nran=atoi(argv[1]);
53 for(int i=0;i<nran;i++) drand01();
54
55 /* compute volume */
56 double vol=HAUT*(2*M_PI*SX*SY)/sqrt(1.-RHO*RHO);
57 cout<<"haut="<<HAUT<<" vol="<<vol<<endl;
58 cout<<"Fond="<<FOND<<endl;
59
60 /* remplissage de la densite de probabilite a fiter */
61 double par[NPAR],epar[NPAR];
62 printf("Donnees a fiter %d :\n",DIMX*DIMY);
63 par[0]=vol; par[1]=X0; par[2]=Y0;
64 par[3]=SX; par[4]=SY; par[5]=RHO;
65 par[6]=FOND;
66 for(int i=0;i<DIMX;i++) {
67 X[i] = X0 +nSX*SX*(2.*(i+0.5)/DIMX-1.);
68 for(int j=0;j<DIMY;j++) {
69 Y[j] = Y0 +nSY*SY*(2.*(j+0.5)/DIMY-1.);
70 double f = Gauss2D(X[i],Y[j],par);
71 Z[i][j] = f;
72 double ef = (fabs(f)>1.) ? ERR*sqrt(fabs(f)): ERR;
73 EZ[i][j] = (ef>ERRMIN)? ef: ERRMIN;
74 Z[i][j] += EZ[i][j]*NorRand();
75 }
76 }
77
78/*********************** minuit minimisation ***********************/
79 printf("\n\n");
80 MinuitAdapt MMM(fcn,futils);
81
82 /* initialise */
83 MMM.SetTitle("Minuit fit Gaussienne 2D+Fond");
84 MMM.Clear();
85 MMM.SetRandom(1000000);
86
87 /* set print and warning level, precision etc... (-1,0,1,2,3) */
88 MMM.PrintLevel(1);
89 MMM.SetWidthPage(120);
90 MMM.SetWarnings(true);
91 MMM.SetErrorDef(1.);
92 MMM.SetEpsMachine(1.e-13);
93 MMM.SetStrategy(1);
94 if(USERGRAD) MMM.SetGradient(1); else MMM.SetGradient(-1);
95
96 /* set parameters */
97 MMM.DefineParameter(1,"Vol",vol,fabs(vol)/50.);
98 MMM.DefineParameter(2,"X0",X0,SX/5.);
99 MMM.DefineParameter(3,"Y0",Y0,SY/5.);
100 MMM.DefineParameter(4,"Sx",SX,SX/5.,0.01*SX,10.*SX);
101 MMM.DefineParameter(5,"Sy",SY,SY/5.,0.01*SY,10.*SY);
102 MMM.DefineParameter(6,"Rho",RHO,0.0001,-1.,1.);
103 double x=(FOND!=0.)? fabs(FOND)/10.: 0.01;
104 MMM.DefineParameter(7,"Fond",FOND,x);
105
106 /* set parameters */
107 x = (FIX[0])? vol: vol+VARAND*vol/5.;
108 MMM.SetParameter(1,x);
109 x = (FIX[1])? X0: X0+VARAND*SX;
110 MMM.SetParameter(2,x);
111 x=(FIX[2])? Y0: Y0+VARAND*SY;
112 MMM.SetParameter(3,x);
113 x=(FIX[3])? SX: SX+VARAND*SX/2.;
114 MMM.SetParameter(4,x);
115 x=(FIX[4])? SY: SY+VARAND*SY/2.;
116 MMM.SetParameter(5,x);
117 x=(FIX[5])? RHO: 0.;
118 MMM.SetParameter(6,x);
119 x=(FIX[6])? FOND: FOND+VARAND*FOND/3.;
120 MMM.SetParameter(7,x);
121
122 /* fix parameters */
123 for(int i=0;i<NPAR;i++) if(FIX[i]) MMM.SetFix(i+1);
124 MMM.ShowParameter();
125
126 /* minimize */
127 MMM.Migrad(MAXCALL,0.01);
128 //MMM.Minimize(MAXCALL,0.01);
129 //MMM.Simplex(MAXCALL,0.01);
130 //MMM.Seek(MAXCALL,5.);
131 MMM.ShowFcnValue();
132
133 if(DOIMPROVE) MMM.Improve(MAXCALL);
134 MMM.ShowFcnValue();
135
136 if(DOMINOS) MMM.Minos(MAXCALL);
137 MMM.ShowFcnValue();
138
139 /* get parameters and errors */
140 cout<<endl;
141 for(int i=0;i<NPAR;i++) {
142 string dum; int_4 ivarbl;
143 double b1,b2,eparab,eplus,eminus,globcc;
144 MMM.GetParameter(i+1,dum,par[i],epar[i],b1,b2,ivarbl);
145 MMM.GetErrors(i+1,eplus,eminus,eparab,globcc);
146 printf("> parameter %d \"%s\" = %g %g (%g,%g) int var=%d\n"
147 ,i+1,dum.c_str(),par[i],epar[i],b1,b2,ivarbl);
148 printf(" e+=%g e-=%g eparab=%g globcc=%g\n"
149 ,eplus,eminus,eparab,globcc);
150 }
151 fflush(stdout);
152 cout<<"haut(sig)="<<par[0]*sqrt(1-RHO*RHO)/(2*M_PI*SX*SY)
153 <<" haut(sig_fit)="<<par[0]*sqrt(1-par[5]*par[5])/(2*M_PI*par[3]*par[4])<<endl;
154
155 TMatrix<r_8> emat = MMM.GetErrorsMatrix();
156 cout<<"GetErrorsMatrix:"<<emat<<endl;
157
158 /* get covariance matrix */
159 MMM.ShowCovariance();
160 MMM.ShowCorrelations();
161 MMM.ShowEigenValues();
162
163 /* contour plot */
164 if(DOCONT) {
165 for(int i=1;i<NPAR;i++) {
166 if(FIX[i]) continue;
167 for(int j=i+1;j<=NPAR;j++) {
168 if(FIX[j]) continue;
169 //MMM.DrawContour(i,j,20);
170 TVector<r_8> xcont,ycont;
171 int_4 ncontok = MMM.GetContour(i,j,20,xcont,ycont);
172 cout<<"Contour "<<i<<" "<<j<<" ncontok="<<ncontok<<endl;
173 if(ncontok<1) continue;
174 for(int k=0;k<ncontok;k++) cout<<" ("<<xcont[k]<<","<<ycont[k]<<")";
175 cout<<endl;
176 }
177 }
178 }
179
180 /* scan parameters */
181 if(DOSCAN) {
182 MMM.ShowFcnValue();
183 for(int i=0;i<NPAR;i++) {
184 if(FIX[i]) continue;
185 MMM.Scan(i+1,par[i]-2.*epar[i],par[i]+2.*epar[i],20);
186 }
187 MMM.ShowFcnValue();
188 }
189
190 /* end */
191 MMM.ShowRandom();
192 MMM.Return();
193
194 printf("\n\n");
195/*******************************************************************/
196
197 /* fin de minimisation, print ! */
198 double xi2=0.;
199 for(int i=0;i<DIMX;i++) for(int j=0;j<DIMY;j++) {
200 double f = Z[i][j]-Gauss2D(X[i],Y[j],par);
201 xi2 += f*f/(EZ[i][j]*EZ[i][j]);
202 }
203 cout<<"1: "<<vol<<"\tfit="<<par[0]<<"\tefit="<<epar[0]<<"\td="<<par[0]-vol;
204 if(epar[0]>0.) cout<<"\td/e="<<(par[0]-vol)/epar[0]; cout<<endl;
205 cout<<"2: "<<X0<<"\tfit="<<par[1]<<"\tefit="<<epar[1]<<"\td="<<par[1]-X0;
206 if(epar[1]>0.) cout<<"\td/e="<<(par[1]-X0)/epar[1]; cout<<endl;
207 cout<<"3: "<<Y0<<"\tfit="<<par[2]<<"\tefit="<<epar[2]<<"\td="<<par[2]-Y0;
208 if(epar[2]>0.) cout<<"\td/e="<<(par[2]-Y0)/epar[2]; cout<<endl;
209 cout<<"4: "<<SX<<"\tfit="<<par[3]<<"\tefit="<<epar[3]<<"\td="<<par[3]-SX;
210 if(epar[3]>0.) cout<<"\td/e="<<(par[3]-SX)/epar[3]; cout<<endl;
211 cout<<"5: "<<SY<<"\tfit="<<par[4]<<"\tefit="<<epar[4]<<"\td="<<par[4]-SY;
212 if(epar[4]>0.) cout<<"\td/e="<<(par[4]-SY)/epar[4]; cout<<endl;
213 cout<<"6: "<<RHO<<"\tfit="<<par[5]<<"\tefit="<<epar[5]<<"\td="<<par[5]-RHO;
214 if(epar[5]>0.) cout<<"\td/e="<<(par[5]-RHO)/epar[5]; cout<<endl;
215 cout<<"7: "<<FOND<<"\tfit="<<par[6]<<"\tefit="<<epar[6]<<"\td="<<par[6]-FOND;
216 if(epar[6]>0.) cout<<"\td/e="<<(par[6]-FOND)/epar[6]; cout<<endl;
217 cout<<"Xi2="<<xi2<<"\txi2/n="<<xi2/(DIMX*DIMY-NPAR)<<endl;
218
219 exit(0);
220}
221
222void fcn(int_4 *npar,double *grad,double *fval,double *xval
223 ,int_4 *iflag,double futils(double *))
224{
225 //cout<<"iflag="<<*iflag<<endl;
226 IFLAG[0]++;
227 if(*iflag>0 && *iflag<10) IFLAG[*iflag]++;
228
229 // Read input,init,... data values
230 // if(*iflag==1) {...}
231
232 // Instruct Minuit to redefine the problem
233 // and forget about previously best fitted values.
234 // if(*iflag==5) {...}
235
236 // Always compute Chi2 or Likelyhood (here iflag==4)
237 *fval=0.;
238 for(int i=0;i<DIMX;i++) for(int j=0;j<DIMY;j++) {
239 double f = Z[i][j]-Gauss2D(X[i],Y[j],xval);
240 *fval += f*f/(EZ[i][j]*EZ[i][j]);
241 }
242
243 // Compute (optionnal) the first derivative of Chi2 / parameters
244 if(*iflag==2) {
245 // Return gradient of chi2 (if SET GRA called)
246 // C'est DChi2/DPi = -2*sum{(Yi-F(Xi))/EYi^2 * dF/dPi(Xi)}
247 double dpar[NPAR];
248 for(int j=0;j<NPAR;j++) grad[j]=0.;
249 for(int i=0;i<DIMX;i++) for(int j=0;j<DIMY;j++) {
250 double f=-2.*(Z[i][j]-Gauss2D(X[i],Y[j],xval))/(EZ[i][j]*EZ[i][j]);
251 dGauss2D(X[i],Y[j],xval,dpar);
252 for(int k=0;k<NPAR;k++) grad[k]+= f*dpar[k];
253 }
254 }
255
256 // Called at the end of the fit (on the Minuit RETURN)
257 if(*iflag==3) {
258 cout<<"Call fcn iflag="<<*iflag<<" npar="<<*npar<<endl;
259 for(int k=0;k<NPAR;k++) cout<<" P"<<k+1<<"="<<xval[k];
260 cout<<endl;
261 cout<<"Number of fcn calls="<<IFLAG[0]<<endl;
262 for(int k=1;k<10;k++)
263 cout<<" iflag="<<k<<" number of calls="<<IFLAG[k]<<endl;
264 }
265}
266
267double Gauss2D(double x,double y,double *param)
268// xc = (x-p1)/p3 ; yc = (y-p2)/p4
269// f(x) = p0*(1-rho^2)/(2*Pi*p3*p4) * exp(-0.5*(xc^2+yc^2-2*p5*xc*yc))
270{
271 double N = sqrt(1.-param[5]*param[5])/(2*M_PI*param[3]*param[4]);
272 double X = (x-param[1])/param[3];
273 double Y = (y-param[2])/param[4];
274 double z2 = (X*X + Y*Y - 2.*param[5]*X*Y)/2.;
275 if(z2<100.) z2=exp(-z2); else z2=0.;
276 return param[0]*N*z2 + param[6];
277}
278
279double dGauss2D(double x,double y,double *param,double *dparam)
280{
281 double unmr2 = 1.-param[5]*param[5];
282 double N = sqrt(unmr2)/(2*M_PI*param[3]*param[4]);
283 double X = (x-param[1])/param[3];
284 double Y = (y-param[2])/param[4];
285
286 double XmrY = X-param[5]*Y;
287 double YmrX = Y-param[5]*X;
288 double z2 = (X*(XmrY-param[5]*Y)+Y*Y)/2.;
289
290 /* g(x,y) */
291 double PSF = 0.;
292 if(z2<100.) PSF = N * exp(-z2);
293 /* dg(x,y)/d(Vol) */
294 dparam[0] = PSF;
295 /* dg(x,y)/d(x0) */
296 dparam[1] = param[0]* PSF* XmrY/param[3];
297 /* dg(x,y)/d(y0) */
298 dparam[2] = param[0]* PSF* YmrX/param[4];
299 /* dg(x,y)/d(sx)*/
300 dparam[3] = param[0]* PSF* (X*XmrY-1.)/param[3];
301 /* dg(x,y)/d(sy) */
302 dparam[4] = param[0]* PSF* (Y*YmrX-1.)/param[4];
303 /* dg(x,y)/d(rho) */
304 dparam[5] = param[0]* PSF* (X*Y-2.*param[5]/unmr2);
305 /* dg(x,y)/d(Fond) */
306 dparam[6] = 1.;
307
308 return param[0] * PSF + param[6];
309}
Note: See TracBrowser for help on using the repository browser.