signtest.c
14 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
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
/**********************************************************************/
/* */
/* FILE: signtest.c */
/* WRITTEN BY: Jonathan G. Fiscus */
/* DATE: April 14 1989 */
/* NATIONAL INSTITUTE OF STANDARDS AND TECHNOLOGY */
/* SPEECH RECOGNITION GROUP */
/* */
/* USAGE: This uses the rank structure to perform */
/* the non-parametric Sign Test and generates */
/* a report. */
/* */
/* SOURCE:Statistics: Basic Techniques for Solving */
/* Applied Problems, by Stephen A. Book, 1977 */
/* */
/* See Also: The documentation file "signtest.doc" */
/* */
/**********************************************************************/
#include "sctk.h"
/****************************************************************/
/* main procedure to perform the sign test on the RANK */
/* structure. */
/****************************************************************/
void perform_signtest(RANK *rank, int verbose, int report, char *formula_str, char formula_id, int ***out_winner, char *outroot, int feedback, double ***out_confidence)
{
int comp1, comp2, **winner;
double **confidence;
FILE *fp = stdout;
if (report || verbose){
char *f = rsprintf("%s.sign",outroot);
if ((fp=(strcmp(outroot,"-") == 0) ? stdout : fopen(f,"w")) ==
(FILE *)0){
fprintf(stderr,"Warning: Open of %s for write failed. "
"Using stdout instead.\n",f);
fp = stdout;
} else
if (feedback >= 1) printf(" Output written to '%s'\n",f);
}
alloc_2dimZ(winner,rank->n_trt,rank->n_trt,int,NO_DIFF);
alloc_2dimZ(confidence,rank->n_trt,rank->n_trt,double,0.0);
*out_confidence = confidence;
*out_winner = winner;
for (comp1=0; comp1 < rank->n_trt -1; comp1++)
for (comp2=comp1+1; comp2< rank->n_trt ; comp2++){
winner[comp1][comp2] =
compute_signtest_for_treatment(rank,comp1,comp2,"Spkr",
formula_str,verbose,
formula_id=='E',fp,
&(confidence[comp1][comp2]));
}
if (report) print_trt_comp_matrix_for_RANK_one_winner(winner,rank,
"Comparison Matrix for the Sign Test",formula_str,
"Speaker",fp);
if (fp != stdout) fclose(fp);
}
/****************************************************************/
/* Given the two indexes of treatments to compare (in the */
/* RANK Struct) compute the Rank Sum statistics */
/* vars: */
/* zero_is_best : This identifies the "ideal" value for */
/* the value computed in the rank struct */
/* percentages. */
/****************************************************************/
int compute_signtest_for_treatment(RANK *rank, int treat1, int treat2, char *block_id, char *formula_str, int verbose, int zero_is_best, FILE *fp, double *confidence)
{
int sum_plus=0, sum_minus=0, sum_equal=0, i;
int block, max_len_block=0, max_len_treat=6;
int tptr[2]; /* a sorting pointer array for the treatment numbers */
char *pct_format, thresh_str[140];
TEXT *title_line = (TEXT *)0;
char *diff_line;
int paper_width = 79, rep_width, diff_col_len;
double equal_thresh = 0.005;
double sum_trt1=0.0, sum_trt2=0.0;
/* compute the maximum block title length */
max_len_block = strlen(block_id);
for (block=0; block <rank->n_blk ; block++)
if (strlen(rank->blk_name [ block ] ) > max_len_block)
max_len_block = strlen(rank->blk_name [ block ] );
if (max_len_treat < strlen(rank->trt_name [ treat1 ] ))
max_len_treat = strlen(rank->trt_name [ treat1 ] );
if (max_len_treat < strlen(rank->trt_name [ treat2 ] ))
max_len_treat = strlen(rank->trt_name [ treat2 ] );
/* set the treatment sorting array */
for (block=0; block <rank->n_blk ; block++){
sum_trt1 += rank->pcts [ block ][ treat1 ] ;
sum_trt2 += rank->pcts [ block ][ treat2 ] ;
}
if (sum_trt1 > sum_trt2)
tptr[0] = treat1, tptr[1] = treat2;
else
tptr[0] = treat2, tptr[1] = treat1;
/* set up format strings and titles */
sprintf(thresh_str,"(Threshold for equal percentages +- %5.3f)",
equal_thresh);
alloc_singarr(pct_format, max_len_treat + 2,char);
alloc_singarr(diff_line, max_len_treat*2 + 7,char);
pct_format[0] = '\0';
strcat(pct_format,center("",(max_len_treat-6)/2));
strcat(pct_format,"%6.2f");
strcat(pct_format,center("",max_len_treat - ((max_len_treat-6)/2 + 6) ));
sprintf(diff_line,"[%s - %s]",rank->trt_name[tptr[0]],
rank->trt_name[tptr[1]]);
diff_col_len = 15;
if (strlen(diff_line) > diff_col_len) diff_col_len=strlen(diff_line);
rep_width = max_len_block + max_len_treat * 2 + 4 * 3 + diff_col_len;
/* Print a detailed table showing sign differences */
if (verbose) {
sum_trt1 = sum_trt2 = 0.0;
fprintf(fp,"%s\n",center("Sign Test Calculations Table Comparing",
paper_width));
title_line = TEXT_strdup(rsprintf("%s %s Percentages for Systems %s and %s",
block_id, formula_str,rank->trt_name[tptr[0]],
rank->trt_name [ tptr[1] ] ));
fprintf(fp,"%s\n",center(title_line,paper_width));
fprintf(fp,"%s\n\n",center(thresh_str,paper_width));
fprintf(fp,"%s",center("",(paper_width - rep_width)/2));
fprintf(fp,"%s",center("",max_len_block));
fprintf(fp," "); fprintf(fp,"%s",center("",max_len_treat));
fprintf(fp," "); fprintf(fp,"%s",center("",max_len_treat));
fprintf(fp," ");
fprintf(fp,"%s\n",center("Difference Sign",diff_col_len));
fprintf(fp,"%s",center("",(paper_width - rep_width)/2));
fprintf(fp,"%s",center(block_id,max_len_block));
fprintf(fp," ");
fprintf(fp,"%s",center(rank->trt_name [ tptr[0] ] ,max_len_treat));
fprintf(fp," ");
fprintf(fp,"%s",center(rank->trt_name [ tptr[1] ] ,max_len_treat));
fprintf(fp," ");
fprintf(fp,"%s\n",center(diff_line,diff_col_len));
fprintf(fp,"%s",center("",(paper_width - rep_width)/2));
for (i=0; i<rep_width; i++)
fprintf(fp,"-");
fprintf(fp,"\n");
}
for (block=0; block <rank->n_blk ; block++){
if (verbose) {
fprintf(fp,"%s",center("",(paper_width - rep_width)/2));
fprintf(fp,"%s",center(rank->blk_name [ block ] ,max_len_block));
fprintf(fp," ");
fprintf(fp,pct_format,rank->pcts [ block ][ tptr[0] ] );
fprintf(fp," ");
fprintf(fp,pct_format,rank->pcts [ block ][ tptr[1] ] );
fprintf(fp," ");
}
if (fabs(rank->pcts [ block ][ tptr[0] ] - rank->pcts [ block ][ tptr[1] ] ) <=
equal_thresh){
if (verbose) fprintf(fp,"%s\n",center("0",diff_col_len));
sum_equal++;
}
else if (rank->pcts [ block ][ tptr[0] ] < rank->pcts [ block ][ tptr[1] ] ){
if (verbose) fprintf(fp,"%s\n",center("-",diff_col_len));
sum_minus++;
}
else {
if (verbose) fprintf(fp,"%s\n",center("+",diff_col_len));
sum_plus++;
}
sum_trt1 += rank->pcts [ block ][ tptr[0] ] ;
sum_trt2 += rank->pcts [ block ][ tptr[1] ] ;
}
if (verbose){
fprintf(fp,"%s",center("",(paper_width - rep_width)/2));
for (i=0; i<rep_width; i++)
fprintf(fp,"-");
fprintf(fp,"\n");
/* an Average line */
fprintf(fp,"%s",center("",(paper_width - rep_width)/2));
fprintf(fp,"%s",center("Avg.",max_len_block));
fprintf(fp," ");
fprintf(fp,pct_format,sum_trt1 /rank->n_blk );
fprintf(fp," ");
fprintf(fp,pct_format,sum_trt2 /rank->n_blk );
fprintf(fp,"\n\n");
sprintf(title_line,"No. Speakers with Positive %s Differences = N(+) = %2d",formula_str,sum_plus);
fprintf(fp,"%s\n",center(title_line,paper_width));
sprintf(title_line,"No. Speakers with Negative %s Differences = N(-) = %2d",formula_str,sum_minus);
fprintf(fp,"%s\n",center(title_line,paper_width));
sprintf(title_line,"No. Speakers with No %s Differences = N(0) = %2d",formula_str,sum_equal);
fprintf(fp,"%s\n",center(title_line,paper_width));
fprintf(fp,"\n\n");
}
free_singarr(pct_format,char);
free_singarr(diff_line,char);
if (title_line != (TEXT *)0)
free_singarr(title_line, TEXT);
/* Analyze the Results */
{ int result;
result = sign_test_analysis(sum_plus,sum_minus,sum_equal,"+","-",0,
0.05,verbose, rank->trt_name [ tptr[0] ] ,
rank->trt_name [ tptr[1] ] ,
tptr,zero_is_best,fp,confidence);
/* if the result is significant, system which is better depends on if */
/* the treatments have been swapped, a negative result means tprt[0] is */
/* better, positive one tprt[1] is better */
return(result * ((tptr[0] == treat1) ? 1 : (-1)));
}
}
/****************************************************************/
/* Given the vital numbers for computing a rank sum test, */
/* Compute it, and if requested, print a verbose analysis */
/****************************************************************/
int sign_test_analysis(int num_a, int num_b, int num_z, char *str_a, char *str_b, int str_z, double alpha, int verbose, char *treat1_str, char *treat2_str, int *tptr, int zero_is_best, FILE *fp, double *confidence)
{
double test_stat, p=0.5;
int i;
num_b += (num_z / 2) + (num_z % 2);
num_a += (num_z / 2);
num_z = 0;
/* multiplication by 2 means it's a two-tailed test */
if (num_a != num_b)
test_stat = 2.0 * compute_acc_binomial(MIN(num_a,num_b),num_a+num_b,p);
else
test_stat = 1.0;
*confidence = test_stat;
if (verbose){
fprintf(fp,"The NULL Hypothesis:\n\n");
fprintf(fp," The number of speakers for which the differences is positive\n");
fprintf(fp," equals the number of speakers for which the differences is\n");
fprintf(fp," negative.\n");
fprintf(fp," P(N(+)) = P(N(-)) = 0.50\n\n");
fprintf(fp,"Alternate Hypothesis:\n\n");
fprintf(fp," The number of speakers for which the differences is positive \n");
fprintf(fp," is NOT equal to the number of speakers for which the difference\n");
fprintf(fp," is negative.\n\n");
fprintf(fp,"Decision Analysis:\n\n");
fprintf(fp," Assumptions:\n");
fprintf(fp," A1: The distibution of positive and negative differences\n");
fprintf(fp," follows the binomial distribution for N fair coin tosses.\n");
fprintf(fp,"\n");
fprintf(fp," A2: In order to resolve the complication caused by cases where the\n");
fprintf(fp," difference in Word Accuracy is zero, half of the cases will\n");
fprintf(fp," be assigned to N(+) and half to N(-). In the event of an\n");
fprintf(fp," odd number of zero differences, N(-) will get one extra, this\n");
fprintf(fp," reduces the probability of there being a difference between\n");
fprintf(fp," the two systems.\n\n");
fprintf(fp," Rejection criterion:\n");
fprintf(fp," Reject the null hypothesis at the 95%% confidence level based\n");
fprintf(fp," on the following critical values table. N is the number of\n");
fprintf(fp," speakers being compared and N(-) is the number of negative\n");
fprintf(fp," differences.\n\n");
/* print a table of critical values */
fprintf(fp," MIN(N(-),N(+)) P(MIN(N(-),N(+)) | N=%2d)\n",num_a+num_b);
fprintf(fp," -------------- ------------------------\n");
for (i=0; i <= (num_a+num_b)/2 && (i - 3) <= num_b; i++){
double val = 2.0*compute_acc_binomial(i,num_a+num_b,p),
valp1 = 2.0*compute_acc_binomial(i+1,num_a+num_b,p);
if (val >= 0.0005)
fprintf(fp," %3d %5.3f", i,val);
else
fprintf(fp," %3d <0.001 ", i);
if ((val < alpha) && (valp1 > alpha))
fprintf(fp," <--- Null Hypothesis rejected at or below this point\n");
else
fprintf(fp,"\n");
}
fprintf(fp,"\n");
fprintf(fp," Decision:\n");
fprintf(fp," There were N(-)=%d negative differences , the probability of\n",num_b);
fprintf(fp," it occuring is %5.3f, therefore the null hypothesis ",test_stat);
if (test_stat < alpha){
fprintf(fp,"is REJECTED\n");
fprintf(fp," in favor of the Alternate Hypothesis. Further, %s is the\n",
(zero_is_best) ? treat2_str : treat1_str);
fprintf(fp," better System.\n");
} else{
fprintf(fp,"is ACCEPTED\n");
fprintf(fp," There is no statistical difference between %s and %s\n",treat1_str,treat2_str);
}
form_feed(fp);
}
if (test_stat < alpha){
if (0) fprintf(fp,"Returning Result %d\n",TEST_DIFF * ((zero_is_best) ? 1 : -1));
return(TEST_DIFF * ((zero_is_best) ? 1 : -1));
}
return(NO_DIFF);
}