Matlab Stan -- Error

235 views
Skip to first unread message

Jalaj Bhandari

unread,
Nov 22, 2015, 6:02:00 AM11/22/15
to stan-...@googlegroups.com
Hi,

I get the following error while running a Stan file in Matlab. Does anybody have any idea -- why does Stan exit badly?

Thanks

Warning: Stan seems to have exited badly. 
> In StanFit/process_exit_failure (line 280)
  In StanFit/process_exit (line 170)
  In %1541206591>@(src,evnt)process_exit(self,src,evnt)
  In processState/set.exitValue (line 30)
  In processManager.pollTimerStop (line 504)
  In timer/timercb (line 30)
  In timercb (line 13)
  In timer/start (line 39)
  In processManager/start (line 399)
  In StanModel/sampling (line 747)
  In stan (line 137)
  In stan_hmc (line 25)
  In Simulated_Gaussians_neff (line 232) 



Mike Boedigheimer

unread,
Nov 22, 2015, 10:04:32 AM11/22/15
to Stan users mailing list
Usually there is a problem with the code. It would be helpful to post the stan model. 

Jalaj Bhandari

unread,
Nov 22, 2015, 5:32:37 PM11/22/15
to stan-...@googlegroups.com
Hi 


I am working with a normal mixture model. So I have a 3-D array in Matlab which is an array of covariance matrices - say A( , , ). Here is the Stan code. I have to construct sigma during the code as I cannot do A(, ,i) in Stan (that's what it seems). I also attatched the error message in the end. Please also give me the general recommendation of how to pass multi-dimensional arrays.



data {
int number_mixtures;
int dimension;
int dimension_sq;
real mixture_weights[number_mixtures];
matrix[number_mixtures, dimension] mixture_means;
real mixture_covariances[dimension, dimension, number_mixtures];
}



parameters {
vector[dimension] x; 
}

model {
   real ps[number_mixtures];
   matrix[dimension, dimension] sigma;

   for (k in 1:number_mixtures) {
           for (i in 1:dimension){
             for (j in 1:dimension){
                 sigma[i,j] <- mixture_covariances[i,j,k];
             }
           }
       ps[k] <- log(mixture_weights[k])
                 + multi_normal_log(x, mixture_means[k] , sigma);
   }
   increment_log_prob(log_sum_exp(ps));
}


I get the following error:


output-1.csv: Exception: variable does not exist; processing stage=data initialization; variable name=mixture_covariances; base type=double
output-1.csv: Diagnostic information: 
output-1.csv: Dynamic exception type: std::runtime_error
output-1.csv: std::exception::what: variable does not exist; processing stage=data initialization; variable name=mixture_covariances; base type=double
Warning: Stan seems to have exited badly. 
> In StanFit/process_exit_failure (line 280)
  In StanFit/process_exit (line 170)
  In %1541206613>@(src,evnt)process_exit(self,src,evnt)
  In processState/set.exitValue (line 30)
  In processManager.pollTimerStop (line 504)
  In timer/timercb (line 30)
  In timercb (line 13)
  In timer/start (line 39)
  In processManager/start (line 399)
  In StanModel/sampling (line 747)
  In stan (line 137)
  In stan_hmc (line 36)
  In Simulated_Gaussians_neff (line 231)  
I get the following error:



output-1.csv: Exception: variable does not exist; processing stage=data initialization; variable name=mixture_covariances; base type=double
output-1.csv: Diagnostic information: 
output-1.csv: Dynamic exception type: std::runtime_error
output-1.csv: std::exception::what: variable does not exist; processing stage=data initialization; variable name=mixture_covariances; base type=double
Warning: Stan seems to have exited badly. 
> In StanFit/process_exit_failure (line 280)
  In StanFit/process_exit (line 170)
  In %1541206613>@(src,evnt)process_exit(self,src,evnt)
  In processState/set.exitValue (line 30)
  In processManager.pollTimerStop (line 504)
  In timer/timercb (line 30)
  In timercb (line 13)
  In timer/start (line 39)
  In processManager/start (line 399)
  In StanModel/sampling (line 747)
  In stan (line 137)
  In stan_hmc (line 36)
  In Simulated_Gaussians_neff (line 231)  

Jalaj Bhandari

unread,
Nov 22, 2015, 6:11:57 PM11/22/15
to stan-...@googlegroups.com
Can I even pass a 3d array in Matlab??

On Sun, Nov 22, 2015 at 5:32 PM, Jalaj Bhandari <jalajb...@gmail.com> wrote:
Hi 


I am working with a normal mixture model. So I have a 3-D array in Matlab which is an array of covariance matrices - say A( , , ). Here is the Stan code. I have to construct sigma during the code as I cannot do A(, ,i) in Stan (that's what it seems). I also attatched the error message in the end. Please also give me the general recommendation of how to pass multi-dimensional arrays.



data {
int number_mixtures;
int dimension;
int dimension_sq;
real mixture_weights[number_mixtures];
matrix[number_mixtures, dimension] mixture_means;
real mixture_covariances[dimension, dimension, number_mixtures];
}



parameters {
vector[dimension] x; 
}

model {
   real ps[number_mixtures];
   matrix[dimension, dimension] sigma;

   for (k in 1:number_mixtures) {
           for (i in 1:dimension){
             for (j in 1:dimension){
                 sigma[i,j] <- mixture_covariances[i,j,k];
             }
           }
       ps[k] <- log(mixture_weights[k])
                 + multi_normal_log(x, mixture_means[k] , sigma);
   }
   increment_log_prob(log_sum_exp(ps));
}

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/dm-R4jZ6wrA/unsubscribe.
To unsubscribe from this group and all its topics, 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.

Mike Boedigheimer

unread,
Nov 25, 2015, 11:08:27 AM11/25/15
to Stan users mailing list
look for a file called temp.data.R. It should be in your current working directory. The error message implies mixture_covariances does not exist. Can you confirm that?
I cannot reproduce this error because I do not have your data. 

Don Mathis

unread,
Feb 28, 2016, 3:53:35 PM2/28/16
to stan-...@googlegroups.com
It looks to me like MatlabStan cannot handle more than 2-dimensional data. The function mstan.rdump creates the file temp.data.R, and you can read the code and see that it only handles scalars, vectors, and matrices. Just type the following and see for yourself:

edit mstan.rdump

Don Mathis

unread,
Feb 28, 2016, 4:33:21 PM2/28/16
to Stan users mailing list
I believe I have a solution for you.
MatlabStan isn't written to handle multidimensional arrays, but I was able to modify the source file mstan.rdump to handle them. In MATLAB, just type

      edit mstan.rdump

in order to edit the file, then replace its entire contents with the code included below. I tried this out with a 3-D array and it works fine.

-------- START OF CODE: -------

% https://github.com/stan-dev/rstan/search?q=stan_rdump&ref=cmdform

% struct or containers.Map

function fid = rdump(fname,content)

if isstruct(content)

vars = fieldnames(content);

data = struct2cell(content);

elseif isa(content,'containers.Map')

vars = content.keys;

data = content.values;

end

fid = fopen(fname,'wt');

for i = 1:numel(vars)

if isscalar(data{i})

if any(data{i}(:) > intmax('int32'))

fprintf(fid,'%s <- %f\n',vars{i},data{i});

else

fprintf(fid,'%s <- %d\n',vars{i},data{i});

end

elseif isvector(data{i})

fprintf(fid,'%s <- c(',vars{i});

if any(data{i}(:) > intmax('int32'))

fprintf(fid,'%f, ',data{i}(1:end-1));

fprintf(fid,'%f)\n',data{i}(end));

else

fprintf(fid,'%d, ',data{i}(1:end-1));

fprintf(fid,'%d)\n',data{i}(end));

end

elseif ismatrix(data{i})

fprintf(fid,'%s <- structure(c(',vars{i});

if any(data{i}(:) > intmax('int32'))

fprintf(fid,'%f, ',data{i}(1:end-1));

fprintf(fid,'%f), .Dim = c(',data{i}(end));

else

fprintf(fid,'%d, ',data{i}(1:end-1));

fprintf(fid,'%d), .Dim = c(',data{i}(end));

end

fprintf(fid,'%g,',size(data{i},1));

fprintf(fid,'%g',size(data{i},2));

fprintf(fid,'))\n');

else

% multidimensional array

fprintf(fid,'%s <- structure(c(',vars{i});

if any(data{i}(:) > intmax('int32'))

fprintf(fid,'%f, ',data{i}(1:end-1));

fprintf(fid,'%f), .Dim = c(',data{i}(end));

else

fprintf(fid,'%d, ',data{i}(1:end-1));

fprintf(fid,'%d), .Dim = c(',data{i}(end));

end

for d = 1:numel(size(data{i}))-1

fprintf(fid,'%g,',size(data{i},d));

end

fprintf(fid,'%g',size(data{i},numel(size(data{i}))));

fprintf(fid,'))\n');

end

end

fclose(fid);

end


----- END OF CODE -----


On Sunday, November 22, 2015 at 6:02:00 AM UTC-5, Jalaj Bhandari wrote:
Reply all
Reply to author
Forward
0 new messages