TransitionKernel
steps.R/mcmcfunctions.R
mcmc_sample_chain.Rd
This function samples from an Markov chain at current_state
and whose
stationary distribution is governed by the supplied TransitionKernel
instance (kernel
).
mcmc_sample_chain( kernel = NULL, num_results, current_state, previous_kernel_results = NULL, num_burnin_steps = 0, num_steps_between_results = 0, trace_fn = NULL, return_final_kernel_results = FALSE, parallel_iterations = 10, seed = NULL, name = NULL )
kernel  An instance of 

num_results  Integer number of Markov chain draws. 
current_state 

previous_kernel_results  A 
num_burnin_steps  Integer number of chain steps to take before starting to collect results. Default value: 0 (i.e., no burnin). 
num_steps_between_results  Integer number of chain steps between collecting
a result. Only one out of every 
trace_fn  A function that takes in the current chain state and the previous
kernel results and return a 
return_final_kernel_results  If 
parallel_iterations  The number of iterations allowed to run in parallel. It
must be a positive integer. See 
seed  Optional, a seed for reproducible sampling. 
name  string prefixed to Ops created by this function. Default value: 
list of:
checkpointable_states_and_trace: if return_final_kernel_results
is
TRUE
. The return value is an instance of CheckpointableStatesAndTrace
.
all_states: if return_final_kernel_results
is FALSE
and trace_fn
is
NULL
. The return value is a Tensor
or Python list of Tensor
s
representing the state(s) of the Markov chain(s) at each result step. Has
same shape as input current_state
but with a prepended
num_results
size dimension.
states_and_trace: if return_final_kernel_results
is FALSE
and
trace_fn
is not NULL
. The return value is an instance of
StatesAndTrace
.
This function can sample from multiple chains, in parallel. (Whether or not
there are multiple chains is dictated by the kernel
.)
The current_state
can be represented as a single Tensor
or a list
of
Tensors
which collectively represent the current state.
Since MCMC states are correlated, it is sometimes desirable to produce
additional intermediate states, and then discard them, ending up with a set of
states with decreased autocorrelation. See Owen (2017). Such "thinning"
is made possible by setting num_steps_between_results > 0
. The chain then
takes num_steps_between_results
extra steps between the steps that make it
into the results. The extra steps are never materialized (in calls to
sess$run
), and thus do not increase memory requirements.
Warning: when setting a seed
in the kernel
, ensure that sample_chain
's
parallel_iterations=1
, otherwise results will not be reproducible.
In addition to returning the chain state, this function supports tracing of
auxiliary variables used by the kernel. The traced values are selected by
specifying trace_fn
. By default, all kernel results are traced but in the
future the default will be changed to no results being traced, so plan
accordingly. See below for some examples of this feature.
Other mcmc_functions:
mcmc_effective_sample_size()
,
mcmc_potential_scale_reduction()
,
mcmc_sample_annealed_importance_chain()
,
mcmc_sample_halton_sequence()
# \donttest{ dims < 10 true_stddev < sqrt(seq(1, 3, length.out = dims)) likelihood < tfd_multivariate_normal_diag(scale_diag = true_stddev) kernel < mcmc_hamiltonian_monte_carlo( target_log_prob_fn = likelihood$log_prob, step_size = 0.5, num_leapfrog_steps = 2 ) states < kernel %>% mcmc_sample_chain( num_results = 1000, num_burnin_steps = 500, current_state = rep(0, dims), trace_fn = NULL ) sample_mean < tf$reduce_mean(states, axis = 0L) sample_stddev < tf$sqrt( tf$reduce_mean(tf$math$squared_difference(states, sample_mean), axis = 0L)) # }