57 : fLearningModel ( kFull )
58 , fImportanceCut ( 0 )
59 , fLinQuantile ( 0.025 )
61 , fAverageSupport ( 0.8 )
62 , fAverageRuleSigma( 0.4 )
66 , fRuleMinDist ( 1
e-3 )
67 , fNRulesGenerated ( 0 )
69 , fEventCacheOK ( true )
73 , fRuleMapEvents ( 0 )
83 : fAverageSupport ( 1 )
96 : fLearningModel ( kFull )
97 , fImportanceCut ( 0 )
98 , fLinQuantile ( 0.025 )
100 , fImportanceRef ( 1.0 )
101 , fAverageSupport ( 0.8 )
102 , fAverageRuleSigma( 0.4 )
106 , fRuleMinDist ( 1
e-3 )
107 , fNRulesGenerated ( 0 )
109 , fEventCacheOK ( true )
110 , fRuleMapOK ( true )
113 , fRuleMapEvents ( 0 )
124 for ( std::vector<Rule *>::iterator itrRule = fRules.begin(); itrRule != fRules.end(); ++itrRule ) {
136 SetAverageRuleSigma(0.4);
138 UInt_t nvars = GetMethodBase()->GetNvar();
139 fVarImportance.clear();
143 fVarImportance.resize( nvars,0.0 );
144 fLinPDFB.resize( nvars,0 );
145 fLinPDFS.resize( nvars,0 );
146 fImportanceRef = 1.0;
147 for (
UInt_t i=0; i<nvars; i++) {
148 fLinTermOK.push_back(
kTRUE);
155 fLogger->SetMinType(t);
164 return ( fRuleFit==0 ? 0:fRuleFit->GetMethodRuleFit());
173 return ( fRuleFit==0 ? 0:fRuleFit->GetMethodBase());
181 MakeRules( fRuleFit->GetForest() );
200 Int_t ncoeffs = fRules.size();
201 if (ncoeffs<1)
return 0;
205 for (
Int_t i=0; i<ncoeffs; i++) {
206 val = fRules[i]->GetCoefficient();
218 UInt_t nrules = fRules.size();
219 for (
UInt_t i=0; i<nrules; i++) {
220 fRules[i]->SetCoefficient(0.0);
229 UInt_t nrules = fRules.size();
230 if (
v.size()!=nrules) {
231 Log() << kFATAL <<
"<SetCoefficients> - BUG TRAP - input vector wrong size! It is = " <<
v.size()
232 <<
" when it should be = " << nrules <<
Endl;
234 for (
UInt_t i=0; i<nrules; i++) {
235 fRules[i]->SetCoefficient(
v[i]);
244 UInt_t nrules = fRules.size();
246 if (nrules==0)
return;
248 for (
UInt_t i=0; i<nrules; i++) {
249 v[i] = (fRules[i]->GetCoefficient());
258 return &(fRuleFit->GetTrainingEvents());
266 return fRuleFit->GetTrainingEvent(i);
274 Log() << kVERBOSE <<
"Removing similar rules; distance = " << fRuleMinDist <<
Endl;
276 UInt_t nrulesIn = fRules.size();
278 std::vector< Char_t > removeMe( nrulesIn,
false );
284 for (
UInt_t i=0; i<nrulesIn; i++) {
287 for (
UInt_t k=i+1; k<nrulesIn; k++) {
293 remind = (
r>0.5 ? k:i);
300 if (!removeMe[remind]) {
301 removeMe[remind] =
true;
311 for (
UInt_t i=0; i<nrulesIn; i++) {
313 theRule = fRules[ind];
315 fRules.erase( std::vector<Rule *>::iterator(&fRules[ind], &fRules) );
317 fRules.erase( fRules.begin() + ind );
324 UInt_t nrulesOut = fRules.size();
325 Log() << kVERBOSE <<
"Removed " << nrulesIn - nrulesOut <<
" out of " << nrulesIn <<
" rules" <<
Endl;
333 UInt_t nrules = fRules.size();
334 if (nrules==0)
return;
335 Log() << kVERBOSE <<
"Removing rules with relative importance < " << fImportanceCut <<
Endl;
336 if (fImportanceCut<=0)
return;
342 for (
UInt_t i=0; i<nrules; i++) {
343 if (fRules[ind]->GetRelImportance()<fImportanceCut) {
344 therule = fRules[ind];
346 fRules.erase( std::vector<Rule *>::iterator(&fRules[ind], &fRules) );
348 fRules.erase( fRules.begin() + ind );
355 Log() << kINFO <<
"Removed " << nrules-ind <<
" out of a total of " << nrules
356 <<
" rules with importance < " << fImportanceCut <<
Endl;
364 UInt_t nlin = fLinNorm.size();
366 Log() << kVERBOSE <<
"Removing linear terms with relative importance < " << fImportanceCut <<
Endl;
369 for (
UInt_t i=0; i<nlin; i++) {
370 fLinTermOK.push_back( (fLinImportance[i]/fImportanceRef > fImportanceCut) );
379 Log() << kVERBOSE <<
"Evaluating Rule support" <<
Endl;
386 SetAverageRuleSigma(0.4);
387 const std::vector<const Event *> *events = GetTrainingEvents();
391 if ((nrules>0) && (events->size()>0)) {
392 for ( std::vector< Rule * >::iterator itrRule=fRules.begin(); itrRule!=fRules.end(); ++itrRule ) {
396 for ( std::vector<const Event * >::const_iterator itrEvent=events->begin(); itrEvent!=events->end(); ++itrEvent ) {
397 if ((*itrRule)->EvalEvent( *(*itrEvent) )) {
398 ew = (*itrEvent)->GetWeight();
400 if (GetMethodRuleFit()->DataInfo().IsSignal(*itrEvent)) ssig += ew;
405 s =
s/fRuleFit->GetNEveEff();
407 t = (t<0 ? 0:
sqrt(t));
412 (*itrRule)->SetSupport(
s);
413 (*itrRule)->SetNorm(t);
414 (*itrRule)->SetSSB( ssb );
415 (*itrRule)->SetSSBNeve(
Double_t(ssig+sbkg));
418 fAverageSupport = stot/nrules;
419 fAverageRuleSigma =
TMath::Sqrt(fAverageSupport*(1.0-fAverageSupport));
420 Log() << kVERBOSE <<
"Standard deviation of support = " << fAverageRuleSigma <<
Endl;
421 Log() << kVERBOSE <<
"Average rule support = " << fAverageSupport <<
Endl;
430 Double_t maxRuleImp = CalcRuleImportance();
431 Double_t maxLinImp = CalcLinImportance();
432 Double_t maxImp = (maxRuleImp>maxLinImp ? maxRuleImp : maxLinImp);
433 SetImportanceRef( maxImp );
441 for (
UInt_t i=0; i<fRules.size(); i++ ) {
442 fRules[i]->SetImportanceRef(impref);
444 fImportanceRef = impref;
453 Int_t nrules = fRules.size();
454 for (
int i=0; i<nrules; i++ ) {
455 fRules[i]->CalcImportance();
456 imp = fRules[i]->GetImportance();
457 if (imp>maxImp) maxImp = imp;
459 for (
Int_t i=0; i<nrules; i++ ) {
460 fRules[i]->SetImportanceRef(maxImp);
472 UInt_t nvars = fLinCoefficients.size();
473 fLinImportance.resize(nvars,0.0);
474 if (!DoLinear())
return maxImp;
484 for (
UInt_t i=0; i<nvars; i++ ) {
485 imp = fAverageRuleSigma*
TMath::Abs(fLinCoefficients[i]);
486 fLinImportance[i] = imp;
487 if (imp>maxImp) maxImp = imp;
497 Log() << kVERBOSE <<
"Compute variable importance" <<
Endl;
499 UInt_t nrules = fRules.size();
500 if (GetMethodBase()==0)
Log() << kFATAL <<
"RuleEnsemble::CalcVarImportance() - should not be here!" <<
Endl;
501 UInt_t nvars = GetMethodBase()->GetNvar();
504 fVarImportance.resize(nvars,0);
507 for (
UInt_t ind=0; ind<nrules; ind++ ) {
508 rimp = fRules[ind]->GetImportance();
509 nvarsUsed = fRules[ind]->GetNumVarsUsed();
511 Log() << kFATAL <<
"<CalcVarImportance> Variables for importance calc!!!??? A BUG!" <<
Endl;
512 rimpN = (nvarsUsed > 0 ? rimp/nvarsUsed:0.0);
513 for (
UInt_t iv=0; iv<nvars; iv++ ) {
514 if (fRules[ind]->ContainsVariable(iv)) {
515 fVarImportance[iv] += rimpN;
522 for (
UInt_t iv=0; iv<fLinTermOK.size(); iv++ ) {
523 if (fLinTermOK[iv]) fVarImportance[iv] += fLinImportance[iv];
530 for (
UInt_t iv=0; iv<nvars; iv++ ) {
531 if ( fVarImportance[iv] > maximp ) maximp = fVarImportance[iv];
534 for (
UInt_t iv=0; iv<nvars; iv++ ) {
535 fVarImportance[iv] *= 1.0/maximp;
549 fRules.resize(rules.size());
550 for (
UInt_t i=0; i<fRules.size(); i++) {
551 fRules[i] = rules[i];
563 if (!DoRules())
return;
572 UInt_t ntrees = forest.size();
573 for (
UInt_t ind=0; ind<ntrees; ind++ ) {
575 MakeRulesFromTree( forest[ind] );
576 nrules = CalcNRules( forest[ind] );
577 nendn = (nrules/2) + 1;
579 sumn2 += nendn*nendn;
580 nrulesCheck += nrules;
582 Double_t nmean = (ntrees>0) ? sumnendn/ntrees : 0;
584 Double_t ndev = 2.0*(nmean-2.0-nsigm)/(nmean-2.0+nsigm);
586 Log() << kVERBOSE <<
"Average number of end nodes per tree = " << nmean <<
Endl;
587 if (ntrees>1)
Log() << kVERBOSE <<
"sigma of ditto ( ~= mean-2 ?) = "
590 Log() << kVERBOSE <<
"Deviation from exponential model = " << ndev <<
Endl;
591 Log() << kVERBOSE <<
"Corresponds to L (eq. 13, RuleFit ppr) = " << nmean <<
Endl;
593 if (nrulesCheck !=
static_cast<Int_t>(fRules.size())) {
595 <<
"BUG! number of generated and possible rules do not match! N(rules) = " << fRules.size()
596 <<
" != " << nrulesCheck <<
Endl;
598 Log() << kVERBOSE <<
"Number of generated rules: " << fRules.size() <<
Endl;
601 fNRulesGenerated = fRules.size();
603 RemoveSimilarRules();
615 if (!DoLinear())
return;
617 const std::vector<const Event *> *events = GetTrainingEvents();
618 UInt_t neve = events->size();
619 UInt_t nvars = ((*events)[0])->GetNVariables();
621 typedef std::pair< Double_t, Int_t> dataType;
622 typedef std::pair< Double_t, dataType > dataPoint;
624 std::vector< std::vector<dataPoint> > vardata(nvars);
625 std::vector< Double_t > varsum(nvars,0.0);
626 std::vector< Double_t > varsum2(nvars,0.0);
631 for (
UInt_t i=0; i<neve; i++) {
632 ew = ((*events)[i])->GetWeight();
634 val = ((*events)[i])->GetValue(
v);
635 vardata[
v].push_back( dataPoint( val, dataType(ew,((*events)[i])->
GetClass()) ) );
641 fLinCoefficients.clear();
643 fLinDP.resize(nvars,0);
644 fLinDM.resize(nvars,0);
645 fLinCoefficients.resize(nvars,0);
646 fLinNorm.resize(nvars,0);
648 Double_t averageWeight = neve ? fRuleFit->GetNEveEff()/
static_cast<Double_t>(neve) : 0;
665 std::sort( vardata[
v].begin(),vardata[
v].end() );
666 nquant = fLinQuantile*fRuleFit->GetNEveEff();
670 while ( (ie<neve) && (neff<nquant) ) {
671 neff += vardata[
v][ie].second.first;
674 indquantM = (ie==0 ? 0:ie-1);
678 while ( (ie>0) && (neff<nquant) ) {
680 neff += vardata[
v][ie].second.first;
682 indquantP = (ie==neve ? ie=neve-1:ie);
684 fLinDM[
v] = vardata[
v][indquantM].first;
685 fLinDP[
v] = vardata[
v][indquantP].first;
689 if (fLinPDFB[
v])
delete fLinPDFB[
v];
690 if (fLinPDFS[
v])
delete fLinPDFS[
v];
691 fLinPDFB[
v] =
new TH1F(
Form(
"bkgvar%d",
v),
"bkg temphist",40,fLinDM[
v],fLinDP[
v]);
692 fLinPDFS[
v] =
new TH1F(
Form(
"sigvar%d",
v),
"sig temphist",40,fLinDM[
v],fLinDP[
v]);
693 fLinPDFB[
v]->Sumw2();
694 fLinPDFS[
v]->Sumw2();
698 const Double_t w = 1.0/fRuleFit->GetNEveEff();
699 for (ie=0; ie<neve; ie++) {
700 val = vardata[
v][ie].first;
701 ew = vardata[
v][ie].second.first;
702 type = vardata[
v][ie].second.second;
705 varsum2[
v] += ew*lx*lx;
708 if (
type==1) fLinPDFS[
v]->Fill(lx,w*ew);
709 else fLinPDFB[
v]->Fill(lx,w*ew);
715 stdl =
TMath::Sqrt( (varsum2[
v] - (varsum[
v]*varsum[
v]/fRuleFit->GetNEveEff()))/(fRuleFit->GetNEveEff()-averageWeight) );
716 fLinNorm[
v] = CalcLinNorm(stdl);
722 fLinPDFS[
v]->Write();
723 fLinPDFB[
v]->Write();
733 UInt_t nvars=fLinDP.size();
741 Int_t bin = fLinPDFS[
v]->FindBin(val);
742 fstot += fLinPDFS[
v]->GetBinContent(bin);
743 fbtot += fLinPDFB[
v]->GetBinContent(bin);
745 if (nvars<1)
return 0;
746 ntot = (fstot+fbtot)/
Double_t(nvars);
748 return fstot/(fstot+fbtot);
763 UInt_t nrules = fRules.size();
764 for (
UInt_t ir=0; ir<nrules; ir++) {
765 if (fEventRuleVal[ir]>0) {
766 ssb = fEventRuleVal[ir]*GetRulesConst(ir)->GetSSB();
767 neve = GetRulesConst(ir)->GetSSBNeve();
777 if (ntot>0)
return nsig/ntot;
806 if (DoLinear()) pl = PdfLinear(nls, nlt);
807 if (DoRules()) pr = PdfRule(nrs, nrt);
809 if ((nlt>0) && (nrt>0)) nt=2.0;
821 const std::vector<const Event *> *events = GetTrainingEvents();
822 const UInt_t neve = events->size();
823 const UInt_t nvars = GetMethodBase()->GetNvar();
824 const UInt_t nrules = fRules.size();
825 const Event *eveData;
841 std::vector<Int_t> varcnt;
849 varcnt.resize(nvars,0);
850 fRuleVarFrac.clear();
851 fRuleVarFrac.resize(nvars,0);
853 for (
UInt_t i=0; i<nrules; i++ ) {
855 if (fRules[i]->ContainsVariable(
v)) varcnt[
v]++;
857 sigRule = fRules[i]->IsSignalRule();
872 eveData = (*events)[
e];
873 tagged = fRules[i]->EvalEvent(*eveData);
874 sigTag = (tagged && sigRule);
875 bkgTag = (tagged && (!sigRule));
877 sigTrue = (eveData->
GetClass() == 0);
880 if (sigTag && sigTrue) nss++;
881 if (sigTag && !sigTrue) nsb++;
882 if (bkgTag && sigTrue) nbs++;
883 if (bkgTag && !sigTrue) nbb++;
887 if (ntag>0 && neve > 0) {
896 fRuleFSig = (nsig>0) ?
static_cast<Double_t>(nsig)/
static_cast<Double_t>(nsig+nbkg) : 0;
907 const UInt_t nrules = fRules.size();
911 for (
UInt_t i=0; i<nrules; i++ ) {
912 nc =
static_cast<Double_t>(fRules[i]->GetNcuts());
919 fRuleNCave = sumNc/nrules;
929 Log() << kHEADER <<
"-------------------RULE ENSEMBLE SUMMARY------------------------" <<
Endl;
931 if (mrf)
Log() << kINFO <<
"Tree training method : " << (mrf->
UseBoost() ?
"AdaBoost":
"Random") <<
Endl;
932 Log() << kINFO <<
"Number of events per tree : " << fRuleFit->GetNTreeSample() <<
Endl;
933 Log() << kINFO <<
"Number of trees : " << fRuleFit->GetForest().size() <<
Endl;
934 Log() << kINFO <<
"Number of generated rules : " << fNRulesGenerated <<
Endl;
935 Log() << kINFO <<
"Idem, after cleanup : " << fRules.size() <<
Endl;
936 Log() << kINFO <<
"Average number of cuts per rule : " <<
Form(
"%8.2f",fRuleNCave) <<
Endl;
937 Log() << kINFO <<
"Spread in number of cuts per rules : " <<
Form(
"%8.2f",fRuleNCsig) <<
Endl;
938 Log() << kVERBOSE <<
"Complexity : " <<
Form(
"%8.2f",fRuleNCave*fRuleNCsig) <<
Endl;
939 Log() << kINFO <<
"----------------------------------------------------------------" <<
Endl;
948 const EMsgType kmtype=kINFO;
949 const Bool_t isDebug = (fLogger->GetMinType()<=kDEBUG);
952 Log() << kmtype <<
"================================================================" <<
Endl;
953 Log() << kmtype <<
" M o d e l " <<
Endl;
954 Log() << kmtype <<
"================================================================" <<
Endl;
957 const UInt_t nvars = GetMethodBase()->GetNvar();
958 const Int_t nrules = fRules.size();
961 for (
UInt_t iv = 0; iv<fVarImportance.size(); iv++) {
962 if (GetMethodBase()->GetInputLabel(iv).Length() > maxL) maxL = GetMethodBase()->GetInputLabel(iv).Length();
966 Log() << kDEBUG <<
"Variable importance:" <<
Endl;
967 for (
UInt_t iv = 0; iv<fVarImportance.size(); iv++) {
968 Log() << kDEBUG << std::setw(maxL) << GetMethodBase()->GetInputLabel(iv)
969 << std::resetiosflags(std::ios::right)
970 <<
" : " <<
Form(
" %3.3f",fVarImportance[iv]) <<
Endl;
974 Log() << kHEADER <<
"Offset (a0) = " << fOffset <<
Endl;
977 if (fLinNorm.size() > 0) {
978 Log() << kmtype <<
"------------------------------------" <<
Endl;
979 Log() << kmtype <<
"Linear model (weights unnormalised)" <<
Endl;
980 Log() << kmtype <<
"------------------------------------" <<
Endl;
981 Log() << kmtype << std::setw(maxL) <<
"Variable"
982 << std::resetiosflags(std::ios::right) <<
" : "
983 << std::setw(11) <<
" Weights"
984 << std::resetiosflags(std::ios::right) <<
" : "
986 << std::resetiosflags(std::ios::right)
988 Log() << kmtype <<
"------------------------------------" <<
Endl;
989 for (
UInt_t i=0; i<fLinNorm.size(); i++ ) {
990 Log() << kmtype << std::setw(std::max(maxL,8)) << GetMethodBase()->GetInputLabel(i);
993 << std::resetiosflags(std::ios::right)
994 <<
" : " <<
Form(
" %10.3e",fLinCoefficients[i]*fLinNorm[i])
995 <<
" : " <<
Form(
" %3.3f",fLinImportance[i]/fImportanceRef) <<
Endl;
998 Log() << kmtype <<
"-> importance below threshold = "
999 <<
Form(
" %3.3f",fLinImportance[i]/fImportanceRef) <<
Endl;
1002 Log() << kmtype <<
"------------------------------------" <<
Endl;
1005 else Log() << kmtype <<
"Linear terms were disabled" <<
Endl;
1007 if ((!DoRules()) || (nrules==0)) {
1009 Log() << kmtype <<
"Rule terms were disabled" <<
Endl;
1012 Log() << kmtype <<
"Even though rules were included in the model, none passed! " << nrules <<
Endl;
1016 Log() << kmtype <<
"Number of rules = " << nrules <<
Endl;
1018 Log() << kmtype <<
"N(cuts) in rules, average = " << fRuleNCave <<
Endl;
1019 Log() << kmtype <<
" RMS = " << fRuleNCsig <<
Endl;
1020 Log() << kmtype <<
"Fraction of signal rules = " << fRuleFSig <<
Endl;
1021 Log() << kmtype <<
"Fraction of rules containing a variable (%):" <<
Endl;
1023 Log() << kmtype <<
" " << std::setw(maxL) << GetMethodBase()->GetInputLabel(
v);
1024 Log() << kmtype <<
Form(
" = %2.2f",fRuleVarFrac[
v]*100.0) <<
" %" <<
Endl;
1030 std::list< std::pair<double,int> > sortedImp;
1031 for (
Int_t i=0; i<nrules; i++) {
1032 sortedImp.push_back( std::pair<double,int>( fRules[i]->GetImportance(),i ) );
1036 Log() << kmtype <<
"Printing the first " << printN <<
" rules, ordered in importance." <<
Endl;
1038 for ( std::list< std::pair<double,int> >::reverse_iterator itpair = sortedImp.rbegin();
1039 itpair != sortedImp.rend(); ++itpair ) {
1040 ind = itpair->second;
1044 fRules[ind]->PrintLogger(
Form(
"Rule %4d : ",pind+1));
1047 if (nrules==printN) {
1048 Log() << kmtype <<
"All rules printed" <<
Endl;
1051 Log() << kmtype <<
"Skipping the next " << nrules-printN <<
" rules" <<
Endl;
1057 Log() << kmtype <<
"================================================================" <<
Endl;
1066 Int_t dp = os.precision();
1067 UInt_t nrules = fRules.size();
1070 os <<
"ImportanceCut= " << fImportanceCut << std::endl;
1071 os <<
"LinQuantile= " << fLinQuantile << std::endl;
1072 os <<
"AverageSupport= " << fAverageSupport << std::endl;
1073 os <<
"AverageRuleSigma= " << fAverageRuleSigma << std::endl;
1074 os <<
"Offset= " << fOffset << std::endl;
1075 os <<
"NRules= " << nrules << std::endl;
1076 for (
UInt_t i=0; i<nrules; i++){
1077 os <<
"***Rule " << i << std::endl;
1078 (fRules[i])->PrintRaw(os);
1080 UInt_t nlinear = fLinNorm.size();
1082 os <<
"NLinear= " << fLinTermOK.size() << std::endl;
1083 for (
UInt_t i=0; i<nlinear; i++) {
1084 os <<
"***Linear " << i << std::endl;
1085 os << std::setprecision(10) << (fLinTermOK[i] ? 1:0) <<
" "
1086 << fLinCoefficients[i] <<
" "
1087 << fLinNorm[i] <<
" "
1090 << fLinImportance[i] <<
" " << std::endl;
1092 os << std::setprecision(dp);
1102 UInt_t nrules = fRules.size();
1103 UInt_t nlinear = fLinNorm.size();
1106 gTools().
AddAttr( re,
"LearningModel", (
int)fLearningModel );
1110 gTools().
AddAttr( re,
"AverageRuleSigma", fAverageRuleSigma );
1112 for (
UInt_t i=0; i<nrules; i++) fRules[i]->AddXMLTo(re);
1114 for (
UInt_t i=0; i<nlinear; i++) {
1134 Int_t iLearningModel;
1139 gTools().
ReadAttr( wghtnode,
"AverageSupport", fAverageSupport );
1140 gTools().
ReadAttr( wghtnode,
"AverageRuleSigma", fAverageRuleSigma );
1147 fRules.resize( nrules );
1149 for (i=0; i<nrules; i++) {
1150 fRules[i] =
new Rule();
1151 fRules[i]->SetRuleEnsemble(
this );
1152 fRules[i]->ReadFromXML( ch );
1158 fLinNorm .resize( nlinear );
1159 fLinTermOK .resize( nlinear );
1160 fLinCoefficients.resize( nlinear );
1161 fLinDP .resize( nlinear );
1162 fLinDM .resize( nlinear );
1163 fLinImportance .resize( nlinear );
1169 fLinTermOK[i] = (iok == 1);
1193 istr >>
dummy >> fImportanceCut;
1194 istr >>
dummy >> fLinQuantile;
1195 istr >>
dummy >> fAverageSupport;
1196 istr >>
dummy >> fAverageRuleSigma;
1197 istr >>
dummy >> fOffset;
1198 istr >>
dummy >> nrules;
1204 for (
UInt_t i=0; i<nrules; i++){
1205 istr >>
dummy >> idum;
1206 fRules.push_back(
new Rule() );
1207 (fRules.back())->SetRuleEnsemble(
this );
1208 (fRules.back())->ReadRaw(istr);
1216 istr >>
dummy >> nlinear;
1218 fLinNorm .resize( nlinear );
1219 fLinTermOK .resize( nlinear );
1220 fLinCoefficients.resize( nlinear );
1221 fLinDP .resize( nlinear );
1222 fLinDM .resize( nlinear );
1223 fLinImportance .resize( nlinear );
1227 for (
UInt_t i=0; i<nlinear; i++) {
1228 istr >>
dummy >> idum;
1230 fLinTermOK[i] = (iok==1);
1231 istr >> fLinCoefficients[i];
1232 istr >> fLinNorm[i];
1235 istr >> fLinImportance[i];
1244 if(
this != &other) {
1271 if (dtree==0)
return 0;
1273 Int_t nendnodes = 0;
1274 FindNEndNodes( node, nendnodes );
1275 return 2*(nendnodes-1);
1283 if (node==0)
return;
1290 FindNEndNodes( nodeR, nendnodes );
1291 FindNEndNodes( nodeL, nendnodes );
1308 if (node==0)
return;
1314 Rule *rule = MakeTheRule(node);
1316 fRules.push_back( rule );
1321 Log() << kFATAL <<
"<AddRule> - ERROR failed in creating a rule! BUG!" <<
Endl;
1335 Log() << kFATAL <<
"<MakeTheRule> Input node is NULL. Should not happen. BUG!" <<
Endl;
1343 std::vector< const Node * > nodeVec;
1344 const Node *parent = node;
1349 nodeVec.push_back( node );
1352 if (!parent)
continue;
1355 nodeVec.insert( nodeVec.begin(), parent );
1358 if (nodeVec.size()<2) {
1359 Log() << kFATAL <<
"<MakeTheRule> BUG! Inconsistent Rule!" <<
Endl;
1362 Rule *rule =
new Rule(
this, nodeVec );
1372 Log() << kVERBOSE <<
"Making Rule map for all events" <<
Endl;
1374 if (events==0) events = GetTrainingEvents();
1375 if ((ifirst==0) || (ilast==0) || (ifirst>ilast)) {
1377 ilast = events->size()-1;
1380 if ((events!=fRuleMapEvents) ||
1381 (ifirst!=fRuleMapInd0) ||
1382 (ilast !=fRuleMapInd1)) {
1387 Log() << kVERBOSE <<
"<MakeRuleMap> Map is already valid" <<
Endl;
1390 fRuleMapEvents = events;
1391 fRuleMapInd0 = ifirst;
1392 fRuleMapInd1 = ilast;
1394 UInt_t nrules = GetNRules();
1396 Log() << kVERBOSE <<
"No rules found in MakeRuleMap()" <<
Endl;
1403 std::vector<UInt_t> ruleind;
1405 for (
UInt_t i=ifirst; i<=ilast; i++) {
1407 fRuleMap.push_back( ruleind );
1409 if (fRules[
r]->EvalEvent(*((*events)[i]))) {
1410 fRuleMap.back().push_back(
r);
1415 Log() << kVERBOSE <<
"Made rule map for event# " << ifirst <<
" : " << ilast <<
Endl;
1423 os <<
"DON'T USE THIS - TO BE REMOVED" << std::endl;
static RooMathCoreReg dummy
R__EXTERN TRandom * gRandom
char * Form(const char *fmt,...)
1-D histogram with a float per channel (see TH1 documentation)}
Short_t GetSelector() const
Implementation of a Decision Tree.
virtual DecisionTreeNode * GetRoot() const
Virtual base Class for all MVA method.
J Friedman's RuleFit method.
ostringstream derivative to redirect and format output
Node for the BinarySearch or Decision Trees.
virtual Node * GetLeft() const
virtual Node * GetParent() const
virtual Node * GetRight() const
virtual ~RuleEnsemble()
destructor
void CalcVarImportance()
Calculates variable importance using eq (35) in RuleFit paper by Friedman et.al.
void SetImportanceRef(Double_t impref)
set reference importance
void CalcImportance()
calculate the importance of each rule
void PrintRuleGen() const
print rule generation info
void MakeRuleMap(const std::vector< const TMVA::Event * > *events=0, UInt_t ifirst=0, UInt_t ilast=0)
Makes rule map for all events.
Int_t CalcNRules(const TMVA::DecisionTree *dtree)
calculate the number of rules
void ResetCoefficients()
reset all rule coefficients
void SetMsgType(EMsgType t)
Double_t GetLinQuantile() const
void ReadRaw(std::istream &istr)
read rule ensemble from stream
void AddRule(const Node *node)
add a new rule to the tree
void ReadFromXML(void *wghtnode)
read rules from XML
Double_t GetImportanceCut() const
const Event * GetTrainingEvent(UInt_t i) const
get the training event from the rule fitter
const std::vector< const TMVA::Event * > * GetTrainingEvents() const
get list of training events from the rule fitter
Double_t GetRuleMinDist() const
void SetRules(const std::vector< TMVA::Rule * > &rules)
set rules
void MakeRules(const std::vector< const TMVA::DecisionTree * > &forest)
Makes rules from the given decision tree.
void RemoveSimilarRules()
remove rules that behave similar
void FindNEndNodes(const TMVA::Node *node, Int_t &nendnodes)
find the number of leaf nodes
RuleEnsemble()
constructor
const std::vector< Double_t > & GetVarImportance() const
void CleanupRules()
cleanup rules
void Initialize(const RuleFit *rf)
Initializes all member variables with default values.
void CleanupLinear()
cleanup linear model
void RuleResponseStats()
calculate various statistics for this rule
const RuleFit * GetRuleFit() const
void * AddXMLTo(void *parent) const
write rules to XML
const std::vector< TMVA::Rule * > & GetRulesConst() const
const MethodRuleFit * GetMethodRuleFit() const
Get a pointer to the original MethodRuleFit.
void MakeModel()
create model
void RuleStatistics()
calculate various statistics for this rule
void SetCoefficients(const std::vector< Double_t > &v)
set all rule coefficients
void Print() const
print function
Double_t PdfRule(Double_t &nsig, Double_t &ntot) const
This function returns Pr( y = 1 | x ) for rules.
const MethodBase * GetMethodBase() const
Get a pointer to the original MethodRuleFit.
Double_t GetOffset() const
void Copy(RuleEnsemble const &other)
copy function
Double_t CalcLinImportance()
calculate the linear importance for each rule
Double_t CalcRuleImportance()
calculate importance of each rule
void PrintRaw(std::ostream &os) const
write rules to stream
Double_t fAverageRuleSigma
void CalcRuleSupport()
calculate the support for all rules
ELearningModel GetLearningModel() const
Double_t PdfLinear(Double_t &nsig, Double_t &ntot) const
This function returns Pr( y = 1 | x ) for the linear terms.
Double_t CoefficientRadius()
Calculates sqrt(Sum(a_i^2)), i=1..N (NOTE do not include a0)
void MakeRulesFromTree(const DecisionTree *dtree)
create rules from the decision tree structure
void MakeLinearTerms()
Make the linear terms as in eq 25, ref 2 For this the b and (1-b) quantiles are needed.
Rule * MakeTheRule(const Node *node)
Make a Rule from a given Node.
void GetCoefficients(std::vector< Double_t > &v)
Retrieve all rule coefficients.
Double_t FStar() const
We want to estimate F* = argmin Eyx( L(y,F(x) ), min wrt F(x) F(x) = FL(x) + FR(x) ,...
A class implementing various fits of rule ensembles.
Implementation of a rule.
void SetMsgType(EMsgType t)
virtual Double_t Rndm()
Machine independent random number generator.
static constexpr double s
static constexpr double second
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Double_t Sqrt(Double_t x)
Short_t Min(Short_t a, Short_t b)