attempt fitting matrix-normal distribution using Kronecker product and inverse Wishart

349 views
Skip to first unread message

Timothée Flutre

unread,
Jan 11, 2016, 7:49:46 AM1/11/16
to Stan users mailing list
Hello,

I am encountering an error when fitting a matrix-normal distribution with this simple example (see the attached R file):
  • Y ~ MN(M, U, V)
  • Y is NxT; M is NxT; U is NxN; V is TxT where N is the number of samples and T the number of traits
  • here, I assume M to be fixed and full of 0, and U to be fixed and equal to the identity
  • goal: estimate V (for the sake of simplicity, I'm using an inverse Wishart as prior, even though the STAN manual gives better alternatives)
  • trick: vec(Y) ~ MVN(vec(M), V kx U) where "kx" is the Kronecker product

Here is the model in STAN syntax (thanks to code from Tanner Sorensen via a message sent on 30/01/2015):


data {
  int<lower=0> N;
  int<lower=0> T;
  vector[N*T] vecY;
  vector[N*T] vecM;
  matrix[N,N] U;
  matrix[T,T] S;
}
parameters {
  matrix[T,T] V;
}
model {
  matrix[T*N,T*N] Sigma;
  V ~ inv_wishart(T+1, S);
  for(ti in 1:T)
    for(tj in 1:T)
      for(ni in 1:N)
        for(nj in 1:N)
          Sigma[N*(ti-1)+ni, N*(tj-1)+nj] <- V[ti,tj] * U[ni,nj];
  vecY ~ multi_normal(vecM, Sigma);
}

But when I use it, I get:


 [1] "Rejecting initial value:"                                                                                                                               
 [2] "  Error evaluating the log probability at the initial value."                                                                                           
 [3] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                                         
 [4] "Exception thrown at line 14: stan::math::inv_wishart_log: LDLT_Factor of random variable is not positive definite. last conditional variance is 3.66872."
 [5] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"                 
 [6] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                                               
 [7] "Rejecting initial value:"                                                                                                                               
 [8] "  Error evaluating the log probability at the initial value."                                                                                           
 [9] "Initialization between (-2, 2) failed after 100 attempts. "                                                                                             
[10] " Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model."                                                  


The problem seems to come from the inverse Wishart prior, but why?


ps: when I draw a random sample from MCMCpack::riwish(v=T+1, S=diag(T)) everything seems fine.


R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] MCMCpack_1.3-3 coda_0.18-1    rstan_2.8.0    ggplot2_1.0.1  Rcpp_0.12.2  
[6] MASS_7.3-44  

loaded via a namespace (and not attached):
 [1] lattice_0.20-33  codetools_0.2-14 digest_0.6.8     grid_3.2.2     
 [5] plyr_1.8.3       gtable_0.1.2     stats4_3.2.2     magrittr_1.5   
 [9] scales_0.3.0     stringi_1.0-1    reshape2_1.4.1   proto_0.3-10   
[13] tools_3.2.2      stringr_1.0.0    munsell_0.4.2    compiler_3.2.2 
[17] inline_0.3.14    colorspace_1.2-6 gridExtra_2.0.0


test_matrix-normal.txt

Daniel Lee

unread,
Jan 11, 2016, 10:10:30 AM1/11/16
to stan-...@googlegroups.com
Hi,

Try declaring V as a cov_matrix.

The Stan model block defines the calculation of the log joint probability distribution. It does not draw a V, set Sigma, then draw vecY; there is no rejection sampling happening. 

I would highly suggest using the lkj priors in the manual. 

Daniel
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<test_matrix-normal.txt>

Timothée Flutre

unread,
Jan 13, 2016, 7:30:21 AM1/13/16
to Stan users mailing list
Thanks! Indeed, using "cov_matrix" stops the error. And yes, without using the lkj prior, it's quite slow...
Reply all
Reply to author
Forward
0 new messages