Hi everyone,
I’m a new user of PyStan and I’m trying to replicate a model that a friend built. I’m using Stan v3.3.0 and I’m coding in notebooks (even in .py files the same error raises). My current issue is that I can build the model but I can’t sample it. The raised error is the following:
Sampling: 0% Sampling: Initialization failed.
RuntimeError: Initialization failed.
RuntimeError Traceback (most recent call last)
Input In [235], in <module>
----> 1 model_fit = model_reg.sample(num_chains=1, num_samples=2000, num_warmup=500)
File ~/Dropbox/Doctorado/Paper RIS - Sim Match Bayers/venv/lib/python3.10/site-packages/stan/model.py:89, in Model.sample(self, num_chains, **kwargs)
61 def sample(self, *, num_chains=4, **kwargs) -> stan.fit.Fit:
62 """Draw samples from the model.
63
64 Parameters in ``kwargs`` will be passed to the default sample function.
(...)
87
88 """
---> 89 return self.hmc_nuts_diag_e_adapt(num_chains=num_chains, **kwargs)
File ~/Dropbox/Doctorado/Paper RIS - Sim Match Bayers/venv/lib/python3.10/site-packages/stan/model.py:108, in Model.hmc_nuts_diag_e_adapt(self, num_chains, **kwargs)
92 """Draw samples from the model using ``stan::services::sample::hmc_nuts_diag_e_adapt``.
93
94 Parameters in ``kwargs`` will be passed to the (Python wrapper of)
(...)
105
106 """
107 function = "stan::services::sample::hmc_nuts_diag_e_adapt"
--> 108 return self._create_fit(function=function, num_chains=num_chains, **kwargs)
File ~/Dropbox/Doctorado/Paper RIS - Sim Match Bayers/venv/lib/python3.10/site-packages/stan/model.py:311, in Model._create_fit(self, function, num_chains, **kwargs)
308 return fit
310 try:
--> 311 return asyncio.run(go())
312 except KeyboardInterrupt:
313 return
File ~/Dropbox/Doctorado/Paper RIS - Sim Match Bayers/venv/lib/python3.10/site-packages/nest_asyncio.py:38, in _patch_asyncio.<locals>.run(main, debug)
36 task = asyncio.ensure_future(main)
37 try:
---> 38 return loop.run_until_complete(task)
39 finally:
40 if not task.done():
File ~/Dropbox/Doctorado/Paper RIS - Sim Match Bayers/venv/lib/python3.10/site-packages/nest_asyncio.py:81, in _patch_loop.<locals>.run_until_complete(self, future)
78 if not f.done():
79 raise RuntimeError(
80 'Event loop stopped before Future completed.')
---> 81 return f.result()
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/futures.py:201, in Future.result(self)
199 self.__log_traceback = False
200 if self._exception is not None:
--> 201 raise self._exception
202 return self._result
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/tasks.py:232, in Task.__step(***failed resolving arguments***)
228 try:
229 if exc is None:
230 # We use the `send` method directly, because coroutines
231 # don't have `__iter__` and `__next__` methods.
--> 232 result = coro.send(None)
233 else:
234 result = coro.throw(exc)
File ~/Dropbox/Doctorado/Paper RIS - Sim Match Bayers/venv/lib/python3.10/site-packages/stan/model.py:235, in Model._create_fit.<locals>.go()
233 sampling_output.clear()
234 sampling_output.write_line("<info>Sampling:</info> <error>Initialization failed.</error>")
--> 235 raise RuntimeError("Initialization failed.")
236 raise RuntimeError(message)
238 resp = await client.get(f"/{fit_name}")
RuntimeError: Initialization failed.
Here is my model:
stan_model = ”””
data {
int<lower=0> N_obs; // number of observations
int N_players; // number of players
int N_goalkeepers; // number of goalkeepers
int N_zones; // number of field zones
int N_times; // number of time frames
int N_results; // number of types of results
int N_locations; // number of types home event
int<lower=1,upper=N_players> player_id[N_obs];
int<lower=1,upper=N_goalkeepers> goalkeeper_id[N_obs];
int<lower=1,upper=N_zones> zone_id[N_obs];
int<lower=1,upper=N_times> time_id[N_obs];
int<lower=1,upper=N_results> result_id[N_obs];
int<lower=1,upper=N_locations> location_id[N_obs];
int goal[N_obs]; // dependent variable
}
parameters {
real alpha; // intercept
vector<lower=0,upper=1>[N_players] beta_player; // coefficient associated with each player
vector[N_goalkeepers] beta_goalkeeper; // coefficient associated with each goalkeeper
vector[N_zones] beta_zone; // coefficient associated with each zone
vector[N_times] beta_time; // coefficient associated with each time frame
vector[N_results] beta_result; // coefficient associated with each result
vector[N_locations] beta_location; // coefficient associated with each type of localia
}
model {
// priors
alpha ~ normal(0,1);
beta_player ~ beta(3.44,7.34);
beta_goalkeeper ~ normal(0,0.38);
beta_zone ~ normal(-0.17,0.58);
beta_time ~ normal(-0.23,0.37);
beta_result ~ normal(-0.5,0.5);
beta_location ~ normal(-0.78,0.6);
goal ~ bernoulli_logit(alpha + beta_player[player_id] + beta_goalkeeper[goalkeeper_id] +
beta_zone[zone_id] + beta_time[time_id] + beta_result[result_id] + beta_location[location_id]);
}
"""
data = {"N_obs": len(df_model),
"N_players": len(df_model.new_id.unique()),
"N_goalkeepers": len(df_model.glk_id.unique()),
"N_zones": len(df_model.cat_zone.unique()),
"N_times": len(df_model.timeFrame.unique()),
"N_results": len(df_model.cat_res.unique()),
"N_locations": len(df_model.home_event.unique()),
"player_id": df_model.new_id.to_list(),
"goalkeeper_id": df_model.glk_id.to_list(),
"zone_id": df_model.cat_zone.to_list(),
"time_id": df_model.timeFrame.to_list(),
"result_id": df_model.cat_res.to_list(),
"location_id": df_model.home_event.to_list(),
"goal": df_model.goal.to_list()
}
Here is the building of the model (which runs well, it raises Building: done without any warning)
model_reg = stan.build(stan_models, data=data, random_seed=1)
Here is the sample of the model that I’m trying run but raises the error.
model_fit = model_reg.sample(num_chains=1, num_samples=2000, num_warmup=500)
Could you help me to understand what is the error and how can I solve it? Thanks in advance!