[BACK]Return to wmain.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / hgm / mh / src

Annotation of OpenXM/src/hgm/mh/src/wmain.c, Revision 1.2

1.1       takayama    1: /*
1.2     ! takayama    2: $OpenXM$
1.1       takayama    3: License: LGPL
                      4:  */
                      5: #include <stdio.h>
                      6: #include <stdlib.h>
                      7: #include <math.h>
                      8: #include <string.h>
                      9: #define SMAX 4096
                     10: #define inci(i) { i++; if (i >= argc) { fprintf(stderr,"Option argument is not given.\n"); return(-1); }}
                     11: extern char *MH_Gfname;
                     12: extern char *MH_Dfname;
                     13:
                     14: /* global variables. They are set in setParam() */
                     15: int MH_RANK;
                     16: int MH_M;
                     17:
                     18: int MH_Mg;  /* m */
                     19: double *MH_Beta; /* beta[0], ..., beta[m-1] */
1.2     ! takayama   20: double *MH_Ng;   /* freedom n.  c=(m+1)/2+n/2; Note that it is a pointer */
        !            21: double MH_X0g;   /* initial point */
        !            22: static double *Iv;   /* Initial values of mhg sorted by mhbase() in rd.rr at beta*x0 */
        !            23: static double Ef;   /* exponential factor at beta*x0 */
        !            24: extern double MH_Hg;   /* step size of rk defined in rk.c */
        !            25: int MH_Dp;      /* Data sampling period */
        !            26: static double Xng=0.0;   /* the last point */
        !            27: int MH_RawName = 0;
        !            28: static int Testrank=0;
        !            29: extern int MH_Verbose;
        !            30:
        !            31: extern int MH_P95;  /* 95 % points */
        !            32: int mh_gopen_file(void);
        !            33: double mh_rkmain(double x0,double y0[],double xn);
        !            34: static int setParamTest(void);
        !            35: static int setParamDefault(void);
        !            36: static int setParam(char *fname);
        !            37: static int showParam(void);
1.1       takayama   38:
                     39: /* #define DEBUG */
                     40: #ifdef DEBUG
1.2     ! takayama   41: char *MH_Dfname; char *MH_Gfname; double MH_Hg;
        !            42: int mh_gopen_file(void) { }
        !            43: double mh_rkmain(double x0,double y0[],double xn) { }
1.1       takayama   44: #endif
                     45:
1.2     ! takayama   46: void *mh_malloc(int s) {
1.1       takayama   47:   void *p;
                     48:   p = (void*)malloc(s);
                     49:   if (p == NULL) {
1.2     ! takayama   50:        fprintf(stderr,"No memory.\n"); mh_exit(-1);
1.1       takayama   51:   }
                     52:   return(p);
                     53: }
1.2     ! takayama   54: mh_free(void *p) {
        !            55:   free(p);
        !            56:   return(0);
        !            57: }
1.1       takayama   58: static mypower(int x,int n) {
                     59:   int a,i;
                     60:   a = 1;
                     61:   for (i=0; i<n; i++) a = a*x;
                     62:   return(a);
                     63: }
1.2     ! takayama   64: #ifdef STANDALONE
1.1       takayama   65: main(int argc,char *argv[]) {
1.2     ! takayama   66:   return mh_main(argc,char *argv[]);
        !            67: }
        !            68: #endif
        !            69: mh_main(int argc,char *argv[]) {
1.1       takayama   70:   double *y0;
                     71:   double x0,xn;
                     72:   double ef;
                     73:   int i,rank;
1.2     ! takayama   74:   setParam(NULL); MH_Gfname = MH_Dfname = NULL; MH_Verbose=1;
1.1       takayama   75:   for (i=1; i<argc; i++) {
                     76:        if (strcmp(argv[i],"--idata")==0) {
                     77:          inci(i);
1.2     ! takayama   78:          setParam(argv[i]); MH_Verbose=0;
1.1       takayama   79:        }else if (strcmp(argv[i],"--gnuplotf")==0) {
                     80:          inci(i);
1.2     ! takayama   81:          MH_Gfname = (char *)mh_malloc(SMAX);
1.1       takayama   82:          strncpy(MH_Gfname,argv[i],SMAX-1);
                     83:        }else if (strcmp(argv[i],"--dataf")==0) {
                     84:          inci(i);
1.2     ! takayama   85:          MH_Dfname = (char *)mh_malloc(SMAX);
1.1       takayama   86:          strncpy(MH_Dfname,argv[i],SMAX-1);
                     87:        }else if (strcmp(argv[i],"--xmax")==0) {
                     88:       inci(i);
                     89:          sscanf(argv[i],"%lf",&Xng);
                     90:        }else if (strcmp(argv[i],"--step")==0) {
                     91:       inci(i);
1.2     ! takayama   92:          sscanf(argv[i],"%lg",&MH_Hg);
1.1       takayama   93:        }else if (strcmp(argv[i],"--help")==0) {
1.2     ! takayama   94:          mh_usage(); return(0);
1.1       takayama   95:        }else if (strcmp(argv[i],"--raw")==0) {
1.2     ! takayama   96:          MH_RawName = 1;
1.1       takayama   97:        }else if (strcmp(argv[i],"--test")==0) {
                     98:          inci(i);
                     99:          sscanf(argv[i],"%d",&Testrank);
                    100:          setParamTest();
                    101:        }else if (strcmp(argv[i],"--95")==0) {
1.2     ! takayama  102:          MH_P95=1;
1.1       takayama  103:        }else if (strcmp(argv[i],"--verbose")==0) {
1.2     ! takayama  104:          MH_Verbose=1;
1.1       takayama  105:        }else {
                    106:          fprintf(stderr,"Unknown option %s\n",argv[i]);
1.2     ! takayama  107:          mh_usage();
1.1       takayama  108:          return(-1);
                    109:        }
                    110:   }
1.2     ! takayama  111:   if (MH_Verbose) showParam();
        !           112:   x0 = MH_X0g;
1.1       takayama  113:   xn = Xng;
                    114:   ef = Ef;
                    115:   rank = mypower(2,MH_Mg);
1.2     ! takayama  116:   y0 = (double *) mh_malloc(sizeof(double)*rank);
1.1       takayama  117:   for (i=0; i<rank; i++) y0[i] = ef*Iv[i];
1.2     ! takayama  118:   mh_gopen_file();
        !           119:   if (MH_Verbose) {for (i=0; i<rank; i++) printf("%lf\n",y0[i]); }
        !           120:   mh_rkmain(x0,y0,xn);
1.1       takayama  121: }
                    122:
1.2     ! takayama  123: mh_usage() {
1.1       takayama  124:   fprintf(stderr,"Usages:\n");
                    125:   fprintf(stderr,"w-m [--idata input_data_file --gnuplotf gnuplot_file_name\n");
                    126:   fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n");
                    127:   fprintf(stderr,"[ --95 --verbose] \n");
                    128:   fprintf(stderr,"--raw does not add data parameters to the output_data_file.\n");
                    129:   fprintf(stderr,"\nThe command w-m [options] evaluates Pr({y | y<xmax}), which is the cumulative distribution function of the largest root of the m by m Wishart matrix with n degrees of freedom and the covariantce matrix sigma.\n");
                    130:   fprintf(stderr,"All the eigenvalues of sigma must be simple.\n");
                    131:   fprintf(stderr,"Parameters are specified by the input_data_file.\n");
                    132:   fprintf(stderr,"The format of the input_data_file.\n");
1.2     ! takayama  133:   fprintf(stderr," MH_Mg: m, MH_Beta: beta=sigma^(-1)/2 (diagonized), MH_Ng: n, Iv: initial values at MH_X0g*MH_Beta (see our paper how to order them), \n");
1.1       takayama  134:   fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n");
1.2     ! takayama  135:   fprintf(stderr," MH_Hg: h (step size), MH_X0g: starting value of x, Xng: terminating value of x\n");
        !           136:   fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n");
1.1       takayama  137:   fprintf(stderr," --95: output the 95% point. --verbose: verbose mode.\n");
                    138:   fprintf(stderr," The line started with %% is a comment line.\n");
                    139:   fprintf(stderr," An example format of the input_data_file can be obtained by executing w-2 with no option.\n");
                    140:   fprintf(stderr,"\nExamples:\n");
                    141:   fprintf(stderr,"[1] ./w-2 \n");
                    142:   fprintf(stderr,"[2] ./w-2 --xmax 20\n");
                    143:   fprintf(stderr,"[3] ./w-6 --test 6\n");
                    144:   fprintf(stderr,"   A test run of w-6.\n");
                    145:   fprintf(stderr,"   The number m and mm must agree for  ./w-m --test mm.\n");
                    146:   fprintf(stderr,"[4] ./w-5 --idata tmp-inm-m5* --gnuplot tmp-graph\n");
                    147:   fprintf(stderr,"  tmp-inm-m5* is a sample input data distributed with this file.\n");
                    148:   fprintf(stderr,"  tmp-graph-gp.txt is an input file of the gnuplot\n");
                    149:   fprintf(stderr,"  It can be executed with the load command in the gnuplot.\n");
                    150:   fprintf(stderr,"  tmp-graph is the table of x and the values of Pr({y | y<x}).\n");
                    151: }
                    152:
1.2     ! takayama  153: static setParamTest() {
1.1       takayama  154:   int rank;
                    155:   int i;
                    156:   extern int Testrank;
1.2     ! takayama  157:   extern int MH_Verbose;
        !           158:   MH_Verbose=1;
1.1       takayama  159:   MH_M= MH_Mg = Testrank ;
                    160:   MH_RANK = rank = mypower(2,MH_Mg);
1.2     ! takayama  161:   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1       takayama  162:   for (i=0; i<MH_Mg; i++) MH_Beta[i] = 1.0+0.1*i;
1.2     ! takayama  163:   MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0;
        !           164:   Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1       takayama  165:   for (i=0; i<rank; i++) Iv[i] = 0;
                    166:   Iv[0] = 0.001;
                    167:   Ef = 1;
1.2     ! takayama  168:   MH_X0g = 0.3;
        !           169:   MH_Hg = 0.001;
        !           170:   MH_Dp = 1;
1.1       takayama  171:   Xng = 10.0;
                    172: }
1.2     ! takayama  173: static setParamDefault() {
1.1       takayama  174:   int rank;
                    175:   MH_M=MH_Mg = 2 ;
                    176:   MH_RANK=rank = mypower(2,MH_Mg);
1.2     ! takayama  177:   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1       takayama  178:   MH_Beta[0] = 1.0; MH_Beta[1] = 2.0;
1.2     ! takayama  179:   MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0;
        !           180:   Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1       takayama  181:   Iv[0] = 1.58693;
                    182:   Iv[1] = 0.811369;
                    183:   Iv[2] = 0.846874;
                    184:   Iv[3] = 0.413438;
                    185:   Ef = 0.01034957388338225707;
1.2     ! takayama  186:   MH_X0g = 0.3;
        !           187:   MH_Hg = 0.001;
        !           188:   MH_Dp = 1;
1.1       takayama  189:   Xng = 10.0;
                    190: }
                    191:
                    192: next(FILE *fp,char *s,char *msg) {
                    193:   s[0] = '%';
                    194:   while (s[0] == '%') {
                    195:        if (!fgets(s,SMAX,fp)) {
                    196:          fprintf(stderr,"Data format error at %s\n",msg);
1.2     ! takayama  197:          mh_exit(-1);
1.1       takayama  198:        }
                    199:        if (s[0] != '%') return(0);
                    200:   }
                    201: }
1.2     ! takayama  202: static setParam(char *fname) {
1.1       takayama  203:   int rank;
                    204:   char s[SMAX];
                    205:   FILE *fp;
                    206:   int i;
                    207:   if (fname == NULL) return(setParamDefault());
                    208:
                    209:   if ((fp=fopen(fname,"r")) == NULL) {
                    210:        fprintf(stderr,"File %s is not found.\n",fname);
1.2     ! takayama  211:        mh_exit(-1);
1.1       takayama  212:   }
                    213:   next(fp,s,"MH_Mg(m)");
                    214:   sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg;
                    215:   MH_RANK=rank = mypower(2,MH_Mg);
                    216:
1.2     ! takayama  217:   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1       takayama  218:   for (i=0; i<MH_Mg; i++) {
                    219:     next(fp,s,"MH_Beta");
                    220:        sscanf(s,"%lf",&(MH_Beta[i]));
                    221:   }
                    222:
1.2     ! takayama  223:   MH_Ng = (double *)mh_malloc(sizeof(double));
        !           224:   next(fp,s,"MH_Ng(freedom parameter n)");
        !           225:   sscanf(s,"%lf",MH_Ng);
1.1       takayama  226:
1.2     ! takayama  227:   next(fp,s,"MH_X0g(initial point)");
        !           228:   sscanf(s,"%lf",&MH_X0g);
1.1       takayama  229:
1.2     ! takayama  230:   Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1       takayama  231:   for (i=0; i<rank; i++) {
                    232:        next(fp,s,"Iv(initial values)");
                    233:        sscanf(s,"%lg",&(Iv[i]));
                    234:   }
                    235:
                    236:   next(fp,s,"Ef(exponential factor)");
                    237:   sscanf(s,"%lg",&Ef);
                    238:
1.2     ! takayama  239:   next(fp,s,"MH_Hg (step size of rk)");
        !           240:   sscanf(s,"%lg",&MH_Hg);
1.1       takayama  241:
1.2     ! takayama  242:   next(fp,s,"MH_Dp (data sampling period)");
        !           243:   sscanf(s,"%d",&MH_Dp);
1.1       takayama  244:
                    245:   next(fp,s,"Xng (the last point, cf. --xmax)");
                    246:   sscanf(s,"%lf",&Xng);
                    247:   fclose(fp);
                    248: }
                    249:
                    250: showParam() {
                    251:   int rank,i;
                    252:   rank = mypower(2,MH_Mg);
                    253:   printf("%%MH_Mg=\n%d\n",MH_Mg);
                    254:   for (i=0; i<MH_Mg; i++) {
                    255:        printf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
                    256:   }
1.2     ! takayama  257:   printf("%%MH_Ng=\n%lf\n",*MH_Ng);
        !           258:   printf("%%MH_X0g=\n%lf\n",MH_X0g);
1.1       takayama  259:   for (i=0; i<rank; i++) {
                    260:        printf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
                    261:   }
                    262:   printf("%%Ef=\n%lf\n",Ef);
1.2     ! takayama  263:   printf("%%MH_Hg=\n%lf\n",MH_Hg);
        !           264:   printf("%%MH_Dp=\n%d\n",MH_Dp);
1.1       takayama  265:   printf("%%Xng=\n%lf\n",Xng);
                    266: }
                    267:
1.2     ! takayama  268: mh_exit(int n) {
        !           269: #ifdef STANDALONE
        !           270:   exit(n);
        !           271: #else
        !           272:   fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n);
        !           273: #endif
        !           274: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>