barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
network-css.hpp
Go to the documentation of this file.
1 #ifndef BARRY_CSS_COUNTERS
2 #define BARRY_CSS_COUNTERS
3 
4 // n: Net size,
5 // s: Start of the i-th network
6 // e: end of the i-th network
7 // ego_id: Ego of the cell (i, j)
8 #define CSS_SIZE() \
9  size_t n = data.indices[0u]; \
10  size_t s = data.indices[1u]; \
11  size_t e = data.indices[2u]; \
12  size_t ctype = data.indices[3u]; \
13  size_t ego_id = data.indices[4u]; \
14  if (ctype > 2) \
15  throw std::range_error("Counter type should be 0, 1, or 2.");
16 
17 // Check whether ego_id is involved in the current cell
18 // ctype: Type of counter
19 // 0: All cells
20 // 1: Only if perceiver
21 // 2: Only if not perceiver
22 #define CSS_MATCH_TYPE() \
23  if (ctype != 0u) { /* all counts */ \
24  if (ctype == 1u) { /* Only if perceiver */ \
25  if ((i_ != ego_id) && (j_ != ego_id)) return 0.0; \
26  } else if (ctype == 2u) { /* Only if not perceiver */ \
27  if ((i_ == ego_id) || (j_ == ego_id)) return 0.0; \
28  } \
29  };
30 
31 // Variables in case that the current cell corresponds to the True
32 #define CSS_CASE_TRUTH() if ((i < n) && (j < n))
33 
34 // i_: i-th index of the cell
35 // j_: j-th index of the cell
36 // tji: True value of the cell (i, j)
37 // pij: Perceived value of the cell (i, j)
38 // pji: Perceived value of the cell (j, i)
39 #define CSS_TRUE_CELLS() \
40  size_t i_ = i; \
41  size_t j_ = j; \
42  CSS_MATCH_TYPE() \
43  double tji = static_cast<double>(Array(j, i, false)); \
44  double pij = static_cast<double>(Array(i + s, j + s, false)); \
45  double pji = static_cast<double>(Array(j + s, i + s, false));
46 
47 // Variables in case that the current cell corresponds to the Perceived
48 #define CSS_CASE_PERCEIVED() else if (((i >= s) && (i < e)) & ((j >= s) && (j < e)))
49 
50 // i_: i-th index of the cell
51 // j_: j-th index of the cell
52 // tji: True value of the cell (i, j)
53 // pji: Perceived value of the cell (i, j)
54 // tij: True value of the cell (j, i)
55 #define CSS_PERCEIVED_CELLS() \
56  size_t i_ = i - s; \
57  size_t j_ = j - s; \
58  CSS_MATCH_TYPE() \
59  double tji = static_cast<double>(Array(j - s, i - s, false)); \
60  double pji = static_cast<double>(Array(j, i, false)); \
61  double tij = static_cast<double>(Array(i - s, j - s, false));
62 
63 
64 
65 // Nothing for else (for now)
66 #define CSS_CASE_ELSE()
67 
68 // Checks whether the start and end of the node (perceived network) falls within
69 // the boundaries of the graph.
70 #define CSS_CHECK_SIZE_INIT() \
71  /* The indices fall within the network */ \
72  if ((data.indices.at(0) > Array.ncol()) \
73  | (data.indices.at(2) > Array.ncol())) \
74  throw std::range_error("The network does not match the prescribed size.");
75 
76 #define CSS_CHECK_SIZE() for (size_t i = 0u; i < end_.size(); ++i) {\
77  if (i == 0u) continue; \
78  else if (end_[i] < end_[i-1u]) \
79  throw std::logic_error("Endpoints should be specified in order.");}
80 
81 #define CSS_APPEND(name) std::string name_ = (name);\
82  for (size_t i = 0u; i < end_.size(); ++i) { \
83  std::string tmpname = name_ + " (" + std::to_string(i) + ")" + \
84  ((counter_type == 1u) ? " (only perceiver)" : ((counter_type == 2u)? " (only alters)": ""));\
85  counters->add_counter(tmp_count, tmp_init, nullptr, \
86  NetCounterData({netsize, i == 0u ? netsize : end_[i-1], end_[i], counter_type, i}, {}),\
87  tmpname);}
88 
89 #define CSS_NET_COUNTER_LAMBDA_INIT() NETWORK_COUNTER_LAMBDA(tmp_init) {\
90  CSS_CHECK_SIZE_INIT() \
91  return 0.0; \
92  };
93 
94 
106 template<typename Tnet = Network>
109  size_t netsize,
110  const std::vector< size_t > & end_,
111  size_t counter_type = 0u
112 ) {
113 
114  NETWORK_COUNTER_LAMBDA(tmp_count) {
115 
116 
117  // Getting the network size
118  CSS_SIZE()
119 
120  // True network
122  {
123 
124  // Checking change stat of the true net
126  return pij * pji * (1.0 - 2.0 * tji) - (1.0 - tji)*(
127  pij * (1.0 - pji) + (1.0 - pij) * pji
128  );
129 
130  } CSS_CASE_PERCEIVED() {
131 
132  // Checking change stat of the percieved net
134  return pji * (tij * (1.0 - tji) + (1.0 - tij) * tji) +
135  (1.0 - tij) * (1.0 - tji) * (1 - 2.0 * pji)
136  ;
137 
138  } CSS_CASE_ELSE()
139  return 0.0;
140 
141  };
142 
144 
145  // checking sizes
147  CSS_APPEND("Partially false recip (comission)")
148 
149  return;
150 
151 }
152 
154 template<typename Tnet = Network>
157  size_t netsize,
158  const std::vector< size_t > & end_,
159  size_t counter_type = 0u
160 ) {
161 
162  NETWORK_COUNTER_LAMBDA(tmp_count) {
163 
164  // Getting the network size
165  CSS_SIZE()
166 
167  // True network
169  {
170 
172  return tji * ((1.0 - pij) * pji + pij * (1.0 - pji)) +
173  (1.0 - 2.0 * tji) * (1.0 - pij) * (1.0 - pji)
174  ;
175 
176  } CSS_CASE_PERCEIVED() {
177 
179  return tji * tij * (1.0 - 2.0 * pji) -
180  (1.0 - pji) * ((1.0 - tij) * tji + tij * (1.0 - tji))
181  ;
182 
183  } CSS_CASE_ELSE()
184  return 0.0;
185 
186  };
187 
189 
190  // checking sizes
192  CSS_APPEND("Partially false recip (omission)")
193 
194  return;
195 
196 }
197 
199 template<typename Tnet = Network>
202  size_t netsize,
203  const std::vector< size_t > & end_,
204  size_t counter_type = 0u
205 ) {
206 
207  NETWORK_COUNTER_LAMBDA(tmp_count) {
208 
209  // Getting the network size
210  CSS_SIZE()
211 
212  // True network
214  {
215 
217  return -(1.0 - tji) * pij * pji;
218 
219  } CSS_CASE_PERCEIVED() {
220 
222  return (1.0 - tij) * (1.0 - tji) * pji;
223 
224  } CSS_CASE_ELSE()
225  return 0.0;
226 
227  };
228 
230 
231  // checking sizes
233  CSS_APPEND("Completely false recip (comission)")
234 
235  return;
236 
237 }
238 
240 template<typename Tnet = Network>
243  size_t netsize,
244  const std::vector< size_t > & end_,
245  size_t counter_type = 0u
246 ) {
247 
248  NETWORK_COUNTER_LAMBDA(tmp_count) {
249 
250  // Getting the network size
251  CSS_SIZE()
252 
253  // True network
255  {
256 
258  return tji * (1.0 - pij) * (1.0 - pji);
259 
260  } CSS_CASE_PERCEIVED() {
261 
263  return - tij * tji * (1.0 - pji);
264 
265  } CSS_CASE_ELSE()
266  return 0.0;
267 
268  };
269 
271 
272  // checking sizes
274  CSS_APPEND("Completely false recip (omission)")
275 
276  return;
277 
278 }
279 
281 template<typename Tnet = Network>
284  size_t netsize,
285  const std::vector< size_t > & end_,
286  size_t counter_type = 0u
287 ) {
288 
289  NETWORK_COUNTER_LAMBDA(tmp_count) {
290 
291  // Getting the network size
292  CSS_SIZE()
293 
294  // True network
296  {
297 
299  return (1.0 - tji) * (1.0 - pij) * pji - tji * pij * (1.0 - pji);
300 
301  } CSS_CASE_PERCEIVED() {
302 
304  return (1.0 - tij) * tji * (1.0 - pji) - tij * (1.0 - tij) * pji;
305 
306  } CSS_CASE_ELSE()
307  return 0.0;
308 
309  };
310 
312 
313  // checking sizes
315  CSS_APPEND("Mixed reciprocity errors")
316 
317  return;
318 
319 }
320 
322 
323 template<typename Tnet = Network>
326  size_t netsize,
327  const std::vector< size_t > & end_,
328  size_t counter_type = 0u
329 ) {
330 
331  NETWORK_COUNTER_LAMBDA(tmp_count)
332  {
333 
334  // Getting the network size
335  CSS_SIZE()
336 
337  // True network
339  {
340 
342  return -(1.0 - pij) * (1.0 - pji) * (1.0 - tji);
343 
344  } CSS_CASE_PERCEIVED() {
345 
347  return -(1.0 - tij) * (1.0 - tji) * (1.0 - pji);
348 
349  } CSS_CASE_ELSE()
350  return 0.0;
351 
352  };
353 
354  // CSS_NET_COUNTER_LAMBDA_INIT()
355  NETWORK_COUNTER_LAMBDA(tmp_init)
356  {
357 
359  double n_dbl = static_cast<double>(data.indices[0u]);
360 
361  // Discount in case of the type of counter
362  size_t ctype = data.indices[3u];
363 
364  if (ctype == 1u) /* Only perceiver */
365  {
366 
367  return (n_dbl - 1.0); // * (Array.D().directed ? 2.0 : 1.0);
368 
369  } else if (ctype == 2u) /* All but the perceiver */
370  {
371  // We remove the perceiver from the eq.
372  n_dbl -= 1.0;
373  }
374 
375  // At the beginning is all zero
376  return n_dbl * (n_dbl - 1.0) / 2.0; // / (Array.D().directed ? 1.0 : 2.0);
377 
378  };
379 
380  // checking sizes
382  CSS_APPEND("(01) Accurate null")
383 
384  return;
385 
386 }
387 
388 template<typename Tnet = Network>
391  size_t netsize,
392  const std::vector< size_t > & end_,
393  size_t counter_type = 0u
394 ) {
395 
396  NETWORK_COUNTER_LAMBDA(tmp_count) {
397 
398  // Getting the network size
399  CSS_SIZE()
400 
401  // True network
403  {
404 
406  return -(1.0 - tji) * ((1.0 - pij) * pji + pij * (1.0 - pji));
407 
408  } CSS_CASE_PERCEIVED() {
409 
411  return (1.0 - tij) * (1.0 - tji) * (1 - 2.0 * pji);
412 
413  } CSS_CASE_ELSE()
414  return 0.0;
415 
416  };
417 
419 
420  // checking sizes
422  CSS_APPEND("(02) Partial false positive (null)")
423 
424  return;
425 
426 }
427 
428 template<typename Tnet = Network>
431  size_t netsize,
432  const std::vector< size_t > & end_,
433  size_t counter_type = 0u
434 ) {
435 
436  NETWORK_COUNTER_LAMBDA(tmp_count) {
437 
438  // Getting the network size
439  CSS_SIZE()
440 
441  // True network
443  {
444 
446  return -(1.0 - tji) * pij * pji;
447 
448  } CSS_CASE_PERCEIVED() {
449 
451  return (1.0 - tij) * (1.0 - tji) *pji;
452 
453  } CSS_CASE_ELSE()
454  return 0.0;
455 
456  };
457 
459 
460  // checking sizes
462  CSS_APPEND("(03) Complete false positive (null)")
463 
464  return;
465 
466 }
467 
468 template<typename Tnet = Network>
471  size_t netsize,
472  const std::vector< size_t > & end_,
473  size_t counter_type = 0u
474 ) {
475 
476  NETWORK_COUNTER_LAMBDA(tmp_count) {
477 
478  // Getting the network size
479  CSS_SIZE()
480 
481  // True network
483  {
484 
486  return (1.0 - pij) * (1.0 - pji) * (1.0 - 2.0 * tji);
487 
488  } CSS_CASE_PERCEIVED() {
489 
491  return -(1.0 - pji) * ((1.0 - tij) * tji + tij * (1.0 - tji));
492 
493  } CSS_CASE_ELSE()
494  return 0.0;
495 
496  };
497 
499 
500  // checking sizes
502  CSS_APPEND("(04) Partial false negative (assym)")
503 
504  return;
505 
506 }
507 
508 template<typename Tnet = Network>
511  size_t netsize,
512  const std::vector< size_t > & end_,
513  size_t counter_type = 0u
514 ) {
515 
516  NETWORK_COUNTER_LAMBDA(tmp_count) {
517 
518  // Getting the network size
519  CSS_SIZE()
520 
521  // True network
523  {
524 
526  return pij * (1.0 - tji) * (1.0 - pji) - (1.0 - pij) * tji * pji;
527 
528  } CSS_CASE_PERCEIVED() {
529 
531  return tij * (1.0 - tji) * (1.0 - pji) - (1.0 - tij) * tji * pji;
532 
533  } CSS_CASE_ELSE()
534  return 0.0;
535 
536  };
537 
539 
540  // checking sizes
542  CSS_APPEND("(05) Accurate assym")
543 
544  return;
545 
546 }
547 
548 template<typename Tnet = Network>
551  size_t netsize,
552  const std::vector< size_t > & end_,
553  size_t counter_type = 0u
554 ) {
555 
556  NETWORK_COUNTER_LAMBDA(tmp_count) {
557 
558  // Getting the network size
559  CSS_SIZE()
560 
561  // True network
563  {
564 
566  return (1.0 - pij) * (1.0 - tji) * pji - pij * tji * (1.0 - pji);
567 
568  } CSS_CASE_PERCEIVED() {
569 
571  return (1.0 - tij) * tji * (1.0 - pji) - tij * (1.0 - tji) * pji;
572 
573  } CSS_CASE_ELSE()
574  return 0.0;
575 
576  };
577 
579 
580  // checking sizes
582  CSS_APPEND("(06) Mixed assym")
583 
584  return;
585 
586 }
587 
588 template<typename Tnet = Network>
591  size_t netsize,
592  const std::vector< size_t > & end_,
593  size_t counter_type = 0u
594 ) {
595 
596  NETWORK_COUNTER_LAMBDA(tmp_count) {
597 
598  // Getting the network size
599  CSS_SIZE()
600 
601  // True network
603  {
604 
606  return pij * pji * (1.0 - 2.0 * tji);
607 
608  } CSS_CASE_PERCEIVED() {
609 
611  return pji * (tij * (1.0 - tji) + (1.0 - tij) * tji);
612 
613  } CSS_CASE_ELSE()
614  return 0.0;
615 
616  };
617 
619 
620  // checking sizes
622  CSS_APPEND("(07) Partial false positive (assym)")
623 
624  return;
625 
626 }
627 
628 template<typename Tnet = Network>
631  size_t netsize,
632  const std::vector< size_t > & end_,
633  size_t counter_type = 0u
634 ) {
635 
636  NETWORK_COUNTER_LAMBDA(tmp_count) {
637 
638  // Getting the network size
639  CSS_SIZE()
640 
641  // True network
643  {
644 
646  return tji * (1.0 - pij) * (1.0 - pji);
647 
648  } CSS_CASE_PERCEIVED() {
649 
651  return - tij * tji * (1.0 - pji);
652 
653  } CSS_CASE_ELSE()
654  return 0.0;
655 
656  };
657 
659 
660  // checking sizes
662  CSS_APPEND("(08) Complete false negative (full)")
663 
664  return;
665 
666 }
667 
668 template<typename Tnet = Network>
671  size_t netsize,
672  const std::vector< size_t > & end_,
673  size_t counter_type = 0u
674 ) {
675 
676  NETWORK_COUNTER_LAMBDA(tmp_count) {
677 
678  // Getting the network size
679  CSS_SIZE()
680 
681  // True network
683  {
684 
686  return tji * (pij * (1.0 - pji) + (1.0 - pij) * pji);
687 
688  } CSS_CASE_PERCEIVED() {
689 
691  return tij * tji * (1.0 - 2.0 * pji);
692 
693  } CSS_CASE_ELSE()
694  return 0.0;
695 
696  };
697 
699 
700  // checking sizes
702  CSS_APPEND("(09) Partial false negative (full)")
703 
704  return;
705 
706 }
707 
708 template<typename Tnet = Network>
711  size_t netsize,
712  const std::vector< size_t > & end_,
713  size_t counter_type = 0u
714 ) {
715 
716  NETWORK_COUNTER_LAMBDA(tmp_count) {
717 
718  // Getting the network size
719  CSS_SIZE()
720 
721  // True network
723  {
724 
726  return tji * pij * pji;
727 
728  } CSS_CASE_PERCEIVED() {
729 
731  return tij * tji * pji;
732 
733  } CSS_CASE_ELSE()
734  return 0.0;
735 
736  };
737 
739 
740  // checking sizes
742  CSS_APPEND("(10) Accurate full")
743 
744  return;
745 
746 }
747 
748 #undef CSS_APPEND
749 #undef CSS_CASE_TRUTH
750 #undef CSS_TRUE_CELLS
751 #undef CSS_CASE_PERCEIVED
752 #undef CSS_PERCEIVED_CELLS
753 #undef CSS_CASE_ELSE
754 #undef CSS_CHECK_SIZE_INIT
755 #undef CSS_CHECK_SIZE
756 #undef CSS_NET_COUNTER_LAMBDA_INIT
757 #undef CSS_MATCH_TYPE
758 #undef CSS_SIZE
759 #endif
Vector of counters.
Data_Type &&counter_ data(std::move(counter_.data))
void counter_css_census06(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_completely_false_recip_omiss(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
Counts completely false reciprocity (omission)
void counter_css_census04(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_census10(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
#define CSS_CHECK_SIZE_INIT()
Definition: network-css.hpp:70
void counter_css_census03(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_census02(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_census08(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_mixed_recip(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
Counts mixed reciprocity errors.
void counter_css_census01(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_partially_false_recip_commi(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
Counts errors of commission.
#define CSS_CHECK_SIZE()
Definition: network-css.hpp:76
void counter_css_census05(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
#define CSS_PERCEIVED_CELLS()
Definition: network-css.hpp:55
#define CSS_CASE_TRUTH()
Definition: network-css.hpp:32
#define CSS_NET_COUNTER_LAMBDA_INIT()
Definition: network-css.hpp:89
void counter_css_census09(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_completely_false_recip_comiss(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
Counts completely false reciprocity (comission)
#define CSS_SIZE()
Definition: network-css.hpp:8
#define CSS_CASE_ELSE()
Definition: network-css.hpp:66
#define CSS_CASE_PERCEIVED()
Definition: network-css.hpp:48
#define CSS_APPEND(name)
Definition: network-css.hpp:81
#define CSS_TRUE_CELLS()
Definition: network-css.hpp:39
void counter_css_census07(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
void counter_css_partially_false_recip_omiss(NetCounters< Tnet > *counters, size_t netsize, const std::vector< size_t > &end_, size_t counter_type=0u)
Counts errors of omission.
#define NETWORK_COUNTER_LAMBDA(a)
Definition: network.hpp:119