/* ========================================================================== BANANA.C -- a program for doing MCMC with multivariate adaptions. Copyright (c) 2008 by 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 "banana.c". Compile with "cc banana.c -lm", then run with "a.out". Upon completion, can run e.g. 'source("adaptx1")' in R to see a trace plot of the first coordinate. Also, can run '< #include #include /* #define NUMITS 1000000 */ #define NUMITS 5000000 #define DIM 2 #define bananicity 0.1 #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 MATHFILE "adaptmath" #define X1FILE "adaptx1" #define X2FILE "adaptx2" #define RFILE "adaptR" #define EFILE "adapte" #define BFILE "adaptb" #define AFILE "adapta" #define PI 3.14159265 double drand48(); main() { int i,j,k,t, firsttime, numaccept, xspacing, bspacing, aspacing; double X[DIM], Y[DIM]; double empcov[DIM][DIM]; double chol[DIM][DIM], normalsvec[DIM]; double meanx[DIM], oldmeanx[DIM]; double covsumsq[DIM][DIM], prevcovsumsq[DIM][DIM], compmat[DIM][DIM]; double Vmatrix[DIM][DIM]; double ell, A, tmpsum, normal(), targlogdens(), sq(); FILE *fpmath, *fpx1, *fpx2, *fpr, *fpe, *fpb, *fpa; struct timeval currtv; double begintime, endtime; /* long beginsec, beginusec, endsec, endusec; */ /* INITIALISATIONS. */ seedrand(); ell = 2.381204; firsttime = 10*DIM; numaccept = 0; for (i=0; i xspacing) { /* Not first one printed, so need comma. */ fprintf(fpx1, ","); fprintf(fpx2, ","); } fprintf(fpx1, " %f", X[0]); fprintf(fpx2, " %f", X[1]); } 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