CMS 3D CMS Logo

Forest.cc
Go to the documentation of this file.
1 // Forest.cxx //
3 // =====================================================================//
4 // This is the object implementation of a forest of decision trees. //
5 // We need this to implement gradient boosting. //
6 // References include //
7 // *Elements of Statistical Learning by Hastie, //
8 // Tibshirani, and Friedman. //
9 // *Greedy Function Approximation: A Gradient Boosting Machine. //
10 // Friedman. The Annals of Statistics, Vol. 29, No. 5. Oct 2001. //
11 // *Inductive Learning of Tree-based Regression Models. Luis Torgo. //
12 // //
14 
16 // _______________________Includes_______________________________________//
18 
21 
23 
24 #include "TStopwatch.h"
25 #include "TString.h"
26 
27 #include <iostream>
28 #include <sstream>
29 #include <algorithm>
30 #include <iterator>
31 #include <fstream>
32 #include <utility>
33 
34 using namespace emtf;
35 
37 // _______________________Constructor(s)________________________________//
39 
41 {
42  events = std::vector< std::vector<Event*> >(1);
43 }
44 
46 // ----------------------------------------------------------------------
48 
49 Forest::Forest(std::vector<Event*>& trainingEvents)
50 {
51  setTrainingEvents(trainingEvents);
52 }
53 
55 // _______________________Destructor____________________________________//
57 
59 {
60 // When the forest is destroyed it will delete the trees as well as the
61 // events from the training and testing sets.
62 // The user may want the events to remain after they destroy the forest
63 // this should be changed in future upgrades.
64 
65  for(unsigned int i=0; i < trees.size(); i++)
66  {
67  if(trees[i]) delete trees[i];
68  }
69 }
70 
71 Forest::Forest(const Forest &forest)
72 {
73  transform(forest.trees.cbegin(),
74  forest.trees.cend(),
75  back_inserter(trees),
76  [] (const Tree *tree) { return new Tree(*tree); }
77  );
78 }
79 
81 {
82  for(unsigned int i=0; i < trees.size(); i++)
83  {
84  if(trees[i]) delete trees[i];
85  }
86  trees.resize(0);
87 
88  transform(forest.trees.cbegin(),
89  forest.trees.cend(),
90  back_inserter(trees),
91  [] (const Tree *tree) { return new Tree(*tree); }
92  );
93  return *this;
94 }
95 
97 // ______________________Get/Set_Functions______________________________//
99 
100 void Forest::setTrainingEvents(std::vector<Event*>& trainingEvents)
101 {
102 // tell the forest which events to use for training
103 
104  Event* e = trainingEvents[0];
105  // Unused variable
106  // unsigned int numrows = e->data.size();
107 
108  // Reset the events matrix.
109  events = std::vector< std::vector<Event*> >();
110 
111  for(unsigned int i=0; i<e->data.size(); i++)
112  {
113  events.push_back(trainingEvents);
114  }
115 }
116 
118 // ----------------------------------------------------------------------
120 
121 // return a copy of the training events
122 std::vector<Event*> Forest::getTrainingEvents(){ return events[0]; }
123 
125 // ----------------------------------------------------------------------
127 
128 // return the ith tree
129 Tree* Forest::getTree(unsigned int i)
130 {
131  if(/*i>=0 && */i<trees.size()) return trees[i];
132  else
133  {
134  //std::cout << i << "is an invalid input for getTree. Out of range." << std::endl;
135  return nullptr;
136  }
137 }
138 
140 // ______________________Various_Helpful_Functions______________________//
142 
143 unsigned int Forest::size()
144 {
145 // Return the number of trees in the forest.
146  return trees.size();
147 }
148 
150 //*** Need to make a data structure that includes the next few functions ***
151 //*** pertaining to events. These don't really have much to do with the ***
152 //*** forest class. ***
154 
156 // ----------------------------------------------------------------------
158 
159 void Forest::listEvents(std::vector< std::vector<Event*> >& e)
160 {
161 // Simply list the events in each event vector. We have multiple copies
162 // of the events vector. Each copy is sorted according to a different
163 // determining variable.
164  std::cout << std::endl << "Listing Events... " << std::endl;
165 
166  for(unsigned int i=0; i < e.size(); i++)
167  {
168  std::cout << std::endl << "Variable " << i << " vector contents: " << std::endl;
169  for(unsigned int j=0; j<e[i].size(); j++)
170  {
171  e[i][j]->outputEvent();
172  }
173  std::cout << std::endl;
174  }
175 }
176 
178 // ----------------------------------------------------------------------
180 
181 // We have to initialize Event::sortingIndex outside of a function since
182 // it is a static member.
183 int Event::sortingIndex = 1;
184 
185 bool compareEvents(Event* e1, Event* e2)
186 {
187 // Sort the events according to the variable given by the sortingIndex.
189 }
191 // ----------------------------------------------------------------------
193 
195 {
196 // Sort the events by ID. We need this to produce rate plots.
197  return e1->id < e2->id;
198 }
200 // ----------------------------------------------------------------------
202 
203 void Forest::sortEventVectors(std::vector< std::vector<Event*> >& e)
204 {
205 // When a node chooses the optimum split point and split variable it needs
206 // the events to be sorted according to the variable it is considering.
207 
208  for(unsigned int i=0; i<e.size(); i++)
209  {
211  std::sort(e[i].begin(), e[i].end(), compareEvents);
212  }
213 }
214 
216 // ----------------------------------------------------------------------
218 
219 void Forest::rankVariables(std::vector<int>& rank)
220 {
221 // This function ranks the determining variables according to their importance
222 // in determining the fit. Use a low learning rate for better results.
223 // Separates completely useless variables from useful ones well,
224 // but isn't the best at separating variables of similar importance.
225 // This is calculated using the error reduction on the training set. The function
226 // should be changed to use the testing set, but this works fine for now.
227 // I will try to change this in the future.
228 
229  // Initialize the vector v, which will store the total error reduction
230  // for each variable i in v[i].
231  std::vector<double> v(events.size(), 0);
232 
233  //std::cout << std::endl << "Ranking Variables by Net Error Reduction... " << std::endl;
234 
235  for(unsigned int j=0; j < trees.size(); j++)
236  {
237  trees[j]->rankVariables(v);
238  }
239 
240  double max = *std::max_element(v.begin(), v.end());
241 
242  // Scale the importance. Maximum importance = 100.
243  for(unsigned int i=0; i < v.size(); i++)
244  {
245  v[i] = 100*v[i]/max;
246  }
247 
248  // Change the storage format so that we can keep the index
249  // and the value associated after sorting.
250  std::vector< std::pair<double, int> > w(events.size());
251 
252  for(unsigned int i=0; i<v.size(); i++)
253  {
254  w[i] = std::pair<double, int>(v[i],i);
255  }
256 
257  // Sort so that we can output in order of importance.
258  std::sort(w.begin(),w.end());
259 
260  // Output the results.
261  for(int i=(v.size()-1); i>=0; i--)
262  {
263  rank.push_back(w[i].second);
264  // std::cout << "x" << w[i].second << ": " << w[i].first << std::endl;
265  }
266 
267  // std::cout << std::endl << "Done." << std::endl << std::endl;
268 }
269 
271 // ----------------------------------------------------------------------
273 
274 void Forest::saveSplitValues(const char* savefilename)
275 {
276 // This function gathers all of the split values from the forest and puts them into lists.
277 
278  std::ofstream splitvaluefile;
279  splitvaluefile.open(savefilename);
280 
281  // Initialize the matrix v, which will store the list of split values
282  // for each variable i in v[i].
283  std::vector<std::vector<double>> v(events.size(), std::vector<double>());
284 
285  //std::cout << std::endl << "Gathering split values... " << std::endl;
286 
287  // Gather the split values from each tree in the forest.
288  for(unsigned int j=0; j<trees.size(); j++)
289  {
290  trees[j]->getSplitValues(v);
291  }
292 
293  // Sort the lists of split values and remove the duplicates.
294  for(unsigned int i=0; i<v.size(); i++)
295  {
296  std::sort(v[i].begin(),v[i].end());
297  v[i].erase( unique( v[i].begin(), v[i].end() ), v[i].end() );
298  }
299 
300  // Output the results after removing duplicates.
301  // The 0th variable is special and is not used for splitting, so we start at 1.
302  for(unsigned int i=1; i<v.size(); i++)
303  {
304  TString splitValues;
305  for(unsigned int j=0; j<v[i].size(); j++)
306  {
307  std::stringstream ss;
308  ss.precision(14);
309  ss << std::scientific << v[i][j];
310  splitValues+=",";
311  splitValues+=ss.str().c_str();
312  }
313 
314  splitValues=splitValues(1,splitValues.Length());
315  splitvaluefile << splitValues << std::endl << std::endl;;
316  }
317 }
319 // ______________________Update_Events_After_Fitting____________________//
321 
322 void Forest::updateRegTargets(Tree* tree, double learningRate, LossFunction* l)
323 {
324 // Prepare the global vector of events for the next tree.
325 // Update the fit for each event and set the new target value
326 // for the next tree.
327 
328  // Get the list of terminal nodes for this tree.
329  std::list<Node*>& tn = tree->getTerminalNodes();
330 
331  // Loop through the terminal nodes.
332  for(std::list<Node*>::iterator it=tn.begin(); it!=tn.end(); it++)
333  {
334  // Get the events in the current terminal region.
335  std::vector<Event*>& v = (*it)->getEvents()[0];
336 
337  // Fit the events depending on the loss function criteria.
338  double fit = l->fit(v);
339 
340  // Scale the rate at which the algorithm converges.
341  fit = learningRate*fit;
342 
343  // Store the official fit value in the terminal node.
344  (*it)->setFitValue(fit);
345 
346  // Loop through each event in the terminal region and update the
347  // the target for the next tree.
348  for(unsigned int j=0; j<v.size(); j++)
349  {
350  Event* e = v[j];
351  e->predictedValue += fit;
352  e->data[0] = l->target(e);
353  }
354 
355  // Release memory.
356  (*it)->getEvents() = std::vector< std::vector<Event*> >();
357  }
358 }
359 
361 // ----------------------------------------------------------------------
363 
365 {
366 // Prepare the test events for the next tree.
367 
368  // Get the list of terminal nodes for this tree.
369  std::list<Node*>& tn = tree->getTerminalNodes();
370 
371  // Loop through the terminal nodes.
372  for(std::list<Node*>::iterator it=tn.begin(); it!=tn.end(); it++)
373  {
374  std::vector<Event*>& v = (*it)->getEvents()[0];
375  double fit = (*it)->getFitValue();
376 
377  // Loop through each event in the terminal region and update the
378  // the global event it maps to.
379  for(unsigned int j=0; j<v.size(); j++)
380  {
381  Event* e = v[j];
382  e->predictedValue += fit;
383  }
384 
385  // Release memory.
386  (*it)->getEvents() = std::vector< std::vector<Event*> >();
387  }
388 }
389 
391 // ____________________Do/Test_the Regression___________________________//
393 
394 void Forest::doRegression(int nodeLimit, int treeLimit, double learningRate, LossFunction* l, const char* savetreesdirectory, bool saveTrees)
395 {
396 // Build the forest using the training sample.
397 
398  //std::cout << std::endl << "--Building Forest..." << std::endl << std::endl;
399 
400  // The trees work with a matrix of events where the rows have the same set of events. Each row however
401  // is sorted according to the feature variable given by event->data[row].
402  // If we only had one set of events we would have to sort it according to the
403  // feature variable every time we want to calculate the best split point for that feature.
404  // By keeping sorted copies we avoid the sorting operation during splint point calculation
405  // and save computation time. If we do not sort each of the rows the regression will fail.
406  //std::cout << "Sorting event vectors..." << std::endl;
408 
409  // See how long the regression takes.
410  TStopwatch timer;
411  timer.Start(kTRUE);
412 
413  for(unsigned int i=0; i< (unsigned) treeLimit; i++)
414  {
415  // std::cout << "++Building Tree " << i << "... " << std::endl;
416  Tree* tree = new Tree(events);
417  trees.push_back(tree);
418  tree->buildTree(nodeLimit);
419 
420  // Update the targets for the next tree to fit.
421  updateRegTargets(tree, learningRate, l);
422 
423  // Save trees to xml in some directory.
424  std::ostringstream ss;
425  ss << savetreesdirectory << "/" << i << ".xml";
426  std::string s = ss.str();
427  const char* c = s.c_str();
428 
429  if(saveTrees) tree->saveToXML(c);
430  }
431  //std::cout << std::endl;
432  //std::cout << std::endl << "Done." << std::endl << std::endl;
433 
434 // std::cout << std::endl << "Total calculation time: " << timer.RealTime() << std::endl;
435 }
436 
438 // ----------------------------------------------------------------------
440 
441 void Forest::predictEvents(std::vector<Event*>& eventsp, unsigned int numtrees)
442 {
443 // Predict values for eventsp by running them through the forest up to numtrees.
444 
445  //std::cout << "Using " << numtrees << " trees from the forest to predict events ... " << std::endl;
446  if(numtrees > trees.size())
447  {
448  //std::cout << std::endl << "!! Input greater than the forest size. Using forest.size() = " << trees.size() << " to predict instead." << std::endl;
449  numtrees = trees.size();
450  }
451 
452  // i iterates through the trees in the forest. Each tree corrects the last prediction.
453  for(unsigned int i=0; i < numtrees; i++)
454  {
455  //std::cout << "++Tree " << i << "..." << std::endl;
456  appendCorrection(eventsp, i);
457  }
458 }
459 
461 // ----------------------------------------------------------------------
463 
464 void Forest::appendCorrection(std::vector<Event*>& eventsp, int treenum)
465 {
466 // Update the prediction by appending the next correction.
467 
468  Tree* tree = trees[treenum];
469  tree->filterEvents(eventsp);
470 
471  // Update the events with their new prediction.
472  updateEvents(tree);
473 }
474 
476 // ----------------------------------------------------------------------
478 
479 void Forest::predictEvent(Event* e, unsigned int numtrees)
480 {
481 // Predict values for eventsp by running them through the forest up to numtrees.
482 
483  //std::cout << "Using " << numtrees << " trees from the forest to predict events ... " << std::endl;
484  if(numtrees > trees.size())
485  {
486  //std::cout << std::endl << "!! Input greater than the forest size. Using forest.size() = " << trees.size() << " to predict instead." << std::endl;
487  numtrees = trees.size();
488  }
489 
490  // just like in line #2470 of https://root.cern.ch/doc/master/MethodBDT_8cxx_source.html for gradient boosting
491  e->predictedValue = trees[0]->getBoostWeight();
492 
493  // i iterates through the trees in the forest. Each tree corrects the last prediction.
494  for(unsigned int i=0; i < numtrees; i++)
495  {
496  //std::cout << "++Tree " << i << "..." << std::endl;
497  appendCorrection(e, i);
498  }
499 }
500 
502 // ----------------------------------------------------------------------
504 
505 void Forest::appendCorrection(Event* e, int treenum)
506 {
507 // Update the prediction by appending the next correction.
508 
509  Tree* tree = trees[treenum];
510  Node* terminalNode = tree->filterEvent(e);
511 
512  // Update the event with its new prediction.
513  double fit = terminalNode->getFitValue();
514  e->predictedValue += fit;
515 }
517 // ----------------------------------------------------------------------------------
519 
520 void Forest::loadForestFromXML(const char* directory, unsigned int numTrees)
521 {
522 // Load a forest that has already been created and stored into XML somewhere.
523 
524  // Initialize the vector of trees.
525  trees = std::vector<Tree*>(numTrees);
526 
527  // Load the Forest.
528  // std::cout << std::endl << "Loading Forest from XML ... " << std::endl;
529  for(unsigned int i=0; i < numTrees; i++)
530  {
531  trees[i] = new Tree();
532 
533  std::stringstream ss;
534  ss << directory << "/" << i << ".xml";
535 
536  trees[i]->loadFromXML(edm::FileInPath(ss.str().c_str()).fullPath().c_str());
537  }
538 
539  //std::cout << "Done." << std::endl << std::endl;
540 }
541 
543 {
544 // Load a forest that has already been created and stored in CondDB.
545  // Initialize the vector of trees.
546  unsigned int numTrees = forest.size();
547 
548  // clean-up leftovers from previous initialization (if any)
549  for(unsigned int i=0; i < trees.size(); i++)
550  {
551  if(trees[i]) delete trees[i];
552  }
553 
554  trees = std::vector<Tree*>(numTrees);
555 
556  // Load the Forest.
557  for(unsigned int i=0; i < numTrees; i++)
558  {
559  trees[i] = new Tree();
560  trees[i]->loadFromCondPayload(forest[i]);
561  }
562 }
563 
565 // ___________________Stochastic_Sampling_&_Regression__________________//
567 
569 {
570 // We use this for Stochastic Gradient Boosting. Basically you
571 // take a subsample of the training events and build a tree using
572 // those. Then use the tree built from the subsample to update
573 // the predictions for all the events.
574 
575  subSample = std::vector< std::vector<Event*> >(events.size()) ;
576  size_t subSampleSize = fraction*events[0].size();
577 
578  // Randomize the first subSampleSize events in events[0].
579  shuffle(events[0].begin(), events[0].end(), subSampleSize);
580 
581  // Get a copy of the random subset we just made.
582  std::vector<Event*> v(events[0].begin(), events[0].begin()+subSampleSize);
583 
584  // Initialize and sort the subSample collection.
585  for(unsigned int i=0; i<subSample.size(); i++)
586  {
587  subSample[i] = v;
588  }
589 
591 }
592 
594 // ----------------------------------------------------------------------
596 
597 void Forest::doStochasticRegression(int nodeLimit, int treeLimit, double learningRate, double fraction, LossFunction* l)
598 {
599 // If the fraction of events to use is one then this algorithm is slower than doRegression due to the fact
600 // that we have to sort the events every time we extract a subsample. Without random sampling we simply
601 // use all of the events and keep them sorted.
602 
603 // Anyways, this algorithm uses a portion of the events to train each tree. All of the events are updated
604 // afterwards with the results from the subsample built tree.
605 
606  // Prepare some things.
608  trees = std::vector<Tree*>(treeLimit);
609 
610  // See how long the regression takes.
611  TStopwatch timer;
612  timer.Start(kTRUE);
613 
614  // Output the current settings.
615  // std::cout << std::endl << "Running stochastic regression ... " << std::endl;
616  //std::cout << "# Nodes: " << nodeLimit << std::endl;
617  //std::cout << "Learning Rate: " << learningRate << std::endl;
618  //std::cout << "Bagging Fraction: " << fraction << std::endl;
619  //std::cout << std::endl;
620 
621 
622  for(unsigned int i=0; i< (unsigned) treeLimit; i++)
623  {
624  // Build the tree using a random subsample.
625  prepareRandomSubsample(fraction);
626  trees[i] = new Tree(subSample);
627  trees[i]->buildTree(nodeLimit);
628 
629  // Fit all of the events based upon the tree we built using
630  // the subsample of events.
631  trees[i]->filterEvents(events[0]);
632 
633  // Update the targets for the next tree to fit.
634  updateRegTargets(trees[i], learningRate, l);
635 
636  // Save trees to xml in some directory.
637  std::ostringstream ss;
638  ss << "trees/" << i << ".xml";
639  std::string s = ss.str();
640  const char* c = s.c_str();
641 
642  trees[i]->saveToXML(c);
643  }
644 
645  //std::cout << std::endl << "Done." << std::endl << std::endl;
646 
647  //std::cout << std::endl << "Total calculation time: " << timer.RealTime() << std::endl;
648 }
void appendCorrection(std::vector< Event * > &eventsp, int treenum)
Definition: Forest.cc:464
void predictEvent(Event *e, unsigned int trees)
Definition: Forest.cc:479
void updateEvents(Tree *tree)
Definition: Forest.cc:364
double getFitValue()
Definition: Node.cc:158
void prepareRandomSubsample(double fraction)
Definition: Forest.cc:568
void doRegression(int nodeLimit, int treeLimit, double learningRate, LossFunction *l, const char *savetreesdirectory, bool saveTrees)
Definition: Forest.cc:394
void buildTree(int nodeLimit)
Definition: Tree.cc:204
std::vector< std::vector< Event * > > subSample
Definition: Forest.h:67
const double w
Definition: UKUtility.cc:23
void doStochasticRegression(int nodeLimit, int treeLimit, double learningRate, double fraction, LossFunction *l)
Definition: Forest.cc:597
void sortEventVectors(std::vector< std::vector< Event * > > &e)
Definition: Forest.cc:203
Definition: Event.h:15
void saveSplitValues(const char *savefilename)
Definition: Forest.cc:274
void loadFromCondPayload(const L1TMuonEndCapForest::DForest &payload)
Definition: Forest.cc:542
virtual double fit(std::vector< Event * > &v)=0
void updateRegTargets(Tree *tree, double learningRate, LossFunction *l)
Definition: Forest.cc:322
U second(std::pair< T, U > const &p)
double predictedValue
Definition: Event.h:21
int id
Definition: Event.h:30
std::vector< Event * > getTrainingEvents()
Definition: Forest.cc:122
def unique(seq, keepstr=True)
Definition: tier0.py:24
std::vector< DTree > DForest
#define end
Definition: vmac.h:39
std::vector< Tree * > trees
Definition: Forest.h:68
std::list< Node * > & getTerminalNodes()
Definition: Tree.cc:172
void listEvents(std::vector< std::vector< Event * > > &e)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Definition: Forest.cc:159
bool compareEvents(Event *e1, Event *e2)
Definition: Forest.cc:185
unsigned int size()
Definition: Forest.cc:143
virtual double target(Event *e)=0
static int sortingIndex
Definition: Event.h:29
void loadForestFromXML(const char *directory, unsigned int numTrees)
Definition: Forest.cc:520
Node * filterEvent(Event *e)
Definition: Tree.cc:299
void rankVariables(std::vector< int > &rank)
Definition: Forest.cc:219
Tree * getTree(unsigned int i)
Definition: Forest.cc:129
void saveToXML(const char *filename)
Definition: Tree.cc:429
Forest & operator=(const Forest &forest)
Definition: Forest.cc:80
#define begin
Definition: vmac.h:32
void predictEvents(std::vector< Event * > &eventsp, unsigned int trees)
Definition: Forest.cc:441
Definition: tree.py:1
std::vector< std::vector< Event * > > events
Definition: Forest.h:66
bidiiter shuffle(bidiiter begin, bidiiter end, size_t num_random)
Definition: Utilities.h:27
void filterEvents(std::vector< Event * > &tEvents)
Definition: Tree.cc:265
bool compareEventsById(Event *e1, Event *e2)
Definition: Forest.cc:194
void setTrainingEvents(std::vector< Event * > &trainingEvents)
Definition: Forest.cc:100
std::vector< double > data
Definition: Event.h:31