// @(#)root/tmva $Id$
// Author: Andreas Hoecker, Xavier Prudent, Joerg Stelzer, Helge Voss, Kai Voss
/**********************************************************************************
* Project: TMVA - a Root-integrated toolkit for multivariate Data analysis *
* Package: TMVA *
* Class : MethodFisher *
* Web : http://tmva.sourceforge.net *
* *
* Description: *
* Implementation (see header for description) *
* *
* Original author of this Fisher-Discriminant implementation: *
* Andre Gaidot, CEA-France; *
* (Translation from FORTRAN) *
* *
* Authors (alphabetical): *
* Andreas Hoecker - CERN, Switzerland *
* Xavier Prudent - LAPP, France *
* Helge Voss - MPI-K Heidelberg, Germany *
* Kai Voss - U. of Victoria, Canada *
* *
* Copyright (c) 2005: *
* CERN, Switzerland *
* U. of Victoria, Canada *
* MPI-K Heidelberg, Germany *
* LAPP, Annecy, France *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted according to the terms listed in LICENSE *
* (http://tmva.sourceforge.net/LICENSE) *
**********************************************************************************/
//_______________________________________________________________________
/* Begin_Html
Fisher and Mahalanobis Discriminants (Linear Discriminant Analysis)
In the method of Fisher discriminants event selection is performed
in a transformed variable space with zero linear correlations, by
distinguishing the mean values of the signal and background
distributions.
The linear discriminant analysis determines an axis in the (correlated)
hyperspace of the input variables
such that, when projecting the output classes (signal and background)
upon this axis, they are pushed as far as possible away from each other,
while events of a same class are confined in a close vicinity.
The linearity property of this method is reflected in the metric with
which "far apart" and "close vicinity" are determined: the covariance
matrix of the discriminant variable space.
The classification of the events in signal and background classes
relies on the following characteristics (only): overall sample means,
xi, for each input variable, i,
class-specific sample means, xS(B),i,
and total covariance matrix Tij. The covariance matrix
can be decomposed into the sum of a within- (Wij)
and a between-class (Bij) class matrix. They describe
the dispersion of events relative to the means of their own class (within-class
matrix), and relative to the overall sample means (between-class matrix).
The Fisher coefficients, Fi, are then given by
where in TMVA is set NS=NB, so that the factor
in front of the sum simplifies to ½.
The Fisher discriminant then reads
The offset F0 centers the sample mean of xFi
at zero. Instead of using the within-class matrix, the Mahalanobis variant
determines the Fisher coefficients as follows:
with resulting xMa that are very similar to the
xFi.
TMVA provides two outputs for the ranking of the input variables:
- Fisher test: the Fisher analysis aims at simultaneously maximising
the between-class separation, while minimising the within-class dispersion.
A useful measure of the discrimination power of a variable is hence given
by the diagonal quantity: Bii/Wii.
- Discrimination power: the value of the Fisher coefficient is a
measure of the discriminating power of a variable. The discrimination power
of set of input variables can therefore be measured by the scalar
The corresponding numbers are printed on standard output.
End_Html */
//_______________________________________________________________________
#include
#include
#include "TMath.h"
#include "Riostream.h"
#include "TMVA/VariableTransformBase.h"
#include "TMVA/MethodFisher.h"
#include "TMVA/Tools.h"
#include "TMatrix.h"
#include "TMVA/Ranking.h"
#include "TMVA/Types.h"
#include "TMVA/ClassifierFactory.h"
REGISTER_METHOD(Fisher)
ClassImp(TMVA::MethodFisher);
//_______________________________________________________________________
TMVA::MethodFisher::MethodFisher( const TString& jobName,
const TString& methodTitle,
DataSetInfo& dsi,
const TString& theOption,
TDirectory* theTargetDir ) :
MethodBase( jobName, Types::kFisher, methodTitle, dsi, theOption, theTargetDir ),
fMeanMatx ( 0 ),
fTheMethod ( "Fisher" ),
fFisherMethod ( kFisher ),
fBetw ( 0 ),
fWith ( 0 ),
fCov ( 0 ),
fSumOfWeightsS( 0 ),
fSumOfWeightsB( 0 ),
fDiscrimPow ( 0 ),
fFisherCoeff ( 0 ),
fF0 ( 0 )
{
// standard constructor for the "Fisher"
}
//_______________________________________________________________________
TMVA::MethodFisher::MethodFisher( DataSetInfo& dsi,
const TString& theWeightFile,
TDirectory* theTargetDir ) :
MethodBase( Types::kFisher, dsi, theWeightFile, theTargetDir ),
fMeanMatx ( 0 ),
fTheMethod ( "Fisher" ),
fFisherMethod ( kFisher ),
fBetw ( 0 ),
fWith ( 0 ),
fCov ( 0 ),
fSumOfWeightsS( 0 ),
fSumOfWeightsB( 0 ),
fDiscrimPow ( 0 ),
fFisherCoeff ( 0 ),
fF0 ( 0 )
{
// constructor from weight file
}
//_______________________________________________________________________
void TMVA::MethodFisher::Init( void )
{
// default initialization called by all constructors
// allocate Fisher coefficients
fFisherCoeff = new std::vector( GetNvar() );
// the minimum requirement to declare an event signal-like
SetSignalReferenceCut( 0.0 );
// this is the preparation for training
InitMatrices();
}
//_______________________________________________________________________
void TMVA::MethodFisher::DeclareOptions()
{
//
// MethodFisher options:
// format and syntax of option string: "type"
// where type is "Fisher" or "Mahalanobis"
//
DeclareOptionRef( fTheMethod = "Fisher", "Method", "Discrimination method" );
AddPreDefVal(TString("Fisher"));
AddPreDefVal(TString("Mahalanobis"));
}
//_______________________________________________________________________
void TMVA::MethodFisher::ProcessOptions()
{
// process user options
if (fTheMethod == "Fisher" ) fFisherMethod = kFisher;
else fFisherMethod = kMahalanobis;
// this is the preparation for training
InitMatrices();
}
//_______________________________________________________________________
TMVA::MethodFisher::~MethodFisher( void )
{
// destructor
if (fBetw ) { delete fBetw; fBetw = 0; }
if (fWith ) { delete fWith; fWith = 0; }
if (fCov ) { delete fCov; fCov = 0; }
if (fDiscrimPow ) { delete fDiscrimPow; fDiscrimPow = 0; }
if (fFisherCoeff) { delete fFisherCoeff; fFisherCoeff = 0; }
}
//_______________________________________________________________________
Bool_t TMVA::MethodFisher::HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t /*numberTargets*/ )
{
// Fisher can only handle classification with 2 classes
if (type == Types::kClassification && numberClasses == 2) return kTRUE;
return kFALSE;
}
//_______________________________________________________________________
void TMVA::MethodFisher::Train( void )
{
// computation of Fisher coefficients by series of matrix operations
// get mean value of each variables for signal, backgd and signal+backgd
GetMean();
// get the matrix of covariance 'within class'
GetCov_WithinClass();
// get the matrix of covariance 'between class'
GetCov_BetweenClass();
// get the matrix of covariance 'between class'
GetCov_Full();
//--------------------------------------------------------------
// get the Fisher coefficients
GetFisherCoeff();
// get the discriminating power of each variables
GetDiscrimPower();
// nice output
PrintCoefficients();
}
//_______________________________________________________________________
Double_t TMVA::MethodFisher::GetMvaValue( Double_t* err, Double_t* errUpper )
{
// returns the Fisher value (no fixed range)
const Event * ev = GetEvent();
Double_t result = fF0;
for (UInt_t ivar=0; ivarGetValue(ivar);
// cannot determine error
NoErrorCalc(err, errUpper);
return result;
}
//_______________________________________________________________________
void TMVA::MethodFisher::InitMatrices( void )
{
// initializaton method; creates global matrices and vectors
// average value of each variables for S, B, S+B
fMeanMatx = new TMatrixD( GetNvar(), 3 );
// the covariance 'within class' and 'between class' matrices
fBetw = new TMatrixD( GetNvar(), GetNvar() );
fWith = new TMatrixD( GetNvar(), GetNvar() );
fCov = new TMatrixD( GetNvar(), GetNvar() );
// discriminating power
fDiscrimPow = new std::vector( GetNvar() );
}
//_______________________________________________________________________
void TMVA::MethodFisher::GetMean( void )
{
// compute mean values of variables in each sample, and the overall means
// initialize internal sum-of-weights variables
fSumOfWeightsS = 0;
fSumOfWeightsB = 0;
const UInt_t nvar = DataInfo().GetNVariables();
// init vectors
Double_t* sumS = new Double_t[nvar];
Double_t* sumB = new Double_t[nvar];
for (UInt_t ivar=0; ivarGetNEvents(); ievt++) {
// read the Training Event into "event"
const Event * ev = GetEvent(ievt);
// sum of weights
Double_t weight = ev->GetWeight();
if (DataInfo().IsSignal(ev)) fSumOfWeightsS += weight;
else fSumOfWeightsB += weight;
Double_t* sum = DataInfo().IsSignal(ev) ? sumS : sumB;
for (UInt_t ivar=0; ivarGetValue( ivar )*weight;
}
for (UInt_t ivar=0; ivarPrint();
delete [] sumS;
delete [] sumB;
}
//_______________________________________________________________________
void TMVA::MethodFisher::GetCov_WithinClass( void )
{
// the matrix of covariance 'within class' reflects the dispersion of the
// events relative to the center of gravity of their own class
// assert required
assert( fSumOfWeightsS > 0 && fSumOfWeightsB > 0 );
// product matrices (x-)(y-) where x;y are variables
// init
const Int_t nvar = GetNvar();
const Int_t nvar2 = nvar*nvar;
Double_t *sumSig = new Double_t[nvar2];
Double_t *sumBgd = new Double_t[nvar2];
Double_t *xval = new Double_t[nvar];
memset(sumSig,0,nvar2*sizeof(Double_t));
memset(sumBgd,0,nvar2*sizeof(Double_t));
// 'within class' covariance
for (Int_t ievt=0; ievtGetNEvents(); ievt++) {
// read the Training Event into "event"
const Event* ev = GetEvent(ievt);
Double_t weight = ev->GetWeight(); // may ignore events with negative weights
for (Int_t x=0; xGetValue( x );
Int_t k=0;
for (Int_t x=0; x 0 && fSumOfWeightsB > 0);
Double_t prodSig, prodBgd;
for (UInt_t x=0; x 0 && fSumOfWeightsB > 0);
// invert covariance matrix
TMatrixD* theMat = 0;
switch (GetFisherMethod()) {
case kFisher:
theMat = fWith;
break;
case kMahalanobis:
theMat = fCov;
break;
default:
Log() << kFATAL << " undefined method" << GetFisherMethod() << Endl;
}
TMatrixD invCov( *theMat );
if ( TMath::Abs(invCov.Determinant()) < 10E-24 ) {
Log() << kWARNING << " matrix is almost singular with deterninant="
<< TMath::Abs(invCov.Determinant())
<< " did you use the variables that are linear combinations or highly correlated?"
<< Endl;
}
if ( TMath::Abs(invCov.Determinant()) < 10E-120 ) {
theMat->Print();
Log() << kFATAL << " matrix is singular with determinant="
<< TMath::Abs(invCov.Determinant())
<< " did you use the variables that are linear combinations? \n"
<< " do you any clue as to what went wrong in above printout of the covariance matrix? "
<< Endl;
}
invCov.Invert();
// apply rescaling factor
Double_t xfact = TMath::Sqrt( fSumOfWeightsS*fSumOfWeightsB ) / (fSumOfWeightsS + fSumOfWeightsB);
// compute difference of mean values
std::vector diffMeans( GetNvar() );
UInt_t ivar, jvar;
for (ivar=0; ivarAddRank( Rank( GetInputLabel(ivar), (*fDiscrimPow)[ivar] ) );
}
return fRanking;
}
//_______________________________________________________________________
void TMVA::MethodFisher::PrintCoefficients( void )
{
// display Fisher coefficients and discriminating power for each variable
// check maximum length of variable name
Log() << kINFO << "Results for Fisher coefficients:" << Endl;
if (GetTransformationHandler().GetTransformationList().GetSize() != 0) {
Log() << kINFO << "NOTE: The coefficients must be applied to TRANFORMED variables" << Endl;
Log() << kINFO << " List of the transformation: " << Endl;
TListIter trIt(&GetTransformationHandler().GetTransformationList());
while (VariableTransformBase *trf = (VariableTransformBase*) trIt()) {
Log() << kINFO << " -- " << trf->GetName() << Endl;
}
}
std::vector vars;
std::vector coeffs;
for (UInt_t ivar=0; ivar maxL) maxL = GetInputLabel(ivar).Length();
// Print normalisation expression (see Tools.cxx): "2*(x - xmin)/(xmax - xmin) - 1.0"
for (UInt_t ivar=0; ivar 0 ? " - " : " + ")
<< std::setw(6) << TMath::Abs(GetXmin(ivar)) << std::setw(3) << ")/"
<< std::setw(6) << (GetXmax(ivar) - GetXmin(ivar) )
<< std::setw(3) << " - 1"
<< Endl;
}
Log() << kINFO << "The TMVA Reader will properly account for this normalisation, but if the" << Endl;
Log() << kINFO << "Fisher classifier is applied outside the Reader, the transformation must be" << Endl;
Log() << kINFO << "implemented -- or the \"Normalise\" option is removed and Fisher retrained." << Endl;
Log() << kINFO << Endl;
}
}
//_______________________________________________________________________
void TMVA::MethodFisher::ReadWeightsFromStream( std::istream& istr )
{
// read Fisher coefficients from weight file
istr >> fF0;
for (UInt_t ivar=0; ivar> (*fFisherCoeff)[ivar];
}
//_______________________________________________________________________
void TMVA::MethodFisher::AddWeightsXMLTo( void* parent ) const
{
// create XML description of Fisher classifier
void* wght = gTools().AddChild(parent, "Weights");
gTools().AddAttr( wght, "NCoeff", GetNvar()+1 );
void* coeffxml = gTools().AddChild(wght, "Coefficient");
gTools().AddAttr( coeffxml, "Index", 0 );
gTools().AddAttr( coeffxml, "Value", fF0 );
for (UInt_t ivar=0; ivarresize(ncoeff-1);
void* ch = gTools().GetChild(wghtnode);
Double_t coeff;
while (ch) {
gTools().ReadAttr( ch, "Index", coeffidx );
gTools().ReadAttr( ch, "Value", coeff );
if (coeffidx==0) fF0 = coeff;
else (*fFisherCoeff)[coeffidx-1] = coeff;
ch = gTools().GetNextChild(ch);
}
}
//_______________________________________________________________________
void TMVA::MethodFisher::MakeClassSpecific( std::ostream& fout, const TString& className ) const
{
// write Fisher-specific classifier response
Int_t dp = fout.precision();
fout << " double fFisher0;" << std::endl;
fout << " std::vector fFisherCoefficients;" << std::endl;
fout << "};" << std::endl;
fout << "" << std::endl;
fout << "inline void " << className << "::Initialize() " << std::endl;
fout << "{" << std::endl;
fout << " fFisher0 = " << std::setprecision(12) << fF0 << ";" << std::endl;
for (UInt_t ivar=0; ivar& inputValues ) const" << std::endl;
fout << "{" << std::endl;
fout << " double retval = fFisher0;" << std::endl;
fout << " for (size_t ivar = 0; ivar < fNvars; ivar++) {" << std::endl;
fout << " retval += fFisherCoefficients[ivar]*inputValues[ivar];" << std::endl;
fout << " }" << std::endl;
fout << std::endl;
fout << " return retval;" << std::endl;
fout << "}" << std::endl;
fout << std::endl;
fout << "// Clean up" << std::endl;
fout << "inline void " << className << "::Clear() " << std::endl;
fout << "{" << std::endl;
fout << " // clear coefficients" << std::endl;
fout << " fFisherCoefficients.clear(); " << std::endl;
fout << "}" << std::endl;
fout << std::setprecision(dp);
}
//_______________________________________________________________________
void TMVA::MethodFisher::GetHelpMessage() const
{
// get help message text
//
// typical length of text line:
// "|--------------------------------------------------------------|"
Log() << Endl;
Log() << gTools().Color("bold") << "--- Short description:" << gTools().Color("reset") << Endl;
Log() << Endl;
Log() << "Fisher discriminants select events by distinguishing the mean " << Endl;
Log() << "values of the signal and background distributions in a trans- " << Endl;
Log() << "formed variable space where linear correlations are removed." << Endl;
Log() << Endl;
Log() << " (More precisely: the \"linear discriminator\" determines" << Endl;
Log() << " an axis in the (correlated) hyperspace of the input " << Endl;
Log() << " variables such that, when projecting the output classes " << Endl;
Log() << " (signal and background) upon this axis, they are pushed " << Endl;
Log() << " as far as possible away from each other, while events" << Endl;
Log() << " of a same class are confined in a close vicinity. The " << Endl;
Log() << " linearity property of this classifier is reflected in the " << Endl;
Log() << " metric with which \"far apart\" and \"close vicinity\" are " << Endl;
Log() << " determined: the covariance matrix of the discriminating" << Endl;
Log() << " variable space.)" << Endl;
Log() << Endl;
Log() << gTools().Color("bold") << "--- Performance optimisation:" << gTools().Color("reset") << Endl;
Log() << Endl;
Log() << "Optimal performance for Fisher discriminants is obtained for " << Endl;
Log() << "linearly correlated Gaussian-distributed variables. Any deviation" << Endl;
Log() << "from this ideal reduces the achievable separation power. In " << Endl;
Log() << "particular, no discrimination at all is achieved for a variable" << Endl;
Log() << "that has the same sample mean for signal and background, even if " << Endl;
Log() << "the shapes of the distributions are very different. Thus, Fisher " << Endl;
Log() << "discriminants often benefit from suitable transformations of the " << Endl;
Log() << "input variables. For example, if a variable x in [-1,1] has a " << Endl;
Log() << "a parabolic signal distributions, and a uniform background" << Endl;
Log() << "distributions, their mean value is zero in both cases, leading " << Endl;
Log() << "to no separation. The simple transformation x -> |x| renders this " << Endl;
Log() << "variable powerful for the use in a Fisher discriminant." << Endl;
Log() << Endl;
Log() << gTools().Color("bold") << "--- Performance tuning via configuration options:" << gTools().Color("reset") << Endl;
Log() << Endl;
Log() << "" << Endl;
}