-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeasure.h
91 lines (69 loc) · 2.12 KB
/
Measure.h
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
#ifndef __MEASURE__
#define __MEASURE__
#include "Profile.h"
#include "Outcome.h"
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
inline double entropy( int up, int dn ) {
if ( up == 0 ) return 0.0;
else return - double(up) / dn * log2( double(up) / dn );
}
double chiSquare( vector<int> A, const int numTypeA,
vector<int> B, const int numTypeB,
Outcome outcome ) {
const int N = (int)outcome.size();
const int maxState = numTypeA * numTypeB;
int freq[ outcome.getNumTypes() ][ maxState ];
int rowsum[ outcome.getNumTypes() ];
int colsum[ maxState ];
memset( freq, 0, sizeof freq );
memset(rowsum, 0, sizeof rowsum);
memset(colsum, 0, sizeof colsum);
for( size_t i = 0 ; i < N ; ++i ) {
freq[ outcome[i] ][ A[i] * numTypeB + B[i] ]++;
rowsum[ outcome[i] ]++;
colsum[ A[i] * numTypeB + B[i] ]++;
}
double ret = 0.0;
for( int i = 0 ; i < outcome.getNumTypes() ; ++i ) {
for( int j = 0 ; j < maxState ; ++j ) {
double expected = rowsum[i] * colsum[j] / (double)N;
if( fabs(expected) < 1e-9 ) continue;
ret += (expected-freq[i][j]) * (expected-freq[i][j]) / expected;
}
}
return ret;
}
double mutualInformation( vector<int> A, const int numTypeA,
vector<int> B, const int numTypeB,
Outcome outcome ) {
const int maxState = numTypeA * numTypeB;
int freq[ outcome.getNumTypes() ][ maxState ];
memset( freq, 0, sizeof freq );
for( size_t i = 0 ; i < outcome.size() ; ++i ) {
freq[ outcome[i] ][ A[i] * numTypeB + B[i] ]++;
}
double H_Y = 0;
for( int i = 0 ; i < outcome.getNumTypes() ; ++i ) {
H_Y += entropy( outcome.getNumSubjects(i), outcome.size() );
}
double H_X = 0;
for( int i = 0 ; i < maxState ; ++i ) {
int colsum = 0;
for( int j = 0 ; j < outcome.getNumTypes() ; ++j ) {
colsum += freq[j][i];
}
H_X += entropy( colsum, outcome.size() );
}
double H_XY = 0;
for( int i = 0 ; i < outcome.getNumTypes() ; ++i ) {
for( int j = 0 ; j < maxState ; ++j ) {
H_XY += entropy( freq[i][j], outcome.size() );
}
}
return ( H_X + H_Y - H_XY );
return 0;
}
#endif