Using pymc3 / posterior probability outside of pymc3: how?

For comparison, I want to use the back density function outside of PyMC3.

In my research project, I want to know how well PyMC3 performs compared to my own code. So I need to compare it with my own probes and likelihood functions.

I think I figured out how to call the inner PyMC3 from behind, but it feels very awkward and I want to know if there is a better way. Right now I am converting the variables manually, whereas I should just pass the parameter dictionary to pymc and get the back density. Is this possible straightforwardly?

Thank you so much!

Demo code:

import numpy as np
import pymc3 as pm
import scipy.stats as st

# Simple data, with sigma = 4. We want to estimate sigma
sigma_inject = 4.0
data = np.random.randn(10) * sigma_inject

# Prior interval for sigma
a, b = 0.0, 20.0

# Build PyMC model
with pm.Model() as model:
    sigma = pm.Uniform('sigma', a, b)      # Prior uniform between 0.0 and 20.0
    likelihood = pm.Normal('data', 0.0, sd=sigma, observed=data)

# Write my own likelihood
def logpost_self(sig, data):
    loglik = np.sum(st.norm(loc=0.0, scale=sig).logpdf(data))   # Gaussian
    logpr = np.log(1.0 / (b-a))                                 # Uniform prior
    return loglik + logpr

# Utilize PyMC likelihood (Have to hand-transform parameters)
def logpost_pymc(sig, model):
    sigma_interval = np.log((sig - a) / (b - sig))    # Parameter transformation
    ldrdx = np.log(1.0/(sig-a) + 1.0/(b-sig))         # Jacobian
    return model.logp({'sigma_interval':sigma_interval}) + ldrdx

print("Own posterior:   {0}".format(logpost_self(1.0, data)))
print("PyMC3 posterior: {0}".format(logpost_pymc(1.0, model)))

      

+3
python-2.7 numpy theano bayesian pymc3


source to share


No one has answered this question yet

Check out similar questions:

21
How does pymc represent the previous distribution and likelihood function?
3
Calculate the maximum probability of using PyMC3
2
pymc3 NUTS can't work well with my hierarchical model for Bayesian neural networks?
2
PKMC3 PK simulation. Model solution cant for parameters used to create dataset
2
setting up MCMC with log-likelihood and log-normal to PyMC
2
pymc3 with custom likelihood function from kernel density estimate
0
Apply a mixture model of two shifted gamma distributions in PyMC3
0
Back sampling using custom probability in pymc3
0
Use Chi-Squared stats in pymc3
0
How to Build Provisional, Logical Likelihood and Posterior on the Same Site Using the BSTS Package from R



All Articles
Loading...
X
Show
Funny
Dev
Pics