forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 0
BayesFactor
Chris Fonnesbeck edited this page Feb 20, 2011
·
1 revision
#!/usr/bin/env python
# encoding: utf-8
"""
bayesfactor.py
Computation of Bayes factor for comparing two models, following the example in
panel 7.1 in Link and Barker (2010). The posterior model probabilities are the
product of the BF and the prior probabilities, so the BF can be calculated by
dividing the posterior odds (the inverse of the complement of the mean of
'true_model' int the model below) by the prior odds.
Created by Christopher J. Fonnesbeck on 2010-12-15.
Copyright (c) 2010 Vanderbilt University. All rights reserved.
"""
from pymc import Uniform, Categorical, Lambda, observed, Bernoulli, geometric_like, poisson_like
from pymc.flib import factln
from numpy import exp, log, array, ones
# Data
Y = array([0,1,2,3,8])
# Prior model probabilities
pi = (0.1, 0.9)
# Index to true model
true_model = Categorical("true_model", p=pi, value=0)
# Poisson mean
mu = Uniform('mu', lower=0, upper=1000, value=4)
# Geometric probability
p = Lambda('p', lambda mu=mu: 1/(1+mu))
@observed
def Ylike(value=Y, mu=mu, p=p, M=true_model):
"""Either Poisson or geometric, depending on M"""
return geometric_like(value+1, p)*(M==0) or poisson_like(value, mu)