summaryrefslogtreecommitdiff
path: root/structure/correlations/rank_utils.c
blob: 4b8ef416d48e51236654e542c84cab533be40c00 (plain)
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
/**
 *
 * Some utility functions to be used with tune_rho, compute_rho and
 * the like
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>

#include "rank_utils.h"

void load_ranking(char *fname, int *N, double **R){
  
  char buff[256];
  int size = 10;
  FILE *f;
  
  if (*R == NULL){
    *R = malloc(size * sizeof (double));
  }
  f = fopen(fname, "r");
  if (!f){
    printf("Unable to open file: %s!!! Exiting\n");
    exit(1);
  }
  
  *N = 0;
 
  while (fgets(buff, 255, f)){
    if (* N == size){
      size += 10;
      *R = realloc(*R, size * sizeof(double));
    }
    (*R)[*N] = atof(buff);
    *N += 1;
  }
}

double avg_array(double *v, int N){
  
  double sum = 0.0;
  int i;
  
  for (i = 0; i < N; i ++){
    sum += v[i];
  }
  return sum/N;
}

double compute_C(double *R1, double *R2, int N){
  double mu1, mu2, sum1, sum2;

  mu1 = avg_array(R1, N);
  mu2 = avg_array(R2, N);
  
  sum1 = mu1 * N;
  sum2 = mu2 * N;
  
  return N * mu1 * mu2 - mu2 * sum1 - mu1 * sum2;
}


double compute_D(double *R1, double *R2, int N){
  
  double mu1, mu2, s1, s2;
  int i;

  mu1 = avg_array(R1, N);
  mu2 = avg_array(R1, N);
  
  s1 = s2 = 0.0;
  
  for (i=0 ; i < N; i ++){
    s1 += pow((R1[i] - mu1), 2);
    s2 += pow((R2[i] - mu2), 2);
  }
  
  return sqrt(s1 * s2);
}


double compute_rho(double *R1, double *R2, int N, int *pairing){
  
  double rho = 0;
  int i;
  
  for (i=0; i < N; i ++){
    rho += R1[i] * R2[ pairing[i] ];
  }

  rho = (rho + compute_C(R1, R2, N))/ compute_D(R1, R2, N);
  return rho;
}

void dump_ranking(double *R, int N){
  int i;

  for (i=0; i < N; i ++){
    printf("%d: %f\n", i, R[i] );
  }
}


void init_pairing_natural(int *pairing, int N){
  int i;

  for (i = 0; i< N; i ++){
    pairing[i] = i;
  }
}

void init_pairing_inverse(int *pairing, int N){
  int i;

  for (i = 0; i< N; i ++){
    pairing[i] = N-i-1;
  }
}

void select_pairing(int *pairing, int N, int argc, char *argv[], int pos){

  if (argc < pos + 1 || !strncasecmp("rnd", argv[pos], 3)){
    init_pairing_random(pairing, N);
  }
  else if (!strncasecmp("nat", argv[pos], 3)){
    init_pairing_natural(pairing, N);
  }
  else if (!strncasecmp("inv", argv[pos], 3)){
    init_pairing_inverse(pairing, N);
  }
  else{
    printf ("Pairing strategy \"%s\" unknown!!! Exiting...\n", argv[pos]);
    exit(1);
  }

}


void shuffle_sequence(int *s, int N){
  
  int i, j, tmp;

  for (i=N-1; i>=0; i--){
    j = rand() % (i+1);
    tmp = s[j];
    s[j] = s[i];
    s[i] = tmp;
  }
}



void init_pairing_random(int *pairing, int N){

  init_pairing_natural(pairing, N);
  shuffle_sequence(pairing, N);

}

/* Loads a pairing from a file, in the format:
 * 
 * rank1 rank2
 * ...........
 */
void load_pairing(int **pairing, int N, char *fname){
  
  FILE *f;
  int i, j, num;
  char buff[256];
  char *ptr;

  f = fopen(fname, "r");
  if (!f){
    printf("Error opening file \"%s\"!!!! Exiting....\n", fname);
    exit(2);
  }

  if (*pairing == NULL){
    *pairing = malloc(N * sizeof(int));
    init_pairing_natural(*pairing, N);
  }
  
  num = 0;
  while(num < N){
    fgets(buff, 255, f);
    if (buff[0] == '#')
      continue;
    ptr = strtok(buff, " "); /* read the first node */
    i = atoi(ptr);
    ptr = strtok(NULL, " "); /* read the second node */
    j = atoi(ptr);
    (*pairing)[i] = j;
    num += 1;
  }
}


void dump_pairing(int *pairing, int N){
  int i;

  for(i=0; i< N; i ++){
    printf("%d %d\n", i, pairing[i]);
  }
}

void copy_pairing(int *p1, int *p2, int N){
  int i;
  
  for (i=0 ; i < N; i ++){
    p2[i] = p1[i];
  }
}