Autocorrelation
Autocorrelation is a useful statistic for assessing mixing of a Markov chain. Keanu provides a method of calculating autocorrelation on samples.
Example
With a network defined, we can get the autocorrelation vertex A. The result is a tensor containing the autocorrelation at varying lags.
NetworkSamples posteriorSamples = MetropolisHastings.withDefaultConfig().getPosteriorSamples(
bayesNet,
bayesNet.getLatentVertices(),
100
);
DoubleTensor autocorrelation = posteriorSamples.getDoubleTensorSamples(A).getAutocorrelation();
When the samples are tensors, we need to specify the tensor index on which to calculate the autocorrelation.
For example, if the sample shape is [1,5]
we can evaluate the autocorrelation at index [0,1]
.
NetworkSamples posteriorSamples = MetropolisHastings.withDefaultConfig().getPosteriorSamples(
bayesNet,
bayesNet.getLatentVertices(),
100
);
DoubleTensor autocorrelation = posteriorSamples.getDoubleTensorSamples(A).getAutocorrelation(0,1);