Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleLDA.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3 Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9 BIAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13 
14 BIAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with BIAS; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 
25 /** @example ExampleLDA.cpp
26  @relates LDA
27  @ingroup g_examples
28  @brief example for linear discriminant analysis on a set of classes of vectors
29  @author MIP
30 */
31 
32 #include <bias_config.h>
33 
34 #include <MathAlgo/LDA.hh>
35 #include <Base/Math/Random.hh>
36 
37 using namespace BIAS;
38 using namespace std;
39 
40 int main(int args, char **arg)
41 {
42  LDA myLDA(true);//equalweights for all classes ?
43  myLDA.SetDebugLevel(1+2);
44 
45  std::vector<std::vector<BIAS::Vector<LDAType> > > vec;
46 
47 
48  int reductionSize = 2;
49  std::vector<BIAS::Matrix<LDAType> > *covs = NULL;
50  unsigned int classes = 3;
51  unsigned int numsamples = 1000;
52  unsigned int dim = 2;
53  BIAS::Matrix<LDAType> matrix(reductionSize,dim,MatrixZero);
54  Vector<double> sigma(dim);
55  sigma[0] = 1.1;
56  sigma[1] = 0.01;
57  for (unsigned int i=2; i<dim; i++) sigma[i] = 0.1;
58 
59  Random R;
60 
61 
62  vector<Vector<LDAType> > means;
63  for (unsigned int c=0; c<classes; c++){
64  Vector<LDAType> curMean(dim), estMean(dim);
65  means.push_back(curMean);
66  switch (c) {
67  case 0: curMean[0] = -1;curMean[1] = 0;break;
68  case 1: curMean[0] = 1;curMean[1] = 0;break;
69  case 2: curMean[0] = 0.1f;curMean[1] = -0.1f;break;
70  default: {
71  for (unsigned int d=0; d<dim; d++) {
72  curMean[d] = (float) R.GetUniformDistributed(-1.0*double(d),
73  double(d));
74  estMean[d] = 0;
75  }
76  }
77  }
78  cout<<"mean "<<c<<" is "<<curMean<<endl;
79  std::vector<BIAS::Vector<LDAType> > curvec;
80  for (unsigned int i=0; i<numsamples; i++) {
81  Vector<LDAType> sample(dim);
82  for (unsigned int d=0; d<dim; d++) {
83  sample[d] = (float) R.GetNormalDistributed(curMean[d], sigma[d]);
84  estMean[d] += sample[d];
85  }
86  curvec.push_back(sample);
87  }
88  for (unsigned int d=0; d<dim; d++) {
89  estMean[d] /= float(numsamples);
90  }
91  cout<<"estimated mean is "<<estMean<<endl;
92  vec.push_back(curvec);
93  }
94 
95  /*
96 GenerateRandomTestData(int vectorSize, int numberOfClasses,
97  int classSizeMin, int classSizeMax, int vectorEntryMin, int vectorEntryMax,
98  double stdDeviation, double mainDirectionStdDeviation,
99  std::vector<std::vector<BIAS::Vector<LDAType> > > &vec,
100  std::vector<BIAS::Vector<LDAType> > &means, bool Normalize);
101 
102  */
103 
104  myLDA.ComputeReductionMatrix(vec, matrix, reductionSize, covs);
105  cout<<"Reduction matrix is "<<matrix<<endl;
106  return 0;
107 }
108 
linear discriminant analysis on a set of classes of vectors LDA is a approach for dimension-reduction...
Definition: LDA.hh:46
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
matrix class with arbitrary size, indexing is row major.
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
class for producing random numbers from different distributions
Definition: Random.hh:51