Bernoulli not found

Hi all, I am new to Stan, and I met a very basic question that I googled a lot but can’t solve. I met an error message “Ill-typed arguments to ‘~’ statement. No distribution ‘bernoulli’ was found with the correct signature”. Attached is my code. I want to create a slab-and-spike prior, where I draw a Bernoulli Y, and draw a normal W, then times them up. Can anyone tell me why I got the error? I use cmdstanr V0.4.0.

parameters {
  matrix[n_genes, n_TFs] W;               // normal
  matrix[n_genes, n_TFs] Y;               // bernoulli
  real<lower=0> alpha;                       // gamma
  }

for(d in 1:n_genes){
for(k in 1:n_TFs){
  Y[d,k] ~ bernoulli(P[d,k]);
  W[d,k] ~ normal(0,1/sqrt(alpha));
  W[d,k] = W[d,k]*Y[d,k];
}
}

A traditional spike-and-slab prior is not possible in Stan because it does not support discrete parameters. This means that the bernoulli distribution cannot be used for parameters, only observed data.

@betanalpha has an excellent case-study which discusses different approaches to sparsity inducing priors with Stan, which might be of help here: Sparsity Blues

4 Likes

Hello @andrjohns, many thanks for your reply. The referred link saves me!

I have a follow-up question. Because I want to create a very sparse structure in matrix W above, if I set some elements in this matrix always be zero, do you think it can improve convergence speed? If it works, I wonder what syntax I should use to set some constant zeros to a parameter matrix?

It will be pretty inefficient to specify the full matrix as a parameter, and zero the elements that you don’t need. Unfortunately, Stan will treat all of the zero values as parameters to be sampled/

There’s a section of the User’s Guide which talks about different ways of handling sparse structures with Stan, which might be a good starting point: 8.1 Sparse data structures | Stan User’s Guide