1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "mrilib.h"
8 
Syntax(void)9 void Syntax(void)
10 {
11    int ii ;
12 
13    printf("Usage 1: cdf [-v] -t2p statname t params\n"
14           "Usage 2: cdf [-v] -p2t statname p params\n"
15           "Usage 3: cdf [-v] -t2z statname t params\n"
16           "\n"
17           "This program does various conversions using the cumulative distribution\n"
18           "function (cdf) of certain canonical probability functions.  The optional\n"
19           "'-v' indicates to be verbose -- this is for debugging purposes, mostly.\n"
20           "Use this option if you get results you don't understand!\n"
21           "\n"
22           "Usage 1: Converts a statistic 't' to a tail probability.\n"
23           "Usage 2: Converts a tail probability 'p' to a statistic.\n"
24           "Usage 3: Converts a statistic 't' to a N(0,1) value (or z-score)\n"
25           "         that has the same tail probability.\n"
26           "\n"
27           "The parameter 'statname' refers to the type of distribution to be used.\n"
28           "The numbers in the params list are the auxiliary parameters for the\n"
29           "particular distribution.  The following table shows the available\n"
30           "distribution functions and their parameters:\n\n"
31    ) ;
32 
33    printf("   statname  Description  PARAMETERS\n"
34           "   --------  -----------  ----------------------------------------\n" ) ;
35    for( ii=FIRST_FUNC_TYPE ; ii <= LAST_FUNC_TYPE ; ii++ ){
36       if( FUNC_IS_STAT(ii) )
37          printf("       %4s  %-11.11s  %s\n",
38                 FUNC_prefixstr[ii] , FUNC_typestr[ii]+6 , FUNC_label_stat_aux[ii] ) ;
39    }
40 
41    printf("\nEXAMPLES:\n"
42           " Goal:    find p-value for t-statistic of 5.5 with 30 degrees of freedom\n"
43           " COMMAND: cdf -t2p fitt 5.5 30\n"
44           " OUTPUT:  p = 5.67857e-06\n"
45           "\n"
46           " Goal:    find F(8,200) threshold that gives a p-value of 0.001\n"
47           " COMMAND: cdf -p2t fift 0.001 8 200\n"
48           " OUTPUT:  t = 3.4343\n"
49           "\n"
50           "The same functionality is also available in 3dcalc, 1deval, and\n"
51           "ccalc, using functions such as 'fift_t2p(t,a,b)'.  In particular,\n"
52           "if you are scripting, ccalc is probably better to use than cdf,\n"
53           "since the output of\n"
54           "  ccalc -expr 'fitt_t2p(3,20)'\n"
55           "is the string '0.007076', while the output of\n"
56           "  cdf -t2p fitt 3 20\n"
57           "is the string 'p = 0.0070759'.\n"
58           "\n"
59          ) ;
60    exit(0) ;
61 }
62 
63 static char * Usage_str[3] = { "-t2p = statistic-to-probability" ,
64                                "-p2t = probability-to-statistic" ,
65                                "-t2z = statistic-to-N(0,1) [z-score]" } ;
66 
67 static char * Value_str[3] = { "statistic" , "p-value" , "statistic" } ;
68 
main(int argc,char * argv[])69 int main( int argc , char *argv[] )
70 {
71    int usage = -1 , statcode = -1 , ii,fc,iarg , npar , verb=0 ;
72    float stat , prob=0.0 , val ;
73    float par[MAX_STAT_AUX] ;
74    char *cpt , *statname ;
75 
76    if( argc < 4 || strcmp(argv[1],"-help") == 0 ) Syntax() ;
77 
78    iarg = 1 ;
79 
80    if( strcmp(argv[iarg],"-v") == 0 ){ verb = 1 ; iarg++ ; }
81 
82         if( strcmp(argv[iarg],"-t2p")==0 || strcmp(argv[iarg],"t2p")==0 ) usage = 1 ;
83    else if( strcmp(argv[iarg],"-p2t")==0 || strcmp(argv[iarg],"p2t")==0 ) usage = 2 ;
84    else if( strcmp(argv[iarg],"-t2z")==0 || strcmp(argv[iarg],"t2z")==0 ) usage = 3 ;
85 
86    if( usage < 0 ){
87       fprintf(stderr,"Don't recognize usage code: %s\n",argv[iarg]) ; exit(1) ;
88    }
89    if( verb ) printf("++ usage=%d: %s\n",usage,Usage_str[usage-1]) ;
90 
91    iarg++ ;
92 
93    fc = (argv[iarg][0] == '-') ? 1 : 0 ;
94    for( ii=FIRST_FUNC_TYPE ; ii <= LAST_FUNC_TYPE ; ii++ ){
95       if( ! FUNC_IS_STAT(ii) ) continue ;
96       if( strcmp( &(argv[iarg][fc]) , FUNC_prefixstr[ii] ) == 0 ){
97          statcode = ii ; break ;
98       }
99    }
100 
101    if( statcode < 0 ){
102       fprintf(stderr,"Don't recognize statname: %s\n",argv[iarg]) ; exit(1) ;
103    }
104    statname = FUNC_typestr[ii]+6 ;
105    if( verb ) printf("++ statcode=%d  type=%s\n",statcode,statname) ;
106    iarg++ ;
107 
108    stat = strtod( argv[iarg] , &cpt ) ;
109    if( usage == 2 ) prob = stat ;
110 
111    if( verb ) printf("++ %s value=%g\n",Value_str[usage-1],stat) ;
112 
113    if( *cpt != '\0' ){
114       fprintf(stderr,"Illegal numeric parameter: %s\n",argv[iarg]) ; exit(1) ;
115    }
116 
117    iarg++ ; ii = 0 ;
118    while( iarg < argc && ii < MAX_STAT_AUX ){
119       val = strtod( argv[iarg] , &cpt ) ;
120       if( *cpt != '\0' ){
121          fprintf(stderr,"Illegal numeric parameter: %s\n",argv[iarg]) ; exit(1) ;
122       }
123       par[ii++] = val ; iarg++ ;
124    }
125    npar = ii ;
126 
127    if( verb ){
128       printf("++ number of %s parameters=%d  parameter list=",statname,npar) ;
129       for( ii=0 ; ii < npar ; ii++ ) printf("%g ",par[ii]) ;
130       printf("\n") ;
131    }
132 
133    if( npar < FUNC_need_stat_aux[statcode] ){
134       fprintf(stderr,"Need %d parameters, but you only gave %d\n",
135               FUNC_need_stat_aux[statcode] , npar ) ;
136       exit(1) ;
137    }
138 
139    switch( usage ){
140 
141       case 1:  prob = THD_stat_to_pval( stat , statcode , par ) ;
142                printf("p = %g\n",prob) ;
143       break ;
144 
145       case 2:  stat = THD_pval_to_stat( prob , statcode , par ) ;
146                printf("t = %g\n",stat) ;
147       break ;
148 
149       case 3:  val = THD_stat_to_zscore( stat , statcode , par ) ;
150                printf("z = %g\n",val) ;
151       break ;
152    }
153 
154    exit(0) ;
155 }
156