|View source on GitHub|
Runs one step of the elliptic slice sampler.
tfp.experimental.mcmc.EllipticalSliceSampler( normal_sampler_fn, log_likelihood_fn, seed=None, name=None )
Elliptical Slice Sampling is a Markov Chain Monte Carlo (MCMC) algorithm based, as stated in [Murray, 2010].
normal_sampler_fn, the goal of Elliptical
Slice Sampling is to sample from:
p(f) = N(f; 0, Sigma)L(f) / Z
L = log_likelihood_fn
Sigmais a covariance matrix.
- Samples from
normal_sampler_fnare distributed as
N(f; 0, Sigma).
Zis a normalizing constant.
In other words, sampling from a posterior distribution that is proportional to a multivariate gaussian prior multiplied by some likelihood function.
one_step function can update multiple chains in parallel. It assumes
that all leftmost dimensions of
current_state index independent chain states
(and are therefore updated independently). The output of
log_likelihood_fn(*current_state) should sum log-probabilities across all
event dimensions. Slices along the rightmost dimensions may have different
target distributions; for example,
current_state[0, :] could have a
different target distribution from
These semantics are governed both by
Note that the sampler only supports states where all components have a common dtype.
Simple chain with warm-up.
In this example we have the following model.
p(loc | loc0, scale0) ~ N(loc0, scale0) p(x | loc, sigma) ~ N(mu, sigma)
What we would like to do is sample from
p(loc | x, loc0, scale0). In other
words, given some data, we would like to infer the posterior distribution
of the mean that generated that data point.
We can use elliptical slice sampling here.
import tensorflow as tf import tensorflow_probability as tfp import numpy as np tfd = tfp.distributions dtype = np.float64 # loc0 = 0, scale0 = 1 normal_sampler_fn = lambda seed: return tfd.Normal( loc=dtype(0), scale=dtype(1)).sample(seed=seed) # We saw the following data. data_points = np.random.randn(20) # scale = 2. log_likelihood_fn = lambda state: return tf.reduce_sum( tfd.Normal(state, dtype(2.)).log_prob(data_points)) kernel = tfp.mcmc.EllipticalSliceSampler( normal_sampler_fn=normal_sampler_fn, log_likelihood_fn=log_likelihood_fn, seed=1234) samples, _ = tfp.mcmc.sample_chain( num_results=int(3e5), current_state=dtype(1), kernel=kernel, num_burnin_steps=1000, parallel_iterations=1) # For determinism. sample_mean = tf.reduce_mean(samples, axis=0) sample_std = tf.sqrt( tf.reduce_mean(tf.squared_difference(samples, sample_mean), axis=0)) with tf.Session() as sess: [sample_mean, sample_std] = sess.run([sample_mean, sample_std]) print("Sample mean: ", sample_mean) print("Sample Std: ", sample_std)
: Ian Murray, Ryan P. Adams, David J.C. MacKay. Elliptical slice sampling. proceedings.mlr.press/v9/murray10a/murray10a.pdf
normal_sampler_fn: Python callable that takes in a seed and returns a sample from a multivariate normal distribution. Note that the shape of the samples must agree with
log_likelihood_fn: Python callable which takes an argument like
*current_stateif it is a list) and returns its (possibly unnormalized) log-likelihood.
seed: Python integer to seed the random number generator.
strname prefixed to Ops created by this function. Default value:
Trueif Markov chain converges to specified distribution.
TransitionKernels which are "uncalibrated" are often calibrated by composing them with the
__init__arguments and their values.
bootstrap_results( init_state )
Returns an object with the same type as returned by
Tensors representing the initial state(s) of the Markov chain(s).
kernel_results: A (possibly nested)
Tensors representing internal calculations made within this function.
one_step( current_state, previous_kernel_results )
Runs one iteration of the Elliptical Slice Sampler.
Tensors representing the current state(s) of the Markov chain(s). The first
rdimensions index independent chains,
r = tf.rank(log_likelihood_fn(*normal_sampler_fn())).
Tensors representing values from previous calls to this function (or from the
next_state: Tensor or Python list of
Tensors representing the state(s) of the Markov chain(s) after taking exactly one step. Has same type and shape as
collections.namedtupleof internal calculations used to advance the chain.