LaplacesDemon: When Should I Sum Previous Density?
I am switching from JAGS to LaplacesDemon and am trying to rewrite some of my codes. I've read the LaplacesDemon Tutorial and LaplacesDemon vignettes and I'm a little confused by some of the vignettes examples.
In a simple example in the LaplacesDemon Tutorial (page 5), the model is written as:
Model <- function(parm, Data)
{beta <- parm[Data$pos.beta]
sigma <- interval(parm[Data$pos.sigma], 1e-100, Inf)
parm[Data$pos.sigma] <- sigma
beta.prior <- dnormv(beta, 0, 1000, log=TRUE)
sigma.prior <- dhalfcauchy(sigma, 25, log=TRUE)
mu <- tcrossprod(beta, Data$X)
LL <- sum(dnorm(Data$y, mu, sigma, log=TRUE))
LP <- LL + sum(beta.prior) + sigma.prior
Modelout <- list(LP=LP, Dev=-2*LL, Monitor=LP,
yhat=rnorm(length(mu), mu, sigma), parm=parm)
return(Modelout)}
It has beta.prior
been summarized here for LP
as there is more than one beta parameter.
But I found in the more complex examples in the LaplacesDemon Example vignette it doesn't seem to always follow the rule. For example, in example 87 (p. 162):
Model <- function(parm, Data)
{### Log-Prior
beta.prior <- sum(dnormv(beta[,1], 0, 1000, log=TRUE), dnorm(beta[,-1], beta[,-Data$T], matrix(tau, Data$K, Data$T-1), log=TRUE))
zeta.prior <- dmvn(zeta, rep(0,Data$S), Sigma[ , , 1], log=TRUE)
phi.prior <- sum(dhalfnorm(phi[1], sqrt(1000), log=TRUE), dtrunc(phi[-1], "norm", a=0, b=Inf, mean=phi[-Data$T], sd=sigma[2], log=TRUE))
### Log-Posterior
LP <- LL + beta.prior + zeta.prior + sum(phi.prior) + sum(kappa.prior) + sum(lambda.prior) + sigma.prior + tau.prior
Modelout <- list(LP=LP, Dev=-2*LL, Monitor=LP, yhat=rnorm(prod(dim(mu)), mu, sigma[1]), parm=parm)
return(Modelout)}
(Place only part of the codes due to the length of the example codes)
Here there is zeta
more than one, but it is not summed in either part Log-Prior
, nor Log-Posterior
, beta
more than one, and summed up in Log-Prior
and phi
also more than one parameter, but it was summed up in both parts Log-Prior
and Log-Posterior
.
And in the next example on page 167, it seems different.
I was wondering in which scenario should we sum the previous density? Many thanks!
source to share
Have you tried running code line by line? You would know that there is nothing to add, since dmvn
is the density function of the multivariate normal distribution and returns one value, the probability density of the observation vector zeta
. The reason for all the sums is that we multiply their marginal probabilities (or sum their logs) to get the probability of seeing two independent events. So we multiply the probabilities of seeing all the feathers together to get the joint distribution.
source to share