I have a problem with a numerical integration to normalize a posteriori distribution. My code is:
a1 <- 0.012
sigma_a1 <- 0.001
likelihood <- function(a_true) {
(1 / (sqrt(2 * pi) * sigma_a1 )) *
exp(-((a1- a_true) ^ 2) / (2 * (sigma_a1 ^ 2)))
}
prior <- function(a_true) {
if (a_true >= (a1 - sigma_a1 ) && a_true <= (a1+ sigma_a1 )) {
return (1 / (2 * sigma_a1 ))
} else {
return (0)
}
}
posterior <- function(a_true) {
likelihood(a_true) * prior(a_true)
}
# step: Normalization of Posterior Distribution
integr <- integrate(posterior, lower = a1 - sigma_a1 , upper = a1 + sigma_a1)
a_true_estimated <- integrate(function(a_true) {
a_true* posterior(a_true) / integral$value
}, lower = a1 - sigma_a1 , upper = a1 - sigma_a1)$value
The error presented was
Error in z_verdadeiro >= (z_photo - sigma_z_galphoto) && z_verdadeiro <= :
'length = 21' in coercion to 'logical(1)'
How can I fix this?
priorneeds to be vectorized:But notice that since
prioris uniform over [a1 - sigma_a1,a1 + sigma_a1], and the extents of the integration are also [a1 - sigma_a1,a1 + sigma_a1],posterioris simplydnorm(a_true, a1, sigma_a1)/(2*sigma_a1), and the normalization constant isdiff(pnorm(a1 + c(1, -1)*sigma_a1, a1, sigma_a1))/(2*sigma_a1). Furthermore, since the extents of the integration are centered on the mean, the integral ends up being the mean of the normal distribution in the likelihood (a1):In other words, all this appears to be calculating the mean of a normal distribution with mean
a1and standard deviationsigma_a1, truncated ata1 - sigma_a1anda1 + sigma_a1. But truncating the tails symmetrically does not change the mean.