Hi,
Thanks for simplifying your code. Your trick is clever, because it proves that the must
be a problem with either pbeta or qbeta. Your two lines:
p(0) = pbeta(x1[i], s1, s2);
q(0) = qbeta(p(0),s1,s2);
means that q(0)=x1[i]. Hence the "likelihood contribution"
ans -= dbeta(q(0),s1 , s2, true);
does not depend on the random effect u. This again means that the Laplace
approximation is exact (due the normal prior on u). This is of course
assuming infinite numerical precission.
What goes wrong in finite precission with the AD is not clear to me.
You are differenting a code for q(0) which, as a computer program depends on u,
but mathematically does not. The solution must lie in the underlying
numerical procedure procedure for pbeta(), which I have not looked into.
It is hard to say if how difficult it is to fix.
Sorry for not being able to come up with a more constructive answer.
Hans