Value error while running control model in pystan

Following code I used for control model

# mean-centralize: sales, numeric base_vars
df_ctrl, sc_ctrl = mean_center_trandform(df, ['sales']+me_cols+st_cols+mrkdn_cols)
df_ctrl = pd.concat([df_ctrl, df[hldy_cols+seas_cols]], axis=1)

# variables positively related to sales: macro economy, store count, markdown, holiday
pos_vars = [col for col in base_vars if col not in seas_cols]
X1 = df_ctrl[pos_vars].values

# variables may have either positive or negtive impact on sales: seasonality
pn_vars = seas_cols
X2 = df_ctrl[pn_vars].values

ctrl_data = {
    'N': len(df_ctrl),
    'K1': len(pos_vars), 
    'K2': len(pn_vars), 
    'X1': X1,
    'X2': X2, 
    'y': df_ctrl['sales'].values,
    'max_intercept': min(df_ctrl['sales'])

ctrl_code1 = '''
data {
  int N; // number of observations
  int K1; // number of positive predictors
  int K2; // number of positive/negative predictors
  real max_intercept; // restrict the intercept to be less than the minimum y
  matrix[N, K1] X1;
  matrix[N, K2] X2;
  vector[N] y; 

parameters {
  vector<lower=0>[K1] beta1; // regression coefficients for X1 (positive)
  vector[K2] beta2; // regression coefficients for X2
  real<lower=0, upper=max_intercept> alpha; // intercept
  real<lower=0> noise_var; // residual variance

model {
  // Define the priors
  beta1 ~ normal(0, 1); 
  beta2 ~ normal(0, 1); 
  noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
  // The likelihood
  y ~ normal(X1*beta1 + X2*beta2 + alpha, sqrt(noise_var));

sm1 = pystan.StanModel(model_code=ctrl_code1, verbose=True)
fit1 = sm1.sampling(data=ctrl_data, iter=2000, chains=4)
fit1_result = fit1.extract()

The error I am getting is

ValueError                                Traceback (most recent call last)
C:\Users\703310~1\AppData\Local\Temp/ipykernel_8672/ in <module>
     49 '''
---> 51 sm1 = pystan.StanModel(model_code=ctrl_code1, verbose=True)
     52 fit1 = sm1.sampling(data=ctrl_data, iter=2000, chains=4)
     53 fit1_result = fit1.extract()

~\anaconda\lib\site-packages\pystan\ in __init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args, allow_undefined, include_dirs, includes)
    382                 os.dup2(orig_stderr, sys.stderr.fileno())
--> 384         self.module = load_module(self.module_name, lib_dir)
    385         self.module_filename = os.path.basename(self.module.__file__)
    386         # once the module is in memory, we no longer need the file on disk

~\anaconda\lib\site-packages\pystan\ in load_module(module_name, module_path)
     48         pyximport.install()
     49         sys.path.append(module_path)
---> 50         return __import__(module_name)
     51     else:
     52         import imp

C:\Users\703310~1\AppData\Local\Temp\pystan_wq8ht9o8\stanfit4anon_model_548939bc33801f8115bc26206558c913_4645238327189794297.pyx in init stanfit4anon_model_548939bc33801f8115bc26206558c913_4645238327189794297()

ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject

I got this error for the example dataset which is provided with the pystan code.
any suggestions will be appreciated.


I’m not sure what’s going wrong here in PyStan, and it would probably help others if you say where the error is happening. Is the extract() function the one raising errors?

As an alternative, I’d suggest trying cmdstanpy as it has a much lighter interface to Stan than PyStan and is usually easier to get up and running.

Thanks @Bob_Carpenter,

Sorry for the late reply. Yes from sampling line I am getting error.

I think this is problem with used numpy version vs numpy version used to compile the binary.

Can you downgrade your numpy something like 1.21.6

pip install numpy==1.21.6