Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
cdf.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mrilib.h"
00008
00009 void Syntax(void)
00010 {
00011 int ii ;
00012
00013 printf("Usage 1: cdf [-v] -t2p statname t params\n"
00014 "Usage 2: cdf [-v] -p2t statname p params\n"
00015 "Usage 3: cdf [-v] -t2z statname t params\n"
00016 "\n"
00017 "This program does various conversions using the cumulative distribution\n"
00018 "function (cdf) of certain canonical probability functions. The optional\n"
00019 "'-v' indicates to be verbose -- this is for debugging purposes, mostly.\n"
00020 "\n"
00021 "Usage 1: Converts a statistic 't' to a tail probability.\n"
00022 "Usage 2: Converts a tail probability 'p' to a statistic.\n"
00023 "Usage 3: Converts a statistic 't' to a N(0,1) value (or z-score)\n"
00024 " that has the same tail probability.\n"
00025 "\n"
00026 "The parameter 'statname' refers to the type of distribution to be used.\n"
00027 "The numbers in the params list are the auxiliary parameters for the\n"
00028 "particular distribution. The following table shows the available\n"
00029 "distribution functions and their parameters:\n\n"
00030 ) ;
00031
00032 printf(" statname Description PARAMETERS\n"
00033 " -------- ----------- ----------------------------------------\n" ) ;
00034 for( ii=FIRST_FUNC_TYPE ; ii <= LAST_FUNC_TYPE ; ii++ ){
00035 if( FUNC_IS_STAT(ii) )
00036 printf(" %4s %-11.11s %s\n",
00037 FUNC_prefixstr[ii] , FUNC_typestr[ii]+6 , FUNC_label_stat_aux[ii] ) ;
00038 }
00039
00040 printf("\n") ; exit(0) ;
00041 }
00042
00043 static char * Usage_str[3] = { "-t2p = statistic-to-probability" ,
00044 "-p2t = probability-to-statistic" ,
00045 "-t2z = statistic-to-N(0,1) [z-score]" } ;
00046
00047
00048 int main( int argc , char * argv[] )
00049 {
00050 int usage = -1 , statcode = -1 , ii,fc,iarg , npar , verb=0 ;
00051 float stat , prob , val ;
00052 float par[MAX_STAT_AUX] ;
00053 char * cpt ;
00054
00055 if( argc < 4 || strcmp(argv[1],"-help") == 0 ) Syntax() ;
00056
00057 iarg = 1 ;
00058
00059 if( strcmp(argv[iarg],"-v") == 0 ){ verb = 1 ; iarg++ ; }
00060
00061 if( strcmp(argv[iarg],"-t2p")==0 || strcmp(argv[iarg],"t2p")==0 ) usage = 1 ;
00062 else if( strcmp(argv[iarg],"-p2t")==0 || strcmp(argv[iarg],"p2t")==0 ) usage = 2 ;
00063 else if( strcmp(argv[iarg],"-t2z")==0 || strcmp(argv[iarg],"t2z")==0 ) usage = 3 ;
00064
00065 if( usage < 0 ){
00066 fprintf(stderr,"Don't recognize usage code: %s\n",argv[iarg]) ; exit(1) ;
00067 }
00068 if( verb ) printf("*** usage=%d: %s\n",usage,Usage_str[usage-1]) ;
00069
00070 iarg++ ;
00071
00072 fc = (argv[iarg][0] == '-') ? 1 : 0 ;
00073 for( ii=FIRST_FUNC_TYPE ; ii <= LAST_FUNC_TYPE ; ii++ ){
00074 if( ! FUNC_IS_STAT(ii) ) continue ;
00075 if( strcmp( &(argv[iarg][fc]) , FUNC_prefixstr[ii] ) == 0 ){
00076 statcode = ii ; break ;
00077 }
00078 }
00079
00080 if( statcode < 0 ){
00081 fprintf(stderr,"Don't recognize statname: %s\n",argv[iarg]) ; exit(1) ;
00082 }
00083 if( verb ) printf("*** statcode=%d type=%s\n",statcode,FUNC_typestr[ii]+6) ;
00084 iarg++ ;
00085
00086 stat = strtod( argv[iarg] , &cpt ) ;
00087 if( usage == 2 ) prob = stat ;
00088
00089 if( verb ) printf("*** value=%g\n",stat) ;
00090
00091 if( *cpt != '\0' ){
00092 fprintf(stderr,"Illegal numeric parameter: %s\n",argv[iarg]) ; exit(1) ;
00093 }
00094
00095 iarg++ ; ii = 0 ;
00096 while( iarg < argc && ii < MAX_STAT_AUX ){
00097 val = strtod( argv[iarg] , &cpt ) ;
00098 if( *cpt != '\0' ){
00099 fprintf(stderr,"Illegal numeric parameter: %s\n",argv[iarg]) ; exit(1) ;
00100 }
00101 par[ii++] = val ; iarg++ ;
00102 }
00103 npar = ii ;
00104
00105 if( verb ){
00106 printf("*** npar=%d parameters=",npar) ;
00107 for( ii=0 ; ii < npar ; ii++ ) printf("%g ",par[ii]) ;
00108 printf("\n") ;
00109 }
00110
00111 if( npar < FUNC_need_stat_aux[statcode] ){
00112 fprintf(stderr,"Need %d parameters, but you only gave %d\n",
00113 FUNC_need_stat_aux[statcode] , npar ) ;
00114 exit(1) ;
00115 }
00116
00117 switch( usage ){
00118
00119 case 1: prob = THD_stat_to_pval( stat , statcode , par ) ;
00120 printf("p = %g\n",prob) ;
00121 break ;
00122
00123 case 2: stat = THD_pval_to_stat( prob , statcode , par ) ;
00124 printf("t = %g\n",stat) ;
00125 break ;
00126
00127 case 3: val = THD_stat_to_zscore( stat , statcode , par ) ;
00128 printf("z = %g\n",val) ;
00129 break ;
00130 }
00131
00132 exit(0) ;
00133 }