forked from whuang08/TreeScaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.cpp
246 lines (216 loc) · 5.88 KB
/
info.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* info.cpp
* Copyright (C) (2011) V.A. Traag, P. Van Dooren, Y. Nesterov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In case of any problems or bugs, please contact Vincent Traag at
* vincent (dot) traag (at) uclouvain (dot) be
*
* This software is based on the article
*
* V.A. Traag, P. Van Dooren, Y. Nesterov, "Narrow scope for resolution-free
* community detection" (2011) arXiv:1104.3083v1.
*
*/
// Originally based on:
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// This program must not be distributed without agreement of the above mentionned authors.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume
// Email : [email protected]
// Location : Paris, France
// Time : February 2008
//-----------------------------------------------------------------------------
#include "info.h"
using namespace std;
int* get_array(deque<int> m)
{
int* a = new int[m.size()];
int i = 0;
for(deque<int>::iterator it=m.begin(); it != m.end(); it++)
a[i++] = (*it);
return a;
}
std::deque<int> get_deque(int n, int* m)
{
std::deque<int> d(n);
for (int i = 0; i < n; i++)
d[i] = m[i];
return d;
}
double entropy(deque<int> m)
{
double e = entropy_un(m);
return (e/m.size()) + log((double)m.size());
}
double entropy_un(deque<int> m)
{
int* ma = get_array(m);
double e = entropy_un(m.size(), ma);
delete [] ma;
return e;
}
double entropy(int n, int* m)
{
double e = entropy_un(n, m);
return (e/n) + log((double)n);
}
double entropy_un(int n, int* m)
{
double entrpy = 0.0;
//Create probabilities
map<int, double> table;
for ( int i = 0; i < n; i++)
{
table[m[i]] += 1;
}
//Calc entropy
for ( map<int, double>::iterator e=table.begin(); e != table.end(); e++)
{
if (e->second > 0)
entrpy -= (e->second) * log(e->second);
}
return entrpy;
}
double joint_entropy(deque<int> m1, deque<int> m2)
{
double je = joint_entropy_un(m1, m2);
return (je/m1.size()) + log((double)m1.size());
}
double joint_entropy_un(deque<int> m1, deque<int> m2)
{
int* ma1 = get_array(m1);
int* ma2 = get_array(m2);
double e = joint_entropy_un(m1.size(), ma1, ma2);
delete [] ma1;
delete [] ma2;
return e;
}
double joint_entropy(int n, int* m1, int* m2)
{
double je = joint_entropy_un(n, m1, m2);
return (je/n) + log((double)n);
}
double joint_entropy_un(int n, int* m1, int* m2)
{
//Create probabilities
map< int, map<int, double> > table;
for (int i = 0; i < n; i++)
{
table[m1[i]][m2[i]] += 1;
}
//Calc joing entropy
double joint_e = 0.0;
for ( map< int, map<int, double> >::iterator ex=table.begin(); ex != table.end(); ex++)
{
for (map<int, double>::iterator ey=(ex->second).begin(); ey != (ex->second).end(); ey++)
{
if (ey->second > 0)
joint_e -= (ey->second) * log(ey->second);
}
}
return joint_e;
}
double calc_vi(deque<int> m1, deque<int> m2)
{
int* ma1 = get_array(m1);
int* ma2 = get_array(m1);
double e = calc_nvoi(m1.size(), ma1, ma2);
delete ma1;
delete ma2;
return e;
}
double calc_vi(int n, int* m1, int* m2)
{
double e1 = entropy_un(n, m1);
double e2 = entropy_un(n, m2);
double joint_e = joint_entropy_un(n, m1, m2);
double nvoi = 0.0;
nvoi = (2*joint_e - (e1 + e2))/n;
if (nvoi < 0) { nvoi *= -1; }
return nvoi;
}
double calc_nvoi(deque<int> m1, deque<int> m2)
{
int* ma1 = get_array(m1);
int* ma2 = get_array(m1);
double e = calc_nvoi(m1.size(), ma1, ma2);
delete ma1;
delete ma2;
return e;
}
double calc_nvoi(int n, int* m1, int* m2)
{
double e1 = entropy_un(n, m1);
double e2 = entropy_un(n, m2);
double joint_e = joint_entropy_un(n, m1, m2);
double nvoi = 0.0;
double nlogn = (double)n*log((double)n);
nvoi = 2 - (e1 + e2 + 2*nlogn)/(joint_e + nlogn);
if (nvoi < 0) { nvoi *= -1; }
return nvoi;
}
double calc_nmi(deque<int> m1, deque<int> m2)
{
int* ma1 = get_array(m1);
int* ma2 = get_array(m1);
double e = calc_nmi(m1.size(), ma1, ma2);
delete [] ma1;
delete [] ma2;
return e;
}
double calc_nmi(int n, int* m1, int* m2)
{
//Create probabilities 1
map<int, double> p1;
for ( int i = 0; i < n; i++)
{
p1[m1[i]] += 1;
}
//Create probabilities 2
map<int, double> p2;
for ( int i = 0; i < n; i++)
{
p2[m2[i]] += 1;
}
//Create joint probabilities
map< int, map<int, double> > table;
for (int i = 0; i < n; i++)
{
table[m1[i]][m2[i]] += 1;
}
//Calc mutual inf
double mutual_inf = 0.0;
for ( map< int, map<int, double> >::iterator ex=table.begin(); ex != table.end(); ex++)
{
for (map<int, double>::iterator ey=(ex->second).begin(); ey != (ex->second).end(); ey++)
{
if ( p1[ex->first] > 0 && p2[ey->first] > 0)
{
if (ey->second > 0)
mutual_inf += (ey->second) * log((ey->second) / (p1[ex->first] * p2[ey->first]) );
}
}
}
double e1 = entropy_un(n, m1);
double e2 = entropy_un(n, m2);
double nd = (double)n;
double nmi = 2*(mutual_inf + nd*log(nd))/(e1 + e2 + 2*nd*log(nd));
if (nmi < 0) { nmi *= -1; }
return nmi;
}