115 fInvHMatrixS =
new TMatrixD( GetNvar(), GetNvar() );
116 fInvHMatrixB =
new TMatrixD( GetNvar(), GetNvar() );
117 fVecMeanS =
new TVectorD( GetNvar() );
118 fVecMeanB =
new TVectorD( GetNvar() );
121 SetSignalReferenceCut( 0.0 );
129 if (NULL != fInvHMatrixS)
delete fInvHMatrixS;
130 if (NULL != fInvHMatrixB)
delete fInvHMatrixB;
131 if (NULL != fVecMeanS )
delete fVecMeanS;
132 if (NULL != fVecMeanB )
delete fVecMeanB;
165 ComputeCovariance(
kTRUE, fInvHMatrixS );
166 ComputeCovariance(
kFALSE, fInvHMatrixB );
169 if (
TMath::Abs(fInvHMatrixS->Determinant()) < 10
E-24) {
170 Log() << kWARNING <<
"<Train> H-matrix S is almost singular with determinant= "
172 <<
" did you use the variables that are linear combinations or highly correlated ???"
175 if (
TMath::Abs(fInvHMatrixB->Determinant()) < 10
E-24) {
176 Log() << kWARNING <<
"<Train> H-matrix B is almost singular with determinant= "
178 <<
" did you use the variables that are linear combinations or highly correlated ???"
182 if (
TMath::Abs(fInvHMatrixS->Determinant()) < 10
E-120) {
183 Log() << kFATAL <<
"<Train> H-matrix S is singular with determinant= "
185 <<
" did you use the variables that are linear combinations ???"
188 if (
TMath::Abs(fInvHMatrixB->Determinant()) < 10
E-120) {
189 Log() << kFATAL <<
"<Train> H-matrix B is singular with determinant= "
191 <<
" did you use the variables that are linear combinations ???"
196 fInvHMatrixS->Invert();
197 fInvHMatrixB->Invert();
208 const UInt_t nvar = DataInfo().GetNVariables();
213 TMatrixD mat2(nvar, nvar); mat2 *= 0;
220 for (
Int_t i=0, iEnd=Data()->GetNEvents(); i<iEnd; ++i) {
223 const Event* origEvt = Data()->GetEvent(i);
227 if (IgnoreEventsWithNegWeightsInTraining() && weight <= 0)
continue;
229 if (DataInfo().IsSignal(origEvt) != isSignal)
continue;
232 GetTransformationHandler().SetTransformationReferenceClass( origEvt->
GetClass() );
233 const Event* ev = GetTransformationHandler().Transform( origEvt );
236 sumOfWeights += weight;
239 for (ivar=0; ivar<nvar; ivar++) xval[ivar] = ev->
GetValue(ivar);
242 for (ivar=0; ivar<nvar; ivar++) {
244 vec(ivar) += xval[ivar]*weight;
245 mat2(ivar, ivar) += (xval[ivar]*xval[ivar])*weight;
247 for (jvar=ivar+1; jvar<nvar; jvar++) {
248 mat2(ivar, jvar) += (xval[ivar]*xval[jvar])*weight;
249 mat2(jvar, ivar) = mat2(ivar, jvar);
255 for (ivar=0; ivar<nvar; ivar++) {
257 if (isSignal) (*fVecMeanS)(ivar) = vec(ivar)/sumOfWeights;
258 else (*fVecMeanB)(ivar) = vec(ivar)/sumOfWeights;
260 for (jvar=0; jvar<nvar; jvar++) {
261 (*mat)(ivar, jvar) = mat2(ivar, jvar)/sumOfWeights - vec(ivar)*vec(jvar)/(sumOfWeights*sumOfWeights);
276 if (
s+
b < 0)
Log() << kFATAL <<
"big trouble: s+b: " <<
s+
b <<
Endl;
279 NoErrorCalc(err, errUpper);
281 return (
b -
s)/(
s +
b);
291 const Event* origEvt = fTmpEvent ? fTmpEvent:Data()->GetEvent();
294 UInt_t ivar(0), jvar(0), nvar(GetNvar());
295 std::vector<Double_t> val( nvar );
299 GetTransformationHandler().SetTransformationReferenceClass( fSignalClass );
301 GetTransformationHandler().SetTransformationReferenceClass( fBackgroundClass );
303 const Event* ev = GetTransformationHandler().Transform( origEvt );
305 for (ivar=0; ivar<nvar; ivar++) val[ivar] = ev->
GetValue( ivar );
308 for (ivar=0; ivar<nvar; ivar++) {
309 for (jvar=0; jvar<nvar; jvar++) {
311 chi2 += ( (val[ivar] - (*fVecMeanS)(ivar))*(val[jvar] - (*fVecMeanS)(jvar))
312 * (*fInvHMatrixS)(ivar,jvar) );
314 chi2 += ( (val[ivar] - (*fVecMeanB)(ivar))*(val[jvar] - (*fVecMeanB)(jvar))
315 * (*fInvHMatrixB)(ivar,jvar) );
320 if (chi2 < 0)
Log() << kFATAL <<
"<GetChi2> negative chi2: " << chi2 <<
Endl;
365 for (ivar=0; ivar<GetNvar(); ivar++)
366 istr >> (*fVecMeanS)(ivar) >> (*fVecMeanB)(ivar);
369 for (ivar=0; ivar<GetNvar(); ivar++)
370 for (jvar=0; jvar<GetNvar(); jvar++)
371 istr >> (*fInvHMatrixS)(ivar,jvar);
374 for (ivar=0; ivar<GetNvar(); ivar++)
375 for (jvar=0; jvar<GetNvar(); jvar++)
376 istr >> (*fInvHMatrixB)(ivar,jvar);
384 fout <<
" // arrays of input evt vs. variable " << std::endl;
385 fout <<
" double fInvHMatrixS[" << GetNvar() <<
"][" << GetNvar() <<
"]; // inverse H-matrix (signal)" << std::endl;
386 fout <<
" double fInvHMatrixB[" << GetNvar() <<
"][" << GetNvar() <<
"]; // inverse H-matrix (background)" << std::endl;
387 fout <<
" double fVecMeanS[" << GetNvar() <<
"]; // vector of mean values (signal)" << std::endl;
388 fout <<
" double fVecMeanB[" << GetNvar() <<
"]; // vector of mean values (background)" << std::endl;
389 fout <<
" " << std::endl;
390 fout <<
" double GetChi2( const std::vector<double>& inputValues, int type ) const;" << std::endl;
391 fout <<
"};" << std::endl;
392 fout <<
" " << std::endl;
393 fout <<
"void " << className <<
"::Initialize() " << std::endl;
394 fout <<
"{" << std::endl;
395 fout <<
" // init vectors with mean values" << std::endl;
396 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
397 fout <<
" fVecMeanS[" << ivar <<
"] = " << (*fVecMeanS)(ivar) <<
";" << std::endl;
398 fout <<
" fVecMeanB[" << ivar <<
"] = " << (*fVecMeanB)(ivar) <<
";" << std::endl;
400 fout <<
" " << std::endl;
401 fout <<
" // init H-matrices" << std::endl;
402 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
403 for (
UInt_t jvar=0; jvar<GetNvar(); jvar++) {
404 fout <<
" fInvHMatrixS[" << ivar <<
"][" << jvar <<
"] = "
405 << (*fInvHMatrixS)(ivar,jvar) <<
";" << std::endl;
406 fout <<
" fInvHMatrixB[" << ivar <<
"][" << jvar <<
"] = "
407 << (*fInvHMatrixB)(ivar,jvar) <<
";" << std::endl;
410 fout <<
"}" << std::endl;
411 fout <<
" " << std::endl;
412 fout <<
"inline double " << className <<
"::GetMvaValue__( const std::vector<double>& inputValues ) const" << std::endl;
413 fout <<
"{" << std::endl;
414 fout <<
" // returns the H-matrix signal estimator" << std::endl;
415 fout <<
" std::vector<double> inputValuesSig = inputValues;" << std::endl;
416 fout <<
" std::vector<double> inputValuesBgd = inputValues;" << std::endl;
417 if (GetTransformationHandler().GetTransformationList().GetSize() != 0) {
419 UInt_t signalClass =DataInfo().GetClassInfo(
"Signal")->GetNumber();
420 UInt_t backgroundClass=DataInfo().GetClassInfo(
"Background")->GetNumber();
422 fout <<
" Transform(inputValuesSig," << signalClass <<
");" << std::endl;
423 fout <<
" Transform(inputValuesBgd," << backgroundClass <<
");" << std::endl;
428 fout <<
" double s = GetChi2( inputValuesSig, " <<
Types::kSignal <<
" );" << std::endl;
429 fout <<
" double b = GetChi2( inputValuesBgd, " <<
Types::kBackground <<
" );" << std::endl;
433 fout <<
" " << std::endl;
434 fout <<
" if (s+b <= 0) std::cout << \"Problem in class " << className <<
"::GetMvaValue__: s+b = \"" << std::endl;
435 fout <<
" << s+b << \" <= 0 \" << std::endl;" << std::endl;
436 fout <<
" " << std::endl;
437 fout <<
" return (b - s)/(s + b);" << std::endl;
438 fout <<
"}" << std::endl;
439 fout <<
" " << std::endl;
440 fout <<
"inline double " << className <<
"::GetChi2( const std::vector<double>& inputValues, int type ) const" << std::endl;
441 fout <<
"{" << std::endl;
442 fout <<
" // compute chi2-estimator for event according to type (signal/background)" << std::endl;
443 fout <<
" " << std::endl;
444 fout <<
" size_t ivar,jvar;" << std::endl;
445 fout <<
" double chi2 = 0;" << std::endl;
446 fout <<
" for (ivar=0; ivar<GetNvar(); ivar++) {" << std::endl;
447 fout <<
" for (jvar=0; jvar<GetNvar(); jvar++) {" << std::endl;
449 fout <<
" chi2 += ( (inputValues[ivar] - fVecMeanS[ivar])*(inputValues[jvar] - fVecMeanS[jvar])" << std::endl;
450 fout <<
" * fInvHMatrixS[ivar][jvar] );" << std::endl;
451 fout <<
" else" << std::endl;
452 fout <<
" chi2 += ( (inputValues[ivar] - fVecMeanB[ivar])*(inputValues[jvar] - fVecMeanB[jvar])" << std::endl;
453 fout <<
" * fInvHMatrixB[ivar][jvar] );" << std::endl;
454 fout <<
" }" << std::endl;
455 fout <<
" } // loop over variables " << std::endl;
456 fout <<
" " << std::endl;
457 fout <<
" // sanity check" << std::endl;
458 fout <<
" if (chi2 < 0) std::cout << \"Problem in class " << className <<
"::GetChi2: chi2 = \"" << std::endl;
459 fout <<
" << chi2 << \" < 0 \" << std::endl;" << std::endl;
460 fout <<
" " << std::endl;
461 fout <<
" return chi2;" << std::endl;
462 fout <<
"}" << std::endl;
463 fout <<
" " << std::endl;
464 fout <<
"// Clean up" << std::endl;
465 fout <<
"inline void " << className <<
"::Clear() " << std::endl;
466 fout <<
"{" << std::endl;
467 fout <<
" // nothing to clear" << std::endl;
468 fout <<
"}" << std::endl;
482 Log() <<
"The H-Matrix classifier discriminates one class (signal) of a feature" <<
Endl;
483 Log() <<
"vector from another (background). The correlated elements of the" <<
Endl;
484 Log() <<
"vector are assumed to be Gaussian distributed, and the inverse of" <<
Endl;
485 Log() <<
"the covariance matrix is the H-Matrix. A multivariate chi-squared" <<
Endl;
486 Log() <<
"estimator is built that exploits differences in the mean values of" <<
Endl;
487 Log() <<
"the vector elements between the two classes for the purpose of" <<
Endl;
488 Log() <<
"discrimination." <<
Endl;
492 Log() <<
"The TMVA implementation of the H-Matrix classifier has been shown" <<
Endl;
493 Log() <<
"to underperform in comparison with the corresponding Fisher discriminant," <<
Endl;
494 Log() <<
"when using similar assumptions and complexity. Its use is therefore" <<
Endl;
495 Log() <<
"depreciated. Only in cases where the background model is strongly" <<
Endl;
496 Log() <<
"non-Gaussian, H-Matrix may perform better than Fisher. In such" <<
Endl;
497 Log() <<
"occurrences the user is advised to employ non-linear classifiers. " <<
Endl;
#define REGISTER_METHOD(CLASS)
for example
static RooMathCoreReg dummy
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
Class that contains all the data information.
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
Virtual base Class for all MVA method.
H-Matrix method, which is implemented as a simple comparison of chi-squared estimators for signal and...
virtual ~MethodHMatrix()
destructor
MethodHMatrix(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="")
standard constructor for the H-Matrix method
void ComputeCovariance(Bool_t, TMatrixD *)
compute covariance matrix
void DeclareOptions()
MethodHMatrix options: none (apart from those implemented in MethodBase)
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
FDA can handle classification with 2 classes and regression with one regression-target.
Double_t GetChi2(Types::ESBType)
compute chi2-estimator for event according to type (signal/background)
void ProcessOptions()
process user options
void MakeClassSpecific(std::ostream &, const TString &) const
write Fisher-specific classifier response
void Init()
default initialization called by all constructors
void GetHelpMessage() const
get help message text
void ReadWeightsFromXML(void *wghtnode)
read weights from XML file
void Train()
computes H-matrices for signal and background samples
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
returns the H-matrix signal estimator
void ReadWeightsFromStream(std::istream &istr)
read variable names and min/max NOTE: the latter values are mandatory for the normalisation in the re...
void AddWeightsXMLTo(void *parent) const
create XML description for HMatrix classification
Singleton class for Global types used by TMVA.
static constexpr double s
Abstract ClassifierFactory template that handles arbitrary types.
MsgLogger & Endl(MsgLogger &ml)
constexpr Double_t E()
Base of natural log: