How would I take this prior distribution and evidence and plug it into pymc3?

Hi, I’m familiar with Bayesian updates using discrete data - but I’m confused on how to do the same thing for continuous data, and someone recommended PyMC3. Here’s my example:

My somewhat informative prior distribution of outcomes is this:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

prior = np.array([20.5,15.25,5.0,29.0,11.75,8.5,8.25,14.5,14.25,
 23.25,31.75,44.5,9.75,2.75, 14.25, 7.0])
prior.sort()
plt.plot(prior, norm.pdf(prior, prior.mean(), prior.std()))

figure

And my observed evidence is:

evidence = np.array([27, 20.75, 24.5]

How would I update this very specific prior distribution, given 3 samples of evidence, using PyMC3? I would expect it to lower the density of outcomes below 20 and above 30 and increase the concentration between 20 and 30.

Thanks so much!

There are many choices one could make in modeling this data with this prior. But here’s a pretty straightforward implementation of what you have described:

import pymc3 as pm
import numpy as np
import arviz as az
import matplotlib.pyplot as plt

prior = np.array([20.5,15.25,5.0,29.0,11.75,8.5,8.25,14.5,14.25,
 23.25,31.75,44.5,9.75,2.75, 14.25, 7.0])

# we'll throw away this data and instead use a normal 
# prior with mean of np.mean(prior)=16.265625
#  and a SD of np.std(prior)=10.784634885306735

data = np.array([27, 20.75, 24.5])

with pm.Model() as model:
    mean = pm.Normal('mean',
                     mu=prior.mean(),
                     sigma=.5*prior.std())
    sigma = pm.Gamma('sigma',
                     alpha=prior.std(),
                     beta=1)
    likelihood = pm.Normal('likelihood',
                           mu=sigma,
                           sigma=sigma,
                           observed=data)

    trace = pm.sample(return_inferencedata=True)

az.plot_trace(trace)
plt.show()
1 Like

Another option is to use pm.Interpolated, there is a bit more info in Updating priors — PyMC3 3.11.4 documentation

1 Like