# :: Pystan :: Init values

**URL:** <https://discourse.mc-stan.org/t/pystan-init-values/26008>\
**Category:** Modeling\
**Created:** [January 18, 2022, 7:25am UTC](https://discourse.mc-stan.org/t/pystan-init-values/26008 "2022-01-18T07:25:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliverk](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@oliverk](https://discourse.mc-stan.org/u/oliverk)\
**Post date:** [January 18, 2022, 7:25am UTC](https://discourse.mc-stan.org/t/pystan-init-values/26008/1 "2022-01-18T07:25:23Z")

</div>

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.

```stan
#################################################
# 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?

```stan
#################################################
# 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 )”.

```stan
    fit = sm.sampling( data = my_data
                       , iter = iter_
                       , warmup = warmup_
                       , init = lambda: stan_init( init_dict )
                       )

```

Hope this saves some people some time.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [February 1, 2022, 11:24pm UTC](https://discourse.mc-stan.org/t/pystan-init-values/26008/2 "2022-02-01T23:24:13Z")

</div>

> [@oliverk](#):
>
> Maybe this could be simplified even more with return init\_dict?

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).

---

<div class="post-metadata">

**Author:** ![oliverk](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@oliverk](https://discourse.mc-stan.org/u/oliverk)\
**Post date:** [February 2, 2022, 11:58am UTC](https://discourse.mc-stan.org/t/pystan-init-values/26008/3 "2022-02-02T11:58:35Z")

</div>

Thank you Bob!
