SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
18 
19 using namespace shogun;
20 using namespace Eigen;
21 
22 namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); };
23 
25 {
26  init();
27 }
28 
30 {
31  m_tau = SGVector<float64_t>(4);
32  m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3;
33 
34  m_covs = SGNDArray<float64_t>();
35 
36  SG_ADD(&m_tau, "tau", "tau vector", MS_AVAILABLE);
37 }
38 
40 {
41 }
42 
44 {
45  m_tau = tau;
46 }
47 
49 {
50  return m_tau;
51 }
52 
54 {
55  return m_covs;
56 }
57 
59 {
60  REQUIRE(features, "features is null");
61  SG_REF(features);
62 
63  SGMatrix<float64_t> X = ((CDenseFeatures<float64_t>*)features)->get_feature_matrix();
64 
65  int n = X.num_rows;
66  int m = X.num_cols;
67  int N = m_tau.vlen;
68 
69  Map<MatrixXd> EX(X.matrix,n,m);
70 
71  // Compute Correlation Matrices
72  index_t * M_dims = SG_MALLOC(index_t, 3);
73  M_dims[0] = n;
74  M_dims[1] = n;
75  M_dims[2] = N;
76  m_covs = SGNDArray< float64_t >(M_dims, 3);
77 
78  for (int t = 0; t < N; t++)
79  {
80  Map<MatrixXd> EM(m_covs.get_matrix(t),n,n);
81  EM = cor(EX,m_tau[t]);
82  }
83 
84  // Diagonalize
86  Map<MatrixXd> EQ(Q.matrix,n,n);
87 
88  // Compute Mixing Matrix
91  C = EQ.inverse();
92 
93  // Normalize Estimated Mixing Matrix
94  for (int t = 0; t < C.cols(); t++)
95  C.col(t) /= C.col(t).maxCoeff();
96 
97  // Unmix
98  EX = C.inverse() * EX;
99 
100  return features;
101 }
102 
103 // Computing time delayed correlation matrix
104 namespace
105 {
106  MatrixXd cor(MatrixXd x, int tau, bool mean_flag)
107  {
108  int m = x.rows();
109  int n = x.cols();
110 
111  // Center the data
112  if ( mean_flag )
113  {
114  VectorXd mean = x.rowwise().sum();
115  mean /= n;
116  x = x.colwise() - mean;
117  }
118 
119  // Time-delayed Signal Matrix
120  MatrixXd L = x.leftCols(n-tau);
121  MatrixXd R = x.rightCols(n-tau);
122 
123  // Compute Correlations
124  MatrixXd K(m,m);
125  K = (L * R.transpose()) / (n-tau);
126 
127  // Symmetrize
128  K = (K + K.transpose()) / 2.0;
129 
130  return K;
131  }
132 };
133 
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:376
#define SG_REF(x)
Definition: SGObject.h:51
index_t num_rows
Definition: SGMatrix.h:374
T * get_matrix(index_t matIdx) const
Definition: SGNDArray.h:72
SGNDArray< float64_t > get_covs() const
Definition: JediSep.cpp:53
index_t vlen
Definition: SGVector.h:492
SGVector< float64_t > get_tau() const
Definition: JediSep.cpp:48
void set_tau(SGVector< float64_t > tau)
Definition: JediSep.cpp:43
virtual ~CJediSep()
Definition: JediSep.cpp:39
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
class ICAConverter Base class for ICA algorithms
Definition: ICAConverter.h:26
The class Features is the base class of all feature objects.
Definition: Features.h:68
SGMatrix< float64_t > m_mixing_matrix
Definition: ICAConverter.h:82
static SGMatrix< float64_t > diagonalize(SGNDArray< float64_t > C, SGMatrix< float64_t > V0=SGMatrix< float64_t >(NULL, 0, 0, false), double eps=CMath::MACHINE_EPSILON, int itermax=200)
Definition: JediDiag.cpp:18
virtual CFeatures * apply(CFeatures *features)
Definition: JediSep.cpp:58
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:881
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation