Pystan stan model object gets deleted

Dear all,
I am coding with python. I created a very simple model and run it with python. The Stan model object (sm) gets created by pystan. However, when I want to sample, then it gets deleted.

While debugging I see that after (LOCATION 1) I have the sm-Object.

If I continue to (LOCATION 2) then, after that step, the sm-Object disappeared AND the process jumps/stops again at the breakpoint at (LOCATION 1); this allthough there is no loop.

I would be gratefull to learn more what I made wrong to understand this unexpected behavior.

Thanx in advance

PYTHON CODE:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

#########################################################

investigation 01: beta distribution

############################################################

simulate x. beta distributed

n_sim = 1000
alpha = 5
beta = 5
x = stats.beta.rvs( alpha, beta,size=n_sim)
cdf = np.linspace( 0, 1, len(x))
plt.figure(1)
plt.plot( np.sort(x), cdf )

############################################################

build stan model

my_data={} # create data dictionary
my_data[“data_n”] = n_sim
my_data[“X”] = x

print(“compile stan model”)
fname = “scr_82_model.stan”

LOCATION 1

sm = pystan.StanModel(file=fname) # build stan model

############################################################

fit with stan model

print(“fit with stan”)

LOCATION 2

fit = sm.sampling( data = my_data)

alpha = fit[“alpha”]
beta = fit[“beta”]

STAN MODEL:
// MODEL 82
// check beta distribution

data {
int data_n; // number of values
vector[data_n] X; // data values
}

parameters {
real <lower= 1e-15> alpha;
real <lower= 1e-15> beta;
}

model {
// priors
alpha ~ normal(5, 2);
beta ~ normal(5, 2);

//  likelihood
X ~ beta( alpha, beta );

}

Short addition: If I run the code, then it will run in an endless loop. This although there is no such loop in the code.

What OS do you have? (macOS?)

Windows10

One thing I note from the sample python code is that there is no “import stan” statement – so the name “pystan” under the heading location 1 will not be defined.

Are the samples of code shared a complete self-contained runnable example, or are they fragments copied from various places in a larger python project?

For someone to have a better chance of being able to assist, can you share information about:

  • what distribution and version of python you are using
  • what version of pystan you are using
  • exactly how you are running your python script. e.g. are you running something like “python3 script.py” on the command line or are you running the script from inside some kind of notebook or IDE
  • the exact error message (e.g. exception raised by python & traceback) that is displayed when the problem occurs
  • a complete self-contained python script that can reproduce the issue when run on the command line with python3 interpreter, outside of any notebook or IDE

Thank you for your reply.

I use Python 3.7 with Pycharm to run and debugg code and Pystan is at V2.19.1.1

The “import pystan” is included but was missing above.
I found a workaround. I copied the lines into another script and things work fine. → urgency is significantly reduces. Still within this script this unwanted feature remains.

I think there might be something that pycharm does under the hood that causes these problems.

But in any case, you should run your code in if name == “main” block.

GREAT! That did the trick!
Thank you very much!

For other newbies: Here the python code (excl. import statements) that worked. STAN model as above.


############################################################

RUN PYSTAN FUNCTION

def run_pystan():

############################################################
#   simulate x. beta distributed
n_sim = 1000
alpha = 5
beta = 5
x = stats.beta.rvs( alpha, beta,size=n_sim)
cdf = np.linspace( 0, 1, len(x))
plt.figure(1)
plt.plot( np.sort(x), cdf )

############################################################
#   build stan model
my_data={}  #   create data dictionary
my_data["data_n"] = n_sim
my_data["X"] = x

print("compile stan model")
fname = "scr_82_model.stan"
# LOCATION 1
# sm = build_stan(fname)
sm = pystan.StanModel(file=fname)   #   build stan model

############################################################
#   fit with stan model
print("fit with stan")
# LOCATION 2
# fit = fit_stan( sm )
fit = sm.sampling( data = my_data, iter = 2000)

alpha = fit["alpha"]
beta = fit["beta"]
plt.figure(2)
plt.plot( alpha, beta, "." )

############################################################

MAIN

if name == “main”:
run_pystan()