简体   繁体   中英

predicting p of binomial with beta prior in edward2 & tensorflow2

The following code predicts the p of the binomial distribution by using beta as prior. Somehow, sometimes, I get meaningless results (acceptance rate = 0). When I write the same logic with pymc3, I have no issue. I couldn't see what I am missing here.

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
import edward2 as ed
from pymc3.stats import hpd
import numpy as np
import seaborn
import matplotlib.pyplot as plt

p_true = .15

N = [10, 100, 1000]
successN = np.random.binomial(p=p_true, n=N)
print(N)
print(successN)


def beta_binomial(N):
    p = ed.Beta(
        concentration1=tf.ones( len(N) ),
        concentration0=tf.ones( len(N) ),
        name='p'
    )
    return ed.Binomial(total_count=N, probs=p, name='obs')

log_joint = ed.make_log_joint_fn(beta_binomial)

def target_log_prob_fn(p):
    return log_joint(N=N, p=p, obs=successN)

#kernel = tfp.mcmc.HamiltonianMonteCarlo(
#    target_log_prob_fn=target_log_prob_fn,
#    step_size=0.01,
#    num_leapfrog_steps=5)
kernel = tfp.mcmc.NoUTurnSampler(
    target_log_prob_fn=target_log_prob_fn,
    step_size=.01
    )
trace, kernel_results = tfp.mcmc.sample_chain(
    num_results=1000,
    kernel=kernel,
    num_burnin_steps=500,
    current_state=[
        tf.random.uniform(( len(N) ,))
    ],
    trace_fn=(lambda current_state, kernel_results: kernel_results),
    return_final_kernel_results=False)

p, = trace
p = p.numpy()
print(p.shape)
print('acceptance rate ', np.mean(kernel_results.is_accepted))
def printSummary(name, v):
    print(name, v.shape)
    print(np.mean(v, axis=0))
    print(hpd(v))

printSummary('p', p)
for data in p.T:
    print(data.shape)
    seaborn.distplot(data, kde=False)

plt.savefig('p.png')

Libraries:

pip install -U pip
pip install -e git+https://github.com/google/edward2.git@4a8ed9f5b1dac0190867c48e816168f9f28b5129#egg=edward2
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-2.0.0-cp37-cp37m-manylinux2010_x86_64.whl#egg=tensorflow
pip install tensorflow-probability

Sometimes I see the following (when acceptance rate=0): 接受率=0时的p分布

And, sometimes I see the following (when acceptance rate>.9): 接受率>.9时的p分布

When I get unstable results in Bayesian inference (I use mc-stan, but it's also using NUTS), it's usually because either the priors and likelihood are mis-specified, or the hyperparameters are not good for the problem.

That first graph shows that the sampler never moved away from the initial guess at the answers (hence the 0 acceptance rate). It also worries me that the green distribution seems to be right on 0. The beta(1,1) has positive probability at 0 but ap=0 might be an unstable solution here? (as in, the sampler may not be able to calculate the derivative at that point and returns a NaN, so doesn't know where to sample next?? Complete guess there).

Can you force the initial condition to be 0 and see if that always creates a failed sampling?

Other than that, I would try tweaking the hyperparameters, such as step size, number of iterations, etc...

Also, you may want to simplify the example by only using one N. Might help you diagnose. Good luck!

random.uniform 's maxval default value is None. I changed it to 1, the result became stable.

random.uniform(( len(N) ,), minval=0, maxval=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM