SourceForge has been redesigned. Learn more.
Close

outer product in JAGS

Help
R B
2011-01-16
2012-09-01
  • R B

    R B - 2011-01-16

    Hi,

    I'm using rJAGS for a hierarchical nested logit model and I need to speed up
    the computation. I'm trying to remove for loops and use instead matrix
    operations. I have a problem though, when it comes to multiply a column vector
    x and a row vector y' to obtain a matrix xy'. JAGS does not compile. I've
    tried the methods mentioned in "JAGS: A Program for Analysis of Bayesian
    Graphical Models Using Gibbs Sampling" by Martyn Plummer but these don't work
    either. Any suggestion will be greatly appreciated.

     
  • Martyn Plummer

    Martyn Plummer - 2011-01-17

    You can't currently do outer product in JAGS without a loop.

    Suppose you want to take the outer product of two vectors of length 3,
    creating a 3x3 matrix. You can do this with two loops:

    model {
       for (j in 1:3) {
          for (k in 1:3) {
             z[j,k] <- x[j] * y[k]
          }
       }
    }
    

    You can get partial vectorization by suppressing the inner loop:

    model {
       for (j in 1:3) {
          z[j,] <- x[j] * y
       }
    }
    

    But there is nothing you can do to suppress the outer loop because

    z[j,] <- x[j] * y
    

    is already a vectorized operation: you can only vectorize functions taking
    scalar arguments and returning a scalar value.

     
  • R B

    R B - 2011-01-18

    Thank you!

     

Log in to post a comment.