Can not pickle a compilable stan file


#1

I have a file which can not be pickled. I reduced it to just a few lines, and still have problems.

functions {
	
	real iLogLikelihood_lpmf(int[,] visit_C_array, int T, real[,] xi, vector[,] beta, row_vector[] x_array, real[] alpha_array, vector[] gamma_array, row_vector[] z_array, real[] eta_array){	// x is ad stock
		real L_i;
		
		row_vector[3] one_vector = [1, 1, 1];
		vector[3] pi_t = [1.0, 0.0, 0.0]';			// all users starts from state 1
		
		return L_i;
	}

}

Its giving
sm = StanModel(file=filename+’.stan’, model_name=model_name)
File “/Users/bruceho/anaconda/envs/adtech/lib/python2.7/site-packages/pystan/model.py”, line 211, in init
obfuscate_model_name=obfuscate_model_name)
File “/Users/bruceho/anaconda/envs/adtech/lib/python2.7/site-packages/pystan/api.py”, line 129, in stanc
raise ValueError(error_msg)
ValueError: Failed to parse Stan model ‘AdAttribEP_sg_350f8cce03e9fb95bcdc7bb7a71138f7’. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:

ERROR at line 8

6: real L_i;
7:
8: row_vector[3] one_vector = [1, 1, 1];
^
9: vector[3] pi_t = [1.0, 0.0, 0.0]’; // all users starts from state 1

PARSER EXPECTED:

line 8 looks perfectly ok to me.


#2

Hi,

I could not replicate the error. Try to remove the extra whitespace from the file and run it through pystan.stanc.

I’m not sure if pickle has anything to do with this problem.


#3

I already compiled successfully only happens during pickle. Used code from util.py to pickle


#4

In the first message you give a stan function and then describe that it can not be compiled?

sm = StanModel(file=filename+’.stan’,  ...
....

I had no problem compiling this model, and apparently you managed to compile it too.
How are you pickling the model?

Like this?

Dumping

import pickle
with open("...", "wb") as f:
    pickle.dump(sm, f, protocol=-1)

And then loading

with open("...", "rb") as f:
    sm = pickle.load(f)

The util.py file not part of the PyStan. So I take a guess it is the util.py file in ep-stan?


#5

I don’t understand this. I used command line to compile the stan file (using the make command) and it compiles just fine. But using the same code you have

sm = StanModel(file=filename+'.stan', model_name=model_name)

it fails with the that error message point to the perfectly good line

row_vector[3] one_vector = [1, 1, 1];


#6

This appears to be a bug in the lib. I tested this out thoroughly, and it is not working as expected.

row_vector[3] one_vector = [1, 1, 1];

generates an error PARSER EXPECTED:

finally, I changed the code to

	row_vector[3] one_vector ; 
	one_vector[1] = 1.0;
	one_vector[2] = 1.0;
	one_vector[3] = 1.0;

That got me thru. Someone should be able to reproduce this. This is all in the function {} block.