-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TMB implementations of LKJ, Wishart, and inverse Wishart distributions #351
Comments
Nice work @jaganmn - thanks for sharing! I'll have it mind for future addition to TMB. |
I wonder if this should happen soon-ish, so that glmmTMB can get the density functions directly from TMB instead of copying the code into its own sources. Probably it is not the only TMB-using package that would want |
@jaganmn I was about to copy your code, but then got confused over a couple of details... Given that your version of integrate(Vectorize(function(x)dwishart(x,df=2,scale=1)),-100,100)
## 6.838354 with absolute error < 7.9e-05 |
The I think that your integral approximation exceeds unity due to numerical instability for very small nonzero standard deviations. For example, in the one-dimensional case with set.seed(0)
x1 <- rnorm(10L)
dwishart1 <- Vectorize(function(x) dwishart(x, df = 2, scale = 0))
dchisq1 <- function(x) dchisq(exp(2 * x), df = 2)
max(abs(dwishart1(x1) - dchisq1(x1)))
## [1] 8.673617e-19
integrate(dwishart1, -100, 100)
## 50.02898 with absolute error < 0.0037
integrate(dchisq1, -100, 100)
## 50.02898 with absolute error < 0.0037
x2 <- rlnorm(10L)
dwishart2 <- Vectorize(function(x) dwishart(0.5 * log(x), df = 2, scale = 0))
dchisq2 <- function(x) dchisq(x, df = 2)
max(abs(dwishart2(x2) - dchisq2(x2)))
## [1] 5.551115e-17
integrate(dwishart2, exp(-10), exp(10))
## 0.9999773 with absolute error < 6.1e-07
integrate(dchisq2, exp(-10), exp(10))
## 0.9999773 with absolute error < 6.1e-07 One can think about ways to improve the speed/stability, e.g., constant additive terms in the log densities can typically be dropped (as in |
The point of my example was only to demonstrate that the density doesn't integrate to one. |
Er, right, yes, I managed to confuse myself, not having read carefully enough (your comment and my own documentation) ... As you say, dwishart1.j <- Vectorize(function(x) dwishart(x, df = 2, scale = 0) * 2 * exp(2 * x))
integrate(dwishart1.j, -100, 100)
## 1 with absolute error < 3.6e-07 AFAICT the current behaviour is "correct" as long as it is clearly documented what is being computed, namely a probability density in the space of the inverse transformed parameter. |
Conventionally, all densities in TMB integrate to one, and I'm quite determined not to break that pattern. Beyond Bayesian priors the two typical use cases are:
I claim that your version is not directly applicable in either of these cases. So, to follow the TMB convention there should probably be two versions: // Constrained parameter space
// (MJ version with added parameter transform)
dwishart(...);
// Unconstrained parameter space.
// (MJ version with added jacobian adjustment)
dwishart_unconstrained(...); |
OK. I'll work on those alternate implementations "soon" and update here. |
FWIW I continue to be interested in this ... |
Not forgotten ... maybe I'll work on this during the glmmTMB hack. |
Having
dlkj
,dwishart
, anddinvwishart
in one of the TMB namespaces would enable users to assign likelihoods to correlation or covariance matrices. I have implemented these here. Documentation and tests against independent R implementations are there, too.Following
UNSTRUCTURED_CORR_t
, users would pass a vectortheta
orc(log_sd, theta)
instead of the full correlation or covariance matrix. This simplifies computation quite a bit.Anyway, you are welcome to use or modify the code. At your discretion, I could also work on a pull request.
The text was updated successfully, but these errors were encountered: