Annotation of OpenXM/src/hgm/mh/src/wmain.c, Revision 1.14
1.1 takayama 1: /*
1.14 ! takayama 2: $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.13 2013/03/08 07:32:28 takayama Exp $
1.10 takayama 3: License: LGPL
4: */
1.1 takayama 5: #include <stdio.h>
6: #include <stdlib.h>
7: #include <math.h>
8: #include <string.h>
1.4 takayama 9: #include "sfile.h"
1.11 takayama 10: #include "mh.h"
1.1 takayama 11: #define SMAX 4096
1.4 takayama 12: #define inci(i) { i++; if (i >= argc) { fprintf(stderr,"Option argument is not given.\n"); return(NULL); }}
1.3 takayama 13: int MH_deallocate=0;
14:
1.1 takayama 15: extern char *MH_Gfname;
16: extern char *MH_Dfname;
17:
18: /* global variables. They are set in setParam() */
1.4 takayama 19: int MH_byFile=1;
1.1 takayama 20: int MH_RANK;
21: int MH_M;
22:
23: int MH_Mg; /* m */
24: double *MH_Beta; /* beta[0], ..., beta[m-1] */
1.2 takayama 25: double *MH_Ng; /* freedom n. c=(m+1)/2+n/2; Note that it is a pointer */
26: double MH_X0g; /* initial point */
27: static double *Iv; /* Initial values of mhg sorted by mhbase() in rd.rr at beta*x0 */
28: static double Ef; /* exponential factor at beta*x0 */
29: extern double MH_Hg; /* step size of rk defined in rk.c */
30: int MH_Dp; /* Data sampling period */
31: static double Xng=0.0; /* the last point */
32: int MH_RawName = 0;
33: static int Testrank=0;
34: extern int MH_Verbose;
35:
36: extern int MH_P95; /* 95 % points */
37: int mh_gopen_file(void);
38: static int setParamTest(void);
39: static int setParamDefault(void);
40: static int setParam(char *fname);
41: static int showParam(void);
1.6 takayama 42: static int next(struct SFILE *fp,char *s, char *msg);
1.1 takayama 43:
44: /* #define DEBUG */
45: #ifdef DEBUG
1.2 takayama 46: char *MH_Dfname; char *MH_Gfname; double MH_Hg;
47: int mh_gopen_file(void) { }
1.4 takayama 48: struct MH_RESULT mh_rkmain(double x0,double y0[],double xn) { }
1.1 takayama 49: #endif
50:
1.3 takayama 51: void mh_freeWorkArea(void) {
52: extern int MH_deallocate;
53: MH_deallocate=1; /* switch to deallocation mode. */
54: mh_main(0,NULL);
55: setParam(NULL);
56: mh_rkmain(0.0, NULL, 0.0);
57: mh_rf(0.0, NULL, 0, NULL, 0);
58: MH_deallocate=0; /* switch to the normal mode. */
59: }
1.8 takayama 60: static int mypower(int x,int n) {
1.1 takayama 61: int a,i;
62: a = 1;
63: for (i=0; i<n; i++) a = a*x;
64: return(a);
65: }
1.9 takayama 66: #ifdef STANDALONE2
1.1 takayama 67: main(int argc,char *argv[]) {
1.12 takayama 68: int strategy=STRATEGY_DEFAULT;
69: double err[2]={-1.0,-1.0};
70: int i;
71: for (i=1; i<argc; i++) {
72: if (strcmp(argv[i],"--strategy")==0) {
73: i++; sscanf(argv[i],"%d",&strategy);
74: }else if (strcmp(argv[i],"--abserr")==0) {
75: i++; sscanf(argv[i],"%lg",&(err[0]));
76: }else if (strcmp(argv[i],"--relerr")==0) {
77: i++; sscanf(argv[i],"%lg",&(err[1]));
78: }else ;
79: }
80: mh_set_strategy(strategy,err);
1.6 takayama 81: mh_exit(MH_RESET_EXIT); /* standalone mode */
1.10 takayama 82: /* mh_main(argc,argv);
83: mh_freeWorkArea(); */
1.3 takayama 84: mh_main(argc,argv);
1.12 takayama 85: /* showParam(); */
1.2 takayama 86: }
87: #endif
1.4 takayama 88: struct MH_RESULT *mh_main(int argc,char *argv[]) {
1.13 takayama 89: static double *y0=NULL;
1.1 takayama 90: double x0,xn;
91: double ef;
92: int i,rank;
1.4 takayama 93: struct MH_RESULT *rp=NULL;
1.3 takayama 94: extern int MH_deallocate;
1.4 takayama 95: extern int MH_byFile;
96: MH_byFile=1;
97: if (MH_deallocate) { if (y0) mh_free(y0); return(rp); }
1.2 takayama 98: setParam(NULL); MH_Gfname = MH_Dfname = NULL; MH_Verbose=1;
1.1 takayama 99: for (i=1; i<argc; i++) {
1.10 takayama 100: if (strcmp(argv[i],"--idata")==0) {
1.1 takayama 101: inci(i);
1.10 takayama 102: setParam(argv[i]); MH_Verbose=0;
103: }else if (strcmp(argv[i],"--gnuplotf")==0) {
1.1 takayama 104: inci(i);
1.10 takayama 105: MH_Gfname = (char *)mh_malloc(SMAX);
106: strcpy(MH_Gfname,argv[i]);
107: }else if (strcmp(argv[i],"--dataf")==0) {
108: inci(i);
109: MH_Dfname = (char *)mh_malloc(SMAX);
110: strcpy(MH_Dfname,argv[i]);
111: }else if (strcmp(argv[i],"--xmax")==0) {
112: inci(i);
113: sscanf(argv[i],"%lf",&Xng);
114: }else if (strcmp(argv[i],"--step")==0) {
115: inci(i);
116: sscanf(argv[i],"%lg",&MH_Hg);
117: }else if (strcmp(argv[i],"--help")==0) {
118: mh_usage(); return(rp);
119: }else if (strcmp(argv[i],"--raw")==0) {
120: MH_RawName = 1;
121: }else if (strcmp(argv[i],"--test")==0) {
122: inci(i);
123: sscanf(argv[i],"%d",&Testrank);
124: setParamTest();
125: }else if (strcmp(argv[i],"--95")==0) {
126: MH_P95=1;
127: }else if (strcmp(argv[i],"--verbose")==0) {
128: MH_Verbose=1;
129: }else if (strcmp(argv[i],"--bystring")==0) {
130: MH_byFile = 0;
1.12 takayama 131: }else if (strcmp(argv[i],"--strategy")==0) {
132: i++; /* ignore */
133: }else if (strcmp(argv[i],"--abserr")==0) {
134: i++; /* ignore */
135: }else if (strcmp(argv[i],"--relerr")==0) {
136: i++; /* ignore */
1.10 takayama 137: }else {
138: fprintf(stderr,"Unknown option %s\n",argv[i]);
139: mh_usage();
140: return(rp);
141: }
1.1 takayama 142: }
1.2 takayama 143: if (MH_Verbose) showParam();
144: x0 = MH_X0g;
1.1 takayama 145: xn = Xng;
146: ef = Ef;
147: rank = mypower(2,MH_Mg);
1.2 takayama 148: y0 = (double *) mh_malloc(sizeof(double)*rank);
1.1 takayama 149: for (i=0; i<rank; i++) y0[i] = ef*Iv[i];
1.2 takayama 150: mh_gopen_file();
151: if (MH_Verbose) {for (i=0; i<rank; i++) printf("%lf\n",y0[i]); }
1.4 takayama 152: rp = (struct MH_RESULT*) mh_malloc(sizeof(struct MH_RESULT));
153: *rp=mh_rkmain(x0,y0,xn);
154: return(rp);
1.1 takayama 155: }
156:
1.8 takayama 157: int mh_usage() {
1.1 takayama 158: fprintf(stderr,"Usages:\n");
1.14 ! takayama 159: fprintf(stderr,"hgm_w-n [--idata input_data_file --gnuplotf gnuplot_file_name\n");
1.1 takayama 160: fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n");
161: fprintf(stderr,"[ --95 --verbose] \n");
162: fprintf(stderr,"--raw does not add data parameters to the output_data_file.\n");
1.14 ! takayama 163: fprintf(stderr,"\nThe command hgm_w-n [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");
1.1 takayama 164: fprintf(stderr,"All the eigenvalues of sigma must be simple.\n");
165: fprintf(stderr,"Parameters are specified by the input_data_file.\n");
1.14 ! takayama 166: fprintf(stderr,"The format of the input_data_file, which should be generated by the command hgm_jack-n.\n");
! 167: fprintf(stderr," MH_Mg: m, MH_Beta: beta=sigma^(-1)/2 (diagonized), MH_Ng: n, MH_X0g: starting value of x,\n");
! 168: fprintf(stderr," Iv: initial values at MH_X0g*MH_Beta (see our paper how to order them), \n");
1.1 takayama 169: fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n");
1.14 ! takayama 170: fprintf(stderr," MH_Hg: h (step size),\n");
1.2 takayama 171: fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n");
1.14 ! takayama 172: fprintf(stderr," Xng: terminating value of x.\n");
1.7 takayama 173: fprintf(stderr," --95: output the 95%% point. --verbose: verbose mode.\n");
1.1 takayama 174: fprintf(stderr," The line started with %% is a comment line.\n");
175: fprintf(stderr," An example format of the input_data_file can be obtained by executing w-2 with no option.\n");
176: fprintf(stderr,"\nExamples:\n");
1.14 ! takayama 177: fprintf(stderr,"[1] ./hgm_w-n \n");
! 178: fprintf(stderr,"[2] ./hgm_w-n --xmax 20\n");
! 179: fprintf(stderr,"[3] ./hgm_w-n --test 6\n");
! 180: fprintf(stderr," A test run in Mg=6.\n");
! 181: fprintf(stderr,"[4] ./hgm_jack-n --idata Testdata/tmp-idata3.txt --degree 15 >t.txt\n");
! 182: fprintf(stderr," ./hgm_w-n --idata t.txt --gnuplotf test-g\n");
! 183: fprintf(stderr," gnuplot -persist <test-g-gp.txt\n");
! 184: fprintf(stderr," tmp-idata3.txt is a sample input data distributed with this file.\n");
! 185: fprintf(stderr," test-g-gp.txt is an input file of the gnuplot\n");
! 186: fprintf(stderr," test-g is the table of x and the values of Pr({y | y<x}).\n");
1.1 takayama 187: }
188:
1.8 takayama 189: static int setParamTest() {
1.1 takayama 190: int rank;
191: int i;
192: extern int Testrank;
1.2 takayama 193: extern int MH_Verbose;
194: MH_Verbose=1;
1.1 takayama 195: MH_M= MH_Mg = Testrank ;
196: MH_RANK = rank = mypower(2,MH_Mg);
1.2 takayama 197: MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1 takayama 198: for (i=0; i<MH_Mg; i++) MH_Beta[i] = 1.0+0.1*i;
1.2 takayama 199: MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0;
200: Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1 takayama 201: for (i=0; i<rank; i++) Iv[i] = 0;
202: Iv[0] = 0.001;
203: Ef = 1;
1.2 takayama 204: MH_X0g = 0.3;
205: MH_Hg = 0.001;
206: MH_Dp = 1;
1.1 takayama 207: Xng = 10.0;
208: }
1.8 takayama 209: static int setParamDefault() {
1.1 takayama 210: int rank;
211: MH_M=MH_Mg = 2 ;
212: MH_RANK=rank = mypower(2,MH_Mg);
1.2 takayama 213: MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1 takayama 214: MH_Beta[0] = 1.0; MH_Beta[1] = 2.0;
1.2 takayama 215: MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0;
216: Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1 takayama 217: Iv[0] = 1.58693;
218: Iv[1] = 0.811369;
219: Iv[2] = 0.846874;
220: Iv[3] = 0.413438;
221: Ef = 0.01034957388338225707;
1.2 takayama 222: MH_X0g = 0.3;
223: MH_Hg = 0.001;
224: MH_Dp = 1;
1.1 takayama 225: Xng = 10.0;
226: }
227:
1.8 takayama 228: static int next(struct SFILE *sfp,char *s,char *msg) {
1.1 takayama 229: s[0] = '%';
230: while (s[0] == '%') {
1.10 takayama 231: if (!mh_fgets(s,SMAX,sfp)) {
232: fprintf(stderr,"Data format error at %s\n",msg);
233: mh_exit(-1);
234: }
235: if (s[0] != '%') return(0);
1.1 takayama 236: }
237: }
1.8 takayama 238: static int setParam(char *fname) {
1.1 takayama 239: int rank;
240: char s[SMAX];
1.4 takayama 241: struct SFILE *fp;
1.1 takayama 242: int i;
1.3 takayama 243: extern int MH_deallocate;
1.4 takayama 244: extern int MH_byFile;
1.3 takayama 245: if (MH_deallocate) {
1.10 takayama 246: if (MH_Beta) mh_free(MH_Beta);
247: if (MH_Ng) mh_free(MH_Ng);
248: if (Iv) mh_free(Iv);
249: return(0);
1.3 takayama 250: }
1.1 takayama 251: if (fname == NULL) return(setParamDefault());
252:
1.4 takayama 253: if ((fp=mh_fopen(fname,"r",MH_byFile)) == NULL) {
1.10 takayama 254: fprintf(stderr,"File %s is not found.\n",fname);
255: mh_exit(-1);
1.1 takayama 256: }
257: next(fp,s,"MH_Mg(m)");
258: sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg;
259: MH_RANK=rank = mypower(2,MH_Mg);
260:
1.2 takayama 261: MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1 takayama 262: for (i=0; i<MH_Mg; i++) {
263: next(fp,s,"MH_Beta");
1.10 takayama 264: sscanf(s,"%lf",&(MH_Beta[i]));
1.1 takayama 265: }
266:
1.2 takayama 267: MH_Ng = (double *)mh_malloc(sizeof(double));
268: next(fp,s,"MH_Ng(freedom parameter n)");
269: sscanf(s,"%lf",MH_Ng);
1.1 takayama 270:
1.2 takayama 271: next(fp,s,"MH_X0g(initial point)");
272: sscanf(s,"%lf",&MH_X0g);
1.1 takayama 273:
1.2 takayama 274: Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1 takayama 275: for (i=0; i<rank; i++) {
1.10 takayama 276: next(fp,s,"Iv(initial values)");
277: sscanf(s,"%lg",&(Iv[i]));
1.1 takayama 278: }
279:
280: next(fp,s,"Ef(exponential factor)");
281: sscanf(s,"%lg",&Ef);
282:
1.2 takayama 283: next(fp,s,"MH_Hg (step size of rk)");
284: sscanf(s,"%lg",&MH_Hg);
1.1 takayama 285:
1.2 takayama 286: next(fp,s,"MH_Dp (data sampling period)");
287: sscanf(s,"%d",&MH_Dp);
1.1 takayama 288:
289: next(fp,s,"Xng (the last point, cf. --xmax)");
290: sscanf(s,"%lf",&Xng);
1.4 takayama 291: mh_fclose(fp);
1.1 takayama 292: }
293:
1.8 takayama 294: int showParam() {
1.1 takayama 295: int rank,i;
1.12 takayama 296: extern int MH_strategy;
297: extern double MH_abserr;
298: extern double MH_relerr;
1.1 takayama 299: rank = mypower(2,MH_Mg);
300: printf("%%MH_Mg=\n%d\n",MH_Mg);
301: for (i=0; i<MH_Mg; i++) {
1.10 takayama 302: printf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
1.1 takayama 303: }
1.2 takayama 304: printf("%%MH_Ng=\n%lf\n",*MH_Ng);
305: printf("%%MH_X0g=\n%lf\n",MH_X0g);
1.1 takayama 306: for (i=0; i<rank; i++) {
1.10 takayama 307: printf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
1.1 takayama 308: }
309: printf("%%Ef=\n%lf\n",Ef);
1.2 takayama 310: printf("%%MH_Hg=\n%lf\n",MH_Hg);
311: printf("%%MH_Dp=\n%d\n",MH_Dp);
1.1 takayama 312: printf("%%Xng=\n%lf\n",Xng);
1.12 takayama 313: printf("%%strategy=%d\n",MH_strategy);
314: printf("%%abserr=%lg, relerr=%lg\n",MH_abserr,MH_relerr);
1.1 takayama 315: }
316:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>