summaryrefslogtreecommitdiff
path: root/models/correlations/tune_rho.c
blob: 0e681c97d5a6c7841ee2645d02a25070e120583d (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
/*
 * This file is part of MAMMULT: Metrics And Models for Multilayer Networks
 *  
 * 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/>.
*/
/**
 *
 * Tune the Spearman's \rho correlation coefficient between two rankings
 * given as input, using a simulated-anneling procedure
 *
 */


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

#include "rank_utils.h"

//void select_pairing(int *pairing, int N, int argc, char *argv[]){
//
//  if (argc < 7 || !strncasecmp("rnd", argv[6], 3)){
//    init_pairing_random(pairing, N);
//  }
//  else if (!strncasecmp("nat", argv[6], 3)){
//    init_pairing_natural(pairing, N);
//  }
//  else if (!strncasecmp("inv", argv[6], 3)){
//    init_pairing_inverse(pairing, N);
//  }
//  else{
//    printf ("Pairing strategy \"%s\" unknown!!! Exiting...\n", argv[6]);
//    exit(1);
//  }
//
//}

void tune_rho(double *R1, double *R2, int N, int *pairing, double eps, 
              double beta, double rho_target){
  
  double act_rho, test_rho, F, F_new;
  double val, prob;
  int *new_pairing;
  int p1, p2, tmp_val;
  int tot;
 
  new_pairing = malloc(N * sizeof(int));
  copy_pairing(pairing, new_pairing, N);
  
  act_rho = compute_rho(R1, R2, N, pairing);
  
  F = fabs(act_rho - rho_target);
  
  //fprintf("%f %f %f %f %f\n", eps, beta, rho_target, act_rho, F);
  
  tot = 0;
  while (F > eps){
    /* sample two positions of "pairing" and shuffle them in "new_pairing" */
    p1 = rand() % N;
    p2 = rand() % N;
    while (p2 == p1){
      p2 = rand() % N;
    }
    tmp_val = new_pairing[p1];
    new_pairing[p1] = new_pairing[p2];
    new_pairing[p2] = tmp_val;
    test_rho = compute_rho(R1, R2, N, new_pairing);
    F_new = fabs(test_rho - rho_target);
    if (F_new < F){/* Accept this swap with probability 1 */
      tmp_val = pairing[p1];
      pairing[p1] = pairing[p2];
      pairing[p2] = tmp_val;
      act_rho = test_rho;
    }
    else{/* Accept the swap with a certain probability */
      val = 1.0 * rand() / RAND_MAX;

      //prob = pow(fabs(F - F_new)/(F+F_new), beta);
      prob = exp(-(F_new - F)/beta);
      //fprintf(stderr, "-- %f\n", prob);
      if (val < prob){ /* Accept the swap */
        tmp_val = pairing[p1];
        pairing[p1] = pairing[p2];
        pairing[p2] = tmp_val;
        act_rho = test_rho;
      }
      else{ /* Rollback the swap */
        tmp_val = new_pairing[p1];
        new_pairing[p1] = new_pairing[p2];
        new_pairing[p2] = tmp_val;
      }
      
    }
    F = fabs(act_rho - rho_target);
    if (tot % 200 == 0){
      fprintf(stderr, "%d %g\n", tot, act_rho);
    }
    tot += 1;
  }
}




int main (int argc, char *argv[]){

  int N1, N2;
  double *R1 = NULL;
  double *R2 = NULL;
  double eps, beta, rho, rho_target;
  
  int *pairing = NULL;

  srand(time(NULL));
  
  if (argc < 6){
    printf("Usage: %s <rank1> <rank2> <rho> <eps> <beta> [RND|NAT|INV]\n", argv[0]);
    exit(1);
  }
  
  rho_target = atof(argv[3]);
  eps = atof(argv[4]);
  beta = atof(argv[5]);
  
  load_ranking(argv[1], &N1, &R1);
  load_ranking(argv[2], &N2, &R2);
  
  if (N1 != N2){
    printf("Error!!!! The two files must have the same number of lines!!!! Exiting...\n");
    exit(1);
  }
  
  pairing = malloc(N1 * sizeof(int));

  select_pairing(pairing, N1, argc, argv, 6);
  
  rho = compute_rho(R1, R2, N1, pairing);
  
  fprintf(stderr, "%g\n", rho);
  
  tune_rho(R1, R2, N1, pairing, eps, beta, rho_target);

  dump_pairing(pairing, N1);
  
}