-
Notifications
You must be signed in to change notification settings - Fork 0
/
cigam_functions.stan
131 lines (105 loc) · 2.89 KB
/
cigam_functions.stan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
functions {
real binomial_real_lpdf(real m, real n, real p) {
return m * log(p) + (n - m) * log(1 - p);
}
int[,] order_edges(int[,] edges_vector, int[] ordering_vector, int M_size, int K_size) {
int ordered_edges[M_size, K_size];
for (m in 1:M_size) {
for (k in 1:K_size) {
ordered_edges[m, k] = ordering_vector[edges_vector[m, k] + 1];
}
}
return ordered_edges;
}
int[] get_layers(vector ranks_vector, real[] thresholds, int N_size, int L_size) {
int layers[N_size];
int j = 1;
for (i in 1:N_size) {
if (thresholds[L_size] - ranks_vector[i] > thresholds[j]) {
j = j + 1;
}
layers[i] = j;
}
return layers;
}
int[,] get_num_layers(int[] layers, int N_size, int L_size) {
int temp[L_size];
int i = 1;
int j = 1;
int result[N_size, L_size];
for (l in 1:L_size) {
temp[l] = 0;
for (i1 in 1:N_size) {
result[i1, l] = 0;
}
}
for (l in 1:L_size) {
while (i <= N_size && layers[i] == l) {
temp[l] = temp[l] + 1;
i = i + 1;
}
}
// print("Temp ", temp);
for (i1 in 1:N_size) {
while (temp[j] == 0 && j <= L_size) {
j = j + 1;
}
temp[j] = temp[j] - 1;
// for (l in 1:L_size) {
// result[i1, l] = temp[l];
// }
result[i1, 1:L_size] = temp;
}
return result;
}
matrix get_partition_sizes(int[,] ordered_edges_vector, vector ranks_vector, int[] layers_vector, real[] H_vector, int N_size, int L_size, int M_size, int K_size) {
matrix[N_size, L_size] sizes;
int j;
real min_value;
real max_value;
int argmin;
int argmax;
for (i in 1:N_size) {
for (l in 1:L_size) {
sizes[i, l] = 0;
}
}
for (m in 1:M_size) {
// ranks are ordered decreasing
argmin = max(ordered_edges_vector[m, 1:K_size]);
argmax = min(ordered_edges_vector[m, 1:K_size]);
// argmin = -1;
// argmax = -1;
// for (k in 1:K_size) {
// if (edges_vector[m, k] == -1) break;
// if (argmin == -1 || ranks_vector[edges_vector[m, k]] <= min_value) {
// argmin = edges_vector[m, k];
// min_value = ranks_vector[argmin];
// }
// if (argmax == -1 || ranks_vector[edges_vector[m, k]] >= max_value) {
// argmax = edges_vector[m, k];
// max_value = ranks_vector[argmax];
// }
// }
sizes[argmax, layers_vector[argmin]] += 1;
}
return sizes;
}
matrix get_binomial_sizes(int[,] num_layers_vector, matrix binomial_coefficients_vector, int N_size, int L_size, int K_size) {
matrix[N_size, L_size] binomial_sizes;
int j;
for (i in 1:N_size) {
for (l in 1:L_size) {
binomial_sizes[i, l] = 0;
}
}
for (i in 1:(N_size - 1)) {
j = i + 1;
for (l in 1:L_size) {
binomial_sizes[i, l] = binomial_coefficients_vector[j + num_layers_vector[i, l] - i, K_size - 1 + 1] - binomial_coefficients_vector[j - i - 1 + 1, K_size - 1 + 1];
j += num_layers_vector[i, l];
}
}
return binomial_sizes;
}
}