问题描述
我只是从TensorFlow开始,我不确定如何从不容易表达为股票分布组成的自定义概率分布中采样。
如何为MCMC采样器的target_log_prob
输入使用自定义函数?
解决方法
您可以将任何target_log_prob_fn
传递给tfp.mcmc.HamiltonianMonteCarlo
TransitionKernel
,只要它计算出与目标密度成比例的值(并且相对于其输入是可微分的)。例如
def target_log_prob_fn(x):
return -.5 * x ** 2
是完全有效的目标日志概率函数。如果要并行采样多个链,则需要注意您的目标是“批次友好”的。例如,如果您需要reduce_sum
遍历状态的某个部分(例如针对多变量分布),请确保明确说明要累加的轴
def target_log_prob_fn(x):
return -.5 * tf.reduce_sum(x ** 2,axis=-1) # don't sum over chains!
...
tfp.mcmc.sample_chain(
kernel,num_burnin_steps=100,num_results=100,current_state=tf.zeros(10,5),# 10 parallel chains of 5-D variables
)
HTH!