d44d607fa059d9391eb6e87b06609a86ef03918c
[snark14.git] / src / snark / signif1.c
1 /*
2  ***********************************************************
3  $SNARK_Header: S N A R K  1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4  $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/signif1.c $
5  $LastChangedRevision: 90 $
6  $Date: 2014-07-02 17:31:12 -0400 (Wed, 02 Jul 2014) $
7  $Author: olangthaler $
8  ***********************************************************
9
10  This function computes and stores the significance levels
11
12  INPUTS:
13  iexp - experiment number.
14  nrun - number of runs in the current experiment.
15  tot_PAIRS - number of phantoms with PAIRed structures used for a given experiment.
16  compare - counter for the number of COMPAREs read in so far.
17  niters - number of iterations to be compared.
18  itr1 - array of length niters containing the iteration numbers for the first algorithm.
19  itr2 - array of length niters containing the iteration numbers for the second algorithm.
20  sigfl - file pointer to the file which stores the significance levels.
21  fom - character string denoting the figure of merit.
22  keywrd1 - string containing the keyword which defines the first algorithm.
23  keywrd2 - string containing the keyword which defines the second algorithm.
24  lowden - low clip value for pointwise accuracy calculation.
25  highden - high clip value for pointwise accuracy calculation.
26  */
27
28 #include <malloc.h>
29 #include <string.h>
30 #include <math.h>
31 #include <stdio.h>
32 #include "experimenter.h"
33 #include "read_fomfile.h"
34 #include "signif1.h"
35
36 void signif(int iexp, int nrun, int tot_PAIRS, int compare, int niters, int* itr1, int* itr2, FILE* sigfl, char* fom, char* keywrd1, char* keywrd2, float lowden, float highden)
37 {
38         int i, j;
39         double *fom1, *fom2, fom_diff, *sum_diff, *var, *sum_fom1, *sum_fom2;
40
41         //  go here only if, for hit-ratio, there are no PAIRed structures
42         if (strstr(fom, "Hit-ratio") != NULL)
43         {
44                 if (tot_PAIRS == 0)
45                 {
46                         fprintf(sigfl, "\n \t \t FIGURE OF MERIT: %s\n", fom);
47                         fprintf(sigfl, "%s \t %s \t Significance Level\n", keywrd1,
48                                         keywrd2);
49
50                         for (i = 0; i < niters; i++)
51                                 fprintf(sigfl,
52                                                 "%d \t %d \t No paired structures in this experiment\n",
53                                                 itr1[i], itr2[i]);
54                         return;
55                 }
56         }
57
58         // allocate memory for various arrays
59
60         // array to store figures of merit for algorithm 1
61         fom1 = (double *) malloc(niters * sizeof(double));
62
63         //  array to store figures of merit for algorithm 2
64         fom2 = (double *) malloc(niters * sizeof(double));
65
66         // array to store sum of the differences between the foms for the two algorithms
67         sum_diff = (double *) malloc(niters * sizeof(double));
68
69         // array to store sum of the squares of this difference
70         var = (double *) malloc(niters * sizeof(double));
71
72         // array to store sum of foms for algorithm 1
73         sum_fom1 = (double *) malloc(niters * sizeof(double));
74
75         // array to store sum of foms for algorithm 2
76         sum_fom2 = (double *) malloc(niters * sizeof(double));
77
78         for (j = 0; j < niters; j++) // initialize arrays
79         {
80                 sum_diff[j] = 0.0;
81                 var[j] = 0.0;
82                 sum_fom1[j] = 0.0;
83                 sum_fom2[j] = 0.0;
84         }
85
86         for (i = 0; i < nrun; i++)
87         {
88                 // read in foms and iteration numbers for both algorithms
89                 read_fomfile(iexp, i + 1, compare, niters, fom, fom1, fom2);
90                 for (j = 0; j < niters; j++)
91                 {
92                         /*
93                          a) compute difference between foms of the two algorithms
94                          b) sum these differences
95                          c) sum the squares of these differences
96                          d) sum the foms for algorithms 1 and 2
97                         */
98
99                         fom_diff = fom1[j] - fom2[j];
100                         sum_diff[j] = sum_diff[j] + fom_diff;
101                         var[j] = var[j] + (fom_diff * fom_diff);
102                         sum_fom1[j] = sum_fom1[j] + fom1[j];
103                         sum_fom2[j] = sum_fom2[j] + fom2[j];
104                 }
105         }
106
107         // For hit-ratio, number of phantoms = tot_PAIRS. While for other foms number of phantoms = nrun
108
109         if (strstr(fom, "Hit-ratio") != NULL)
110         {
111                 nrun = tot_PAIRS;
112         }
113
114         for (j = 0; j < niters; j++)
115         {
116                 // compute the average fom for algorithms 1 and 2
117                 sum_fom1[j] = sum_fom1[j] / nrun;
118                 sum_fom2[j] = sum_fom2[j] / nrun;
119
120                 if (var[j] == 0.0)
121                         sum_diff[j] = 0.5;
122                 else
123                 {
124                         // calculate significance levels. Note that if var[j]=0.0, the significance level for than iteration is 0.5.
125                         sum_diff[j] = sum_diff[j] / sqrt(var[j]);
126                         sum_diff[j] = 0.5 * erf(sum_diff[j] / sqrt(2.0));
127                         sum_diff[j] = 0.5 - dabs(sum_diff[j]);
128                 }
129         }
130
131         // print results and free memory
132         if (strstr(fom, "Pointwise Accuracy") != NULL)
133         {
134                 if (lowden == 0.0 && highden == 0.0) fprintf(sigfl, "\n \t FIGURE OF MERIT: %s.  NO CLIPPING\n", fom);
135                 else fprintf(sigfl, "\n \t FIGURE OF MERIT: %s.  CLIP VALUES ARE %.3f AND %.3f\n", fom, lowden, highden);
136         }
137         else fprintf(sigfl, "\n \t \t FIGURE OF MERIT: %s\n", fom);
138
139         fprintf(sigfl, "%s \tmean\t\t\t%s\tmean\t\t\tsignificance level\n", keywrd1, keywrd2);
140         for (i = 0; i < niters; i++) fprintf(sigfl, "%d\t%.20f  %d\t%.20f  %.20f\n", itr1[i], sum_fom1[i], itr2[i], sum_fom2[i], sum_diff[i]);
141
142         free(fom1);
143         free(fom2);
144         free(sum_diff);
145         free(var);
146         free(sum_fom1);
147         free(sum_fom2);
148 }
149
150 // function to return the absolute value
151 double dabs(double x)
152 {
153         return x < 0 ? -x : x;
154 }