:: Pystan :: Init values

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:

  1. create a dictionary
  2. create a function which returns a dictionary and from input values e.g. a dictionary as input
  3. 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.

Yup. No reason not to just return a copy here.

Stan typically only requires initial values in hard problems. Often you can get by with just lowering the bounds of the initial random initialization (for instance in long time series problems with trends or really large-scale regressions).

1 Like

Thank you Bob!