Logo ROOT   6.12/07
Reference Guide
RuleFit.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Fredrik Tegenfeldt, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : RuleFit *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * A class implementing various fits of rule ensembles *
12  * *
13  * Authors (alphabetical): *
14  * Fredrik Tegenfeldt <Fredrik.Tegenfeldt@cern.ch> - Iowa State U., USA *
15  * Helge Voss <Helge.Voss@cern.ch> - MPI-KP Heidelberg, Ger. *
16  * *
17  * Copyright (c) 2005: *
18  * CERN, Switzerland *
19  * Iowa State U. *
20  * MPI-K Heidelberg, Germany *
21  * *
22  * Redistribution and use in source and binary forms, with or without *
23  * modification, are permitted according to the terms listed in LICENSE *
24  * (http://tmva.sourceforge.net/LICENSE) *
25  **********************************************************************************/
26 
27 #ifndef ROOT_TMVA_RuleFit
28 #define ROOT_TMVA_RuleFit
29 
30 #include "TMVA/DecisionTree.h"
31 #include "TMVA/RuleEnsemble.h"
32 #include "TMVA/RuleFitParams.h"
33 #include "TMVA/Event.h"
34 
35 #include <algorithm>
36 #include <random>
37 
38 namespace TMVA {
39 
40 
41  class MethodBase;
42  class MethodRuleFit;
43  class MsgLogger;
44 
45  class RuleFit {
46 
47  public:
48 
49  // main constructor
50  RuleFit( const TMVA::MethodBase *rfbase );
51 
52  // empty constructor
53  RuleFit( void );
54 
55  virtual ~RuleFit( void );
56 
57  void InitNEveEff();
58  void InitPtrs( const TMVA::MethodBase *rfbase );
59  void Initialize( const TMVA::MethodBase *rfbase );
60 
61  void SetMsgType( EMsgType t );
62 
63  void SetTrainingEvents( const std::vector<const TMVA::Event *> & el );
64 
66  {
67  std::shuffle(fTrainingEventsRndm.begin(), fTrainingEventsRndm.end(), fRNGEngine);
68  }
69 
70  void SetMethodBase( const MethodBase *rfbase );
71 
72  // make the forest of trees for rule generation
73  void MakeForest();
74 
75  // build a tree
76  void BuildTree( TMVA::DecisionTree *dt );
77 
78  // save event weights
79  void SaveEventWeights();
80 
81  // restore saved event weights
82  void RestoreEventWeights();
83 
84  // boost events based on the given tree
85  void Boost( TMVA::DecisionTree *dt );
86 
87  // calculate and print some statistics on the given forest
88  void ForestStatistics();
89 
90  // calculate the discriminating variable for the given event
91  Double_t EvalEvent( const Event& e );
92 
93  // calculate sum of
94  Double_t CalcWeightSum( const std::vector<const TMVA::Event *> *events, UInt_t neve=0 );
95 
96  // do the fitting of the coefficients
97  void FitCoefficients();
98 
99  // calculate variable and rule importance from a set of events
100  void CalcImportance();
101 
102  // set usage of linear term
104  // set usage of rules
106  // set usage of linear term
108  // set minimum importance allowed
110  // set minimum rule distance - see RuleEnsemble
112  // set path related parameters
113  void SetGDTau( Double_t t=0.0 ) { fRuleFitParams.SetGDTau(t); }
116  // make visualization histograms
120  void MakeVisHists();
121  void FillVisHistCut(const Rule * rule, std::vector<TH2F *> & hlist);
122  void FillVisHistCorr(const Rule * rule, std::vector<TH2F *> & hlist);
123  void FillCut(TH2F* h2,const TMVA::Rule *rule,Int_t vind);
124  void FillLin(TH2F* h2,Int_t vind);
125  void FillCorr(TH2F* h2,const TMVA::Rule *rule,Int_t v1, Int_t v2);
126  void NormVisHists(std::vector<TH2F *> & hlist);
127  void MakeDebugHists();
128  Bool_t GetCorrVars(TString & title, TString & var1, TString & var2);
129  // accessors
130  UInt_t GetNTreeSample() const { return fNTreeSample; }
131  Double_t GetNEveEff() const { return fNEveEffTrain; } // reweighted number of events = sum(wi)
132  const Event* GetTrainingEvent(UInt_t i) const { return static_cast< const Event *>(fTrainingEvents[i]); }
133  Double_t GetTrainingEventWeight(UInt_t i) const { return fTrainingEvents[i]->GetWeight(); }
134 
135  // const Event* GetTrainingEvent(UInt_t i, UInt_t isub) const { return &(fTrainingEvents[fSubsampleEvents[isub]])[i]; }
136 
137  const std::vector< const TMVA::Event * > & GetTrainingEvents() const { return fTrainingEvents; }
138  // const std::vector< Int_t > & GetSubsampleEvents() const { return fSubsampleEvents; }
139 
140  // void GetSubsampleEvents(Int_t sub, UInt_t & ibeg, UInt_t & iend) const;
141  void GetRndmSampleEvents(std::vector< const TMVA::Event * > & evevec, UInt_t nevents);
142  //
143  const std::vector< const TMVA::DecisionTree *> & GetForest() const { return fForest; }
144  const RuleEnsemble & GetRuleEnsemble() const { return fRuleEnsemble; }
146  const RuleFitParams & GetRuleFitParams() const { return fRuleFitParams; }
148  const MethodRuleFit * GetMethodRuleFit() const { return fMethodRuleFit; }
149  const MethodBase * GetMethodBase() const { return fMethodBase; }
150 
151  private:
152 
153  // copy constructor
154  RuleFit( const RuleFit & other );
155 
156  // copy method
157  void Copy( const RuleFit & other );
158 
159  std::vector<const TMVA::Event *> fTrainingEvents; // all training events
160  std::vector<const TMVA::Event *> fTrainingEventsRndm; // idem, but randomly shuffled
161  std::vector<Double_t> fEventWeights; // original weights of the events - follows fTrainingEvents
162  UInt_t fNTreeSample; // number of events in sub sample = frac*neve
163 
164  Double_t fNEveEffTrain; // reweighted number of events = sum(wi)
165  std::vector< const TMVA::DecisionTree *> fForest; // the input forest of decision trees
166  RuleEnsemble fRuleEnsemble; // the ensemble of rules
167  RuleFitParams fRuleFitParams; // fit rule parameters
168  const MethodRuleFit *fMethodRuleFit; // pointer the method which initialized this RuleFit instance
169  const MethodBase *fMethodBase; // pointer the method base which initialized this RuleFit instance
170  Bool_t fVisHistsUseImp; // if true, use importance as weight; else coef in vis hists
171 
172  mutable MsgLogger* fLogger; // message logger
173  MsgLogger& Log() const { return *fLogger; }
174 
175  static const Int_t randSEED = 0; // set to 1 for debugging purposes or to zero for random seeds
176  std::default_random_engine fRNGEngine;
177 
178  ClassDef(RuleFit,0); // Calculations for Friedman's RuleFit method
179  };
180 }
181 
182 #endif
std::vector< const TMVA::Event * > fTrainingEventsRndm
Definition: RuleFit.h:160
void ForestStatistics()
summary of statistics of all trees
Definition: RuleFit.cxx:386
A class doing the actual fitting of a linear model using rules as base functions. ...
Definition: RuleFitParams.h:53
void MakeForest()
make a forest of decisiontrees
Definition: RuleFit.cxx:222
J Friedman&#39;s RuleFit method.
Definition: MethodRuleFit.h:47
const RuleEnsemble & GetRuleEnsemble() const
Definition: RuleFit.h:144
void SetVisHistsUseImp(Bool_t f)
Definition: RuleFit.h:117
void SetGDTau(Double_t t=0.0)
Definition: RuleFit.h:113
void CalcImportance()
calculates the importance of each rule
Definition: RuleFit.cxx:418
const std::vector< const TMVA::Event *> & GetTrainingEvents() const
Definition: RuleFit.h:137
A class implementing various fits of rule ensembles.
Definition: RuleFit.h:45
MsgLogger & Log() const
Definition: RuleFit.h:173
void NormVisHists(std::vector< TH2F *> &hlist)
normalize rule importance hists
Definition: RuleFit.cxx:486
void SetGDTau(Double_t t)
Definition: RuleFitParams.h:86
void SetMsgType(EMsgType t)
set the current message type to that of mlog for this class and all other subtools ...
Definition: RuleFit.cxx:191
const MethodBase * fMethodBase
Definition: RuleFit.h:169
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
Bool_t GetCorrVars(TString &title, TString &var1, TString &var2)
get first and second variables from title
Definition: RuleFit.cxx:754
void InitNEveEff()
init effective number of events (using event weights)
Definition: RuleFit.cxx:98
void FitCoefficients()
Fit the coefficients for the rule ensemble.
Definition: RuleFit.cxx:409
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetModelFull()
Definition: RuleFit.h:107
std::vector< Double_t > fEventWeights
Definition: RuleFit.h:161
Double_t fNEveEffTrain
Definition: RuleFit.h:164
const Event * GetTrainingEvent(UInt_t i) const
Definition: RuleFit.h:132
void SetTrainingEvents(const std::vector< const TMVA::Event *> &el)
set the training events randomly
Definition: RuleFit.cxx:440
void SetModelLinear()
Definition: RuleFit.h:103
void SetModelRules()
Definition: RuleFit.h:105
Implementation of a rule.
Definition: Rule.h:48
const std::vector< const TMVA::DecisionTree * > & GetForest() const
Definition: RuleFit.h:143
RuleFit(void)
default constructor
Definition: RuleFit.cxx:76
#define ClassDef(name, id)
Definition: Rtypes.h:320
void BuildTree(TMVA::DecisionTree *dt)
build the decision tree using fNTreeSample events from fTrainingEventsRndm
Definition: RuleFit.cxx:201
void UseImportanceVisHists()
Definition: RuleFit.h:118
void ReshuffleEvents()
Definition: RuleFit.h:65
void GetRndmSampleEvents(std::vector< const TMVA::Event * > &evevec, UInt_t nevents)
draw a random subsample of the training events without replacement
Definition: RuleFit.cxx:467
const RuleFitParams & GetRuleFitParams() const
Definition: RuleFit.h:146
virtual ~RuleFit(void)
destructor
Definition: RuleFit.cxx:90
RuleEnsemble * GetRuleEnsemblePtr()
Definition: RuleFit.h:145
void SetGDNPathSteps(Int_t n=100)
Definition: RuleFit.h:115
void SetMethodBase(const MethodBase *rfbase)
set MethodBase
Definition: RuleFit.cxx:151
void SetGDNPathSteps(Int_t np)
Definition: RuleFitParams.h:69
UInt_t fNTreeSample
Definition: RuleFit.h:162
void RestoreEventWeights()
save event weights - must be done before making the forest
Definition: RuleFit.cxx:321
const MethodBase * GetMethodBase() const
Definition: RuleFit.h:149
RuleFitParams * GetRuleFitParamsPtr()
Definition: RuleFit.h:147
void Copy(const RuleFit &other)
copy method
Definition: RuleFit.cxx:160
void FillVisHistCut(const Rule *rule, std::vector< TH2F *> &hlist)
help routine to MakeVisHists() - fills for all variables
Definition: RuleFit.cxx:684
void FillCorr(TH2F *h2, const TMVA::Rule *rule, Int_t v1, Int_t v2)
fill rule correlation between vx and vy, weighted with either the importance or the coefficient ...
Definition: RuleFit.cxx:608
void MakeDebugHists()
this will create a histograms intended rather for debugging or for the curious user ...
Definition: RuleFit.cxx:937
static const Int_t randSEED
Definition: RuleFit.h:175
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
Implementation of a Decision Tree.
Definition: DecisionTree.h:59
unsigned int UInt_t
Definition: RtypesCore.h:42
void SetImportanceCut(Double_t minimp=0)
Definition: RuleEnsemble.h:131
void FillLin(TH2F *h2, Int_t vind)
fill lin
Definition: RuleFit.cxx:584
RuleEnsemble fRuleEnsemble
Definition: RuleFit.h:166
void Boost(TMVA::DecisionTree *dt)
Boost the events.
Definition: RuleFit.cxx:339
std::default_random_engine fRNGEngine
Definition: RuleFit.h:176
const Bool_t kFALSE
Definition: RtypesCore.h:88
void SetGDPathStep(Double_t s=0.01)
Definition: RuleFit.h:114
void SaveEventWeights()
save event weights - must be done before making the forest
Definition: RuleFit.cxx:309
Double_t GetNEveEff() const
Definition: RuleFit.h:131
Double_t GetTrainingEventWeight(UInt_t i) const
Definition: RuleFit.h:133
double Double_t
Definition: RtypesCore.h:55
const MethodRuleFit * fMethodRuleFit
Definition: RuleFit.h:168
void MakeVisHists()
this will create histograms visualizing the rule ensemble
Definition: RuleFit.cxx:777
static constexpr double s
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
void FillVisHistCorr(const Rule *rule, std::vector< TH2F *> &hlist)
help routine to MakeVisHists() - fills for all correlation plots
Definition: RuleFit.cxx:715
void InitPtrs(const TMVA::MethodBase *rfbase)
initialize pointers
Definition: RuleFit.cxx:110
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
void SetRuleMinDist(Double_t d)
Definition: RuleFit.h:111
Abstract ClassifierFactory template that handles arbitrary types.
void FillCut(TH2F *h2, const TMVA::Rule *rule, Int_t vind)
Fill cut.
Definition: RuleFit.cxx:533
Bool_t fVisHistsUseImp
Definition: RuleFit.h:170
RuleFitParams fRuleFitParams
Definition: RuleFit.h:167
Double_t EvalEvent(const Event &e)
evaluate single event
Definition: RuleFit.cxx:432
std::vector< const TMVA::DecisionTree * > fForest
Definition: RuleFit.h:165
void UseCoefficientsVisHists()
Definition: RuleFit.h:119
void Initialize(const TMVA::MethodBase *rfbase)
initialize the parameters of the RuleFit method and make rules
Definition: RuleFit.cxx:120
Double_t CalcWeightSum(const std::vector< const TMVA::Event *> *events, UInt_t neve=0)
calculate the sum of weights
Definition: RuleFit.cxx:176
const MethodRuleFit * GetMethodRuleFit() const
Definition: RuleFit.h:148
std::vector< const TMVA::Event * > fTrainingEvents
Definition: RuleFit.h:159
UInt_t GetNTreeSample() const
Definition: RuleFit.h:130
const Bool_t kTRUE
Definition: RtypesCore.h:87
const Int_t n
Definition: legend1.C:16
void SetGDPathStep(Double_t s)
Definition: RuleFitParams.h:72
void SetRuleMinDist(Double_t d)
Definition: RuleEnsemble.h:128
void SetImportanceCut(Double_t minimp=0)
Definition: RuleFit.h:109
MsgLogger * fLogger
Definition: RuleFit.h:172