133 TMVA::MethodLikelihood::MethodLikelihood( const
TString& jobName,
135 DataSetInfo& theData,
138 TMVA::MethodBase( jobName, Types::kLikelihood, methodTitle, theData, theOption, theTargetDir ),
139 fEpsilon ( 1.e3 * DBL_MIN ),
140 fTransformLikelihoodOutput(
kFALSE ),
144 fHistSig_smooth( 0 ),
145 fHistBgd_smooth( 0 ),
146 fDefaultPDFLik ( 0 ),
152 fAverageEvtPerBin( 0 ),
153 fAverageEvtPerBinVarS (0),
154 fAverageEvtPerBinVarB (0),
155 fKDEfineFactor ( 0 ),
156 fInterpolateString(0)
167 fEpsilon ( 1.e3 * DBL_MIN ),
168 fTransformLikelihoodOutput(
kFALSE ),
172 fHistSig_smooth( 0 ),
173 fHistBgd_smooth( 0 ),
174 fDefaultPDFLik ( 0 ),
180 fAverageEvtPerBin( 0 ),
181 fAverageEvtPerBinVarS (0),
182 fAverageEvtPerBinVarB (0),
183 fKDEfineFactor ( 0 ),
184 fInterpolateString(0)
193 if (
NULL != fDefaultPDFLik)
delete fDefaultPDFLik;
194 if (
NULL != fHistSig)
delete fHistSig;
195 if (
NULL != fHistBgd)
delete fHistBgd;
196 if (
NULL != fHistSig_smooth)
delete fHistSig_smooth;
197 if (
NULL != fHistBgd_smooth)
delete fHistBgd_smooth;
198 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
199 if ((*fPDFSig)[ivar] !=0)
delete (*fPDFSig)[ivar];
200 if ((*fPDFBgd)[ivar] !=0)
delete (*fPDFBgd)[ivar];
202 if (
NULL != fPDFSig)
delete fPDFSig;
203 if (
NULL != fPDFBgd)
delete fPDFBgd;
223 fHistSig =
new std::vector<TH1*> ( GetNvar(), (
TH1*)0 );
224 fHistBgd =
new std::vector<TH1*> ( GetNvar(), (
TH1*)0 );
225 fHistSig_smooth =
new std::vector<TH1*> ( GetNvar(), (
TH1*)0 );
226 fHistBgd_smooth =
new std::vector<TH1*> ( GetNvar(), (
TH1*)0 );
227 fPDFSig =
new std::vector<TMVA::PDF*>( GetNvar(), (
TMVA::PDF*)0 );
228 fPDFBgd =
new std::vector<TMVA::PDF*>( GetNvar(), (
TMVA::PDF*)0 );
237 DeclareOptionRef( fTransformLikelihoodOutput =
kFALSE,
"TransformOutput",
238 "Transform likelihood output by inverse sigmoid function" );
243 TString updatedOptions = GetOptions();
244 fDefaultPDFLik =
new PDF(
TString(GetName()) +
" PDF", updatedOptions );
245 fDefaultPDFLik->DeclareOptions();
246 fDefaultPDFLik->ParseOptions();
247 updatedOptions = fDefaultPDFLik->GetOptions();
248 for (
UInt_t ivar = 0; ivar< DataInfo().GetNVariables(); ivar++) {
249 (*fPDFSig)[ivar] =
new PDF(
Form(
"%s PDF Sig[%d]", GetName(), ivar), updatedOptions,
250 Form(
"Sig[%d]",ivar), fDefaultPDFLik );
251 (*fPDFSig)[ivar]->DeclareOptions();
252 (*fPDFSig)[ivar]->ParseOptions();
253 updatedOptions = (*fPDFSig)[ivar]->GetOptions();
254 (*fPDFBgd)[ivar] =
new PDF(
Form(
"%s PDF Bkg[%d]", GetName(), ivar), updatedOptions,
255 Form(
"Bkg[%d]",ivar), fDefaultPDFLik );
256 (*fPDFBgd)[ivar]->DeclareOptions();
257 (*fPDFBgd)[ivar]->ParseOptions();
258 updatedOptions = (*fPDFBgd)[ivar]->GetOptions();
262 SetOptions( updatedOptions );
271 DeclareOptionRef( fNsmooth = 1,
"NSmooth",
272 "Number of smoothing iterations for the input histograms");
273 DeclareOptionRef( fAverageEvtPerBin = 50,
"NAvEvtPerBin",
274 "Average number of events per PDF bin");
275 DeclareOptionRef( fKDEfineFactor =1. ,
"KDEFineFactor",
276 "Fine tuning factor for Adaptive KDE: Factor to multyply the width of the kernel");
277 DeclareOptionRef( fBorderMethodString =
"None",
"KDEborder",
278 "Border effects treatment (1=no treatment , 2=kernel renormalization, 3=sample mirroring)" );
279 DeclareOptionRef( fKDEiterString =
"Nonadaptive",
"KDEiter",
280 "Number of iterations (1=non-adaptive, 2=adaptive)" );
281 DeclareOptionRef( fKDEtypeString =
"Gauss",
"KDEtype",
282 "KDE kernel type (1=Gauss)" );
283 fAverageEvtPerBinVarS =
new Int_t[GetNvar()];
284 fAverageEvtPerBinVarB =
new Int_t[GetNvar()];
285 fNsmoothVarS =
new Int_t[GetNvar()];
286 fNsmoothVarB =
new Int_t[GetNvar()];
287 fInterpolateString =
new TString[GetNvar()];
288 for(
UInt_t i=0; i<GetNvar(); ++i) {
289 fAverageEvtPerBinVarS[i] = fAverageEvtPerBinVarB[i] = 0;
290 fNsmoothVarS[i] = fNsmoothVarB[i] = 0;
291 fInterpolateString[i] =
"";
293 DeclareOptionRef( fAverageEvtPerBinVarS, GetNvar(),
"NAvEvtPerBinSig",
294 "Average num of events per PDF bin and variable (signal)");
295 DeclareOptionRef( fAverageEvtPerBinVarB, GetNvar(),
"NAvEvtPerBinBkg",
296 "Average num of events per PDF bin and variable (background)");
297 DeclareOptionRef(fNsmoothVarS, GetNvar(),
"NSmoothSig",
298 "Number of smoothing iterations for the input histograms");
299 DeclareOptionRef(fNsmoothVarB, GetNvar(),
"NSmoothBkg",
300 "Number of smoothing iterations for the input histograms");
301 DeclareOptionRef(fInterpolateString, GetNvar(),
"PDFInterpol",
"Method of interpolating reference histograms (e.g. Spline2 or KDE)");
313 SetSignalReferenceCut( TransformLikelihoodOutput( 0.5, 0.5 ) );
315 fDefaultPDFLik->ProcessOptions();
316 for (
UInt_t ivar = 0; ivar< DataInfo().GetNVariables(); ivar++) {
317 (*fPDFBgd)[ivar]->ProcessOptions();
318 (*fPDFSig)[ivar]->ProcessOptions();
333 std::vector<Double_t>
xmin(nvar),
xmax(nvar);
334 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
xmin[ivar]=1e30; xmax[ivar]=-1e30;}
337 for (
UInt_t ievt=0; ievt<nevents; ievt++) {
340 const Event* origEv =
Data()->GetEvent(ievt);
341 if (IgnoreEventsWithNegWeightsInTraining() && origEv->
GetWeight()<=0)
continue;
343 for (
int cls=0;cls<2;cls++){
344 GetTransformationHandler().SetTransformationReferenceClass(cls);
345 const Event* ev = GetTransformationHandler().Transform( origEv );
346 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
349 if (value > xmax[ivar]) xmax[ivar] =
value;
356 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
357 TString var = (*fInputVars)[ivar];
364 if (DataInfo().GetVariableInfo(ivar).GetVarType() ==
'I') {
367 xmax[ivar]=xmax[ivar]+1;
371 (*fHistSig)[ivar] =
new TH1F( var +
"_sig", var +
" signal training", nbins, ixmin, ixmax );
372 (*fHistBgd)[ivar] =
new TH1F( var +
"_bgd", var +
" background training", nbins, ixmin, ixmax );
376 Int_t nbinsS = (*fPDFSig)[ivar]->GetHistNBins( minNEvt );
377 Int_t nbinsB = (*fPDFBgd)[ivar]->GetHistNBins( minNEvt );
379 (*fHistSig)[ivar] =
new TH1F(
Form(
"%s_sig",var.
Data()),
380 Form(
"%s signal training",var.
Data()), nbinsS,
xmin[ivar], xmax[ivar] );
381 (*fHistBgd)[ivar] =
new TH1F(
Form(
"%s_bgd",var.
Data()),
382 Form(
"%s background training",var.
Data()), nbinsB,
xmin[ivar], xmax[ivar] );
387 Log() <<
kINFO <<
"Filling reference histograms" <<
Endl;
390 for (
Int_t ievt=0; ievt<
Data()->GetNEvents(); ievt++) {
394 const Event* origEv =
Data()->GetEvent(ievt);
395 if (IgnoreEventsWithNegWeightsInTraining() && origEv->
GetWeight()<=0)
continue;
396 GetTransformationHandler().SetTransformationReferenceClass( origEv->
GetClass() );
397 const Event* ev = GetTransformationHandler().Transform( origEv );
403 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
406 if (value >= xmax[ivar]) value = xmax[ivar] - 1.0e-10;
407 else if (value <
xmin[ivar]) value =
xmin[ivar] + 1.0e-10;
409 if (value >=(*fHistSig)[ivar]->GetXaxis()->GetXmax() ||
410 value <(*fHistSig)[ivar]->GetXaxis()->GetXmin()){
412 <<
"error in filling likelihood reference histograms var="
413 <<(*fInputVars)[ivar]
414 <<
", xmin="<<(*fHistSig)[ivar]->GetXaxis()->GetXmin()
416 <<
", xmax="<<(*fHistSig)[ivar]->GetXaxis()->GetXmax()
419 if (DataInfo().IsSignal(ev)) (*fHistSig)[ivar]->Fill( value, weight );
420 else (*fHistBgd)[ivar]->Fill( value, weight );
425 Log() <<
kINFO <<
"Building PDF out of reference histograms" <<
Endl;
426 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
430 (*fPDFSig)[ivar]->BuildPDF( (*fHistSig)[ivar] );
431 (*fPDFBgd)[ivar]->BuildPDF( (*fHistBgd)[ivar] );
433 (*fPDFSig)[ivar]->ValidatePDF( (*fHistSig)[ivar] );
434 (*fPDFBgd)[ivar]->ValidatePDF( (*fHistBgd)[ivar] );
437 if ((*fPDFSig)[ivar]->GetSmoothedHist() != 0) (*fHistSig_smooth)[ivar] = (*fPDFSig)[ivar]->GetSmoothedHist();
438 if ((*fPDFBgd)[ivar]->GetSmoothedHist() != 0) (*fHistBgd_smooth)[ivar] = (*fPDFBgd)[ivar]->GetSmoothedHist();
451 NoErrorCalc(err, errUpper);
460 GetTransformationHandler().SetTransformationReferenceClass( fSignalClass );
463 const Event* ev = GetEvent();
464 for (ivar=0; ivar<GetNvar(); ivar++) vs(ivar) = ev->
GetValue(ivar);
466 GetTransformationHandler().SetTransformationReferenceClass( fBackgroundClass );
470 for (ivar=0; ivar<GetNvar(); ivar++) vb(ivar) = ev->
GetValue(ivar);
474 for (ivar=0; ivar<GetNvar(); ivar++) {
477 if ((
Int_t)ivar == fDropVariable)
continue;
481 for (
UInt_t itype=0; itype < 2; itype++) {
484 if (x[itype] >= (*fPDFSig)[ivar]->GetXmax()) x[itype] = (*fPDFSig)[ivar]->GetXmax() - 1.0e-10;
485 else if (x[itype] < (*fPDFSig)[ivar]->GetXmin()) x[itype] = (*fPDFSig)[ivar]->GetXmin();
488 PDF* pdf = (itype == 0) ? (*fPDFSig)[ivar] : (*fPDFBgd)[ivar];
489 if (pdf == 0)
Log() <<
kFATAL <<
"<GetMvaValue> Reference histograms don't exist" <<
Endl;
499 DataInfo().GetVariableInfo(ivar).GetVarType() ==
'N') {
516 if (itype == 0) ps *= p;
522 return TransformLikelihoodOutput( ps, pb );
530 if (ps < fEpsilon) ps = fEpsilon;
531 if (pb < fEpsilon) pb = fEpsilon;
533 if (r >= 1.0) r = 1. - 1.e-15;
535 if (fTransformLikelihoodOutput) {
539 if (r <= 0.0) r = fEpsilon;
540 else if (r >= 1.0) r = 1. - 1.e-15;
557 if (fDefaultPDFLik != 0) {
558 o << prefix << std::endl << prefix <<
"#Default Likelihood PDF Options:" << std::endl << prefix << std::endl;
559 fDefaultPDFLik->WriteOptionsToStream( o, prefix );
561 for (
UInt_t ivar = 0; ivar < fPDFSig->size(); ivar++) {
562 if ((*fPDFSig)[ivar] != 0) {
563 o << prefix << std::endl << prefix <<
Form(
"#Signal[%d] Likelihood PDF Options:",ivar) << std::endl << prefix << std::endl;
564 (*fPDFSig)[ivar]->WriteOptionsToStream( o, prefix );
566 if ((*fPDFBgd)[ivar] != 0) {
567 o << prefix << std::endl << prefix <<
"#Background[%d] Likelihood PDF Options:" << std::endl << prefix << std::endl;
568 (*fPDFBgd)[ivar]->WriteOptionsToStream( o, prefix );
582 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
583 if ( (*fPDFSig)[ivar]==0 || (*fPDFBgd)[ivar]==0 )
584 Log() <<
kFATAL <<
"Reference histograms for variable " << ivar
585 <<
" don't exist, can't write it to weight file" <<
Endl;
589 (*fPDFSig)[ivar]->AddXMLTo(pdfwrap);
593 (*fPDFBgd)[ivar]->AddXMLTo(pdfwrap);
603 if (fRanking)
delete fRanking;
604 fRanking =
new Ranking( GetName(),
"Delta Separation" );
607 for (
Int_t ivar=-1; ivar<(
Int_t)GetNvar(); ivar++) {
610 fDropVariable = ivar;
614 TH1* rS =
new TH1F( nameS, nameS, 80, 0, 1 );
615 TH1* rB =
new TH1F( nameB, nameB, 80, 0, 1 );
618 for (
Int_t ievt=0; ievt<
Data()->GetNTrainingEvents(); ievt++) {
620 const Event* origEv =
Data()->GetEvent(ievt);
621 GetTransformationHandler().SetTransformationReferenceClass( origEv->
GetClass() );
622 const Event* ev = GetTransformationHandler().Transform(
Data()->GetEvent(ievt));
626 if (DataInfo().IsSignal(ev)) rS->
Fill( lk, w );
627 else rB->
Fill( lk, w );
632 if (ivar == -1) sepRef =
sep;
639 if (ivar >= 0) fRanking->AddRank(
Rank( DataInfo().GetVariableInfo(ivar).GetInternalName(), sep ) );
653 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
654 (*fPDFSig)[ivar]->Write( pname + GetInputVar( ivar ) +
"_S" );
655 (*fPDFBgd)[ivar]->Write( pname + GetInputVar( ivar ) +
"_B" );
669 for (
UInt_t ivar=0; ivar<nvars; ivar++){
671 Log() <<
kINFO <<
"Reading signal and background PDF for variable: " << GetInputVar( ivar ) <<
Endl;
672 if ((*fPDFSig)[ivar] !=0)
delete (*fPDFSig)[ivar];
673 if ((*fPDFBgd)[ivar] !=0)
delete (*fPDFBgd)[ivar];
674 (*fPDFSig)[ivar] =
new PDF( GetInputVar( ivar ) +
" PDF Sig" );
675 (*fPDFBgd)[ivar] =
new PDF( GetInputVar( ivar ) +
" PDF Bkg" );
676 (*fPDFSig)[ivar]->SetReadingVersion( GetTrainingTMVAVersionCode() );
677 (*fPDFBgd)[ivar]->SetReadingVersion( GetTrainingTMVAVersionCode() );
678 (*(*fPDFSig)[ivar]).ReadXML(pdfnode);
681 (*(*fPDFBgd)[ivar]).ReadXML(pdfnode);
695 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
696 Log() <<
kINFO <<
"Reading signal and background PDF for variable: " << GetInputVar( ivar ) <<
Endl;
697 if ((*fPDFSig)[ivar] !=0)
delete (*fPDFSig)[ivar];
698 if ((*fPDFBgd)[ivar] !=0)
delete (*fPDFBgd)[ivar];
699 (*fPDFSig)[ivar] =
new PDF(GetInputVar( ivar ) +
" PDF Sig" );
700 (*fPDFBgd)[ivar] =
new PDF(GetInputVar( ivar ) +
" PDF Bkg");
701 (*fPDFSig)[ivar]->SetReadingVersion( GetTrainingTMVAVersionCode() );
702 (*fPDFBgd)[ivar]->SetReadingVersion( GetTrainingTMVAVersionCode() );
703 istr >> *(*fPDFSig)[ivar];
704 istr >> *(*fPDFBgd)[ivar];
717 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
729 Log() <<
kINFO <<
"Write monitoring histograms to file: " << BaseDir()->GetPath() <<
Endl;
732 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
733 (*fHistSig)[ivar]->
Write();
734 (*fHistBgd)[ivar]->Write();
735 if ((*fHistSig_smooth)[ivar] != 0) (*fHistSig_smooth)[ivar]->Write();
736 if ((*fHistBgd_smooth)[ivar] != 0) (*fHistBgd_smooth)[ivar]->Write();
737 (*fPDFSig)[ivar]->GetPDFHist()->Write();
738 (*fPDFBgd)[ivar]->GetPDFHist()->Write();
740 if ((*fPDFSig)[ivar]->GetNSmoothHist() != 0) (*fPDFSig)[ivar]->GetNSmoothHist()->Write();
741 if ((*fPDFBgd)[ivar]->GetNSmoothHist() != 0) (*fPDFBgd)[ivar]->GetNSmoothHist()->Write();
744 Float_t xmin=((*fPDFSig)[ivar]->GetPDFHist()->GetXaxis())->GetXmin();
745 Float_t xmax=((*fPDFSig)[ivar]->GetPDFHist()->GetXaxis())->GetXmax();
746 TH1F* mm =
new TH1F( (*fInputVars)[ivar]+
"_additional_check",
747 (*fInputVars)[ivar]+
"_additional_check", 15000, xmin, xmax );
749 for (
Int_t bin=0; bin < 15000; bin++) {
756 TH1*
h[2] = { (*fHistSig)[ivar], (*fHistBgd)[ivar] };
757 for (
UInt_t i=0; i<2; i++) {
758 TH1* hclone = (
TH1F*)h[i]->Clone(
TString(h[i]->GetName()) +
"_nice" );
763 hclone->
Rebin( resFactor );
764 hclone->
Scale( 1.0/resFactor );
777 fout <<
"#include <math.h>" << std::endl;
778 fout <<
"#include <cstdlib>" << std::endl;
786 Int_t dp = fout.precision();
787 fout <<
" double fEpsilon;" << std::endl;
792 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
793 nbin[ivar]=(*fPDFSig)[ivar]->GetPDFHist()->GetNbinsX();
794 if (nbin[ivar] > nbinMax) nbinMax=nbin[ivar];
797 fout <<
" static float fRefS[][" << nbinMax <<
"]; "
798 <<
"// signal reference vector [nvars][max_nbins]" << std::endl;
799 fout <<
" static float fRefB[][" << nbinMax <<
"]; "
800 <<
"// backgr reference vector [nvars][max_nbins]" << std::endl << std::endl;
801 fout <<
"// if a variable has its PDF encoded as a spline0 --> treat it like an Integer valued one" <<std::endl;
802 fout <<
" bool fHasDiscretPDF[" << GetNvar() <<
"]; "<< std::endl;
803 fout <<
" int fNbin[" << GetNvar() <<
"]; "
804 <<
"// number of bins (discrete variables may have less bins)" << std::endl;
805 fout <<
" double fHistMin[" << GetNvar() <<
"]; " << std::endl;
806 fout <<
" double fHistMax[" << GetNvar() <<
"]; " << std::endl;
808 fout <<
" double TransformLikelihoodOutput( double, double ) const;" << std::endl;
809 fout <<
"};" << std::endl;
810 fout <<
"" << std::endl;
811 fout <<
"inline void " << className <<
"::Initialize() " << std::endl;
812 fout <<
"{" << std::endl;
813 fout <<
" fEpsilon = " << fEpsilon <<
";" << std::endl;
814 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
815 fout <<
" fNbin[" << ivar <<
"] = " << (*fPDFSig)[ivar]->GetPDFHist()->GetNbinsX() <<
";" << std::endl;
816 fout <<
" fHistMin[" << ivar <<
"] = " << (*fPDFSig)[ivar]->GetPDFHist()->GetXaxis()->GetXmin() <<
";" << std::endl;
817 fout <<
" fHistMax[" << ivar <<
"] = " << (*fPDFSig)[ivar]->GetPDFHist()->GetXaxis()->GetXmax() <<
";" << std::endl;
819 if ((((*fPDFSig)[ivar]->GetPDFHist()->GetNbinsX() != nbin[ivar] ||
820 (*fPDFBgd)[ivar]->GetPDFHist()->GetNbinsX() != nbin[ivar])
822 (*fPDFSig)[ivar]->GetPDFHist()->GetNbinsX() != (*fPDFBgd)[ivar]->GetPDFHist()->GetNbinsX()) {
823 Log() <<
kFATAL <<
"<MakeClassSpecific> Mismatch in binning of variable "
824 <<
"\"" << GetOriginalVarName(ivar) <<
"\" of type: \'" << DataInfo().GetVariableInfo(ivar).GetVarType()
826 <<
"nxS = " << (*fPDFSig)[ivar]->GetPDFHist()->GetNbinsX() <<
", "
827 <<
"nxB = " << (*fPDFBgd)[ivar]->GetPDFHist()->GetNbinsX()
828 <<
" while we expect " << nbin[ivar]
832 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
834 fout <<
" fHasDiscretPDF[" << ivar <<
"] = true; " << std::endl;
836 fout <<
" fHasDiscretPDF[" << ivar <<
"] = false; " << std::endl;
839 fout <<
"}" << std::endl << std::endl;
841 fout <<
"inline double " << className
842 <<
"::GetMvaValue__( const std::vector<double>& inputValues ) const" << std::endl;
843 fout <<
"{" << std::endl;
844 fout <<
" double ps(1), pb(1);" << std::endl;
845 fout <<
" std::vector<double> inputValuesSig = inputValues;" << std::endl;
846 fout <<
" std::vector<double> inputValuesBgd = inputValues;" << std::endl;
847 if (GetTransformationHandler().GetTransformationList().GetSize() != 0) {
848 fout <<
" Transform(inputValuesSig,0);" << std::endl;
849 fout <<
" Transform(inputValuesBgd,1);" << std::endl;
851 fout <<
" for (size_t ivar = 0; ivar < GetNvar(); ivar++) {" << std::endl;
853 fout <<
" // dummy at present... will be used for variable transforms" << std::endl;
854 fout <<
" double x[2] = { inputValuesSig[ivar], inputValuesBgd[ivar] };" << std::endl;
856 fout <<
" for (int itype=0; itype < 2; itype++) {" << std::endl;
858 fout <<
" // interpolate linearly between adjacent bins" << std::endl;
859 fout <<
" // this is not useful for discrete variables (or forced Spline0)" << std::endl;
860 fout <<
" int bin = int((x[itype] - fHistMin[ivar])/(fHistMax[ivar] - fHistMin[ivar])*fNbin[ivar]) + 0;" << std::endl;
862 fout <<
" // since the test data sample is in general different from the training sample" << std::endl;
863 fout <<
" // it can happen that the min/max of the training sample are trespassed --> correct this" << std::endl;
864 fout <<
" if (bin < 0) {" << std::endl;
865 fout <<
" bin = 0;" << std::endl;
866 fout <<
" x[itype] = fHistMin[ivar];" << std::endl;
867 fout <<
" }" << std::endl;
868 fout <<
" else if (bin >= fNbin[ivar]) {" << std::endl;
869 fout <<
" bin = fNbin[ivar]-1;" << std::endl;
870 fout <<
" x[itype] = fHistMax[ivar];" << std::endl;
871 fout <<
" }" << std::endl;
873 fout <<
" // find corresponding histogram from cached indices" << std::endl;
874 fout <<
" float ref = (itype == 0) ? fRefS[ivar][bin] : fRefB[ivar][bin];" << std::endl;
876 fout <<
" // sanity check" << std::endl;
877 fout <<
" if (ref < 0) {" << std::endl;
878 fout <<
" std::cout << \"Fatal error in " << className
879 <<
": bin entry < 0 ==> abort\" << std::endl;" << std::endl;
880 fout <<
" std::exit(1);" << std::endl;
881 fout <<
" }" << std::endl;
883 fout <<
" double p = ref;" << std::endl;
885 fout <<
" if (GetType(ivar) != 'I' && !fHasDiscretPDF[ivar]) {" << std::endl;
886 fout <<
" float bincenter = (bin + 0.5)/fNbin[ivar]*(fHistMax[ivar] - fHistMin[ivar]) + fHistMin[ivar];" << std::endl;
887 fout <<
" int nextbin = bin;" << std::endl;
888 fout <<
" if ((x[itype] > bincenter && bin != fNbin[ivar]-1) || bin == 0) " << std::endl;
889 fout <<
" nextbin++;" << std::endl;
890 fout <<
" else" << std::endl;
891 fout <<
" nextbin--; " << std::endl;
893 fout <<
" double refnext = (itype == 0) ? fRefS[ivar][nextbin] : fRefB[ivar][nextbin];" << std::endl;
894 fout <<
" float nextbincenter = (nextbin + 0.5)/fNbin[ivar]*(fHistMax[ivar] - fHistMin[ivar]) + fHistMin[ivar];" << std::endl;
896 fout <<
" double dx = bincenter - nextbincenter;" << std::endl;
897 fout <<
" double dy = ref - refnext;" << std::endl;
898 fout <<
" p += (x[itype] - bincenter) * dy/dx;" << std::endl;
899 fout <<
" }" << std::endl;
901 fout <<
" if (p < fEpsilon) p = fEpsilon; // avoid zero response" << std::endl;
903 fout <<
" if (itype == 0) ps *= p;" << std::endl;
904 fout <<
" else pb *= p;" << std::endl;
905 fout <<
" } " << std::endl;
906 fout <<
" } " << std::endl;
908 fout <<
" // the likelihood ratio (transform it ?)" << std::endl;
909 fout <<
" return TransformLikelihoodOutput( ps, pb ); " << std::endl;
910 fout <<
"}" << std::endl << std::endl;
912 fout <<
"inline double " << className <<
"::TransformLikelihoodOutput( double ps, double pb ) const" << std::endl;
913 fout <<
"{" << std::endl;
914 fout <<
" // returns transformed or non-transformed output" << std::endl;
915 fout <<
" if (ps < fEpsilon) ps = fEpsilon;" << std::endl;
916 fout <<
" if (pb < fEpsilon) pb = fEpsilon;" << std::endl;
917 fout <<
" double r = ps/(ps + pb);" << std::endl;
918 fout <<
" if (r >= 1.0) r = 1. - 1.e-15;" << std::endl;
920 fout <<
" if (" << (fTransformLikelihoodOutput ?
"true" :
"false") <<
") {" << std::endl;
921 fout <<
" // inverse Fermi function" << std::endl;
923 fout <<
" // sanity check" << std::endl;
924 fout <<
" if (r <= 0.0) r = fEpsilon;" << std::endl;
925 fout <<
" else if (r >= 1.0) r = 1. - 1.e-15;" << std::endl;
927 fout <<
" double tau = 15.0;" << std::endl;
928 fout <<
" r = - log(1.0/r - 1.0)/tau;" << std::endl;
929 fout <<
" }" << std::endl;
931 fout <<
" return r;" << std::endl;
932 fout <<
"}" << std::endl;
935 fout <<
"// Clean up" << std::endl;
936 fout <<
"inline void " << className <<
"::Clear() " << std::endl;
937 fout <<
"{" << std::endl;
938 fout <<
" // nothing to clear" << std::endl;
939 fout <<
"}" << std::endl << std::endl;
941 fout <<
"// signal map" << std::endl;
942 fout <<
"float " << className <<
"::fRefS[][" << nbinMax <<
"] = " << std::endl;
943 fout <<
"{ " << std::endl;
944 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
946 for (
Int_t ibin=1; ibin<=nbinMax; ibin++) {
947 if (ibin-1 < nbin[ivar])
948 fout << (*fPDFSig)[ivar]->GetPDFHist()->GetBinContent(ibin);
952 if (ibin < nbinMax) fout <<
", ";
954 fout <<
" }, " << std::endl;
956 fout <<
"}; " << std::endl;
959 fout <<
"// background map" << std::endl;
960 fout <<
"float " << className <<
"::fRefB[][" << nbinMax <<
"] = " << std::endl;
961 fout <<
"{ " << std::endl;
962 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
964 fout << std::setprecision(8);
965 for (
Int_t ibin=1; ibin<=nbinMax; ibin++) {
966 if (ibin-1 < nbin[ivar])
967 fout << (*fPDFBgd)[ivar]->GetPDFHist()->GetBinContent(ibin);
971 if (ibin < nbinMax) fout <<
", ";
973 fout <<
" }, " << std::endl;
975 fout <<
"}; " << std::endl;
977 fout << std::setprecision(dp);
993 Log() <<
"The maximum-likelihood classifier models the data with probability " <<
Endl;
994 Log() <<
"density functions (PDF) reproducing the signal and background" <<
Endl;
995 Log() <<
"distributions of the input variables. Correlations among the " <<
Endl;
996 Log() <<
"variables are ignored." <<
Endl;
1000 Log() <<
"Required for good performance are decorrelated input variables" <<
Endl;
1001 Log() <<
"(PCA transformation via the option \"VarTransform=Decorrelate\"" <<
Endl;
1002 Log() <<
"may be tried). Irreducible non-linear correlations may be reduced" <<
Endl;
1003 Log() <<
"by precombining strongly correlated input variables, or by simply" <<
Endl;
1004 Log() <<
"removing one of the variables." <<
Endl;
1008 Log() <<
"High fidelity PDF estimates are mandatory, i.e., sufficient training " <<
Endl;
1009 Log() <<
"statistics is required to populate the tails of the distributions" <<
Endl;
1010 Log() <<
"It would be a surprise if the default Spline or KDE kernel parameters" <<
Endl;
1011 Log() <<
"provide a satisfying fit to the data. The user is advised to properly" <<
Endl;
1012 Log() <<
"tune the events per bin and smooth options in the spline cases" <<
Endl;
1013 Log() <<
"individually per variable. If the KDE kernel is used, the adaptive" <<
Endl;
1014 Log() <<
"Gaussian kernel may lead to artefacts, so please always also try" <<
Endl;
1015 Log() <<
"the non-adaptive one." <<
Endl;
1017 Log() <<
"All tuning parameters must be adjusted individually for each input" <<
Endl;
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
Return Global bin number corresponding to x,y,z.
void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
void WriteWeightsToStream(TFile &rf) const
write reference PDFs to ROOT file
ClassImp(TMVA::MethodLikelihood) TMVA
standard constructor
MsgLogger & Endl(MsgLogger &ml)
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
virtual void WriteOptionsToStream(std::ostream &o, const TString &prefix) const
write options to stream
void Train()
create reference distributions (PDFs) from signal and background events: fill histograms and smooth t...
virtual ~MethodLikelihood()
destructor
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
static Bool_t AddDirectoryStatus()
static function: cannot be inlined on Windows/NT
1-D histogram with a float per channel (see TH1 documentation)}
void WriteMonitoringHistosToFile() const
write histograms and PDFs to file for monitoring purposes
Short_t Min(Short_t a, Short_t b)
virtual Int_t GetNbinsX() const
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
void WriteOptionsToStream(std::ostream &o, const TString &prefix) const
write options to output stream (e.g. in writing the MVA weight files
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
const char * Data() const
void ReadWeightsFromXML(void *wghtnode)
read weights from XML
std::vector< std::vector< double > > Data
Double_t TransformLikelihoodOutput(Double_t ps, Double_t pb) const
returns transformed or non-transformed output
virtual Double_t GetBinCenter(Int_t bin) const
return bin center for 1D historam Better to use h1.GetXaxis().GetBinCenter(bin)
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=0)
Rebin this histogram.
void GetHelpMessage() const
get help message text
MethodLikelihood(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="", TDirectory *theTargetDir=0)
const Ranking * CreateRanking()
computes ranking of input variables
char * Form(const char *fmt,...)
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
FDA can handle classification with 2 classes.
virtual void SetName(const char *name)
Change the name of this histogram.
void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
void DeclareOptions()
define the options (their key words) that can be set in the option string TransformOutput tran...
Describe directory structure in memory.
void AddWeightsXMLTo(void *parent) const
write weights to XML
void Init()
default initialisation called by all constructors
void ReadWeightsFromStream(std::istream &istr)
read weight info from file nothing to do for this method
#define REGISTER_METHOD(CLASS)
for example
Abstract ClassifierFactory template that handles arbitrary types.
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
Short_t Max(Short_t a, Short_t b)
void MakeClassSpecificHeader(std::ostream &, const TString &="") const
write specific header of the classifier (mostly include files)
virtual void SetTitle(const char *title)
Change (i.e.
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
returns the likelihood estimator for signal fill a new Likelihood branch into the testTree ...