utils.cpp
Go to the documentation of this file.
1 
10 #include <target/utils.hpp>
11 #include <algorithm>
12 
13 namespace target {
14 
15  // Complex step derivative
16  arma::mat deriv(cx_func f, arma::vec theta) {
17  arma::cx_vec thetac = arma::conv_to<arma::cx_vec>::from(theta);
18  arma::cx_mat val0 = f(thetac);
19  unsigned n = val0.n_elem;
20  unsigned p = theta.n_elem;
21  arma::mat res(n, p);
22  double h = DBL_MIN;
23  cx_dbl h0 = cx_dbl(0, h);
24  for (unsigned i=0; i < p; i++) {
25  arma::cx_vec theta0 = thetac;
26  theta0[i] += h0;
27  arma::mat val = imag(f(theta0))/h;
28  for (unsigned j=0; j < n; j++)
29  res(j, i) = val[j];
30  }
31  return(res);
32  }
33 
34 
35  // Find index of clusters/ids in sorted integer vector
36  arma::umat clusterid(const arma::uvec &id) {
37  unsigned ncl = 1;
38  unsigned n = id.size();
39  unsigned id0 = id(0);
40  for (unsigned i=0; i < n; i++) {
41  if (id(i) != id0) {
42  ncl++;
43  id0 = id(i);
44  }
45  }
46  arma::umat res(ncl, 2); // column 1: index, column 2: size
47  res.fill(0);
48  unsigned cl = 0;
49  id0 = id(0);
50  for (unsigned i=0; i < n; i++) {
51  if (id(i) != id0) {
52  cl++;
53  res(cl, 0) = i; // index
54  id0 = id(i);
55  }
56  res(cl, 1)++; // size
57  }
58  return res;
59  }
60 
61 
62  arma::mat groupsum(const arma::mat &x,
63  const arma::uvec &cluster,
64  bool reduce) {
65  unsigned ncl = cluster.n_elem;
66  unsigned n = ncl;
67  if (!reduce) {
68  n = x.n_rows;
69  }
70  unsigned stop;
71  arma::mat res(n, x.n_cols);
72  arma::rowvec tmp(x.n_cols);
73  for (unsigned i=0; i < ncl; i++) { // Iterate over individuals
74  unsigned start = cluster(i);
75  if (i == (ncl-1)) {
76  stop = x.n_rows;
77  } else {
78  stop = cluster(i+1);
79  }
80  tmp.fill(0);
81  for (unsigned j=start; j < stop; j++) {
82  tmp += x.row(j);
83  }
84  if (reduce) {
85  res.row(i) = tmp;
86  } else {
87  for (unsigned j=start; j < stop; j++) {
88  res.row(j) = tmp;
89  }
90  }
91  }
92  return(res);
93  }
94 
95 
96  void fastpattern(const arma::umat &y,
97  arma::umat &pattern,
98  arma::uvec &group,
99  unsigned categories) {
100  unsigned n = y.n_rows;
101  unsigned k = y.n_cols;
102 
103  arma::uvec mygroup(n);
104  double lognpattern = k*std::log(static_cast<double>(categories));
105  unsigned npattern = n;
106  if (lognpattern<std::log(static_cast<double>(npattern))) {
107  npattern = (unsigned) pow(static_cast<double>(categories),
108  static_cast<double>(k));
109  }
110  arma::umat mypattern(npattern, k);
111  mypattern.fill(1);
112  unsigned K = 0;
113 
114  for (unsigned i=0; i < n; i++) {
115  arma::urowvec ri = y.row(i);
116  bool found = false;
117  for (unsigned j=0; j < K; j++) {
118  if (sum(ri != mypattern.row(j)) == 0) {
119  found = true;
120  mygroup(i) = j;
121  break;
122  }
123  }
124  if (!found) {
125  K++;
126  mypattern.row(K-1) = ri;
127  mygroup(i) = K-1;
128  }
129  }
130  pattern = mypattern.rows(0, K-1);
131  group = mygroup;
132  }
133 
134 
135  arma::umat fastapprox(arma::vec &time, // sorted times
136  const arma::vec &newtime,
137  bool equal,
138  // type: (0: nearedst, 1: right, 2: left)
139  unsigned type) {
140  arma::uvec idx(newtime.n_elem);
141  arma::uvec eq(newtime.n_elem);
142 
143  double vmax = time(time.n_elem-1);
144  arma::mat::iterator it;
145  double upper = 0.0;
146  int pos;
147  for (unsigned i=0; i < newtime.n_elem; i++) {
148  eq[i] = 0;
149  if (newtime(i) > vmax) {
150  pos = time.n_elem-1;
151  } else {
152  it = std::lower_bound(time.begin(), time.end(), newtime(i));
153  upper = *it;
154  if (it == time.begin()) {
155  pos = 0;
156  if (equal && (newtime(i) == upper)) { eq(i) = 1; }
157  } else {
158  pos = static_cast<int>(it-time.begin());
159  if (type == 0 && std::fabs(newtime(i)-time(pos-1)) <
160  std::fabs(newtime(i)-time(pos)))
161  pos -= 1;
162  if (equal && (newtime(i) == upper)) { eq[i] = pos+1; }
163  }
164  }
165  if (type == 2 && newtime(i) < upper) pos--;
166  idx(i) = pos;
167  }
168  if (equal) {
169  return( arma::join_horiz(idx, eq) );
170  }
171  return( idx );
172  }
173 
174 
175  double SupTest(const arma::vec &D) {
176  return arma::max(arma::abs(D));
177  }
178 
179  double L2Test(const arma::vec &D, const arma::vec &t) {
180  arma::vec delta(t.n_elem);
181  for (unsigned i=0; i < t.n_elem-1; i++) delta(i) = t[i+1]-t[i];
182  delta(delta.n_elem-1) = 0;
183  return std::sqrt(sum(delta % D % D));
184  }
185 
186  // Comparison of ECDF of (x1,...,xn) with null CDF
187  // evaluated in G = (G(x1),...,G(xn))
188  double CramerVonMises(const arma::vec &x, arma::vec G) {
189  // back to original order of input data:
190  arma::uvec ord = arma::stable_sort_index(x);
191  G = G.elem(ord);
192  unsigned n = G.n_elem;
193  double res = 1/(12*n);
194  for (unsigned i=0; i < G.n_elem; i++) {
195  double val = (2*i-1)/(2*n)-G(i);
196  res += val*val;
197  }
198  return res;
199  }
200 
201 
202  arma::mat const EmptyMat = arma::mat();
203  arma::vec const EmptyVec = arma::vec();
204 
205 
206  // Foreground colors are in form of 3x, bacground are 4x
207  const char* COL_RESET = "\x1b[0m";
208  const char* COL_DEF = "\x1b[39m";
209  const char* BLACK = "\x1b[30m";
210  const char* RED = "\x1b[31m";
211  const char* MAGENTA = "\x1b[35m";
212  const char* YELLOW = "\x1b[33m";
213  const char* GREEN = "\x1b[32m";
214  const char* BLUE = "\x1b[34m";
215  const char* CYAN = "\x1b[36m";
216  const char* WHITE = "\x1b[37m";
217  const char* GRAY = "\x1b[90m";
218  const char* LRED = "\x1b[91m";
219  const char* LGREEN = "\x1b[92m";
220  const char* LYELLOW = "\x1b[93m";
221  const char* LBLUE = "\x1b[94m";
222  const char* LMAGENTA = "\x1b[95m";
223  const char* LCYAN = "\x1b[96m";
224  const char* LWHITE = "\x1b[97m";
225 
226 } // namespace target
Various utility functions and constants.