32 #ifndef ROOT_TDecompSVD
42 #ifndef ROOT_TMVA_Types
45 #ifndef ROOT_TMVA_MsgLogger
53 : fTolerence(tolerence),
75 Log() <<
kDEBUG <<
"There are: " << inputSignalEvents.size() <<
" input signal events " <<
Endl;
76 Log() <<
kDEBUG <<
"There are: " << inputBackgroundEvents.size() <<
" input background events " <<
Endl;
78 fNumParams = inputSignalEvents[0].size();
80 UInt_t numSignalEvents = inputSignalEvents.size();
81 UInt_t numBackEvents = inputBackgroundEvents.size();
82 UInt_t numTotalEvents = numSignalEvents + numBackEvents;
83 fEventFraction[0] = (
Float_t)numBackEvents/numTotalEvents;
84 fEventFraction[1] = (
Float_t)numSignalEvents/numTotalEvents;
88 std::vector<Float_t> m_muSignal (fNumParams,0.0);
89 std::vector<Float_t> m_muBackground (fNumParams,0.0);
90 for (
UInt_t param=0; param < fNumParams; ++param) {
91 for (
UInt_t eventNumber=0; eventNumber < numSignalEvents; ++eventNumber)
92 m_muSignal[param] += inputSignalEvents[eventNumber][param];
93 for (
UInt_t eventNumber=0; eventNumber < numBackEvents; ++eventNumber)
94 m_muBackground[param] += inputBackgroundEvents[eventNumber][param]/numBackEvents;
95 if (numSignalEvents > 0) m_muSignal[param] /= numSignalEvents;
96 if (numBackEvents > 0 ) m_muBackground[param] /= numBackEvents;
98 fMu[0] = m_muBackground;
103 for (
UInt_t param=0; param < fNumParams; ++param)
106 for (
UInt_t param=0; param < inputBackgroundEvents[0].size(); ++param)
114 TMatrixF sigmaSignal(fNumParams, fNumParams);
115 TMatrixF sigmaBack(fNumParams, fNumParams);
116 if (fSigma!=0)
delete fSigma;
117 fSigma =
new TMatrixF(fNumParams, fNumParams);
118 for (
UInt_t row=0; row < fNumParams; ++row) {
119 for (
UInt_t col=0; col < fNumParams; ++col) {
120 sigmaSignal[row][col] = 0;
121 sigmaBack[row][col] = 0;
122 (*fSigma)[row][col] = 0;
126 for (
UInt_t eventNumber=0; eventNumber < numSignalEvents; ++eventNumber) {
127 for (
UInt_t row=0; row < fNumParams; ++row) {
128 for (
UInt_t col=0; col < fNumParams; ++col) {
129 sigmaSignal[row][col] += (inputSignalEvents[eventNumber][row] - m_muSignal[row]) * (inputSignalEvents[eventNumber][col] - m_muSignal[col] );
134 for (
UInt_t eventNumber=0; eventNumber < numBackEvents; ++eventNumber) {
135 for (
UInt_t row=0; row < fNumParams; ++row) {
136 for (
UInt_t col=0; col < fNumParams; ++col) {
137 sigmaBack[row][col] += (inputBackgroundEvents[eventNumber][row] - m_muBackground[row]) * (inputBackgroundEvents[eventNumber][col] - m_muBackground[col] );
143 *fSigma = sigmaBack + sigmaSignal;
144 *fSigma *= 1.0/(numTotalEvents -
K);
147 Log() <<
"after filling sigmaSignal" <<
Endl;
149 Log() <<
"after filling sigmaBack" <<
Endl;
151 Log() <<
"after filling total Sigma" <<
Endl;
157 TMatrixF diag ( fNumParams, fNumParams );
158 TMatrixF uTrans( fNumParams, fNumParams );
159 TMatrixF vTrans( fNumParams, fNumParams );
161 for (
UInt_t i = 0; i< fNumParams; ++i) {
162 if (solutionSVD.
GetSig()[i] > fTolerence)
163 diag(i,i) = solutionSVD.
GetSig()[i];
165 diag(i,i) = fTolerence;
169 Log() <<
"the diagonal" <<
Endl;
173 decomposed = solutionSVD.
GetV();
175 decomposed *= solutionSVD.
GetU();
178 Log() <<
"the decomposition " <<
Endl;
183 *fSigmaInverse /= diag;
187 Log() <<
"the SigmaInverse " <<
Endl;
188 fSigmaInverse->
Print();
195 for (
UInt_t i =0; i< fNumParams; ++i) {
196 for (
UInt_t j =0; j< fNumParams; ++j) {
198 Log() <<
"problem, i= "<< i <<
" j= " << j <<
Endl;
199 Log() <<
"Sigma(i,j)= "<< (*fSigma)(i,j) <<
" SigmaInverse(i,j)= " << (*fSigmaInverse)(i,j) <<Endl;
205 if (problem)
Log() <<
kWARNING <<
"Problem with the inversion!" <<
Endl;
219 std::vector<Float_t> m_transPoseTimesSigmaInverse;
221 for (
UInt_t j=0; j < fNumParams; ++j) {
223 for (
UInt_t i=0; i < fNumParams; ++i) {
224 m_temp += (x[i] - fMu[k][i]) * (*fSigmaInverse)(j,i);
226 m_transPoseTimesSigmaInverse.push_back(m_temp);
230 for (
UInt_t i=0; i< fNumParams; ++i) {
231 exponent += m_transPoseTimesSigmaInverse[i]*(x[i] - fMu[k][i]);
246 Float_t m_numerator = FSub(x,k)*fEventFraction[k];
247 Float_t m_denominator = FSub(x,0)*fEventFraction[0]+FSub(x,1)*fEventFraction[1];
249 return m_numerator/m_denominator;
LDA(Float_t tolerence=1.0e-5, Bool_t debug=false)
constructor
Float_t FSub(const std::vector< Float_t > &x, Int_t k)
Probability value using Gaussian approximation.
const TVectorD & GetSig()
MsgLogger & Endl(MsgLogger &ml)
TMatrixT< Element > & Transpose(const TMatrixT< Element > &source)
Transpose matrix source.
TMatrixT< Float_t > TMatrixF
Float_t GetProb(const std::vector< Float_t > &x, Int_t k)
Signal probability with Gaussian approximation.
virtual Bool_t Decompose()
SVD decomposition of matrix If the decomposition succeeds, bit kDecomposed is set ...
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
void Print(Option_t *name="") const
Print the matrix as a table of elements.
Float_t GetLogLikelihood(const std::vector< Float_t > &x, Int_t k)
Log likelihood function with Gaussian approximation.
void Initialize(const LDAEvents &inputSignal, const LDAEvents &inputBackground)
Create LDA matrix using local events found by knn method.
Vc_INTRINSIC Vc_CONST m256 exponent(param256 v)
std::vector< std::vector< Float_t > > LDAEvents
Double_t Sqrt(Double_t x)