53 TMVA::OptimizeConfigParameters::OptimizeConfigParameters(MethodBase * const method, std::map<
TString,TMVA::Interval*> tuneParameters, TString fomType, TString optimizationFitType)
55 fTuneParameters(tuneParameters),
57 fOptimizationFitType(optimizationFitType),
64 std::string
name =
"OptimizeConfigParameters_";
65 name += std::string(
GetMethod()->GetName());
66 fLogger =
new MsgLogger(name);
67 if (fMethod->DoRegression()){
68 Log() <<
kFATAL <<
" ERROR: Sorry, Regression is not yet implement for automatic parameter optimization"
69 <<
" --> exit" <<
Endl;
72 Log() <<
kINFO <<
"Automatic optimisation of tuning parameters in "
75 std::map<TString,TMVA::Interval*>::iterator it;
76 for (it=fTuneParameters.begin(); it!=fTuneParameters.end();it++) {
78 <<
" in range from: " << it->second->GetMin()
79 <<
" to: " << it->second->GetMax()
80 <<
" in : " << it->second->GetNbins() <<
" steps"
83 Log() <<
kINFO <<
" using the options: " << fFOMType <<
" and " << fOptimizationFitType <<
Endl;
104 if (ymin>y[i]) ymin=y[i];
105 if (ymax<y[i]) ymax=y[i];
124 if (fOptimizationFitType ==
"Scan" ) this->optimizeScan();
125 else if (fOptimizationFitType ==
"FitGA" || fOptimizationFitType ==
"Minuit" ) this->optimizeFit();
127 Log() <<
kFATAL <<
"You have chosen as optimization type " << fOptimizationFitType
128 <<
" that is not (yet) coded --> exit()" <<
Endl;
132 std::map<TString,Double_t>::iterator it;
133 for(it=fTunedParameters.begin(); it!= fTunedParameters.end(); it++){
134 Log() <<
kINFO << it->first <<
" = " << it->second <<
Endl;
136 return fTunedParameters;
145 std::vector < int > indices;
146 for (
UInt_t i=0; i< base.size(); i++){
147 indices.push_back(val % base[i] );
148 val = int(
floor(
float(val) /
float(base[i]) ) );
162 Double_t bestFOM=-1000000, currentFOM;
164 std::map<TString,Double_t> currentParameters;
165 std::map<TString,TMVA::Interval*>::iterator it;
169 currentParameters.clear();
170 fTunedParameters.clear();
172 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
173 currentParameters.insert(std::pair<TString,Double_t>(it->first,it->second->GetMin()));
174 fTunedParameters.insert(std::pair<TString,Double_t>(it->first,it->second->GetMin()));
181 std::vector< std::vector <Double_t> >
v;
182 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
183 std::vector< Double_t > tmp;
184 for (
Int_t k=0; k<it->second->GetNbins(); k++){
185 tmp.push_back(it->second->GetElement(k));
190 std::vector< int > Nindividual;
191 for (
UInt_t i=0; i<v.size(); i++) {
193 Nindividual.push_back(v[i].size());
197 for (
int i=0; i<Ntot; i++){
199 std::vector<int> indices = GetScanIndices(i, Nindividual );
200 for (it=fTuneParameters.begin(), index=0; index< indices.size(); index++, it++){
201 currentParameters[it->first] = v[index][indices[index]];
205 for (std::map<TString,Double_t>::iterator it_print=currentParameters.begin();
206 it_print!=currentParameters.end(); it_print++){
207 Log() <<
kINFO <<
" " << it_print->first <<
" = " << it_print->second <<
Endl;
211 GetMethod()->SetTuneParameters(currentParameters);
214 if (i==0)
GetMethod()->GetTransformationHandler().CalcTransformations(
219 currentFOM = GetFOM();
220 Log() <<
kINFO <<
"FOM was found : " << currentFOM <<
"; current best is " << bestFOM <<
Endl;
222 if (currentFOM > bestFOM) {
223 bestFOM = currentFOM;
224 for (std::map<TString,Double_t>::iterator
iter=currentParameters.begin();
225 iter != currentParameters.end();
iter++){
226 fTunedParameters[
iter->first]=
iter->second;
232 GetMethod()->SetTuneParameters(fTunedParameters);
238 std::vector<TMVA::Interval*> ranges;
239 std::map<TString, TMVA::Interval*>::iterator it;
240 std::vector<Double_t> pars;
242 for (it=fTuneParameters.begin(); it != fTuneParameters.end(); it++){
244 pars.push_back( (it->second)->GetMean() );
253 if ( fOptimizationFitType ==
"Minuit" ) {
256 "FitterMinuit_BDTOptimize",
258 }
else if ( fOptimizationFitType ==
"FitGA" ) {
259 TString opt=
"PopSize=20:Steps=30:Cycles=3:ConvCrit=0.01:SaveBestCycle=5";
261 "FitterGA_BDTOptimize",
264 Log() <<
kWARNING <<
" you did not specify a valid OptimizationFitType "
265 <<
" will use the default (FitGA) " <<
Endl;
266 TString opt=
"PopSize=20:Steps=30:Cycles=3:ConvCrit=0.01:SaveBestCycle=5";
268 "FitterGA_BDTOptimize",
278 for (
UInt_t ipar=0; ipar<ranges.size(); ipar++)
delete ranges[ipar];
283 fTunedParameters.clear();
285 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
286 fTunedParameters.insert(std::pair<TString,Double_t>(it->first,pars[jcount++]));
289 GetMethod()->SetTuneParameters(fTunedParameters);
298 std::map< std::vector<Double_t> ,
Double_t>::const_iterator
iter;
299 iter = fAlreadyTrainedParCombination.find(pars);
301 if (iter != fAlreadyTrainedParCombination.end()) {
307 std::map<TString,Double_t> currentParameters;
310 std::map<TString, TMVA::Interval*>::iterator it;
311 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
312 currentParameters[it->first] = pars[icount++];
315 GetMethod()->SetTuneParameters(currentParameters);
320 CalcTransformations(
GetMethod()->
Data()->GetEventCollection());
330 fAlreadyTrainedParCombination.insert(std::make_pair(pars,-currentFOM));
342 if (fMethod->DoRegression()){
343 std::cout <<
" ERROR: Sorry, Regression is not yet implement for automatic parameter optimisation"
344 <<
" --> exit" << std::endl;
347 if (fFOMType ==
"Separation") fom = GetSeparation();
348 else if (fFOMType ==
"ROCIntegral") fom = GetROCIntegral();
349 else if (fFOMType ==
"SigEffAtBkgEff01") fom = GetSigEffAtBkgEff(0.1);
350 else if (fFOMType ==
"SigEffAtBkgEff001") fom = GetSigEffAtBkgEff(0.01);
351 else if (fFOMType ==
"SigEffAtBkgEff002") fom = GetSigEffAtBkgEff(0.02);
352 else if (fFOMType ==
"BkgRejAtSigEff05") fom = GetBkgRejAtSigEff(0.5);
353 else if (fFOMType ==
"BkgEffAtSigEff05") fom = GetBkgEffAtSigEff(0.5);
355 Log()<<
kFATAL <<
" ERROR, you've specified as Figure of Merit in the "
356 <<
" parameter optimisation " << fFOMType <<
" which has not"
357 <<
" been implemented yet!! ---> exit " <<
Endl;
360 fFOMvsIter.push_back(fom);
370 if (fMvaSig) fMvaSig->Delete();
371 if (fMvaBkg) fMvaBkg->Delete();
372 if (fMvaSigFineBin) fMvaSigFineBin->Delete();
373 if (fMvaBkgFineBin) fMvaBkgFineBin->Delete();
381 fMvaSig =
new TH1D(
"fMvaSig",
"",100,-1.5,1.5);
382 fMvaBkg =
new TH1D(
"fMvaBkg",
"",100,-1.5,1.5);
383 fMvaSigFineBin =
new TH1D(
"fMvaSigFineBin",
"",100000,-1.5,1.5);
384 fMvaBkgFineBin =
new TH1D(
"fMvaBkgFineBin",
"",100000,-1.5,1.5);
386 const std::vector< Event*> events=fMethod->Data()->GetEventCollection(
Types::kTesting);
388 UInt_t signalClassNr = fMethod->DataInfo().GetClassInfo(
"Signal")->GetNumber();
392 for (
UInt_t iev=0; iev < events.size() ; iev++){
396 if (events[iev]->
GetClass() == signalClassNr) {
397 fMvaSig->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
398 fMvaSigFineBin->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
400 fMvaBkg->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
401 fMvaBkgFineBin->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
417 std::cout <<
"Separation caclulcaton via histograms (not PDFs) seems to give still strange results!! Don't do that, check!!"<<std::endl;
449 for (
UInt_t i=0; i<nsteps; i++){
456 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
457 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
458 std::cout <<
" Error in OptimizeConfigParameters GetROCIntegral, unequal histograms for sig and bkg.." << std::endl;
462 Double_t *cumulator = fMvaBkgFineBin->GetIntegral();
469 sigIntegral += fMvaSigFineBin->GetBinContent(ibin) * fMvaSigFineBin->GetBinWidth(ibin);
474 integral += (cumulator[ibin]) * fMvaSigFineBin->GetBinContent(ibin)/sigIntegral * fMvaSigFineBin->GetBinWidth(ibin) ;
492 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
493 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
494 std::cout <<
" Error in OptimizeConfigParameters GetSigEffAt, unequal histograms for sig and bkg.." << std::endl;
497 Double_t *bkgCumulator = fMvaBkgFineBin->GetIntegral();
498 Double_t *sigCumulator = fMvaSigFineBin->GetIntegral();
509 while (bkgCumulator[nbins-ibin] > (1-bkgEff)) {
510 sigEff = sigCumulator[
nbins]-sigCumulator[nbins-ibin];
528 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
529 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
530 std::cout <<
" Error in OptimizeConfigParameters GetBkgEffAt, unequal histograms for sig and bkg.." << std::endl;
534 Double_t *bkgCumulator = fMvaBkgFineBin->GetIntegral();
535 Double_t *sigCumulator = fMvaSigFineBin->GetIntegral();
546 while ( sigCumulator[nbins]-sigCumulator[nbins-ibin] < sigEff) {
547 bkgEff = bkgCumulator[
nbins]-bkgCumulator[nbins-ibin];
564 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
565 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
566 std::cout <<
" Error in OptimizeConfigParameters GetBkgEffAt, unequal histograms for sig and bkg.." << std::endl;
570 Double_t *bkgCumulator = fMvaBkgFineBin->GetIntegral();
571 Double_t *sigCumulator = fMvaSigFineBin->GetIntegral();
582 while ( sigCumulator[nbins]-sigCumulator[nbins-ibin] < sigEff) {
583 bkgRej = bkgCumulator[nbins-ibin];
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
MsgLogger & Endl(MsgLogger &ml)
static void SetIsTraining(Bool_t)
when this static function is called, it sets the flag whether events with negative event weight shoul...
void CheckForUnusedOptions() const
checks for unused options in option string
virtual void SetName(const char *name)
Change (i.e.
Double_t GetSeparation()
return the searation between the signal and background MVA ouput distribution
void optimizeScan()
do the actual optimization using a simple scan method, i.e.
Short_t Min(Short_t a, Short_t b)
virtual void SetYTitle(const char *title)
Double_t GetFOM()
Return the Figure of Merit (FOM) used in the parameter optimization process.
void GetMVADists()
fill the private histograms with the mva distributinos for sig/bkg
Double_t Run()
estimator function interface for fitting
std::vector< Float_t > fFOMvsIter
std::map< std::string, std::string >::const_iterator iter
std::vector< std::vector< double > > Data
TCppMethod_t GetMethod(TCppScope_t scope, TCppIndex_t imeth)
ClassImp(TMVA::OptimizeConfigParameters) TMVA
Constructor which sets either "Classification or Regression".
Double_t GetROCIntegral()
calculate the area (integral) under the ROC curve as a overall quality measure of the classification ...
std::map< TString, Double_t > optimize()
virtual ~OptimizeConfigParameters()
the destructor (delete the OptimizeConfigParameters, store the graph and .. delete it) ...
Double_t GetSigEffAtBkgEff(Double_t bkgEff=0.1)
calculate the signal efficiency for a given background efficiency
1-D histogram with a double per channel (see TH1 documentation)}
Double_t GetVal(Double_t x) const
returns value PDF(x)
TDirectory * BaseDir() const
returns the ROOT directory where info/histograms etc of the corresponding MVA method instance are sto...
Double_t GetBkgEffAtSigEff(Double_t sigEff=0.5)
calculate the background efficiency for a given signal efficiency
virtual void SetXTitle(const char *title)
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
std::vector< int > GetScanIndices(int val, std::vector< int > base)
helper function to scan through the all the combinations in the parameter space
Double_t GetBkgRejAtSigEff(Double_t sigEff=0.5)
calculate the background rejection for a given signal efficiency
Short_t Max(Short_t a, Short_t b)
A Graph is a graphics object made of two arrays X and Y with npoints each.
Double_t EstimatorFunction(std::vector< Double_t > &)
return the estimator (from current FOM) for the fitting interface
Double_t GetIntegral(Double_t xmin, Double_t xmax)
computes PDF integral within given ranges
2-D histogram with a double per channel (see TH1 documentation)}
TString fOptimizationFitType