ATSuite C++  v1.0
Scientific C++ routines originally developed by Alexis Tantet
atmarkov.hpp
Go to the documentation of this file.
1 #ifndef ATMARKOV_HPP
2 #define ATMARKOV_HPP
3 
4 #include <list>
5 #include <gsl/gsl_vector.h>
6 #include <gsl/gsl_vector_int.h>
7 #include <gsl/gsl_matrix.h>
8 #include <Eigen/Dense>
9 #include <Eigen/Sparse>
10 #include <ATSuite/atmatrix.hpp>
11 
13 #define plog2p( x ) ( (x) > 0.0 ? (x) * log(x) / log(2) : 0.0 )
14 
15 using namespace Eigen;
16 
23 // Typedef declaration
25 typedef SparseMatrix<double, RowMajor> SpMatCSR;
27 typedef SparseMatrix<int, RowMajor> SpMatIntCSR;
29 typedef SparseMatrix<double, ColMajor> SpMatCSC;
31 typedef SparseMatrix<bool, ColMajor> SpMatCSCBool;
32 
33 // Declarations
35 void toRightStochastic(SpMatCSR *);
36 void toLeftStochastic(SpMatCSR *);
37 void toAndStochastic(SpMatCSR *);
39 void filterTransitionMatrix(SpMatCSR *, gsl_vector *, gsl_vector *, double, int);
40 // double entropy(VectorXd *);
41 // double entropyRate(SpMatCSC *, VectorXd *);
42 // double entropyRate(MatrixXd *, VectorXd *);
43 // void condition4Entropy(SpMatCSC *);
44 // void lowlevelTransition(SpMatCSC *, VectorXd *, VectorXi *,
45 // MatrixXd *, VectorXd *);
46 
47 // Definitions
56 {
57  int j;
58  VectorXd rowSum = VectorXd::Zero(T->rows());
59  // Calculate row sums
60  for (j = 0; j < T->cols(); j++)
61  for (SpMatCSC::InnerIterator it(*T, j); it; ++it)
62  rowSum(it.row()) += it.value();
63 
64  // Normalize rows
65  for (j = 0; j < T->cols(); j++)
66  for (SpMatCSC::InnerIterator it(*T, j); it; ++it)
67  if (rowSum(it.row()) > 0.)
68  it.valueRef() /= rowSum(it.row());
69 
70  return;
71 }
72 
80 void toRightStochastic(SpMatCSR *T)
81 {
82  // Get row sum vector
83  gsl_vector *rowSum = getRowSum(T);
84 
85  // Normalize rows
86  normalizeRows(T, rowSum);
87 
88  // Free row sum vector
89  gsl_vector_free(rowSum);
90 
91  return;
92 }
93 
101 void toLeftStochastic(SpMatCSR *T)
102 {
103  int j;
104  VectorXd colSum = VectorXd::Zero(T->cols());
105  // Calculate row sums
106  for (j = 0; j < T->rows(); j++)
107  for (SpMatCSR::InnerIterator it(*T, j); it; ++it)
108  colSum(it.col()) += it.value();
109 
110  // Normalize rows
111  for (j = 0; j < T->rows(); j++)
112  for (SpMatCSR::InnerIterator it(*T, j); it; ++it)
113  if (colSum(it.col()) > 0.)
114  it.valueRef() /= colSum(it.col());
115 
116  return;
117 }
118 
126 void toAndStochastic(SpMatCSR *T)
127 {
128  double norm = getSum(T);
129  // Normalize
130  for (int outerIdx = 0; outerIdx < T->outerSize(); outerIdx++)
131  for (SpMatCSR::InnerIterator it(*T, outerIdx); it; ++it)
132  it.valueRef() /= norm;
133  return;
134 }
135 
144 {
145  double norm = 0.;
146  // Get Norm
147  for (int outerIdx = 0; outerIdx < T->outerSize(); outerIdx++)
148  for (SpMatCSC::InnerIterator it(*T, outerIdx); it; ++it)
149  norm += it.value();
150  // Normalize
151  for (int outerIdx = 0; outerIdx < T->outerSize(); outerIdx++)
152  for (SpMatCSC::InnerIterator it(*T, outerIdx); it; ++it)
153  it.valueRef() /= norm;
154  return;
155 }
156 
172 void
174  gsl_vector *rowCut, gsl_vector *colCut,
175  double tol, int norm)
176 {
177  double valRow, valCol;
178 
179  for (int outerIdx = 0; outerIdx < T->outerSize(); outerIdx++){
180  valRow = gsl_vector_get(rowCut, outerIdx);
181  for (SpMatCSR::InnerIterator it(*T, outerIdx); it; ++it){
182  valCol = gsl_vector_get(colCut, it.col());
183  // Remove elements of states to be removed
184  if ((valRow < tol) || (valCol < tol))
185  it.valueRef() = 0.;
186  }
187  }
188  //T->prune();
189 
190  // Normalize
191  switch (norm){
192  case 0:
193  toAndStochastic(T);
194  break;
195  case 1:
197  break;
198  case 2:
199  toLeftStochastic(T);
200  break;
201  default:
202  break;
203  }
204  normalizeVector(rowCut);
205  normalizeVector(colCut);
206 
207  return;
208 }
209 
210 // double entropy(VectorXd *dist)
211 // {
212 // double s = 0.;
213 // for (int k = 0; k < dist->size(); k++)
214 // s -= plog2p((*dist)(k));
215 // return s;
216 // }
217 
218 // double entropyRate(SpMatCSC *T, VectorXd *dist)
219 // {
220 // int i, j;
221 // double s = 0.;
222 // for (j = 0; j < T->outerSize(); j++){
223 // for (SpMatCSC::InnerIterator it(*T, j); it; ++it){
224 // i = it.row();
225 // // Increment low-level transition matrix
226 // s -= (*dist)(i) * plog2p(it.value());
227 // }
228 // }
229 // return s;
230 // }
231 
232 // double entropyRate(MatrixXd *T, VectorXd *dist)
233 // {
234 // int i, j;
235 // double s = 0.;
236 // for(j = 0; j < T->cols(); j++){
237 // for (i = 0; i < T->rows(); i++){
238 // // Increment low-level transition matrix
239 // s -= (*dist)(i) * plog2p((*T)(i, j));
240 // }
241 // }
242 // return s;
243 // }
244 
245 // void condition4Entropy(SpMatCSC *T)
246 // {
247 // int j;
248 // VectorXd rowSum = VectorXd::Zero(T->rows());
249 // // Calculate row sums
250 // for (j = 0; j < T->cols(); j++){
251 // for (SpMatCSC::InnerIterator it(*T, j); it; ++it){
252 // if (it.value() > 1.)
253 // it.valueRef() = 1.;
254 // if (it.value() < 0.)
255 // it.valueRef() = 0.;
256 // }
257 // }
258 
259 // return;
260 // }
261 
262 // void lowlevelTransition(SpMatCSC *highT, VectorXd *highDist, VectorXi *member,
263 // MatrixXd *lowT, VectorXd *lowDist)
264 // {
265 // const int N = highT->rows();
266 // int ncom;
267 // int i, j;
268 
269 // // Get set of communities from membership vector
270 // list<int> coms(member->data(), member->data() + N);
271 // coms.sort();
272 // coms.unique();
273 // ncom = coms.size();
274 
275 // // Allocate and initialize low-level transition matrix
276 // *lowT = MatrixXd::Zero(ncom, ncom);
277 // *lowDist = VectorXd::Zero(ncom);
278 
279 // // Get community map
280 // map<int, int> comMap;
281 // list<int>::iterator it = coms.begin();
282 // for (int k = 0; k < ncom; k++){
283 // comMap[*it] = k;
284 // advance(it, 1);
285 // }
286 
287 // // Increment low-level stationary distribution
288 // for (int i = 0; i < N; i++)
289 // (*lowDist)(comMap[(*member)(i)]) += (*highDist)(i);
290 
291 // for (j = 0; j < N; j++){
292 // for (SpMatCSC::InnerIterator it(*highT, j); it; ++it){
293 // i = it.row();
294 // // Increment low-level transition matrix
295 // (*lowT)(comMap[(*member)(i)], comMap[(*member)(j)]) += (*highDist)(i) * it.value();
296 // }
297 // }
298 // // Normalize
299 // for (i = 0; i < ncom; i++)
300 // for (j = 0; j < ncom; j++)
301 // (*lowT)(i, j) = (*lowT)(i, j) / (*lowDist)(i);
302 
303 // return;
304 // }
305 
306 #endif
void filterTransitionMatrix(SpMatCSR *, gsl_vector *, gsl_vector *, double, int)
Remove weak nodes from a transition matrix.
Definition: atmarkov.hpp:173
void toAndStochastic(SpMatCSR *)
Make an Eigen CSR matrix and stochastic.
Definition: atmarkov.hpp:126
void toLeftStochastic(SpMatCSR *)
Make an Eigen CSR matrix left stochastic.
Definition: atmarkov.hpp:101
SparseMatrix< double, ColMajor > SpMatCSC
Eigen CSC matrix of double type.
Definition: atmarkov.hpp:29
void toRightStochastic(SpMatCSC *)
Make an Eigen CSC matrix right stochastic.
Definition: atmarkov.hpp:55
SparseMatrix< bool, ColMajor > SpMatCSCBool
Eigen CSC matrix of boolean type.
Definition: atmarkov.hpp:31
SparseMatrix< double, RowMajor > SpMatCSR
Eigen CSR matrix of double type.
Definition: atmarkov.hpp:25
gsl_vector * getRowSum(SpMatCSR *)
Get the sum of each row of an Eigen CSR matrix.
Definition: atmatrix.hpp:56
double getSum(SpMatCSR *)
Returns a the sum of the elements of an Eigen CSR matrix.
Definition: atmatrix.hpp:184
SparseMatrix< int, RowMajor > SpMatIntCSR
Eigen CSR matrix of integer type.
Definition: atmarkov.hpp:27
void normalizeVector(gsl_vector *)
Normalize a GSL vector by the sum of its elements.
Definition: atmatrix.hpp:222
void normalizeRows(SpMatCSR *, gsl_vector *)
Normalize each row of an Eigen CSR matrix by a GSL vector.
Definition: atmatrix.hpp:240
Eigen::SparseMatrix< double, Eigen::ColMajor > SpMatCSC
Eigen sparse CSC matrix of double type.
Definition: atio.hpp:27