-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMeasureCalculator.java
More file actions
178 lines (167 loc) · 6.46 KB
/
Copy pathFMeasureCalculator.java
File metadata and controls
178 lines (167 loc) · 6.46 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fmeasurecalculator;
import java.util.ArrayList;
import java.util.List;
/**
* FMeasureCalculator
* @author funlu
*/
public class FMeasureCalculator {
private boolean fn4Each = true;
private int fn4EachValue = 0;
private final List<ClusterInstance> instances;
public FMeasureCalculator(List<ClusterInstance> instances) {
this.instances = instances;
}
/**
* False Negative (FN) Assignment: when similar members are assigned to different communities. This is an incorrect decision
* @return
*/
public int calculateFN(){
this.fn4EachValue = 0;
this.fn4Each = true;
for(ClusterInstance instance : instances){
List<ClusterInstance> operationalClusters = new ArrayList<ClusterInstance>();
for(ClusterInstance tobeChecked : instances){
if(!instance.getClusterName().equals(tobeChecked.getClusterName())){
//for(ClusterMember member : instance.getMembers()){
if(tobeChecked.hasMember(instance.getClusterMember())){
//falseNegativeValue = falseNegativeValue + (tobeChecked.getMember(instance.getClusterMember()).getMemberCount() * instance.getClusterMember().getMemberCount());
operationalClusters.add(tobeChecked);
}
//}
}
}
calculateFN4EachCluster(instance,operationalClusters);
this.fn4Each = true;
}
return this.fn4EachValue;
}
/**
* calculateFN4EachCluster
* @param instance
* @param operationalClusters
* @return
*/
private int calculateFN4EachCluster(ClusterInstance instance,List<ClusterInstance> operationalClusters){
if(operationalClusters.size() >= 1){
if(fn4Each){
for(ClusterInstance tobeChecked : operationalClusters){
fn4EachValue = fn4EachValue + (tobeChecked.getMember(instance.getClusterMember()).getMemberCount() * instance.getClusterMember().getMemberCount());
}
fn4Each = false;
}else{
ClusterInstance temp = operationalClusters.get(0);
for(ClusterInstance tobeChecked : operationalClusters){
if(!temp.getClusterName().equals(tobeChecked.getClusterName()))
fn4EachValue = fn4EachValue + (tobeChecked.getMember(instance.getClusterMember()).getMemberCount() * temp.getMember(instance.getClusterMember()).getMemberCount());
}
operationalClusters.remove(temp);
}
return calculateFN4EachCluster(instance,operationalClusters);
}
return fn4EachValue;
}
/**
* False Positive (FP) Assignment: when dissimilar members are assigned to the same community. This is an incorrect decision
* @return
*/
public int calculateFP(){
int falsePositiveValue = 0;
for(ClusterInstance instance : instances){
//Check if cluster instance contains more than 1 member, otherwise not added to calculation
if(instance.getMembers().size() > 1){
for (int i = 0; i < instance.getMembers().size(); i++) {
for (int j = i + 1; j < instance.getMembers().size(); j++) {
falsePositiveValue = falsePositiveValue + (instance.getMembers().get(i).getMemberCount() * instance.getMembers().get(j).getMemberCount());
boolean foundExtra = false;
for (int k = 0; k < instance.getMembers().size() && !foundExtra; k++){
if (k != j && k != i){
for (int l = 0; l < instance.getMembers().size(); l++){
if (l != k && l != j && l != i){
foundExtra = true;
break;
}
}
}
}
}
}
}
}
return falsePositiveValue;
}
/**
* calculateTPandFP
* @return
*/
public int calculateTPandFP(){
//total N(N−1)/2 for each class
int toReturn = 0;
for(ClusterInstance instance : instances){
toReturn += (instance.getTotalInstances()*(instance.getTotalInstances()-1))/2;
}
return toReturn;
}
/**
* calculateTotalPairs
* @return
*/
public int calculateTotalPairs(){
//N(N−1)/2
int totalClusterMembers = 0;
for(ClusterInstance instance : instances){
totalClusterMembers += instance.getTotalInstances();
}
return (totalClusterMembers*(totalClusterMembers-1))/2;
}
/**
* True Positive (TP) Assignment: when similar members are assigned to the same community. This is a correct decision.
* easier version
* @return
*/
public int calculateTPv1(){
//TP=totalPositives - FP
return calculateTPandFP() - calculateFP();
}
/**
* True Negative (TN) Assignment: when dissimilar members are assigned to different communities. This is a correct decision.
* @return
*/
public int calculateTNv1(){
//totalNegatives=N−totalPositives
//TN=totalNegatives-FN
this.fn4EachValue = 0;
this.fn4Each = true;
int totalNegatives = calculateTotalPairs()- calculateTPandFP();
return totalNegatives - calculateFN();
}
/**
* calculateRecall
* @return
*/
public double calculateRecall(){
// R = TP/TP + FN
return (double) calculateTPv1() / (double)(calculateTPv1() + calculateFN());
}
/**
* calculatePrecision
* @return
*/
public double calculatePrecision(){
//P = TP /(TP + FP)
return (double)calculateTPv1() / (double)(calculateTPv1() + calculateFP());
}
/**
* calculateFMeasure
* @return
*/
public double calculateFMeasure(){
//F = 2*((P*R)/(P+R));
return 2*((calculatePrecision()*calculateRecall())/(calculatePrecision()+calculateRecall()));
}
}