version 1.17, 2014/03/12 07:50:37 |
version 1.21, 2014/03/20 10:58:37 |
|
|
/* |
/* |
$OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.16 2014/03/11 05:20:45 takayama Exp $ |
$OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.20 2014/03/15 00:43:47 takayama Exp $ |
License: LGPL |
License: LGPL |
*/ |
*/ |
#include <stdio.h> |
#include <stdio.h> |
|
|
#define inci(i) { i++; if (i >= argc) { fprintf(stderr,"Option argument is not given.\n"); return(NULL); }} |
#define inci(i) { i++; if (i >= argc) { fprintf(stderr,"Option argument is not given.\n"); return(NULL); }} |
int MH_deallocate=0; |
int MH_deallocate=0; |
|
|
|
/* |
|
changelog |
|
2014.03.15 --strategy 1 is default. A new parser in setParam() |
|
*/ |
extern char *MH_Gfname; |
extern char *MH_Gfname; |
extern char *MH_Dfname; |
extern char *MH_Dfname; |
|
|
Line 31 int MH_Dp; /* Data sampling period */ |
|
Line 35 int MH_Dp; /* Data sampling period */ |
|
static double Xng=0.0; /* the last point */ |
static double Xng=0.0; /* the last point */ |
int MH_RawName = 0; |
int MH_RawName = 0; |
static int Testrank=0; |
static int Testrank=0; |
|
/* If MH_success is set to 1, then strategy, MH_abserr, MH_relerr seem to |
|
be properly set. |
|
*/ |
|
int MH_success=0; |
|
/* |
|
Estimation of the maximal coeff of A in y'=Ay. |
|
This might be too rough estimate. |
|
*/ |
|
double MH_coeff_max; |
|
/* |
|
Estimation of h by MH_coeff_max; |
|
*/ |
|
double MH_estimated_start_step; |
extern int MH_Verbose; |
extern int MH_Verbose; |
|
extern int MH_strategy; |
|
extern double MH_abserr; |
|
extern double MH_relerr; |
|
|
|
|
extern int MH_P95; /* 95 % points */ |
extern int MH_P95; /* 95 % points */ |
int mh_gopen_file(void); |
int mh_gopen_file(void); |
static int setParamTest(void); |
static int setParamTest(void); |
Line 41 static int setParamDefault(void); |
|
Line 60 static int setParamDefault(void); |
|
static int setParam(char *fname); |
static int setParam(char *fname); |
static int showParam(void); |
static int showParam(void); |
static int next(struct SFILE *fp,char *s, char *msg); |
static int next(struct SFILE *fp,char *s, char *msg); |
|
static double estimateHg(int m, double beta[],double x0); |
|
|
/* #define DEBUG */ |
/* #define DEBUG */ |
#ifdef DEBUG |
#ifdef DEBUG |
Line 69 main(int argc,char *argv[]) { |
|
Line 89 main(int argc,char *argv[]) { |
|
int strategy=STRATEGY_DEFAULT; |
int strategy=STRATEGY_DEFAULT; |
double err[2]={-1.0,-1.0}; |
double err[2]={-1.0,-1.0}; |
int i; |
int i; |
for (i=1; i<argc; i++) { |
|
if (strcmp(argv[i],"--strategy")==0) { |
|
i++; sscanf(argv[i],"%d",&strategy); |
|
}else if (strcmp(argv[i],"--abserr")==0) { |
|
i++; sscanf(argv[i],"%lg",&(err[0])); |
|
}else if (strcmp(argv[i],"--relerr")==0) { |
|
i++; sscanf(argv[i],"%lg",&(err[1])); |
|
}else ; |
|
} |
|
mh_set_strategy(strategy,err); |
|
mh_exit(MH_RESET_EXIT); /* standalone mode */ |
mh_exit(MH_RESET_EXIT); /* standalone mode */ |
/* mh_main(argc,argv); |
/* mh_main(argc,argv); |
mh_freeWorkArea(); */ |
mh_freeWorkArea(); */ |
mh_main(argc,argv); |
mh_main(argc,argv); |
/* showParam(); */ |
/* showParam(); */ |
|
return(0); |
} |
} |
#endif |
#endif |
struct MH_RESULT *mh_main(int argc,char *argv[]) { |
struct MH_RESULT *mh_main(int argc,char *argv[]) { |
Line 129 struct MH_RESULT *mh_main(int argc,char *argv[]) { |
|
Line 140 struct MH_RESULT *mh_main(int argc,char *argv[]) { |
|
MH_Verbose=1; |
MH_Verbose=1; |
}else if (strcmp(argv[i],"--bystring")==0) { |
}else if (strcmp(argv[i],"--bystring")==0) { |
MH_byFile = 0; |
MH_byFile = 0; |
}else if (strcmp(argv[i],"--strategy")==0) { |
}else if (strcmp(argv[i],"--strategy")==0) { |
i++; /* ignore */ |
i++; sscanf(argv[i],"%d",&MH_strategy); |
}else if (strcmp(argv[i],"--abserr")==0) { |
}else if (strcmp(argv[i],"--abserr")==0) { |
i++; /* ignore */ |
i++; sscanf(argv[i],"%lg",&MH_abserr); |
}else if (strcmp(argv[i],"--relerr")==0) { |
}else if (strcmp(argv[i],"--relerr")==0) { |
i++; /* ignore */ |
i++; sscanf(argv[i],"%lg",&MH_relerr); |
}else { |
}else { |
fprintf(stderr,"Unknown option %s\n",argv[i]); |
fprintf(stderr,"Unknown option %s\n",argv[i]); |
mh_usage(); |
mh_usage(); |
return(rp); |
return(rp); |
} |
} |
} |
} |
if (MH_Verbose) showParam(); |
|
x0 = MH_X0g; |
x0 = MH_X0g; |
xn = Xng; |
xn = Xng; |
ef = Ef; |
ef = Ef; |
Line 149 struct MH_RESULT *mh_main(int argc,char *argv[]) { |
|
Line 159 struct MH_RESULT *mh_main(int argc,char *argv[]) { |
|
y0 = (double *) mh_malloc(sizeof(double)*rank); |
y0 = (double *) mh_malloc(sizeof(double)*rank); |
for (i=0; i<rank; i++) y0[i] = ef*Iv[i]; |
for (i=0; i<rank; i++) y0[i] = ef*Iv[i]; |
mh_gopen_file(); |
mh_gopen_file(); |
if (MH_Verbose) {for (i=0; i<rank; i++) printf("%lf\n",y0[i]); } |
|
rp = (struct MH_RESULT*) mh_malloc(sizeof(struct MH_RESULT)); |
rp = (struct MH_RESULT*) mh_malloc(sizeof(struct MH_RESULT)); |
rp->message = NULL; |
|
|
if (MH_strategy) { |
|
if (MH_abserr > SIGDIGIT_DEFAULT*myabs(y0[0])) { |
|
MH_success = 0; |
|
fprintf(stderr,"%%%%Warning, abserr seems not to be small enough, abserr=%lg, y[0]=%lg\n",MH_abserr,y0[0]); |
|
}else{ |
|
MH_success = 1; |
|
} |
|
}else{ |
|
MH_success = 0; |
|
} |
|
MH_estimated_start_step = estimateHg(MH_Mg,MH_Beta,MH_X0g); |
|
if (MH_Verbose) showParam(); |
|
if (MH_Verbose) {for (i=0; i<rank; i++) printf("%lf\n",y0[i]); } |
|
|
*rp=mh_rkmain(x0,y0,xn); |
*rp=mh_rkmain(x0,y0,xn); |
return(rp); |
return(rp); |
} |
} |
Line 163 int mh_usage() { |
|
Line 186 int mh_usage() { |
|
fprintf(stderr,"[ --95 --verbose] \n"); |
fprintf(stderr,"[ --95 --verbose] \n"); |
fprintf(stderr,"[ --strategy s --abserr ae --relerr re] \n"); |
fprintf(stderr,"[ --strategy s --abserr ae --relerr re] \n"); |
fprintf(stderr,"s:0 rk, s:1 adaptive, s:2 adaptive&multiply, see rk.c for the default value of ae and re.\n"); |
fprintf(stderr,"s:0 rk, s:1 adaptive, s:2 adaptive&multiply, see rk.c for the default value of ae and re.\n"); |
|
fprintf(stderr,"strategy default = %d\n",MH_strategy); |
fprintf(stderr,"--raw does not add data parameters to the output_data_file.\n"); |
fprintf(stderr,"--raw does not add data parameters to the output_data_file.\n"); |
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"); |
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"); |
fprintf(stderr,"All the eigenvalues of sigma must be simple.\n"); |
fprintf(stderr,"All the eigenvalues of sigma must be simple.\n"); |
fprintf(stderr,"Parameters are specified by the input_data_file.\n"); |
fprintf(stderr,"Parameters are specified by the input_data_file.\n"); |
|
fprintf(stderr,"Parameters are redefined when they appear more than once in the idata file and the command line options.\n"); |
fprintf(stderr,"The format of the input_data_file, which should be generated by the command hgm_jack-n.\n"); |
fprintf(stderr,"The format of the input_data_file, which should be generated by the command hgm_jack-n.\n"); |
fprintf(stderr," MH_Mg: m, MH_Beta: beta=sigma^(-1)/2 (diagonized), MH_Ng: n, MH_X0g: starting value of x,\n"); |
fprintf(stderr," MH_Mg: m, MH_Beta: beta=sigma^(-1)/2 (diagonized), MH_Ng: n, MH_X0g: starting value of x,\n"); |
fprintf(stderr," Iv: initial values at MH_X0g*MH_Beta (see our paper how to order them), \n"); |
fprintf(stderr," Iv: initial values at MH_X0g*MH_Beta (see our paper how to order them), \n"); |
fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n"); |
fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n"); |
fprintf(stderr," MH_Hg: h (step size),\n"); |
fprintf(stderr," MH_Hg: h (step size),\n"); |
fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n"); |
fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n"); |
fprintf(stderr," Xng: terminating value of x.\n"); |
fprintf(stderr," Xng: terminating value of x.\n\n"); |
fprintf(stderr," --95: output the 95%% point. --verbose: verbose mode.\n"); |
fprintf(stderr,"--95: output the 95%% point. --verbose: verbose mode.\n"); |
fprintf(stderr," The line started with %% is a comment line.\n"); |
fprintf(stderr,"The line started with %%%% or # is a comment line.\n"); |
fprintf(stderr," An example format of the input_data_file can be obtained by executing hgm_jack-n with no option.\n"); |
fprintf(stderr,"An example format of the input_data_file can be obtained by executing hgm_jack-n with no option.\n"); |
fprintf(stderr,"When --idata option is used, this command is quiet. Use --verbose option if you want to see some messages.\n"); |
fprintf(stderr,"When --idata option is used, this command is quiet. Use --verbose option if you want to see some messages.\n"); |
fprintf(stderr,"\nExamples:\n"); |
fprintf(stderr,"\nExamples:\n"); |
fprintf(stderr,"[1] ./hgm_w-n \n"); |
fprintf(stderr,"[1] ./hgm_w-n \n"); |
fprintf(stderr,"[2] ./hgm_w-n --xmax 20\n"); |
fprintf(stderr,"[2] ./hgm_w-n --xmax 20\n"); |
fprintf(stderr,"[3] ./hgm_w-n --test 6\n"); |
fprintf(stderr,"[3] ./hgm_w-n --test 6\n"); |
fprintf(stderr," A test run in Mg=6.\n"); |
fprintf(stderr," A test run in Mg=6.\n"); |
fprintf(stderr,"[4] ./hgm_jack-n --idata Testdata/tmp-idata3.txt --degree 15 >t.txt\n"); |
fprintf(stderr,"[4] ./hgm_jack-n --idata Testdata/tmp-idata3.txt >t.txt\n"); |
fprintf(stderr," ./hgm_w-n --idata t.txt --gnuplotf test-g\n"); |
fprintf(stderr," ./hgm_w-n --idata t.txt --gnuplotf test-g --verbose\n"); |
fprintf(stderr," gnuplot -persist <test-g-gp.txt\n"); |
fprintf(stderr," gnuplot -persist <test-g-gp.txt\n"); |
fprintf(stderr," tmp-idata3.txt is a sample input data distributed with this file.\n"); |
fprintf(stderr," tmp-idata3.txt is a sample input data distributed with this file.\n"); |
fprintf(stderr," test-g-gp.txt is an input file of the gnuplot\n"); |
fprintf(stderr," test-g-gp.txt is an input file of the gnuplot\n"); |
Line 245 static int setParam(char *fname) { |
|
Line 270 static int setParam(char *fname) { |
|
char s[SMAX]; |
char s[SMAX]; |
struct SFILE *fp; |
struct SFILE *fp; |
int i; |
int i; |
|
struct mh_token tk; |
extern int MH_deallocate; |
extern int MH_deallocate; |
extern int MH_byFile; |
extern int MH_byFile; |
if (MH_deallocate) { |
if (MH_deallocate) { |
Line 293 static int setParam(char *fname) { |
|
Line 319 static int setParam(char *fname) { |
|
|
|
next(fp,s,"Xng (the last point, cf. --xmax)"); |
next(fp,s,"Xng (the last point, cf. --xmax)"); |
sscanf(s,"%lf",&Xng); |
sscanf(s,"%lf",&Xng); |
|
|
|
/* Reading the optional parameters */ |
|
while ((tk = mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_EOF) { |
|
/* expect ID */ |
|
if (tk.type != MH_TOKEN_ID) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
if ((strcmp(s,"abserr")==0) || (strcmp(s,"abserror")==0)) { |
|
if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_DOUBLE) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
MH_abserr = tk.dval; |
|
continue; |
|
} |
|
if ((strcmp(s,"relerr")==0) || (strcmp(s,"relerror")==0)) { |
|
if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_DOUBLE) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
MH_relerr = tk.dval; |
|
continue; |
|
} |
|
if (strcmp(s,"strategy")==0) { |
|
if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) { |
|
fprintf(stderr,"Syntax error at %s\n",s); mh_exit(-1); |
|
} |
|
MH_strategy = tk.ival; |
|
continue; |
|
} |
|
fprintf(stderr,"Unknown ID at %s\n",s); mh_exit(-1); |
|
} |
|
|
mh_fclose(fp); |
mh_fclose(fp); |
} |
} |
|
|
int showParam() { |
static int showParam() { |
int rank,i; |
int rank,i; |
extern int MH_strategy; |
extern int MH_strategy; |
extern double MH_abserr; |
extern double MH_abserr; |
Line 316 int showParam() { |
|
Line 382 int showParam() { |
|
printf("%%MH_Dp=\n%d\n",MH_Dp); |
printf("%%MH_Dp=\n%d\n",MH_Dp); |
printf("%%Xng=\n%lf\n",Xng); |
printf("%%Xng=\n%lf\n",Xng); |
printf("%%strategy=%d\n",MH_strategy); |
printf("%%strategy=%d\n",MH_strategy); |
printf("%%abserr=%lg, relerr=%lg\n",MH_abserr,MH_relerr); |
printf("%%abserr=%lg, %%relerr=%lg\n",MH_abserr,MH_relerr); |
|
printf("#MH_success=%d\n",MH_success); |
|
printf("#MH_coeff_max=%lg\n",MH_coeff_max); |
|
printf("#MH_estimated_start_step=%lg\n",MH_estimated_start_step); |
} |
} |
|
|
|
static double estimateHg(int m, double beta[],double x0) { |
|
int i,j; |
|
double dmin; |
|
double cmax; |
|
double h; |
|
/* mynote on 2014.03.15 */ |
|
if (m>1) dmin = myabs(beta[1]-beta[0]); |
|
else dmin=myabs(beta[0]); |
|
for (i=0; i<m; i++) { |
|
for (j=i+1; j<m; j++) { |
|
if (myabs(beta[i]-beta[j]) < dmin) dmin = myabs(beta[i]-beta[j]); |
|
} |
|
} |
|
dmin = dmin*x0*2; |
|
cmax = 1.0; |
|
for (i=0; i<m; i++) cmax = cmax*dmin; |
|
cmax = 1.0/cmax; |
|
MH_coeff_max=cmax; |
|
h = exp(log(MH_abserr/cmax)/5.0); |
|
MH_estimated_start_step = h; |
|
return h; |
|
} |