Computing Bayes factors

The computation of Bayes factors can be framed as a hierarchical model, where the high-level parameter is an index that's assigned to each model and sampled from a categorical distribution. In other words, we perform inference of the two (or more) competing models at the same time and we use a discrete variable that jumps between models. How much time we spend sampling each model is proportional to . Then, we apply equation 5.10 to get Bayes factors.

To exemplify the computation of the Bayes factors, we are going to flip coins one more time:

Figure 5.10

Notice that while we are computing Bayes factors between models that differ only on the prior, the models could differ on the likelihood, or even both. This idea is the same.

Let's create some data so that we can use it in an example:

coins = 30
heads = 9
y_d = np.repeat([0, 1], [coins-heads, heads])

Now, let's look at the PyMC3 model. To switch between priors, we are using the pm.math.switch function. If the first argument of this function evaluates to true, then the second argument is returned, otherwise the third argument is returned. Notice that we are also using the pm.math.eq function to check whether the model_index variable is equal to 0:

with pm.Model() as model_BF:
p = np.array([0.5, 0.5])
model_index = pm.Categorical('model_index', p=p)

m_0 = (4, 8)
m_1 = (8, 4)
m = pm.math.switch(pm.math.eq(model_index, 0), m_0, m_1)

# a priori
θ = pm.Beta('θ', m[0], m[1])
# likelihood
y = pm.Bernoulli('y', θ, observed=y_d)

trace_BF = pm.sample(5000)
az.plot_trace(trace_BF)

Figure 5.11

Now, we need to compute the Bayes factor by counting the model_index variable. Notice that we have included the values of the priors for each model:

pM1 = trace_BF['model_index'].mean()
pM0 = 1 - pM1
BF = (pM0 / pM1) * (p[1] / p[0])

As a result, we get a value of ≈ 11, meaning that model 0 is favored over model 1 by one order of magnitude. This makes total sense since the data has fewer values of heads than expected for and the only difference between both models is that the prior of model 0 is more compatible with  (more tails than heads) and that model 1 is more compatible with  (more heads than tails).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.106.233