/* ========================================================================== ADAPTCHOL.C -- a program for doing MCMC with multivariate adaptions. Copyright (c) 2004, 2006 by Gareth O. Roberts and Jeffrey S. Rosenthal Licensed for general copying, distribution and modification according to the GNU General Public License (http://www.gnu.org/copyleft/gpl.html). ---------------------------------------------------- Save as "adaptchol.c". Compile with "cc adaptchol.c -lm", then run with "a.out". Upon completion, can run 'source("adaptx")' in R to see a trace plot of the first coordinate. Also, can run '< #include #include /* #define NUMITS 1000000 */ #define NUMITS 5000000 /* #define DIM 200 */ #define DIM 200 #define beta 0.05 /* set beta = 0.0 for purest adapting */ #define initstepmultiplier 2 #define identitymultiplier 0.1 /* #define identitymultiplier 2.38 */ #define PRINTSPACING 1000 /* #define bspacing (NUMITS / 20) */ #define MATHFILE "adaptmath" #define XFILE "adaptx" #define RFILE "adaptR" #define EFILE "adapte" #define BFILE "adaptb" #define AFILE "adapta" #define PI 3.14159265 double drand48(); double targetinvcov[DIM][DIM]; main() { int i,j,k,t, firsttime, numaccept, xspacing, bspacing, aspacing; double X[DIM], Y[DIM]; double empcov[DIM][DIM], invtargetsqrt[DIM][DIM]; double chol[DIM][DIM], normalsvec[DIM]; double meanx[DIM], oldmeanx[DIM]; double covsumsq[DIM][DIM], prevcovsumsq[DIM][DIM], compmat[DIM][DIM]; double ell, A, tmpsum, normal(), targlogdens(); FILE *fpmath, *fpx, *fpr, *fpe, *fpb, *fpa; struct timeval currtv; double begintime, endtime; /* long beginsec, beginusec, endsec, endusec; */ /* INITIALISATIONS. */ seedrand(); ell = 2.381204; firsttime = 10*DIM; for (i=0; i xspacing) /* Not first one printed, so need comma. */ fprintf(fpx, ","); fprintf(fpx, " %f", X[0]); } if (t == aspacing * (t/aspacing)) { /* Write X[0] to file. */ if (t > aspacing) /* Not first one printed, so need comma. */ fprintf(fpa, ","); fprintf(fpa, " %f", X[0]); } /* Update meanx[i]. */ for (i=0; i