I am trying to follow example in the book Statistical Rethinking (Page 210, code 7.13 and 7.14) for calculating LPPD of a simple model.
The given example code in the book is:
set.seed(1)
logprob <- sim( m7.1 , ll=TRUE , n=1e4 )
n <- ncol(logprob)
ns <- nrow(logprob)
f <- function( i ) log_sum_exp( logprob[,i] ) - log(ns)
( lppd <- sapply( 1:n , f ) )
I assumed ll=TRUE
provides the logprobability, I tried calculating it as (in julia)
begin
m=10
# Take random samples from posterior of parameters
quap_sample = sample_from_model()
# evaluate mean from sampled parameters
sim_samples = quap_sample.a .+ quap_sample.b * mass_std'
# log of samples, I think this part am doing wrong as it is the log of mean I am evaluating and not logprob?
sim_samples = log.(sim_samples)
nr,nc = size(sim_samples)
lppd_sample = zeros(nc)
for i = 1:nc
lppd_sample[i] = logsumexp(sim_samples[:,i]) - log(nr)
end
lppd_sample
end
Of course results are not matching, Am not totally sure what is ll=TRUE
doing (source code accessible here), and previous discussion were also not much helpful