SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
UWedgeSep.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
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
virtual ~CUWedgeSep()
Definition: UWedgeSep.cpp:40
int32_t index_t
Definition: common.h:62
Definition: SGMatrix.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
#define SG_REF(x)
Definition: SGObject.h:51
index_t num_rows
Definition: SGMatrix.h:376
T * get_matrix(index_t matIdx) const
Definition: SGNDArray.h:72
index_t vlen
Definition: SGVector.h:494
void set_tau(SGVector< float64_t > tau)
Definition: UWedgeSep.cpp:44
virtual CFeatures * apply(CFeatures *features)
Definition: UWedgeSep.cpp:59
static SGMatrix< float64_t > diagonalize(SGNDArray< float64_t > C, SGMatrix< float64_t > V0=SGMatrix< float64_t >(NULL, 0, 0, false), double eps=1e-12, int itermax=200)
Definition: UWedge.cpp:13
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGNDArray< float64_t > get_covs() const
Definition: UWedgeSep.cpp:54
class ICAConverter Base class for ICA algorithms
Definition: ICAConverter.h:27
The class Features is the base class of all feature objects.
Definition: Features.h:68
SGMatrix< float64_t > m_mixing_matrix
Definition: ICAConverter.h:83
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:885
#define SG_ADD(...)
Definition: SGObject.h:81
SGVector< float64_t > get_tau() const
Definition: UWedgeSep.cpp:49

SHOGUN Machine Learning Toolbox - Documentation