Pystan hang on mac with rk45

Operating System: Sierra 10.12.6
Interface Version: pystan
Compiler/Toolkit: 2.17.0.0

Everything installed from anaconda.

I’m trying to get any example with integrate_ode_rk45 to work, and have come up with the minimal example below. It seems to run - I get the terminal message:

Iteration: 20 / 20 [100%] (Sampling)

Elapsed Time: 34.4267 seconds (Warm-up)
0.621933 seconds (Sampling)
35.0487 seconds (Total)

but the sampling command never returns, and I get multiprocessing errors when I do the KeyboardInterrupt.

any ideas?

import pystan

model_code="""
functions {
real[] sho(real t, real[] y, real[] theta, real[] x_r, int[] x_i) {
    
    real dydt[2];
    dydt[1] = y[2];
    dydt[2] = -y[1] - theta[1] * y[2];
    
    return dydt;
}
}
transformed data {
int<lower=1> T = 10;
real t0 = 0;
real ts[T] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10.};
real y[T,2] = { {0.397095, -0.853654},
                {-0.300526, -0.696679},
                {-0.948297, 0.0545529},
                {-0.57433, 0.634292},
                {-0.00316177, 0.772725},
                {0.675424, 0.15645},
                {0.703672, -0.424857},
                {-0.0918871, -0.487648},
                {-0.469737, -0.298427},
                {-0.429309, 0.16981} };
real x[0];
int x_int[0];
}
parameters {
real y0[2];
vector<lower=0>[2] sigma;
real theta[1];
}
model {
real y_hat[T,2];
sigma ~ cauchy(0,2.5);
theta ~ normal(0,1);
y0 ~ normal(0,1);
y_hat = integrate_ode_rk45(sho, y0, t0, ts, theta, x, x_int);
for (t in 1:T)
    y[t] ~ normal(y_hat[t], sigma);
}
"""


# In[3]:

sm = pystan.StanModel(model_code=model_code)


# In[5]:

fit=sm.sampling(iter=20)

This is probably a CVODES issue. Try the CVODES-enabled version of
PyStan here:
https://github.com/stan-dev/pystan/archive/v2.17.1.0-cvodes.tar.gz

You should be able to do pip install https://github.com/stan-dev/pystan/archive/v2.17.1.0-cvodes.tar.gz

Thanks. The pip install command gives errors about not finding Stan source headers. Since I installed pystan originally with conda I’m not sure where those would be or to fix this issue.

here’s a quote of the error:

compile options: '-DBOOST_DISABLE_ASSERTS -DBOOST_NO_DECLTYPE -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_RESULT_OF_USE_TR1 -DFUSION_MAX_VECTOR_SIZE=12 -I./pystan -Ipystan/stan/src -Ipystan/stan/lib/stan_math/ -Ipystan/stan/lib/stan_math/lib/eigen_3.3.3 -Ipystan/stan/lib/stan_math/lib/boost_1.64.0 -Ipystan/stan/lib/stan_math/lib/cvodes_2.9.0/include -I/Users/bblais/anaconda/include/python3.6m -c'
extra options: '-Os -ftemplate-depth-256 -Wno-unused-function -Wno-uninitialized'
gcc: pystan/_api.cpp
In file included from pystan/_api.cpp:608:
./pystan/stanc.hpp:1:10: fatal error: 'stan/version.hpp' file not found
#include <stan/version.hpp>
        ^~~~~~~~~~~~~~~~~~
1 error generated.

There’s now a release which would work. Note that this release is only
for people using models which require the CVODES library. Those who do
not need the CVODES library should continue to use the normal releases.

Here’s the command to install from source:

python3 -m pip install
https://github.com/stan-dev/pystan/releases/download/v2.17.1.0-cvodes/pystan-2.17.1.0-cvodes.tar.gz

I don’t use conda but you might be able to use conda to install this as
well.

Isn’t rk45 from boost? We use COVDES for its BDF.

Yes, but if the rk45 is “hanging,” that’s indicating that it’s in a stiff
region. It’s just taking a really, really long time to solve.

Right. I misunderstood the reply, thought there’s an issue in covdes.

Actually, I don’t think it is a stiff issue - because the output of the mcmc finishes. it’s just that the result never returns.

Yeah, that’s different.

Do you see that behavior with any other models?

This sorta behavior happens I think when you have a ton of generated quantities/transformed parameters that need printed. It can take a really long time to process it

I hadn’t done much with pystan before this example, so the simple stuff was working. Given your question, I went looking for other examples and found: GitHub - fonnesbeck/stan_workshop_2016: Bayesian Modeling using Stan in R (May/June 2016)

This one also hangs, and doesn’t use rk45. It runs, clearly, because the line:

pooled_fit = pystan.stan(model_code=pooled_data + pooled_parameters + pooled_model,
data=pooled_data_dict, iter=1000, chains=2)

I get the output of:


Iteration: 1000 / 1000 [100%] (Sampling)

Elapsed Time: 0.134426 seconds (Warm-up)
0.130557 seconds (Sampling)
0.264983 seconds (Total)

but it never returns to the command line of the python notebook.

other simple models worked, like:

coin_code = """
data {
    int<lower=0> K; // number of coins
    int h[K]; // heads and 
    int N[K]; // total flips
}
parameters {
    real<lower=0,upper=1> theta[K];
}
model {
  theta ~ uniform(0,1); // prior
  for (k in 1:K) 
    h[k] ~ binomial (N[k], theta[k]);
}
"""

[this time updating everything with conda update --all solved it…sorry for the noise]

1 Like

Thanks for the update! That actually helps a lot. If we ever see this again, we’ll suggest updating via conda.