SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JediSep.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Kevin Hughes
8  */
9 
11 
13 
14 #ifdef HAVE_EIGEN3
15 
19 
20 using namespace shogun;
21 using namespace Eigen;
22 
23 namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); };
24 
26 {
27  init();
28 }
29 
31 {
32  m_tau = SGVector<float64_t>(4);
33  m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3;
34 
35  m_covs = SGNDArray<float64_t>();
36 
37  SG_ADD(&m_tau, "tau", "tau vector", MS_AVAILABLE);
38 }
39 
41 {
42 }
43 
45 {
46  m_tau = tau;
47 }
48 
50 {
51  return m_tau;
52 }
53 
55 {
56  return m_covs;
57 }
58 
60 {
61  REQUIRE(features, "features is null");
62  SG_REF(features);
63 
64  SGMatrix<float64_t> X = ((CDenseFeatures<float64_t>*)features)->get_feature_matrix();
65 
66  int n = X.num_rows;
67  int m = X.num_cols;
68  int N = m_tau.vlen;
69 
70  Map<MatrixXd> EX(X.matrix,n,m);
71 
72  // Compute Correlation Matrices
73  index_t * M_dims = SG_MALLOC(index_t, 3);
74  M_dims[0] = n;
75  M_dims[1] = n;
76  M_dims[2] = N;
77  m_covs = SGNDArray< float64_t >(M_dims, 3);
78 
79  for (int t = 0; t < N; t++)
80  {
81  Map<MatrixXd> EM(m_covs.get_matrix(t),n,n);
82  EM = cor(EX,m_tau[t]);
83  }
84 
85  // Diagonalize
87  Map<MatrixXd> EQ(Q.matrix,n,n);
88 
89  // Compute Mixing Matrix
91  Map<MatrixXd> C(m_mixing_matrix.matrix,n,n);
92  C = EQ.inverse();
93 
94  // Normalize Estimated Mixing Matrix
95  for (int t = 0; t < C.cols(); t++)
96  C.col(t) /= C.col(t).maxCoeff();
97 
98  // Unmix
99  EX = C.inverse() * EX;
100 
101  return features;
102 }
103 
104 // Computing time delayed correlation matrix
105 namespace
106 {
107  MatrixXd cor(MatrixXd x, int tau, bool mean_flag)
108  {
109  int m = x.rows();
110  int n = x.cols();
111 
112  // Center the data
113  if ( mean_flag )
114  {
115  VectorXd mean = x.rowwise().sum();
116  mean /= n;
117  x = x.colwise() - mean;
118  }
119 
120  // Time-delayed Signal Matrix
121  MatrixXd L = x.leftCols(n-tau);
122  MatrixXd R = x.rightCols(n-tau);
123 
124  // Compute Correlations
125  MatrixXd K(m,m);
126  K = (L * R.transpose()) / (n-tau);
127 
128  // Symmetrize
129  K = (K + K.transpose()) / 2.0;
130 
131  return K;
132  }
133 };
134 
135 #endif // HAVE_EIGEN3

SHOGUN Machine Learning Toolbox - Documentation