Trouble Initializing

272 views
Skip to first unread message

Daniel Berman

unread,
Jul 5, 2015, 8:24:05 PM7/5/15
to stan-...@googlegroups.com
Hi,

I'm having difficulty initializing my model and I'm very confused about the error because it know that my initial value fits the variable type in stan.

Here is the error message:
Error : Initialization from source failed.
variable chi_chol_Imag missing

error occurred during calling the sampler; sampling not done


The parameter section in my Stan code is:

parameters{
 #Array of matrices. The length of the array is the number of gates that are being estimated.
  cholesky_factor_cov[chiDimensions] chi_chol_Imag[numberOfGates];
  cholesky_factor_cov[chiDimensions] chi_chol_Real[numberOfGates];
}


My R code is here:

 inits = function(){
   return(
     list(chi_chol_Imag=chi_chol_Imag,
               chi_chol_Real=chi_chol_Real)
          )
 }

Bob Carpenter

unread,
Jul 6, 2015, 11:54:03 AM7/6/15
to stan-...@googlegroups.com
That's not enough context to debug. It helps us immensely
to be able to see your whole program and exactly what you
typed to R and exactly what R gets back.

When you call inits(), do you get a value? These
functions that depend on global variables can go wrong
if the globals aren't defined. It's much safer and
easier for others to read if you make your functions
self contained.

And how did you call Stan? If you provide the wrong argument,
you'll get the same problem.

I think this is largely our fault for not making our
error messages more informative --- did it find a list and
just not find that value? Did it find a list with that key that
had an NA?

- Bob
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

Ben Goodrich

unread,
Jul 6, 2015, 12:08:44 PM7/6/15
to stan-...@googlegroups.com, daniel....@gmail.com
At a minimum, your inits function should return list of arrays whose dimensions are numberOfGates x chiDimensions x chiDimensions.

Ben

Daniel Berman

unread,
Jul 6, 2015, 1:57:57 PM7/6/15
to stan-...@googlegroups.com, daniel....@gmail.com
Hi Bob,

Sorry for the incomplete code. I was hoping that it would be enough. I'll post a more complete version:

translatedModel<-stanc(file="GST_bayesian_unconstrained4.stan",model_name='CPTbehavioral')
compiledModel
<-stan_model(stanc_ret=translatedModel,verbose=FALSE)
chiDimensions
<-4
chiDimensionsDoubled
<-chiDimensions*2
numberOfGates
<-4

chi1
<-matrix(c(1.4000, -0.0136,  0.0136, 0.0005,
             
-0.1360,  0.2005, -0.0005, 0.0000,
               
0.0136, -0.0005,  0.2005, 0.0000,
               
0.0005,  0.0000,  0.0000, 0.1990,
               
0.0000,  0.0007,  0.0007, 0.0000,
             
-0.0007,  0.0000, -0.0197, 0.0001,
             
-0.0007,  0.0197,  0.0000, 0.0001,
               
0.0000, -0.0001, -0.0001, 0.0000), chiDimensionsDoubled,chiDimensions,byrow=TRUE)

chi2
<-matrix(c( 0.7985,   -0.0129,   0.0033,    0.0067,
               
-0.0129,    0.8048,  -0.0067,    0.0095,
               
0.0033,   -0.0067,   0.2115,   -0.1449,
               
0.0067,    0.0095,  -0.1449,    0.1852,
               
0.0000,   -0.6154,  -0.0077,    0.0505,
               
0.6154,    0.0000,  -0.0715,    0.0085,
               
0.0077,    0.0715,   0.0000,   -0.0011,
               
-0.0505,   -0.0085,   0.0011,    0.0000), chiDimensionsDoubled,chiDimensions,byrow=TRUE)

chi3
<-matrix(c( 0.7985, -0.0033,  0.0129,  0.0067,
               
-0.0033,  0.2115, -0.0067,  0.1449,
               
0.0129, -0.0067,  0.8048, -0.0095,
               
0.0067,  0.1449, -0.0095,  0.1852,
               
0.0000, -0.0078, -0.6154, -0.0505,
               
0.0078,  0.0000, -0.0715, -0.0011,
               
0.6154,  0.0715,  0.0000,  0.0085,
               
0.0505,  0.0011, -0.0085,  0.0000), chiDimensionsDoubled,chiDimensions,byrow=TRUE)

chi4
<-matrix(c( 0.1992, -0.0134,  0.0548,  0.0005,
               
-0.0134,  1.4028, -0.0005, -0.0412,
               
0.0548, -0.0005,  0.1997, -0.0002,
               
0.0005, -0.0412, -0.0002,  0.1983,
               
0.0000,  0.0047, -0.0018,  0.0594,
               
-0.0047,  0.0000, -0.0790,  0.0026,
               
0.0018,  0.0790,  0.0000, -0.0039,
               
-0.0594, -0.0026,  0.0039,  0.0000), chiDimensionsDoubled,chiDimensions,byrow=TRUE)

chi_Init
<-list(chi1,chi2,chi3,chi4)
chi_Init_Real
<-list(chi1[1:4,1:4],chi2[1:4,1:4],chi3[1:4,1:4],chi4[1:4,1:4])
chi_Init_Imag
<-list(chi1[5:8,1:4],chi2[5:8,1:4],chi3[5:8,1:4],chi4[5:8,1:4])

chi_chol_Real
<-list()
chi_chol_Imag
<-list()
for (i in 1:4){
  chi_chol_Real
[[i]]<-t(chol(chi_Init_Real[[i]]))
  chi_chol_Imag
[[i]]<-chi_chol_Real[[i]]
}
expData
= list('chiDimensions'=chiDimensions,
               
'chiDimensionsDoubled'=chiDimensionsDoubled,
               
'numberOfGates'=numberOfGates)
inits
= function(){
 
return(
    list
(
         chi_chol_Imag
=chi_chol_Imag,
         chi_chol_Real
=chi_chol_Real
         
)
 
)
}

hierarchical
= sampling(compiledModel,data=expData, init=inits, iter=250, chains=1)#,

When I call inits() I get:

$chi_chol_Imag
$chi_chol_Imag
[[1]]
         
[,1]         [,2]          [,3]          [,4]
[1,]  1.1832159566  0.000000e+00  0.000000e+00  0.000000
[2,] -0.0114940979  4.476247e-01  0.000000e+00  0.000000
[3,]  0.0114940979 -8.218619e-04  4.476240e-01  0.000000
[4,]  0.0004225771  1.085093e-05 -1.083102e-05  0.446094
$chi_chol_Imag
[[2]]
         [,1]         [,2]          [,3]      [,4]
[1,] 0.893588272 0.000000000 0.0000000 0.0000000
[
2,] -0.014436179 0.896990299 0.0000000 0.0000000
[
3,] 0.003692976 -0.007409988 0.4598168 0.0000000
[
4,] 0.007497860 0.010711644 -0.3150131 0.2929092
$chi_chol_Imag
[[3]]
         [,1]         [,2]          [,3]      [,4]
[1,] 0.893588272 0.0000000 0.000000000 0.0000000
[
2,] -0.003692976 0.4598765 0.000000000 0.0000000
[
3,] 0.014436179 -0.0144532 0.896873849 0.0000000
[
4,] 0.007497860 0.3151448 -0.005634447 0.2929092
$chi_chol_Imag
[[4]]
         [,1]         [,2]          [,3]      [,4]
[1,] 0.446318272 0.000000000 0.0000000000 0.0000000
[
2,] -0.030023418 1.184017987 0.0000000000 0.0000000
[
3,] 0.122782336 0.002691129 0.4296711019 0.0000000
[
4,] 0.001120277 -0.034768362 -0.0005678391 0.4439477
$chi_chol_Real
$chi_chol_Real
[[1]]
         [,1]         [,2]          [,3]      [,4]
[1,] 1.1832159566 0.000000e+00 0.000000e+00 0.000000
[
2,] -0.0114940979 4.476247e-01 0.000000e+00 0.000000
[
3,] 0.0114940979 -8.218619e-04 4.476240e-01 0.000000
[
4,] 0.0004225771 1.085093e-05 -1.083102e-05 0.446094
$chi_chol_Real
[[2]]
         [,1]         [,2]          [,3]      [,4]
[1,] 0.893588272 0.000000000 0.0000000 0.0000000
[
2,] -0.014436179 0.896990299 0.0000000 0.0000000
[
3,] 0.003692976 -0.007409988 0.4598168 0.0000000
[
4,] 0.007497860 0.010711644 -0.3150131 0.2929092
$chi_chol_Real
[[3]]
         [,1]         [,2]          [,3]      [,4]
[1,] 0.893588272 0.0000000 0.000000000 0.0000000
[
2,] -0.003692976 0.4598765 0.000000000 0.0000000
[
3,] 0.014436179 -0.0144532 0.896873849 0.0000000
[
4,] 0.007497860 0.3151448 -0.005634447 0.2929092
$chi_chol_Real
[[4]]
         [,1]         [,2]          [,3]      [,4]
[1,] 0.446318272 0.000000000 0.0000000000 0.0000000
[
2,] -0.030023418 1.184017987 0.0000000000 0.0000000
[
3,] 0.122782336 0.002691129 0.4296711019 0.0000000
[
4,] 0.001120277 -0.034768362 -0.0005678391 0.4439477

Bob Carpenter

unread,
Jul 6, 2015, 2:37:36 PM7/6/15
to stan-...@googlegroups.com
That's still missing the result! What happens when you run this:

> hierarchical= sampling(compiledModel,data=expData, init=inits, iter=250, chains=1)#,

Is this the only output?

> Here is the error message:
> Error : Initialization from source failed.
> variable chi_chol_Imag missing

Because I don't see how you could get that given the variable's
there. Maybe someone who knows R better than me can help.

We get a lot of messages and don't have a lot of time to recreate
everyone's environment, so let me emphasize again that we want to see

a) full model code

b) full R (or other interface) inputs, ideally for small data
simulation

c) R (or other interface) outputs

And if it's an installation issue (which this presumably isn't), we
also need all your relevant hardware and software config.

- Bob

Daniel Berman

unread,
Jul 6, 2015, 3:12:07 PM7/6/15
to stan-...@googlegroups.com
It doesn't seem like an instillation or hardware issue because I'm able to use initialization with other models, I'm just not sure why this one in particular isn't working.

The output is the same as the one I posted previously, which is:

Bob Carpenter

unread,
Jul 6, 2015, 3:24:22 PM7/6/15
to stan-...@googlegroups.com
I don't see how you could've gotten that message from what
you sent us and I don't know how to debug further without
a reproducible example. If you want us to reproduce the error
you're getting on our end, you need to

***send us everything we need to reproduce it***

That means your model, all the R code to run it with data, etc.
A complete script that will get us to the error message you're
finding.

Even better, trim your example down to the minimum example of
the problem (fake tiny data, etc.) that you can recreate from
***a fresh R session*** and send us that.

- Bob

Ben Goodrich

unread,
Jul 6, 2015, 3:45:11 PM7/6/15
to stan-...@googlegroups.com, daniel....@gmail.com
On Monday, July 6, 2015 at 1:57:57 PM UTC-4, Daniel Berman wrote:
Sorry for the incomplete code. I was hoping that it would be enough. I'll post a more complete version:

This Stan programs works with your R code, so the problem is most likely in your GS_bayesian_unconstrained4.stan file

data {
 
int<lower=1> chiDimensions;
 
int<lower=1> numberOfGates;

}
parameters
{
 
#Array of matrices. The length of the array is the number of gates that are being estimated.
  cholesky_factor_cov
[chiDimensions] chi_chol_Imag[numberOfGates];
  cholesky_factor_cov
[chiDimensions] chi_chol_Real[numberOfGates];
}

model
{
 
}

Ben

Daniel Berman

unread,
Jul 6, 2015, 6:25:57 PM7/6/15
to stan-...@googlegroups.com, daniel....@gmail.com
Here are the complete flies. All you have to do is change the working directory.
GST_Bayesian_unconstrained4.stan
GST_bayesian_estimation.R

Ben Goodrich

unread,
Jul 6, 2015, 11:58:32 PM7/6/15
to stan-...@googlegroups.com, daniel....@gmail.com
On Monday, July 6, 2015 at 6:25:57 PM UTC-4, Daniel Berman wrote:
Here are the complete flies. All you have to do is change the working directory.

The .R file is not complete.
  • Gmeas is not defined
  • alpha needs to be defined farther up in the file before it is used
  • The loop with chi_Init_Real[[i]] needs to be called after chi_Init_Real is created
  • These do not make sense because they input 16 values and rhoDimensions is 2
rho_init<-matrix(c(1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0),nrow=rhoDimensions,ncol=rhoDimensions)
E_init
<-matrix(c(1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0),nrow=rhoDimensions,ncol=rhoDimensions)

I then get an error from Stan saying
pi1.1 initialized to invalid value (inf)
which seems to be due to the previous confusion about the dimensions of rho_init.

Ben


Daniel Berman

unread,
Jul 7, 2015, 1:07:23 AM7/7/15
to stan-...@googlegroups.com, daniel....@gmail.com
Sorry about that. I forgot to clear my environment before I tested it and sent it to you. This version should produce the error I'm experiencing.
GST_bayesian_estimation.R
GST_Bayesian_unconstrained4.stan

Ben Goodrich

unread,
Jul 7, 2015, 1:44:50 AM7/7/15
to stan-...@googlegroups.com, daniel....@gmail.com
On Tuesday, July 7, 2015 at 1:07:23 AM UTC-4, Daniel Berman wrote:
Sorry about that. I forgot to clear my environment before I tested it and sent it to you. This version should produce the error I'm experiencing.

I still get
pi1.1 initialized to invalid value (inf)


Initialization between (-2, 2) failed after 100 attempts.
 
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.

which appears to be due to the fact that pi1 is declared as a simplex
  simplex[rhoDimensions] pi1;
whereas it is initialized to
> pi1_init
[1] 1 0
which is an edge-case that apparently isn't supported because it can't roundtrip it to the unconstrained space and back. If you are going to initialize the parameters, you should initialize them to interior values. But in most cases, you only need to initialize the parameters yourself if you observe problems due to Stan's initial values, and even in that case the first thing to do would be to set init_r to a value less than 2.

Ben



Daniel Berman

unread,
Jul 7, 2015, 9:36:45 AM7/7/15
to stan-...@googlegroups.com, daniel....@gmail.com
That's very strange. I've tried it on two different machines, in new instances of R studio with cleared environments and consoles. and I do not get that error. I even downloaded the code I posted and ran that.

Here's the error message I got:


SAMPLING FOR MODEL 'CPTbehavioral' NOW (CHAIN 1). Error : Initialization from source failed. variable chi_chol_Real missing
error occurred during calling the sampler; sampling not done

I'm using rstan version 2.5.0. Would that cause a problem?

Patrick Miller

unread,
Jul 7, 2015, 10:40:38 AM7/7/15
to stan-...@googlegroups.com
Daniel,

I have an older version of rstan as well and was getting your error. Changing the initialization for chi_chol_Real and chi_chol_Imag got me past it...try making them arrays as opposed to lists of matrices:

#initial values
chi_chol_Real<-array(0, dim = c(numberOfGates,chiDimensions,chiDimensions))
chi_chol_Imag<-array(0, dim = c(numberOfGates,chiDimensions,chiDimensions))
for (i in 1:numberOfGates){
  chi_chol_Real[i,,]<-t(chol(chi_Init_Real[[i]]))
  chi_chol_Imag[i,,]<-chi_chol_Real[i,,]
}

After that, you will still have the issue with pi1 and pi2 that Ben is getting.

Daniel Berman

unread,
Jul 7, 2015, 11:20:08 AM7/7/15
to stan-...@googlegroups.com
So is it the length of the initial value I'm giving for pi1 or is it that I'm giving it a case where the values are 1 and 0. Would perturbing it slightly help? If it's the length, should I then just give it one number?

Bob Carpenter

unread,
Jul 7, 2015, 1:10:27 PM7/7/15
to stan-...@googlegroups.com
I think the issue may be that R conflates arrays of size 1 and
whatever they contain. By explicitly using the array constructor,
you force it to have the right type.

- Bob

Krzysztof Sakrejda

unread,
Jul 7, 2015, 1:26:29 PM7/7/15
to stan-...@googlegroups.com


On Tuesday, July 7, 2015 at 11:20:08 AM UTC-4, Daniel Berman wrote:
So is it the length of the initial value I'm giving for pi1 or is it that I'm giving it a case where the values are 1 and 0. Would perturbing it slightly help? If it's the length, should I then just give it one number?

R does gratuitously drop length 1 dimensions but that's not the problem here. This code:

chi_Init<-list(chi1,chi2,chi3,chi4)
chi_Init_Real
<-list(chi1[1:4,1:4],chi2[1:4,1:4],chi3[1:4,1:4],chi4[1:4,1:4])
chi_Init_Imag
<-list(chi1[5:8,1:4],chi2[5:8,1:4],chi3[5:8,1:4],chi4[5:8,1:4])

explicitely makes a list of matrices.  You need to make an array as in Patrick's post above.

Krzysztof

Daniel Berman

unread,
Jul 8, 2015, 12:28:50 AM7/8/15
to stan-...@googlegroups.com
Thanks for the help everyone. I have the code running now. It doesn't produce the correct answer but I've had it set on a low number of iterations just for testing purposes. Given the number of parameters I'm estimating, more iterations will definitely help.  Thanks again for the help.
Reply all
Reply to author
Forward
0 new messages