0

Does anyone know how I can see the final acceptance-rate in PyMC3 (Metropolis-Hastings) ? Or in general, how can I see all the information that pymc3.sample() returns ?

Thanks

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
KiaSh
  • 147
  • 1
  • 6

3 Answers3

1

Given an example, first, set up the model:

import pymc3 as pm3

sigma = 3 # Note this is the std of our data
data = norm(10,sigma).rvs(100)
mu_prior = 8
sigma_prior = 1.5  # Note this is our prior on the std of mu

plt.hist(data,bins=20)
plt.show()

basic_model = pm3.Model()

with basic_model:
    # Priors for unknown model parameters
    mu = pm3.Normal('Mean of Data',mu_prior,sigma_prior)
    # Likelihood (sampling distribution) of observations
    data_in = pm3.Normal('Y_obs', mu=mu, sd=sigma, observed=data)

Second, perform the simulation:

chain_length = 10000 

with basic_model:
    # obtain starting values via MAP
    startvals = pm3.find_MAP(model=basic_model)
    # instantiate sampler
    step = pm3.Metropolis() 
    # draw 5000 posterior samples
    trace = pm3.sample(chain_length, step=step, start=startvals)

Using the above example, the acceptance rate can be calculated this way:

accept = np.sum(trace['Mean of Data'][1:] != trace['Mean of Data'][:-1])
print("Acceptance Rate: ", accept/trace['Mean of Data'].shape[0])

(I found this solution in an online tutorial, but I don't quite understand it.)

Reference: Introduction to PyMC3

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

Let step = pymc3.Metropolis() be our sampler, we can get the final acceptance-rate through

"step.accepted"

Just for beginners (pymc3) like myself, after each variable/obj. put a "." and hit the tab key; you will see some interesting suggestions ;)

KiaSh
  • 147
  • 1
  • 6
0

I checked for the NUTS algorithm, and found the solution from here pymc3 forum.

trace.mean_tree_accept.mean()
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 25 '21 at 11:00