Print()

I want to check the values using print(), however, when adding this, it took a lot longer for the model to run. why is this? sometimes, the model took forever to initialize…
the only difference is the line of code 'print(lmix[c]) here…

model {

array[C] real lmix;
for (j in 1:J){
  for (c in 1: C){
               lmix[c] = log(alpha[c]) + bernoulli_lpmf(y[j, ] | p[c,]);
               print(lmix[c]); 
              }
  target += log_sum_exp(lmix);
  }
}

How big are C and J?

Hi,
C is 2, J is 1000. They are not too big. I am just playing around with code to learn how to code in stan.

In fact, even with the same code (without print()), when I tried to run the code again, it got stuck as in the following screen …

but when first run, it was relatively fast, as follows: ( I interrupted manually, but it was less than 10 minutes)

and the print() doesnot work at all as in nothing comes out…

These are likely related problems. Stan’s print statements don’t directly print to the console for various reasons. They add to a buffer internally which is then flushed when the iteration ends (see Print statements inside a model are buffered into a stringstream and only printed when log_prob returns · Issue #3233 · stan-dev/stan · GitHub)

If you queue up a lot of print statements, this will lead to a lot of memory usage, which can slow down the program. If your program is already slow due to difficult geometry, a coding error, or some other reason, this will likely only exacerbate the problem.

One solution could be to record a matrix 2xJ, and then print two rows of size J by doing another loop

oh. I see. thanks. I will looked into the post!

thank you!

Hi I have a follow up question, I am running the following model


model {
    M ~ normal(2,2);
    V ~ lognormal(0,2);

    int ob=1;
    //real P=0;
    //array[ncust] real P;
    for (i in 1:ncust){
        int count_i = count[i];
        array[nsimu] real Pi = rep_array(0,nsimu);
        for (t in 1:count_i){
            for (s in 1:nsimu){
                Pi[s] +=bernoulli_logit_lpmf(Initiated[ob]|Ms[ob,s]);
            }
            ob +=1;
        }
        target += log_sum_exp(Pi) - log(nsimu);
    }
}

however, it shows the following error term

Logit transformed probability parameter is nan, but must be not nan! (in ‘test0825.stan’, line 66, column 16 to column 69)

this is the line with the Pi[s]. I want to check what causes this Na by looking at the Ms[ob,s] (Ms is a transformed parameter). How can I acheive this without using print?

never mind. I think I figured it out!! thanks

You may check the constraints on your parameters in the parameters block and/or initialize them with some good values