Search code examples
tensorflowbayesiantensorflow2.0edward

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): p distributions when acceptance rate=0

And, sometimes I see the following (when acceptance rate>.9): p distributions when acceptance rate>.9


Solution

  • 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)