# Measuring dependency of subsequent points from Markov chain

The question is about stimulating different type of species (coded 1-10) based on given species frequencies, and other parameters (eg. mean of normally distributed mass and ratio) using gibbs sampling. I have done all the work above and get a trace plot(shown below), and I want to know after burn in period, how dependent the subsequent points from the Markov chain are. Someone told me to use pacf and acf from time series class but I never learned that before. Could some one explain that or is there any other way to show the dependency?

(I interpret dependency as how the next point is dependent on the previous one from the markov chain) What you are looking for is autocorrelation (R acf function computes it). If $$X_t$$ is a random variable, a time-series with $$t = 1,...,T$$ cases, $$\mu$$ is its mean, then we can define its autocovariance for lag $$\tau$$ as

$$\gamma(\tau) = \mathbb{E}\left[ (X_t - \mu)(X_{t+\tau} - \mu) \right]$$

for $$\tau = 0$$ this reduces to regular variance

$$\gamma(0) = \mathbb{E}\left[ (X_t - \mu)(X_t - \mu) \right] = \mathbb{E}\left[ (X_t - \mu)^2 \right]$$

and from here we can derive correlation (autocorrelation) for lag $$\tau$$ as

$$\rho(\tau) = \gamma(\tau) / \gamma(0)$$

$$r_{\tau} = \frac{ \sum_{t=1}^{N-1} \left[(x_t - \bar x)(x_{t+\tau} - \bar x) \right]}{ \sum_{t=1}^N (x_t - \bar x)^2 }$$

With autocorrelation we compute the correlation between each $$t$$ value and $$t+\tau$$ value of $$X$$. It lies in $$[-1, 1]$$ range, as standard correlation. Autocorrelation is a commonly used summary statistics in time-series analysis (e.g. here). In your case, if you are interested in dependence on previous observation only, you need to compute it only for lag $$\tau = 1$$, but in many other cases you would probably look also for other lags since the dependence structure could be more complicated.

If you want to visualize it to yourself, let me use a simple random walk example:

set.seed(123)

N <- 100
x <- numeric(N)
x <- 0

for (t in 2:N)
x[t] = x[t-1] + rnorm(1) To visualize autocorrelation for lag $$\tau = 1$$ you can just simply plot $$x_t$$ versus $$x_{t+\tau}$$: > cor(x[1:(N-1)], x[2:N])
 0.9186401
> acf(x, lag.max = 1, plot = FALSE)

Autocorrelations of series ‘x’, by lag

0    1
1.00 0.86


as you can see, values of autocorrelation are not exactly the same like with simple correlation, because they are calculated differently but the general idea is the same like with regular correlation. For general introduction you can check some time-series handbooks, for example Chatfield (2003) or Hyndman and Athana­sopou­los (2013, available online).

Chatfield, C. (2003). The Analysis of Time Series: An Introduction. Chapman and Hall/CRC.

Hyndman, R.J. and Athana­sopou­los, G. (2013). Forecasting: principles and practice. OTexts.