Gaussians.cpp
Go to the documentation of this file.
1 /*
2  * Gaussians.cpp
3  *
4  * Created on: Nov 19, 2011
5  * Author: Seungsu KIM
6  */
7 
8 #include "Gaussians.h"
9 
10 #include <math.h>
11 
12 #include <fstream>
13 #include <iostream>
14 
15 using namespace std;
16 /*
17 Gaussians::Gaussians(GMMs *model)
18 {
19  this->model.nbStates = model->nbStates;
20  this->model.nbDim = model->nbDim;
21 
22  this->model.States = (GMMState *)malloc(model->nbStates*sizeof(GMMState) );
23 
24  for(int s=0; s<model->nbStates; s++ ){
25  this->model.States[s].Mu = model->GMMState[s].Mu;
26  this->model.States[s].Sigma = model->GMMState[s].Sigma;
27  this->model.States[s].Prio = model->GMMState[s].Prio;
28  }
29 }
30 */
31 
32 Gaussians::Gaussians(const char* f_mu, const char* f_sigma, const char* f_prio)
33 {
34  int s, i, j;
35  int nbStates;
36  int nbDim;
37 
38  MathLib::Matrix fMatrix;
39  fMatrix.Load(f_prio);
40  if (fMatrix(0, fMatrix.ColumnSize() - 1) > 0.0)
41  {
42  nbStates = fMatrix.ColumnSize();
43  }
44  else
45  {
46  nbStates = fMatrix.ColumnSize() - 1;
47  }
48 
49  for (s = 0; s < nbStates; s++)
50  {
51  model.States[s].Prio = fMatrix(0, s);
52  }
53 
54  fMatrix.Load(f_mu);
55  nbDim = fMatrix.RowSize();
56  model.nbStates = nbStates;
57  model.nbDim = nbDim;
58 
59 
60  for (s = 0; s < nbStates; s++)
61  {
62  model.States[s].Mu.Resize(nbDim);
63  model.States[s].Sigma.Resize(nbDim, nbDim);
64  }
65 
66  for (s = 0; s < nbStates; s++)
67  {
68  model.States[s].Mu = fMatrix.GetColumn(s);
69  }
70 
71  fMatrix.Load(f_sigma);
72  j = 0;
73  for (s = 0; s < nbStates; s++)
74  {
75  for (i = 0; i < nbDim; i++)
76  {
77  model.States[s].Sigma.SetRow(fMatrix.GetRow(j), i);
78  j++;
79  }
80  }
81 }
82 
83 Gaussians::Gaussians(int nbStates,
84  int nbDim,
85  const char* f_mu,
86  const char* f_sigma,
87  const char* f_prio)
88 {
89 
90  int s, i, j;
91 
92  model.nbStates = nbStates;
93  model.nbDim = nbDim;
94 
95  for (s = 0; s < nbStates; s++)
96  {
97  model.States[s].Mu.Resize(nbDim);
98  model.States[s].Sigma.Resize(nbDim, nbDim);
99  }
100 
101  MathLib::Matrix fMatrix(nbDim, nbStates);
102  fMatrix.Load(f_mu);
103  for (s = 0; s < nbStates; s++)
104  {
105  model.States[s].Mu = fMatrix.GetColumn(s);
106  }
107 
108  fMatrix.Resize(nbStates * nbDim, nbDim);
109  fMatrix.Load(f_sigma);
110  j = 0;
111  for (s = 0; s < nbStates; s++)
112  {
113  for (i = 0; i < nbDim; i++)
114  {
115  model.States[s].Sigma.SetRow(fMatrix.GetRow(j), i);
116  j++;
117  }
118  }
119 
120  fMatrix.Resize(1, nbStates);
121  fMatrix.Load(f_prio);
122  MathLib::Vector fVector(nbStates);
123  for (s = 0; s < nbStates; s++)
124  {
125  model.States[s].Prio = fMatrix(0, s);
126  }
127 }
128 
129 Gaussians::Gaussians(const int nbStates,
130  const int nbDim,
131  const vector<double> pri_vec,
132  const vector<double> mu_vec,
133  const vector<double> sig_vec)
134 {
135 
136  model.nbStates = nbStates;
137  model.nbDim = nbDim;
138 
139  for (int s = 0; s < nbStates; s++)
140  {
141  model.States[s].Mu.Resize(nbDim);
142  model.States[s].Sigma.Resize(nbDim, nbDim);
143  }
144 
145  for (int s = 0; s < nbStates; s++)
146  {
147  model.States[s].Prio = pri_vec[s];
148  }
149  // cout << endl << "Printing the constructed Priors" << endl;
150  // for ( int s = 0; s < nbStates; s++ ) {
151  // cout << model.States[s].Prio << "\t";
152  // }
153  // cout << endl;
154 
155 
156  for (int s = 0; s < nbStates; s++)
157  {
158  for (int d = 0; d < nbDim; d++)
159  {
160  model.States[s].Mu[d] = mu_vec[s * nbDim + d];
161  }
162  }
163 
164 
165  // cout << endl << "Printing the constructed Mu" << endl;
166  // for ( int s = 0; s < nbStates; s++ ) {
167  // for (int d = 0; d < nbDim; d++) {
168  // cout << model.States[s].Mu[d] << "\t";
169  // }
170  // cout << endl;
171  // }
172 
173  for (int s = 0; s < nbStates; s++)
174  {
175  for (int row = 0; row < nbDim; row++)
176  {
177  for (int col = 0; col < nbDim; col++)
178  {
179  int ind = s * nbDim * nbDim + row * nbDim + col;
180  model.States[s].Sigma(row, col) = sig_vec[ind];
181  }
182  }
183  }
184 
185 
186  // cout << endl << "Printing the constructed Sigma" << endl;
187  // for ( int s = 0; s < nbStates; s++ ) {
188  // for (int row = 0; row < nbDim; row++) {
189  // for (int col = 0; col < nbDim; col++) {
190  // cout << model.States[s].Sigma(row, col) << "\t";
191  // }
192  // cout <<endl;
193  // }
194  // cout << endl;
195  // }
196 }
197 
198 
199 void
201 {
202  for (unsigned int s = 0; s < model->nbStates; s++)
203  {
204  this->model.States[s].Mu = model->States[s].Mu;
205  this->model.States[s].Sigma = model->States[s].Sigma;
206  this->model.States[s].Prio = model->States[s].Prio;
207  }
208 }
209 
210 
211 void
212 Gaussians::InitFastGaussians(int first_inindex, int last_inindex)
213 {
214  double det;
215  int nbIN = last_inindex - first_inindex + 1;
216 
217  for (unsigned int s = 0; s < model.nbStates; s++)
218  {
219  gmmpinv[s].MuI.Resize(nbIN);
220  gmmpinv[s].SigmaII.Resize(nbIN, nbIN);
221  gmmpinv[s].SigmaIIInv.Resize(nbIN, nbIN);
222  }
223 
224  for (unsigned int s = 0; s < model.nbStates; s++)
225  {
226  for (int i = first_inindex; i <= last_inindex; i++)
227  {
228  gmmpinv[s].MuI(i - first_inindex) = model.States[s].Mu(i);
229  }
230  for (int i = first_inindex; i <= last_inindex; i++)
231  for (int j = first_inindex; j <= last_inindex; j++)
232  {
233  gmmpinv[s].SigmaII(i - first_inindex, j - first_inindex) =
234  model.States[s].Sigma(i, j);
235  }
236 
237  gmmpinv[s].SigmaII.Inverse(gmmpinv[s].SigmaIIInv, &det);
238  //gmmpinv[s].SigmaIIInv = gmmpinv[s].SigmaII.Inverse();
239  //gmmpinv[s].SigmaII.Inverse(&det);
240  if (det < 0)
241  {
242  det = 1e-30;
243  }
244  gmmpinv[s].detSigmaII = det;
245  }
246 
247  nbDimI = last_inindex - first_inindex + 1;
248  gfDiff.Resize(nbDimI);
249  gfDiffp.Resize(nbDimI);
250  gDer.Resize(nbDimI);
251 }
252 
253 double
255 {
256  double p;
257  gfDiff = x - gmmpinv[state].MuI;
258  gfDiffp = gmmpinv[state].SigmaIIInv * gfDiff;
259 
260  p = exp(-0.5 * gfDiff.Dot(gfDiffp)) /
261  sqrt(pow(2.0 * PI, nbDimI) * (gmmpinv[state].detSigmaII + 1e-30));
262 
263  return p;
264 }
265 
266 double
268 {
269  double totalP = 0;
270  for (unsigned int s = 0; s < model.nbStates; s++)
271  {
272  totalP += model.States[s].Prio * GaussianPDFFast(s, x);
273  }
274  return totalP;
275 }
276 
279 {
280  gDer.Zero();
281  for (unsigned int s = 0; s < model.nbStates; s++)
282  {
283  gDer += (gmmpinv[s].SigmaIIInv * (x - gmmpinv[s].MuI)) * model.States[s].Prio *
284  GaussianPDFFast(s, x);
285  }
286  return -gDer;
287 }
288 
289 void
290 Gaussians::InitFastGMR(int first_inindex, int last_inindex, int first_outindex, int last_outindex)
291 {
292  double det;
293  int nbIN = last_inindex - first_inindex + 1;
294  int nbOUT = last_outindex - first_outindex + 1;
295 
296  gPdf.Resize(model.nbStates);
297 
298  for (unsigned int s = 0; s < model.nbStates; s++)
299  {
300  gmmpinv[s].MuI.Resize(nbIN);
301  gmmpinv[s].SigmaII.Resize(nbIN, nbIN);
302  gmmpinv[s].SigmaIIInv.Resize(nbIN, nbIN);
303 
304  gmmpinv[s].muO.Resize(nbOUT);
305  gmmpinv[s].SigmaIO.Resize(nbIN, nbOUT);
306  gmmpinv[s].SigmaIOInv.Resize(nbOUT, nbOUT);
307  }
308 
309  for (unsigned int s = 0; s < model.nbStates; s++)
310  {
311  for (int i = first_inindex; i <= last_inindex; i++)
312  {
313  gmmpinv[s].MuI(i - first_inindex) = model.States[s].Mu(i);
314 
315  for (int j = first_inindex; j <= last_inindex; j++)
316  {
317  gmmpinv[s].SigmaII(i - first_inindex, j - first_inindex) =
318  model.States[s].Sigma(i, j);
319  }
320  for (int j = first_outindex; j <= last_outindex; j++)
321  {
322  gmmpinv[s].SigmaIO(i - first_inindex, j - first_outindex) =
323  model.States[s].Sigma(i, j);
324  }
325  }
326 
327  for (int i = first_outindex; i <= last_outindex; i++)
328  {
329  gmmpinv[s].muO(i - first_outindex) = model.States[s].Mu(i);
330  }
331 
332  gmmpinv[s].SigmaII.Inverse(gmmpinv[s].SigmaIIInv, &det);
333  if (det < 0)
334  {
335  det = 1e-30;
336  }
337  gmmpinv[s].detSigmaII = det;
338  (gmmpinv[s].SigmaIO).Transpose().Inverse(gmmpinv[s].SigmaIOInv, &det);
339  }
340 
341  nbDimI = last_inindex - first_inindex + 1;
342  gfDiff.Resize(nbDimI);
343  gfDiffp.Resize(nbDimI);
344  gDer.Resize(nbDimI);
345 }
346 
347 void
349  MathLib::Vector& outdata,
350  MathLib::Matrix& derGMR)
351 {
352  Regression(indata, outdata);
353  cout << "derivative is not implemented yet " << endl;
354 }
355 
356 void
358 {
359  double pdfall;
360  MathLib::Vector h(model.nbStates);
361  MathLib::Vector r_diff(outdata.Size());
362 
363  for (unsigned int s = 0; s < model.nbStates; s++)
364  {
365  gPdf(s) = model.States[s].Prio * GaussianPDFFast(s, indata);
366  }
367  pdfall = gPdf.Sum();
368 
369  outdata.Zero();
370  for (unsigned int s = 0; s < model.nbStates; s++)
371  {
372  //h(s) = gPdf(s)/(pdfall + 1e-30 );
373  h(s) = gPdf(s) / (pdfall);
374  r_diff = gmmpinv[s].SigmaIO.Transpose() * gmmpinv[s].SigmaIIInv * (indata - gmmpinv[s].MuI);
375 
376  for (unsigned int i = 0; i < r_diff.Size(); i++)
377  {
378  outdata(i) += h(s) * (r_diff(i) + gmmpinv[s].muO(i));
379  }
380  }
381 }
382 
385 {
386  MathLib::Vector outdata(indata.Size());
387  Regression(indata, outdata);
388  return outdata;
389 }
390 
391 
392 /*
393 #include <math.h>
394 
395 #include "Gaussians.h"
396 #include "armadillo"
397 
398 using namespace arma;
399 using namespace std;
400 
401 Gaussians::Gaussians(GMMs *model)
402 {
403  this->model.nbStates = model->nbStates;
404  this->model.nbDim = model->nbDim;
405 
406  this->model.States = (GMMState *)malloc(model->nbStates*sizeof(GMMState) );
407 
408  for(int s=0; s<model->nbStates; s++ ){
409  this->model.States[s].Mu = model->GMMState[s].Mu;
410  this->model.States[s].Sigma = model->GMMState[s].Sigma;
411  this->model.States[s].Prio = model->GMMState[s].Prio;
412  }
413 }
414 
415 Gaussians::Gaussians(int nbStates, int nbDim, char *f_mu, char *f_sigma, char *f_prio)
416 {
417 
418  int s, i, j;
419 
420  model.nbStates = nbStates;
421  model.nbDim = nbDim;
422  model.States = (GMMState *)malloc(nbStates*sizeof(GMMState) );
423 
424  for( s=0; s<nbStates; s++ ){
425  model.States[s].Mu = zeros<vec>(nbDim);
426  model.States[s].Sigma = zeros<mat>(nbDim, nbDim );
427  }
428 
429  // f_mu
430  ifstream fin;
431  fin.open(f_mu);
432  for( i=0; i<nbDim; i++ ){
433  for( s=0; s<nbStates; s++ ){
434  fin >> model.States[s].Mu(i);
435  }
436  }
437  fin.close();
438 
439  // f_sigma
440  fin.open(f_sigma);
441  for( s=0; s<nbStates; s++ ){
442  for( i=0; i<nbDim; i++ ){
443  for( j=0; j<nbDim; j++ ){
444  fin >> model.States[s].Sigma(i,j);
445  }
446  }
447  }
448  fin.close();
449 
450  // f_prio
451  fin.open(f_prio);
452  for( s=0; s<nbStates; s++ ){
453  fin >>model.States[s].Prio;
454  }
455  fin.close();
456 }
457 
458 void Gaussians::setGMMs(GMMs *model)
459 {
460  for(int s=0; s<model->nbStates; s++ ){
461  this->model.States[s].Mu = model->GMMState[s].Mu;
462  this->model.States[s].Sigma = model->GMMState[s].Sigma;
463  this->model.States[s].Prio = model->GMMState[s].Prio;
464  }
465 }
466 
467 
468 void Gaussians::InitFastGaussians(int first_inindex, int last_inindex)
469 {
470  gmmpinv = (GMMStateP *)malloc(model.nbStates*sizeof(GMMStateP) );
471 
472  for(int s=0; s<model.nbStates; s++ ){
473  gmmpinv[s].MuI = model.States[s].Mu.subvec(first_inindex, last_inindex);
474  gmmpinv[s].SigmaII = model.States[s].Sigma.submat(first_inindex, first_inindex, last_inindex, last_inindex);
475  gmmpinv[s].SigmaIIInv = pinv(gmmpinv[s].SigmaII);
476  gmmpinv[s].detSigmaII = det(gmmpinv[s].SigmaII);
477  }
478 
479  nbDimI = last_inindex - first_inindex +1;
480  gfDiff = zeros<vec>(nbDimI);
481  gfDiffp = zeros<vec>(nbDimI);
482  gDer = zeros<vec>(nbDimI);
483 }
484 
485 double Gaussians::GaussianPDFFast(int state, vec x)
486 {
487  double p;
488  gfDiff = x - gmmpinv[state].MuI;
489  gfDiffp = gmmpinv[state].SigmaIIInv * gfDiff;
490 
491  p = exp(-0.5*dot(gfDiff, gfDiffp)) / sqrt(pow(2.0*math::pi(), nbDimI)*( gmmpinv[state].detSigmaII +1e-30));
492 
493  return p;
494 }
495 
496 double Gaussians::GaussianProbFast(vec x)
497 {
498  double totalP=0;
499  for(int s=0; s<model.nbStates; s++ ){
500  totalP += model.States[s].Prio*GaussianPDFFast(s,x);
501  }
502  return totalP;
503 }
504 
505 vec Gaussians::GaussianDerProbFast(vec x)
506 {
507  gDer.zeros();
508  for(int s=0; s<model.nbStates; s++ ){
509  gDer += model.States[s].Prio * gmmpinv[s].SigmaIIInv *(x-gmmpinv[s].MuI)*GaussianPDFFast(s,x);
510  }
511  return -gDer;
512 }
513 
514 //-------------------------------------------------------------------------------------------------------
515 void AllocateGMMs(GMMs *model, int nbDim, int nbStates)
516 {
517  model->nbDim = nbDim;
518  model->nbStates = nbStates;
519  model->GMMState = (GMMState *)malloc(nbStates*sizeof(GMMState) );
520 
521  for(int s=0; s<nbStates; s++ ){
522  model->GMMState[s].Mu = zeros<vec>(nbDim);
523  model->GMMState[s].Sigma = zeros<mat>(nbDim, nbDim );
524  }
525 }
526 
527 
528 double GaussianPDF(vec x, vec Mu, mat Sigma)
529 {
530  double p;
531  vec diff = x - Mu;
532  vec diffp = pinv( Sigma ) * diff;
533  int nbDim = x.size();
534 
535  p = exp(-0.5*dot(diff, diffp)) / sqrt(pow(2.0*math::pi(), nbDim)*( abs(det(Sigma)) +1e-30));
536 
537  if(p < 1e-30){
538  return 1e-30;
539  }
540  else{
541  return p;
542  }
543 }
544 
545 void GaussianMux(GMMs *modelK, GMMs *modelL, GMMs *modelOut)
546 {
547  int k,l,j;
548  int K = modelK->nbStates;
549  int L = modelL->nbStates;
550  int J = K*L;
551 
552  //modelOut->nbDim = modelK->nbDim;
553  //modelOut->nbStates = J;
554  //modelOut->GMMState = (GMMState *)malloc(J*sizeof(GMMState) );
555 
556  j=0;
557  for(k=0; k<K; k++){
558  for(l=0; l<L; l++){
559  modelOut->GMMState[j].Sigma = pinv( pinv(modelK->GMMState[k].Sigma) + pinv( modelL->GMMState[l].Sigma) );
560  modelOut->GMMState[j].Mu = modelOut->GMMState[j].Sigma *( pinv(modelK->GMMState[k].Sigma) * modelK->GMMState[k].Mu + pinv(modelL->GMMState[l].Sigma) * modelL->GMMState[l].Mu );
561  modelOut->GMMState[j].Prio = modelK->GMMState[k].Prio* modelL->GMMState[l].Prio * GaussianPDF( modelK->GMMState[k].Mu, modelL->GMMState[l].Mu, modelK->GMMState[k].Sigma + modelL->GMMState[l].Sigma);
562  j++;
563  }
564  }
565 }
566 
567 void GaussianRotate(GMMs *model, arma::vec P, arma::mat R, GMMs *modelOut)
568 {
569  for(int s=0; s<model->nbStates; s++){
570  modelOut->GMMState[s].Mu = R*model->GMMState[s].Mu + P;
571  modelOut->GMMState[s].Sigma = R*model->GMMState[s].Sigma*trans(R);
572  }
573  //modelOut->nbDim = model->nbDim;
574  //modelOut->nbStates = model->nbStates;
575 }
576 */
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
Gaussians::GaussianProbFast
double GaussianProbFast(MathLib::Vector x)
Definition: Gaussians.cpp:267
Matrix
Eigen::Matrix< T, 3, 3 > Matrix
Definition: UnscentedKalmanFilterTest.cpp:37
Vector
Eigen::Matrix< T, 3, 1 > Vector
Definition: UnscentedKalmanFilterTest.cpp:36
PI
#define PI
Definition: gdiam.cpp:41
GMMs::States
GMMState States[GAUSSIAN_MAXIMUM_NUMBER]
Definition: Gaussians.h:40
GMMState::Mu
MathLib::Vector Mu
Definition: Gaussians.h:17
Gaussians::Gaussians
Gaussians(const char *f_mu, const char *f_sigma, const char *f_prio)
Definition: Gaussians.cpp:32
Gaussians::GaussianDerProbFast
MathLib::Vector GaussianDerProbFast(MathLib::Vector x)
Definition: Gaussians.cpp:278
Gaussians::GaussianPDFFast
double GaussianPDFFast(int state, MathLib::Vector x)
Definition: Gaussians.cpp:254
GMMs
Definition: Gaussians.h:35
Gaussians::InitFastGMR
void InitFastGMR(int first_inindex, int last_inindex, int first_outindex, int last_outindex)
Definition: Gaussians.cpp:290
std
Definition: Application.h:66
Gaussians.h
GMMs::nbStates
unsigned int nbStates
Definition: Gaussians.h:37
Gaussians::InitFastGaussians
void InitFastGaussians(int first_inindex, int last_inindex)
Definition: Gaussians.cpp:212
Gaussians::setGMMs
void setGMMs(GMMs *model)
Definition: Gaussians.cpp:200
GMMState::Sigma
MathLib::Matrix Sigma
Definition: Gaussians.h:18
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
GMMState::Prio
double Prio
Definition: Gaussians.h:19
Gaussians::Regression
void Regression(const MathLib::Vector &indata, MathLib::Vector &outdata, MathLib::Matrix &derGMR)
Definition: Gaussians.cpp:348