-
Notifications
You must be signed in to change notification settings - Fork 0
/
fr_test.pl
executable file
·345 lines (275 loc) · 8.47 KB
/
fr_test.pl
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/perl -w
use lib('/home/munzmatt/lib/perl5');
use strict;
use warnings;
use List::Util qw(sum);
use Statistics::Distributions;
#################################################
# Author: Matthias Munz
# Contact: matthias<DOT>munz<AT>gmx<DOT>de
#################################################
# This program runs the Friedman-Rufsky (FR) Test
# Input: Two datasets consisting of n-dimensional data points
# Workflow:
# 1. Compute distance matrix
# 2. Create minimal spanning tree MST using Prim
# 3. Compute number of runs
# 4. Compute mean, variance, permutation parameter and quantity W
# 5. Compute similarity measure
if (!$ARGV[0] || !$ARGV[1] || !$ARGV[2] || !$ARGV[3]){
print STDOUT "Input: <dataset1> <dataset2> <header_flag> <print_dist_flag>
<dataset1>\t\t\ttab-delimited
<dataset2>\t\t\ttab-delimited
<header_flag>\t\t{-1,1}, files contain header
<print_dist_flag>\t{-1,1}, print distance matrix
";
exit(0);
}
my $data_file1 = $ARGV[0];
my $data_file2 = $ARGV[1];
my $header = $ARGV[2]; $header = 0 if $ARGV[2] == -1;
my $dist = $ARGV[3]; $dist = 0 if $ARGV[3] == -1;
# Parse data (tsv files)
my @dataset1 = &_parse_data($data_file1, $header);
my @dataset2 = &_parse_data($data_file2, $header);
#################################################
# Workflow
# Step 1
# Compute distance table
my $distance_table = &_create_distance_table([@dataset1, @dataset2]);
if ($dist){
&_print_matrix($distance_table);
}
# Step 2
# Compute MST Prim Algo
my $tree = &_prim($distance_table);
# Step 3
# Compute number of runs
my $runs = &_count_FR_runs($tree, @dataset1 - 1);
print "Number of FR-Runs: ".$runs."\n";
# Step 4
# Compute permutation parameter C
my $C = &_compute_FR_permutation_par($tree);
print "FR-Permutation parameter C: $C\n";
# Compute variance
my $variance = &_compute_FR_variance($C, scalar @dataset1, scalar @dataset2);
print "FR-Variance: $variance\n";
# Compute mean
my $mean = &_compute_FR_mean(scalar @dataset1, scalar @dataset2);
print "FR-Mean: $mean\n";
# Compute quantity W (value of random variable)
my $W = &_compute_FR_quantity($variance, $mean);
print "FR-Quantity W: $W\n";
# Step 5
# Compute similarity measure (area under density curve left of W)
# 1 is the best similarity
my $uprob=Statistics::Distributions::uprob($W);
print "Datasets 'A' and 'B' have a similarity score of ".((1-$uprob)*100)."\n";
#################################################
# Routines
# Parse data (tsv files)
# Row: date, Column: attribute
sub _parse_data {
my ($data_file, $header) = @_;
my $first = 1;
my @result;
open(IN, "<$data_file") || die "Can't open file $data_file: $!";
while(<IN>){
if ($header && $first){
$first = 0;
next;
}
chomp($_);
my @date = split(/\t/, $_);
push(@result, \@date);
}
return @result;
}
# Compute quantity W
# Source of formular: Shao, Jie; Distribution-based Similarity Measures for Multi-dimensional Point Set Retrieval Applications; 2008
sub _compute_FR_quantity {
my ($variance, $mean) = @_;
return ($runs - $mean)/sqrt($variance);
}
# Compute mean
# Source of formular: Shao, Jie; Distribution-based Similarity Measures for Multi-dimensional Point Set Retrieval Applications; 2008
sub _compute_FR_mean {
my ($m, $n) = @_;
my $N = $m + $n;
return ((2*$m*$n)/$N)+1;
}
# Compute variance
# Source of formular: Shao, Jie; Distribution-based Similarity Measures for Multi-dimensional Point Set Retrieval Applications; 2008
sub _compute_FR_variance {
my ($C, $m, $n) = @_;
my $N = $m + $n;
my $variance = ((2*$m*$n)/($N*($N-1))) *
( (((2*$m*$n)-$N)/$N) +
((($C-$N+2)/(($N-2)*($N-3))) * (($N*($N-1))-(4*$m*$n)+2)) );
return $variance;
}
# Compute permutation parameter C
# Source of formular: Shao, Jie; Distribution-based Similarity Measures for Multi-dimensional Point Set Retrieval Applications; 2008
sub _compute_FR_permutation_par {
my $tree = shift;
my %vertice2n_edges;
foreach (@{$tree}){
if (exists($vertice2n_edges{${$_}[0]})){
$vertice2n_edges{${$_}[0]}++;
}
else {
$vertice2n_edges{${$_}[0]} = 1;
}
if (exists($vertice2n_edges{${$_}[1]})){
$vertice2n_edges{${$_}[1]}++;
}
else {
$vertice2n_edges{${$_}[1]} = 1;
}
}
my $C = 0;
foreach (keys(%vertice2n_edges)){
my $degree = $vertice2n_edges{$_};
$C += $degree * ($degree - 1);
}
return $C/2;
}
# Count number of runs
# in the context of the FR-Test
sub _count_FR_runs {
my ($tree, $s1_max_idx) = @_;
my $runs = 1;
foreach (@{$tree}){
if ((${$_}[0] <= $s1_max_idx && ${$_}[1] > $s1_max_idx) ||
(${$_}[0] > $s1_max_idx && ${$_}[1] <= $s1_max_idx)){
$runs++;
}
}
return $runs;
}
# Compute euclidian distance
sub _get_distance_between {
if (@_ != 2)
{
die "You must supply 2 coordinates";
}
my $a_ref = shift;
my $b_ref = shift;
if (@{$a_ref} != @{$b_ref}){
die "Coordinates do not have the same number of elements";
}
else {
my @x;
for (my $i = 0; $i < @{$a_ref}; $i++){
push(@x, (${$a_ref}[$i] - ${$b_ref}[$i])**2);
}
return sqrt(sum(@x));
}
}
# Create distance table for data points
sub _create_distance_table {
my $data_points = shift;
my $cols = 0;
my @result;
for (my $i = 0; $i < scalar @{$data_points}; $i++){
$cols++;
for (my $j = 0; $j < $cols; $j++){
if ($i == $j){
$result[$i][$j] = 0;
}
else {
$result[$i][$j] = _get_distance_between(${$data_points}[$i], ${$data_points}[$j]);
}
}
}
return \@result;
}
# Visualize any matrix e.g. distance matrix
sub _print_matrix {
my $D = shift;
my $size = @{$D};
for(my $a=0;$a<$size;$a++){
my $cols = @{${$D}[$a]};
for(my $b=0;$b<$cols;$b++){
my $n = sprintf "%.2f", ${$D}[$a][$b];
print $n."\t";
# printf "%4d", $n;
}
print "\n";
}
}
# Create MST with Prim algorithm
sub _prim {
my ($i,$total_cost);
my (@visited, @cost, @pi, @tree);
my $D = shift;
my $size = @{$D};
for($i=0; $i<$size ;$i++){
$visited[$i] = 0; # No nodes visited yet
$cost[$i] = "inf"; # All edges have infinite costs
}
$cost[0] = 0; # Initial node has cost 0
$pi[0] = -1; # ...and no parents
my $counter = 0;
while(1){
my $mincost = "inf";
my $u;
# Select node that has minimum weight
for($i=0; $i<$size ;$i++){
if ($mincost eq "inf"){
if ($visited[$i] == 0 && !($cost[$i] eq "inf")){
$mincost = $cost[$i];
$u = $i; # Select node with min costs
}
}
elsif (!($cost[$i] eq "inf")){
if ($visited[$i] == 0 && $cost[$i] < $mincost){
$mincost = $cost[$i];
$u = $i; # Select node with min costs
}
}
}
# All visited
if($mincost eq "inf"){
print "Total weight of MST: ".$total_cost."\n";
for($i=0; $i<$size ;$i++){
if ($visited[$i] == 0){
print STDERR "Node ".{$i+1}." unreachable\n";
}
}
last;
}
# Else add new costs to total costs
else{
$total_cost += $mincost;
}
$visited[$u] = 1;
# Update costs and parents
for($i=0; $i<$size ;$i++){
my $row_idx = $u;
my $col_idx = $i;
# Change row and col idx
if (@{${$D}[$u]} < $i + 1){
$row_idx = $i;
$col_idx = $u;
}
# If node has already been visited or has infinite distance to selected node u
if($visited[$i] || (${$D}[$row_idx][$col_idx]) eq "inf"){
next;
}
# If existing costs are higher or equal than costs to selected node u
elsif(!(${$D}[$row_idx][$col_idx] eq "inf") && ($cost[$i] eq "inf" || ${$D}[$row_idx][$col_idx] <= $cost[$i])){
$cost[$i] = ${$D}[$row_idx][$col_idx];
$pi[$i] = $u;
}
}
if($pi[$u] == -1){
# print "Add first node: ".($u+1)."\n";
}
else{
# print "Edge added: e<".($pi[$u]+1).",".($u+1).">\n";
push(@tree, [$pi[$u], $u]);
}
}
return \@tree;
}