46 : fTolerence(tolerence),
51 fLogger( new
MsgLogger(
"LDA", (debug?kINFO:kDEBUG)) )
68 Log() << kDEBUG <<
"There are: " << inputSignalEvents.size() <<
" input signal events " <<
Endl;
69 Log() << kDEBUG <<
"There are: " << inputBackgroundEvents.size() <<
" input background events " <<
Endl;
71 fNumParams = inputSignalEvents[0].size();
73 UInt_t numSignalEvents = inputSignalEvents.size();
74 UInt_t numBackEvents = inputBackgroundEvents.size();
75 UInt_t numTotalEvents = numSignalEvents + numBackEvents;
76 fEventFraction[0] = (
Float_t)numBackEvents/numTotalEvents;
77 fEventFraction[1] = (
Float_t)numSignalEvents/numTotalEvents;
81 std::vector<Float_t> m_muSignal (fNumParams,0.0);
82 std::vector<Float_t> m_muBackground (fNumParams,0.0);
83 for (
UInt_t param=0; param < fNumParams; ++param) {
84 for (
UInt_t eventNumber=0; eventNumber < numSignalEvents; ++eventNumber)
85 m_muSignal[param] += inputSignalEvents[eventNumber][param];
86 for (
UInt_t eventNumber=0; eventNumber < numBackEvents; ++eventNumber)
87 m_muBackground[param] += inputBackgroundEvents[eventNumber][param]/numBackEvents;
88 if (numSignalEvents > 0) m_muSignal[param] /= numSignalEvents;
89 if (numBackEvents > 0 ) m_muBackground[param] /= numBackEvents;
91 fMu[0] = m_muBackground;
95 Log() << kDEBUG <<
"the signal means" <<
Endl;
96 for (
UInt_t param=0; param < fNumParams; ++param)
97 Log() << kDEBUG << m_muSignal[param] <<
Endl;
98 Log() << kDEBUG <<
"the background means" <<
Endl;
99 for (
UInt_t param=0; param < inputBackgroundEvents[0].size(); ++param)
100 Log() << kDEBUG << m_muBackground[param] <<
Endl;
107 TMatrixF sigmaSignal(fNumParams, fNumParams);
108 TMatrixF sigmaBack(fNumParams, fNumParams);
109 if (fSigma!=0)
delete fSigma;
110 fSigma =
new TMatrixF(fNumParams, fNumParams);
111 for (
UInt_t row=0; row < fNumParams; ++row) {
112 for (
UInt_t col=0; col < fNumParams; ++col) {
113 sigmaSignal[row][col] = 0;
114 sigmaBack[row][col] = 0;
115 (*fSigma)[row][col] = 0;
119 for (
UInt_t eventNumber=0; eventNumber < numSignalEvents; ++eventNumber) {
120 for (
UInt_t row=0; row < fNumParams; ++row) {
121 for (
UInt_t col=0; col < fNumParams; ++col) {
122 sigmaSignal[row][col] += (inputSignalEvents[eventNumber][row] - m_muSignal[row]) * (inputSignalEvents[eventNumber][col] - m_muSignal[col] );
127 for (
UInt_t eventNumber=0; eventNumber < numBackEvents; ++eventNumber) {
128 for (
UInt_t row=0; row < fNumParams; ++row) {
129 for (
UInt_t col=0; col < fNumParams; ++col) {
130 sigmaBack[row][col] += (inputBackgroundEvents[eventNumber][row] - m_muBackground[row]) * (inputBackgroundEvents[eventNumber][col] - m_muBackground[col] );
136 *fSigma = sigmaBack + sigmaSignal;
137 *fSigma *= 1.0/(numTotalEvents - K);
140 Log() <<
"after filling sigmaSignal" <<
Endl;
142 Log() <<
"after filling sigmaBack" <<
Endl;
144 Log() <<
"after filling total Sigma" <<
Endl;
150 TMatrixF diag ( fNumParams, fNumParams );
151 TMatrixF uTrans( fNumParams, fNumParams );
152 TMatrixF vTrans( fNumParams, fNumParams );
154 for (
UInt_t i = 0; i< fNumParams; ++i) {
155 if (solutionSVD.
GetSig()[i] > fTolerence)
156 diag(i,i) = solutionSVD.
GetSig()[i];
158 diag(i,i) = fTolerence;
162 Log() <<
"the diagonal" <<
Endl;
166 decomposed = solutionSVD.
GetV();
168 decomposed *= solutionSVD.
GetU();
171 Log() <<
"the decomposition " <<
Endl;
176 *fSigmaInverse /= diag;
180 Log() <<
"the SigmaInverse " <<
Endl;
181 fSigmaInverse->
Print();
183 Log() <<
"the real " <<
Endl;
188 for (
UInt_t i =0; i< fNumParams; ++i) {
189 for (
UInt_t j =0; j< fNumParams; ++j) {
191 Log() <<
"problem, i= "<< i <<
" j= " << j <<
Endl;
192 Log() <<
"Sigma(i,j)= "<< (*fSigma)(i,j) <<
" SigmaInverse(i,j)= " << (*fSigmaInverse)(i,j) <<
Endl;
198 if (problem) Log() << kWARNING <<
"Problem with the inversion!" <<
Endl;
212 std::vector<Float_t> m_transPoseTimesSigmaInverse;
214 for (
UInt_t j=0; j < fNumParams; ++j) {
216 for (
UInt_t i=0; i < fNumParams; ++i) {
217 m_temp += (
x[i] - fMu[k][i]) * (*fSigmaInverse)(j,i);
219 m_transPoseTimesSigmaInverse.push_back(m_temp);
223 for (
UInt_t i=0; i< fNumParams; ++i) {
224 exponent += m_transPoseTimesSigmaInverse[i]*(
x[i] - fMu[k][i]);
239 Float_t m_numerator = FSub(
x,k)*fEventFraction[k];
240 Float_t m_denominator = FSub(
x,0)*fEventFraction[0]+FSub(
x,1)*fEventFraction[1];
242 return m_numerator/m_denominator;
std::vector< std::vector< Float_t > > LDAEvents
TMatrixT< Float_t > TMatrixF
Single Value Decomposition class.
const TVectorD & GetSig()
Bool_t Decompose() override
SVD decomposition of matrix If the decomposition succeeds, bit kDecomposed is set ,...
LDA(Float_t tolerence=1.0e-5, Bool_t debug=false)
constructor
void Initialize(const LDAEvents &inputSignal, const LDAEvents &inputBackground)
Create LDA matrix using local events found by knn method.
Float_t GetLogLikelihood(const std::vector< Float_t > &x, Int_t k)
Log likelihood function with Gaussian approximation.
Float_t GetProb(const std::vector< Float_t > &x, Int_t k)
Signal probability with Gaussian approximation.
Float_t FSub(const std::vector< Float_t > &x, Int_t k)
Probability value using Gaussian approximation.
ostringstream derivative to redirect and format output
void Print(Option_t *name="") const override
Print the matrix as a table of elements.
TMatrixT< Element > & Transpose(const TMatrixT< Element > &source)
Transpose matrix source.
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
MsgLogger & Endl(MsgLogger &ml)
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
constexpr Double_t TwoPi()