Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LDA.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <MathAlgo/LDA.hh>
26 #include <MathAlgo/PCA.hh>
27 #include <MathAlgo/SVD.hh>
28 #include <Base/Math/Random.hh>
29 
30 #include <Base/Common/BIASpragma.hh>
31 #include <Base/Common/FileHandling.hh>
32 
33 using namespace BIAS;
34 using namespace std;
35 
36 
37 #define REDUCTION_ZERO_THRESH 1E-8
38 
39 template<typename T>
40 void outveclda(BIAS::Vector<T> vec){
41  for (int i=0;i<vec.size();i++){
42  cout << vec[i] << " ";
43  }
44  cout << endl;
45 }
46 
47 //only for debugging:
48 void LDA::AnalyzeData(const std::vector<std::vector<BIAS::Vector<LDAType> > > &vec){
49 
50  BIASASSERT(!vec.empty());
51  BIASASSERT(!vec[0].empty());
52  BIAS::Vector<LDAType> classMean;
53 
54  int dimensions=vec[0][0].size();
55 
56  Matrix<double> classCov(dimensions, dimensions,0.0),
57  cov(dimensions, dimensions, 0.0);
58 
59  classMean.newsize(dimensions);
60 
61  double error=0,error2=0, error3=0, maxerror=0, maxerror2=0;
62 
63  int counter=0, counter2=0;
64 
65  ComputeMeans(vec);
66 
67  for (int classNumber=0;classNumber<(int)vec.size();classNumber++){
68  cov.SetZero();
69  // save offset classNumber
70  error=0;
71  const std::vector<BIAS::Vector<LDAType> > & class_i = vec[classNumber];
72  for (int classVecNumber=0; classVecNumber<(int)class_i.size();
73  classVecNumber++){
74  // save offset l
75  const BIAS::Vector<LDAType> & classVector = class_i[classVecNumber];
76  //outveclda(classVector);
77  for (int dim=0; dim<dimensions; dim++){
78  // compute kli once for all j
79  const LDAType classVectordim = classVector[dim];
80  const LDAType classMean = classMeans_[classNumber][dim];
81  for (int dim2=0;dim2<=dim;dim2++){
82  cov[dim][dim2] += (classVectordim-classMean)*(classVector[dim2]-classMeans_[classNumber][dim2]);
83  }
84  error3=fabs(classVectordim-classMean);
85  if (error3>maxerror) maxerror=error3;
86  error+=error3;
87 
88  }
89  counter2++;
90  }
91  cov/=double(class_i.size());
92  for (int i=0; i<dimensions; i++){
93  for (int j=0; j<=i; j++){
94  classCov[i][j] += cov[i][j];
95  }
96  }
97  error=error/(double)(128*vec[classNumber].size());
98  if (error>maxerror2) maxerror2=error;
99  counter++;
100  error2+=error;
101 
102  }
103 
104  for (int i=0; i<dimensions; i++){
105  for (int j=0; j<=i; j++){
106  classCov[j][i] = classCov[i][j];
107  }
108  }
109  classCov/=double(vec.size());
110 
111 
112  SVD svd(false); // no absolute zero threshold
113  Matrix<double> U;
114  Vector<double> S;
115 
116  //get sure that matrix as full rank
117  svd.Compute(classCov, REDUCTION_ZERO_THRESH);
118  S = svd.GetS();
119 
120  error2/=(double)counter;
121 
122  cout << "Trailset has Trails = " << vec.size() << endl;
123  cout << "Mean-Error = " << error2 << endl;
124  cout << "Max-Single-Error = " << maxerror << endl;
125  cout << "Max-Trail-Error = " << maxerror2 << endl;
126 
127  cout << "Eigenvalues from S_w = ";
128  outveclda(S);
129 
130 }
131 
132 void LDA::GenerateRandomTestData(int vectorSize, int numberOfClasses,
133  int classSizeMin, int classSizeMax, int vectorEntryMin, int vectorEntryMax,
134  double stdDeviation, double mainDirectionStdDeviation,
135  std::vector<std::vector<BIAS::Vector<LDAType> > > &vec,
136  std::vector<BIAS::Vector<LDAType> > &means, bool Normalize){
137 
138  int classSize;
139  double l;
140  vec.resize(numberOfClasses);
141  means.resize(numberOfClasses);
142  SVD svd(false);// no absolute zero thresh
143  Matrix<double> U(vectorSize,vectorSize,0.0),S(vectorSize,vectorSize,0.0),
144  UMain(vectorSize,vectorSize,0.0), SMain(vectorSize,vectorSize,0.0);
145  Vector<double> T(vectorSize,0.0), SVec(vectorSize,0.0);
146  Random random;
147 #ifndef BIAS_DEBUG
148  random.Reset();
149 #endif
150 
151  //generate Eigenvalues (in matrix SMain) of covariance-matrix for class-means
152  SMain.SetZero();
153  for (int k=0;k<vectorSize;k++){
154  T[k]=random.GetUniformDistributed(-1,1);
155  SMain[k][k]=fabs(random.GetNormalDistributed(0,mainDirectionStdDeviation*mainDirectionStdDeviation));
156  }
157  //cout << "Haupteigenwerte=" << SMain << endl;
158 
159  //generate eigenvectors (matrix UMain) of covariance-matrix for class-means
160  //we spend power to compute more svds so there is more random-factor
161  UMain.SetZero();
162  for (int k=0;k<vectorSize;k++){
163  T/=T.NormL2();
164  UMain.SetRow(k, T);
165  if (k==vectorSize-1) break;
166  svd.Compute(UMain);
167  for (int j=k+1;j<vectorSize;j++){
168  T+=svd.GetVT().GetRow(j)*random.GetUniformDistributed(-1,1);
169  }
170  }
171 /*
172  UMain.SetZero();
173  T/=T.NormL2();
174  UMain.SetRow(0, T);
175  svd.Compute(UMain);
176  */
177  //compute covariance-matrix (now UMain) for class-means
178  UMain=svd.GetU()*SMain*svd.GetVT();
179  //cout << "HauptKovarianzmatrix=" << UMain << endl;
180 
181  //for each class do:
182  for (int i=0;i<numberOfClasses;i++){
183  #ifdef BIAS_DEBUG
184  if ((GetDebugLevel()&1)!=0)
185  cout << i << " " << flush;
186  #endif
187  means[i].newsize(vectorSize);
188  S.SetZero();
189  //class means are generated, and eigenvalues (S) for class-covariance-matrices
190  for (int k=0;k<vectorSize;k++){
191  //Mean:
192  means[i][k] = LDAType( random.GetUniformDistributedInt(vectorEntryMin,vectorEntryMax) );
193  //Eigenvalues:
194  S[k][k]=fabs(random.GetNormalDistributed(0,stdDeviation*stdDeviation));
195  //init first eigenvector
196  T[k]=random.GetUniformDistributed(-1,1);
197  }
198 
199 
200  //Eigenvectors:
201  /*
202  U.SetZero();
203  for (int k=0;k<vectorSize;k++){
204  T/=T.NormL2();
205  U.SetRow(k, T);
206  if (k==vectorSize-1) break;
207  svd.Compute(U);
208  VT=svd.GetVT();
209  for (int j=k+1;j<vectorSize;j++){
210  T+=VT.GetRow(j);
211  }
212  }
213  */
214 
215  //cout << "Klassen-Eigenwerte=" << S << endl;
216  //generate eigenvectors for each class
217  U.SetZero();
218  T/=T.NormL2();
219  U.SetRow(0, T);
220  svd.Compute(U);
221  U=svd.GetU()*S*svd.GetVT();
222  //cout << "Klassen-Kovarianzmatrix=" << U << endl;
223  U+=UMain;
224  //cout << "Addierte Kovarianzmatrix=" << U << endl;
225  U/=2.0;
226  svd.Compute(U);
227  SVec=svd.GetS();
228  //cout << "Neue Eigenwerte=" << endl;outveclda(SVec);
229  U=svd.GetVT();
230 
231 
232 
233  //generate class-size
234  classSize=random.GetUniformDistributedInt(classSizeMin,classSizeMax);
235  vec[i].resize(classSize);
236  //cout << "S" << S << "class " << i << " mean " << means[i] << endl ;
237  for (int j=0;j<classSize;j++){
238 
239  vec[i][j]=means[i];
240  Vector<LDAType> *vec_ij=&vec[i][j];
241  //for each dimension get vectors position in class
242  for (int k=0;k<vectorSize;k++){
243  //get distance to class mean:
244  l=random.GetNormalDistributed(0, sqrt(SVec[k]));
245 
246  //cout << "class " << i << " vec " << j << " add l=" << l << " * Evec(" << k << ")" << endl;
247  //multiply distance with eigenvector:
248  for (int n=0; n<int(vectorSize); n++){
249  (*vec_ij)[n] += LDAType(l*U[k][n]);
250  }
251  }
252  for (int n=0; n<int(vectorSize); n++){
253  (*vec_ij)[n]=min(LDAType(vectorEntryMax),max(LDAType(vectorEntryMin),(*vec_ij)[n]));
254  }
255  if (Normalize) {
256  (*vec_ij) /= (*vec_ij).NormL2()*255.0;
257  }
258  //cout << "class " << i << " vec " << j << " " << vec[i][j] << endl;
259  }
260  }
261  #ifdef BIAS_DEBUG
262  if ((GetDebugLevel()&1)!=0)
263  cout << endl;
264  #endif
265 
266 }
267 
268 void LDA::
269 ComputeReductionMatrix(const vector<vector<BIAS::Vector<LDAType> > > &vec,
270  BIAS::Matrix<LDAType> &matrix, int reductionSize,
271  std::vector<BIAS::Matrix<LDAType> > *covs){
272 
273  AddDebugLevel(1);
274  //init
275  if (reductionSize==-1)
276  reductionSize = reductionSize_;
277  BIASASSERT(reductionSize>0);
278 
279  BIASASSERT(!vec.empty());
280  BIASASSERT(!vec[0].empty());
281  const int dimensions = vec[0][0].size();
282  if (covs!=NULL) {
283  covs->resize(vec.size(), Matrix<float>(dimensions, dimensions,MatrixZero));
284  }
285 
286  // compute means
287 
288  BIASDOUT(1, "compute means "<<FileHandling::CurrentDateTimeString());
289 
290  ComputeMeans(vec);
291 
292  //covariance-matrix
293  Matrix<LDAType> withinclassCov(dimensions, dimensions,0.0),
294  interclassCov(dimensions, dimensions, 0.0);
295 
296  vector<double> classWeights;
297  classWeights.reserve(vec.size());
298 
299  // for each class compute its covariance matrix and sum them up weighted in classCov
300  BIASDOUT(1, "compute within covariances"<<FileHandling::CurrentDateTimeString() );
301  for (int classNumber=0;classNumber<(int)vec.size();classNumber++){
302  Matrix<LDAType> &cov = (covs==NULL)? interclassCov : (*covs)[classNumber];
303  cov.SetZero();
304  const std::vector<BIAS::Vector<LDAType> > & class_i = vec[classNumber];
305  if (equalClassWeights_) classWeights.push_back(1.0);
306  else classWeights.push_back(class_i.size());
307  // cov approximation only possible when more than one measurement available
308  if (class_i.size()>1) {
309  // save offset classNumber
310  for (int classVecNumber=0; classVecNumber<(int)class_i.size(); classVecNumber++){
311  // save offset l
312  const BIAS::Vector<LDAType> & classVector = class_i[classVecNumber];
313  for (int dim=0; dim<dimensions; dim++){
314  // compute kli once for all j
315  const LDAType classVectordim = classVector[dim];
316  const LDAType classMean = classMeans_[classNumber][dim];
317  const LDAType diff = (classVectordim-classMean);
318  for (int dim2=0;dim2<=dim;dim2++){
319  cov[dim][dim2] += diff *
320  (classVector[dim2]-classMeans_[classNumber][dim2]);
321  }
322  }
323  }
324  // all classes with equal probability ?
325  if (equalClassWeights_)
326  cov *= LDAType(1.0/LDAType(class_i.size()-1));
327 
328  // just one copy of other triangular matrix at the end
329  for (int i=0; i<dimensions; i++){
330  for (int j=0; j<=i; j++){
331  withinclassCov[i][j] += cov[i][j];
332  }
333  }
334  }
335  }
336  // copy symmetic part
337  for (int i=0; i<dimensions; i++){
338  for (int j=0; j<i; j++){
339  withinclassCov[j][i] = withinclassCov[i][j];
340  }
341  }
342 
343  BIASDOUT(2, "within class cov = "<<withinclassCov);
344 
345  // compute second central moment of class means
346  BIASDOUT(1, "compute inter-class covariances "<<FileHandling::CurrentDateTimeString());
347  interclassCov.SetZero();
348  for (int classNumber=0;classNumber<(int)classMeans_.size();classNumber++){
349  Vector<LDAType> & classMean = classMeans_[classNumber];
350  const LDAType w = classWeights[classNumber];
351  for (int i=0;i<classMean.size();i++){
352  const LDAType weight = w * (classMean[i] - mean_[i]);
353  for (int j=0; j<=i; j++){
354  // weight classes ?
355  interclassCov[i][j] += weight * (classMean[j]-mean_[j]);
356  }
357  }
358  }
359 
360  for (int i=0; i<dimensions; i++){
361  for (int j=0;j<i;j++) {
362  interclassCov[j][i] = interclassCov[i][j];
363  }
364  }
365 
366 
367  // within and inter class cov are now scaled by number of classes
368 
369 
370 
371  BIASDOUT(2, "inter class cov = "<<interclassCov);
372  ComputeReductionMatrix(withinclassCov, interclassCov, matrix, reductionSize);
373 
374 }
375 
376 
377 
378 
380  Matrix<LDAType>& interclassCov,
381  BIAS::Matrix<LDAType> &matrix,
382  int reductionSize) {
383 
384  const int dimensions = interclassCov.num_rows();
385 
386  // make symmetric double matrix, enforce positive diagonal and symmetry
387  Matrix<double> interclassCovd(dimensions, dimensions);
388  for (int i=0; i<dimensions; i++){
389  interclassCovd[i][i] = 2.0 * fabs(interclassCov[i][i]);
390  for (int j=0;j<i;j++) {
391  interclassCovd[j][i] = interclassCovd[i][j] = double(interclassCov[i][j])
392  + double(interclassCov[j][i]) ;
393  }
394  }
395  BIASWARNONCE("Copying from float to double ...");
396 
397 
398  // make SVD and decide if to go on with PCA
399  SVD svd(false);// no absolute zero threshold !
400  Matrix<double> U, withinclassCovd(dimensions, dimensions);
401  for (int i=0; i<dimensions; i++){
402  withinclassCovd[i][i] = 2.0*fabs(withinclassCov[i][i]);
403  for (int j=0;j<i;j++) {
404  withinclassCovd[i][j] = withinclassCovd[j][i] =
405  double(withinclassCov[i][j])+double(withinclassCov[j][i]);
406  }
407  }
408 
409  BIASDOUT(1, "classcov svd."<<FileHandling::CurrentDateTimeString());
410  int rank = 0;
411  // make sure that matrix has full rank
412  if (svd.Compute(withinclassCovd, REDUCTION_ZERO_THRESH)==0) {
413  rank=svd.Rank();
414  } else {
415  BIASERR("SVD failed");
416  }
417  BIASDOUT(1, "rank=" << svd.Rank() << ", dimensions=" << dimensions);
418 
419  if (rank==0){
420  // make PCA on means if rank=0
421  BIASDOUT(1, " rank=0, make PCA");
422  svd.Compute(interclassCovd);
423  U = svd.GetVT();
424  } else {
425  // try regularization if rank is not full:
426  if (rank<dimensions){
427  double ev = svd.GetS()[rank-1];
428  BIASDOUT(1, "Singularity, s="<< svd.GetS()<<" rank="<< svd.Rank()
429  <<". Regularizing and trying again."<<FileHandling::CurrentDateTimeString());
430  for (int i=0;i<dimensions;i++){
431  withinclassCovd[i][i] += ev;
432  }
433  svd.Compute(withinclassCovd, REDUCTION_ZERO_THRESH);
434 
435  BIASDOUT(1, "SECOND TRY yielded rank="
436  << svd.Rank() << ", added " << ev);
437 
438  if ((int)svd.Rank()<dimensions) {
439  BIASERR("REGULARIZATION FAILED - ABORTING");
440  BIASABORT;
441  }
442  }
443  //cout << "InnerClassCov=" << withinclassCov << "BetweenClassCov=" << interclassCov ;
444  //cout << "U=" << svd.GetU() << "V=" << svd.GetV() << "S=" << svd.GetS();
445 
446  // last steps for LDA-matrix, see paper
447  // from Max Welling: "Fisher Linear Discriminant Analysis"
448  BIASDOUT(1, "Inverting within class cov ... "<<FileHandling::CurrentDateTimeString());
449  withinclassCovd = svd.Invert();
450 
451  //cout << "INV(InnerClassCov)=" << withinclassCov ;
452  BIASDOUT(1, "Factorizing inter class cov ... "<<FileHandling::CurrentDateTimeString());
453  svd.Compute(interclassCovd);
454  rank = svd.Rank();
455  if (rank<dimensions){
456  BIASDOUT(1, "inter class cov is singular, regularizing and trying "
457  "again ... "<<FileHandling::CurrentDateTimeString());
458  for (int i=0;i<dimensions;i++){
459  interclassCovd[i][i] += svd.GetS()[rank-1];
460  }
461  svd.Compute(interclassCovd);
462  }
463  BIASDOUT(1, "Computing inter class root "<<FileHandling::CurrentDateTimeString());
464  //cout << "U=" << svd.GetU() << "V=" << svd.GetV() << "S=" << svd.GetS();
465  interclassCovd=svd.SqrtT();
466 
467  //cout << "SqrtT(BetweenClassCov)=" << interclassCovd ;
468  BIASDOUT(1, "composing and solving transformation matrix "<<FileHandling::CurrentDateTimeString());
469  svd.Compute(interclassCovd * withinclassCovd * interclassCovd);
470 
471  //cout << "cov*withinclassCov*cov=" << interclassCovd*withinclassCovd*interclassCovd ;
472 
473  U = svd.GetV();
474 
475  //cout << "U=" << svd.GetU() << "V=" << svd.GetV() << "S=" << svd.GetS();
476  BIASDOUT(1, "inverting covd "<<FileHandling::CurrentDateTimeString());
477  interclassCovd = svd.Invert(interclassCovd);
478 
479  //cout << "INV(SqrtT(BetweenClassCov))=" << cov ;
480 
481  U = interclassCovd * U;
482  //cout << "U=" << U ;
483 
484  /*
485  svd.Compute(withinclassCov*cov);
486  U=svd.GetV();
487  */
488  }
489 
490  BIASDOUT(1, "factorizing U "<<FileHandling::CurrentDateTimeString());
491  svd.Compute(U);
492  U /= svd.GetS()[int(dimensions/2)];
493 
494  matrix.newsize(dimensions, reductionSize);
495  for (int j=0;j<matrix.num_rows();j++){
496  for (int i=0;i<matrix.num_cols();i++){
497  matrix[j][i] = LDAType( U[j][i] );
498  }
499  }
500 }
501 
502 void LDA::ComputeMeans(const vector<vector<BIAS::Vector<LDAType> > > &vec){
503  const int dimensions = vec[0][0].size();
504 
505  classMeans_.resize(vec.size());
506  mean_.newsize(dimensions);
507  mean_.SetZero();
508 
509  allCount_=0;
510  for (int i=0;i<(int)vec.size();i++){
511  classMeans_[i].newsize(dimensions);
512  classMeans_[i].SetZero();
513  allCount_+=vec[i].size();
514  for (int k=0;k<(int)vec[i].size();k++){
515  classMeans_[i]+=vec[i][k];
516  }
517  mean_+=classMeans_[i];
518  classMeans_[i] /= LDAType(vec[i].size());
519  }
520  // this is the absaolute mean of all points, not the mean of the class means
521  mean_ /= LDAType(allCount_);
522 
523 }
524 
526  mean=mean_;
527 }
528 
529 void LDA::GetClassMeans(std::vector<BIAS::Vector<LDAType> > &means){
530  means=classMeans_;
531 }
532 
533 void LDA::SetReductionSize(int size){
534  reductionSize_=size;
535 }
536 
537 
539  classmeans,
540  const vector<BIAS::Matrix<LDAType> >&
541  covs,
542  Matrix<LDAType>& withinClassCov,
543  Matrix<LDAType>& interClassCov){
544  BIASASSERT(classmeans.size()==covs.size());
545  BIASASSERT(withinClassCov.num_rows()==withinClassCov.num_cols());
546  BIASASSERT(interClassCov.num_rows()==interClassCov.num_cols());
547  BIASASSERT(withinClassCov.num_rows()==interClassCov.num_cols());
548  BIASASSERT(classmeans.size()==covs.size());
549  BIASASSERT(withinClassCov.num_rows()==covs[0].num_rows());
550  Vector<float> mean(classmeans[0].dim(), 0.0f);
551  PCA pca;
552  pca.ComputeMean(classmeans, mean);
553  pca.ComputeScatter(classmeans, mean, interClassCov);
554  for (unsigned int i=0; i<classmeans.size(); i++) {
555  withinClassCov += covs[i];
556  }
557  interClassCov *= 1.0f / float(interClassCov.NormFrobenius());
558  withinClassCov*= 1.0f / float(withinClassCov.NormFrobenius());
559 }
560 
561 
562 void LDA::ComputeAnonymousReduction(const std::vector<std::vector<
563  BIAS::Vector<LDAType> > > &vec,
564  BIAS::Matrix<LDAType> &matrix,
565  int reductionSize,
566  const std::vector< std::vector<BIAS::
567  Matrix<LDAType> > >*covs) {
568  BIASASSERT(!vec.empty());
569  BIASASSERT(!vec[0].empty());
570  const int dim = vec[0][0].Size();
571  Matrix<LDAType> WithinClassCov(dim, dim, MatrixZero),
572  InterClassCov(dim, dim, MatrixZero),
573  scatter(dim, dim, MatrixZero), interscatter(dim, dim, MatrixZero);
574 
575  PCA myPCA;
576  for (unsigned int i=0; i< vec.size(); i++) {
577  // run over all images
578  Vector<LDAType> mean;
579  myPCA.ComputeMean(vec[i], mean);
580  myPCA.ComputeScatter(vec[i], mean, interscatter);
581  if (covs!=NULL) {
582  scatter.SetZero();
583  for (unsigned int c=0; c<(*covs)[i].size(); c++) {
584  scatter += (*covs)[i][c];
585  }
586  //scatter.Normalize();
587  }
588  InterClassCov += interscatter;
589  WithinClassCov += scatter;
590  }
591  ComputeReductionMatrix(WithinClassCov, InterClassCov, matrix, reductionSize);
592 }
void ComputeScatter(const std::vector< BIAS::Vector< PCAType > > &vec, const BIAS::Vector< PCAType > &mean, BIAS::Matrix< PCAType > &cov)
compute unnomalized covariance
Definition: PCA.cpp:107
void GenerateRandomTestData(int vectorSize, int numberOfClasses, int classSizeMin, int classSizeMax, int vectorEntryMin, int vectorEntryMax, double stdDeviation, double mainDirectionStdDeviation, std::vector< std::vector< BIAS::Vector< LDAType > > > &vec, std::vector< BIAS::Vector< LDAType > > &means, bool Normalize)
Generates random classes of vectors.
Definition: LDA.cpp:132
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
Subscript num_cols() const
Definition: cmat.h:320
class for column vectors with arbitrary size
void ComputeAnonymousReduction(const std::vector< std::vector< BIAS::Vector< LDAType > > > &vec, BIAS::Matrix< LDAType > &matrix, int reductionSize=-1, const std::vector< std::vector< BIAS::Matrix< LDAType > > > *covs=NULL)
Computes a reduction-matrix for features of n sets (e.g.
Definition: LDA.cpp:562
void ComputeReductionMatrix(const std::vector< std::vector< BIAS::Vector< LDAType > > > &vec, BIAS::Matrix< LDAType > &matrix, int reductionSize=-1, std::vector< BIAS::Matrix< LDAType > > *covs=NULL)
Computes a reduction-matrix the first dimension of the matrix is taken out of the first vector...
Definition: LDA.cpp:269
unsigned int Rank()
returns the rank of A_
Definition: SVD.cpp:506
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
static std::string CurrentDateTimeString()
Create a string containing date and time like 20060329_173054.
void SetReductionSize(int size)
reduction size can be set here
Definition: LDA.cpp:533
Matrix< double > SqrtT()
returns the square root of a symmetric positive definite matrix M A = sqrt(M) = U * sqrt(S)...
Definition: SVD.cpp:344
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
int Compute(const Matrix< double > &M, double ZeroThreshold=DEFAULT_DOUBLE_ZERO_THRESHOLD)
set a new matrix and compute its decomposition.
Definition: SVD.cpp:102
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
double NormL2() const
Return the L2 norm: a^2 + b^2 + c^2 + ...
Definition: Matrix.hh:878
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
void ComputeMeans(const std::vector< std::vector< BIAS::Vector< LDAType > > > &vec)
get mean of input get results with methods down under
Definition: LDA.cpp:502
Matrix< double > GetV() const
return V
Definition: SVD.hh:396
principal component analysis on a set of vectors with PCA it is possible to find the most important d...
Definition: PCA.hh:47
const Matrix< double > & GetVT() const
return VT (=transposed(V))
Definition: SVD.hh:177
void GetClassMeans(std::vector< BIAS::Vector< LDAType > > &means)
get mean vector for each class ComputeReductionMatrix or ComputeMeans has to be called first ...
Definition: LDA.cpp:529
Vector< T > GetRow(const int &row) const
return a copy of row &quot;row&quot; of this matrix, zero based counting
Definition: Matrix.cpp:233
const Vector< double > & GetS() const
return S which is a vector of the singular values of A in descending order.
Definition: SVD.hh:167
void AnalyzeData(const std::vector< std::vector< BIAS::Vector< LDAType > > > &vec)
Ouput statistical-data of input-data.
Definition: LDA.cpp:48
void GetMean(BIAS::Vector< LDAType > &mean)
get a mean vector of all vectors ComputeReductionMatrix or ComputeMeans has to be called first ...
Definition: LDA.cpp:525
void Reset()
calls srand() with a seed generated from system call time, also initializes some other variables ...
Definition: Random.cpp:113
const Matrix< double > & GetU() const
return U U is a m x m orthogonal matrix
Definition: SVD.hh:187
void ComputeWithinAndInterClassCovs(const std::vector< BIAS::Vector< LDAType > > &classmeans, const std::vector< BIAS::Matrix< LDAType > > &covs, BIAS::Matrix< LDAType > &withinClassCov, BIAS::Matrix< LDAType > &interClassCov)
given mean and scatter of each class, compute scatter between class means and average class shape ...
Definition: LDA.cpp:538
Matrix< double > Invert()
returns pseudoinverse of A = U * S * V^T A^+ = V * S^+ * U^T
Definition: SVD.cpp:214
Subscript num_rows() const
Definition: cmat.h:319
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Definition: Random.hh:71
void ComputeMean(const std::vector< BIAS::Vector< PCAType > > &vec, BIAS::Vector< PCAType > &mean)
computes mean of a set of vectors
Definition: PCA.cpp:127
double NormFrobenius() const
Return Frobenius norm = sqrt(trace(A^t * A)).
Definition: Matrix.hh:897
class for producing random numbers from different distributions
Definition: Random.hh:51
Subscript size() const
Definition: vec.h:262