quentingronau / bridgesampling

R package for bridge sampling
32 stars 16 forks source link

reason for the difference between result from bridge_samper and analytical result #23

Closed yu-zhang-oYo closed 4 years ago

yu-zhang-oYo commented 4 years ago

I try to calculate the marginal likelihood of the example in the article " a tutorial on bridge sampling", which is estimating the marginal likelihood for a binomial model assuming a uniform prior on the rate parameter θ (i.e., the beta-binomial model). Consider a single participant who answered k = 2 out of n = 10 true/false questions correctly. The analytical result is 1/(n+1), that is 1/11.

And Then I get a stanfit with rstan, and use bridge_sampler(stanfit) to get the log marginal likelihood. After transforming the log marginal likelihood to the marginal likelihood, I find it is totally different from 1/11. I don't know the reason for this.

stanmodelcode <- "
data {
    int<lower=0> N; 
    int y;
}

parameters {
    real<lower=0,upper=1> theta; 
}

model {
    theta ~ beta(1,1);
    y ~ binomial(N,theta) ;
}"

y <- 2
dat <- list(N = 10, y = y);

stanfit <- stan(model_code = stanmodelcode,data=dat,chains=1 , iter=50000 , warmup=500)

(Lik_marginal <- bridgesampling::bridge_sampler(stanfit))
quentingronau commented 4 years ago

You need to write the model in a way that retains all constants, as described in this article. This produces the correct value.

model {

    target += beta_lpdf(theta | 1, 1);
    target += binomial_lpmf(y | N, theta);

}