/* The following C program studies the speed of convergence of a matrix to its stationary distribution using the Metropolis-Hastings algorithm. The 'frog example' is used again */ #define NODES 20 /* size of the state space */ #define ITERATIONS 40 /* number of jumps before we take an approximate sample from the stationary distribution */ #define RUNS 500000 /* number of trials that are averaged together to estimate the stationary distribution */ #define M 5 /* all nodes within M of each other have an edge between them */ #define BETA 2 /* target distribution is proportional to (1 + distance to node zero)^-BETA */ #include #include #include #include void simulate(); int distance (int); int main() { srand((unsigned) time(NULL)); simulate(); exit(0); } void simulate() { int edges[NODES][NODES], total[NODES], sum[NODES]; int current, temp, partsum, proposal, c, i, j, rejects, min; double sdist[NODES], target[NODES]; double rejsum=0, vardist=0, l2distpi=0, l2distpiinv=0, denom=0; double randomnum, randomnum2, alpha, rejperc; FILE *fp; fp = fopen("outputfile", "w"); /* setting all values in arrays to zero so we can add them */ for (i=0; i