[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.1

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

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