Quick / Nimble

A Matcher Framework for Swift and Objective-C
https://quick.github.io/Nimble/documentation/nimble/
Apache License 2.0
4.8k stars 601 forks source link

3D arrays for square matrices #960

Closed ethan-alt closed 2 years ago

ethan-alt commented 2 years ago

What did you do?

I wanted to conduct a mixture of multivariate normals problem. The code I have is as follows:

library(nimble)
## NIMBLE CODE--PRODUCES AN ERROR
code.multi <- nimbleCode({
  gamma ~ dbeta(gamma_shape1, gamma_shape2)
  for ( i in 1:2 ) {
    mu[1:J,i] ~ dmnorm(mu_mean[1:J], cov = mu_cov[1:J,1:J])
    Sigma[1:J,1:J,i] ~ dinvwish(S = S[1:J, 1:J], df = df)
  }
  for ( i in 1:n ) {
    c[i] ~ dbinom(size = 1, prob = gamma)
    d[i] <- c[i] + 1
    y[1:J,i] ~ dmnorm(mu[1:J, d[i]], cov = Sigma[1:J,1:J,d[i]] )
  }
})

## TEST THE CODE
n = 100
constants <- list(
    'n' = n
  , 'J' = 2
  , 'gamma_shape1' = 2
  , 'gamma_shape2' = 2
  , 'mu_mean'      = rep(0, 2)
  , 'mu_cov'       = diag(100, 2)
  , 'S'            = (2 + 10) * diag(2)
  , 'df'           = 2 + 10
)
inits <- list(
    'mu'    = cbind(c(-1, -1), c(1, 1))
  , 'c'     = rbinom(n = constants$n, size = 1, prob = 0.5)
  , 'gamma' = 0.5
  , 'Sigma' = abind( diag(2, 2), diag(1, 2), along = 3 )
)
data <- list('y' = t(mvtnorm::rmvnorm(constants$n, sigma = diag(2))) )
sample <- nimbleMCMC(
  code = code.multi, constants = constants, data = data, inits = inits, thin = 1, niter = 2000, nburnin = 1000
)

What did you expect to happen?

I expected this to be able to run with no issues since it's conjugate.

What actually happened instead?

The sampler threw an error:

Defining model
Building model
Setting data and initial values
Running calculate on model
  [Note] Any error reports that follow may simply reflect missing values in model variables.
Checking model sizes and dimensions
Checking model calculations
Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.
Error: Error, wrong number of indices provided for dep_dmnorm_identity_coeff[iDep].
 This occurred for: dep_dmnorm_identity_coeff[iDep]
 This was part of the call:  dep_dmnorm_identity_coeff[iDep] <<- Interm_4091[1, 1]

Environment

Project that demonstrates the issue

See code above. I found that if non-Gibbs samplers are used for the covariance matrices, then the sampler works. If there is a workaround, please let me know!

ikesyo commented 2 years ago

This is a matcher framework for Swift and Objective-C. Looks like you should visit https://github.com/nimble-dev/nimble.