Dear Pystan beginner,
I would like to share something, which was a game changer to me. I did not find how to use init values with pystan. Here is what allowed me to do it. 3 Steps are necessary:
- create a dictionary
- create a function which returns a dictionary and from input values e.g. a dictionary as input
- call stan with init values
First, define a dictionary with initial values for the selected parameters for which you would like to do so.
#################################################
# create a dictionary with init values
init_dict = {};
init_dict["mean"] = 0.0
init_dict["std"] = 1.0
Second, Pystan needs as input a function/callable that returns a dictionary. So we need a function that returns a dictionary. Maybe this could be simplified even more with return init_dict?
#################################################
# init values for initialisation
def stan_init( init_dict):
res = {}
for name,dict_ in init_dict.items():
res[name] = dict_
return res
Third, call stan with “init = lambda: stan_init( init_dict )”.
fit = sm.sampling( data = my_data
, iter = iter_
, warmup = warmup_
, init = lambda: stan_init( init_dict )
)
Hope this saves some people some time.