#include #include #include #include #include /* Use the Metropolis-Hastings algorithm to sample from a d-dimensional multivariate normal N(0,I) distribution. A random walk proposal distribution is used with a range of different step sizes. The goal is to assess the performance of different step sizes for estimating E X(1) = 0 based on the sample mean from a chain of a given length. The process is replicated to get an estimate of the MSE for each step length. According to theory, an acceptance rate around 0.23 should be optimal. */ int main(int argc, char** argv) { /* The standard deviation of the step. */ double f; /* The current point in the Markov Chain. */ double* X; /* The current sum of the entire history of the Markov Chain. */ double M; /* The current MSE estimate for X(1)^2 * ... * X(d)^2. */ double MSE; /* The proposal point. */ double* x; /* The dimension. */ int d = 10; /* The length of the Markov chain realization. */ int chainlen = 1000; /* The number of replications to estimate the MSE. */ int nrep = 1000; /* Keep track of the number of acceptances. */ int acc; /* Temporary variables. */ int r, it, j; double R; /* Allocations. */ X = (double*)malloc(d*sizeof(double)); x = (double*)malloc(d*sizeof(double)); /* Consider a range of different step lengths. */ for(f=0.3; f<=1.5; f+=0.1) { /* Replication to estimate the MSE. */ MSE = 0; acc = 0; for (r=0; r