# What is the correct format/shape of the initial values for Sequential Monte Carlo (SMC)?

**URL:** <https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267>\
**Category:** v5\
**Tags:** modeling\
**Created:** [April 16, 2024, 5:56pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267 "2024-04-16T17:56:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LongPham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/longpham/32/8059_2.png) [@LongPham](https://discourse.pymc.io/u/LongPham)\
**Post date:** [April 16, 2024, 5:56pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/1 "2024-04-16T17:56:07Z")

</div>

How do I properly format the initial values passed to the parameter `start` in the function `pymc.smc.sample_smc` for Sequential Monte Carlo (SMC)?

To give a concrete example, let me consider the following code for Bayesian linear regression:

```python
import pymc as pm
import numpy as np

def basic_model(observed_data):
    array_sizes = np.array([size for (size, _) in observed_data])
    array_costs = np.array([cost for (_, cost) in observed_data])
    coefficient_sigma = 5

    with pm.Model() as model:
        coefficient0 = pm.HalfNormal(
            "coefficient0", sigma=coefficient_sigma)
        coefficient1 = pm.HalfNormal(
            "coefficient1", sigma=coefficient_sigma)

        predicted_bounds = coefficient0 + coefficient1 * array_sizes
        observed_costs = pm.Normal("observed_costs", mu=predicted_bounds,
                                   sigma=10, observed=array_costs)
    return model

observed_data = [[1, 1], [2, 2], [4, 3], [8, 4], [
    16, 7], [32, 10], [64, 13], [128, 17], [256, 18]]

init_nuts = {"coefficient0": 10,
             "coefficient1": 10}

num_draws = 1000
num_chains = 4

init_smc = {"coefficient0_log__": np.full((num_draws, num_chains), 10),
            "coefficient1_log__": np.full((num_draws, num_chains), 10)}

with basic_model(observed_data):
    idata = pm.sample_smc(num_draws, start=init_smc,
                          chains=num_chains, random_seed=42)

```

When I run this code with PyMC v5.13.1, I get a warning `UserWarning: More chains (4) than draws (1). Passed array should have shape (chains, draws, *shape)`. I believe the warning stems from the improper shape/format of the initial values `init_smc` I pass to the function `pm.sample_smc`. Here, `init_smc` is a dictionary mapping variable names (i.e., `coefficient0_log __` and `coefficient1_log__ `) to numpy arrays of shape `(num_draws, num_chains)`.

I couldn’t find documentation on what the initial values’ format should be. Does anyone know by any chance how to resolve this issue? Thanks a lot in advance!

---

<div class="post-metadata">

**Author:** ![LongPham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/longpham/32/8059_2.png) [@LongPham](https://discourse.pymc.io/u/LongPham)\
**Post date:** [April 23, 2024, 10:15pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/2 "2024-04-23T22:15:14Z")

</div>

For some reason, if we replace

```auto
init_smc = {"coefficient0_log__": np.full((num_draws, num_chains), 10),
            "coefficient1_log__": np.full((num_draws, num_chains), 10)}

```

with randomized initial values

```auto
init_smc = {"coefficient0_log__": np.random.normal(size=(num_draws, num_chains)),
            "coefficient1_log__": np.random.normal(size=(num_draws, num_chains))}

```

the warning is suppressed, even though the shape of the numpy arrays inside `init_smc` remains the same.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 25, 2024, 2:59pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/3 "2024-04-25T14:59:03Z")

</div>

Shouldn’t it be chains x draws instead of the other way around?

---

<div class="post-metadata">

**Author:** ![LongPham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/longpham/32/8059_2.png) [@LongPham](https://discourse.pymc.io/u/LongPham)\
**Post date:** [April 25, 2024, 5:35pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/4 "2024-04-25T17:35:18Z")

</div>

@ricardoV94 That’s what I initially thought as well. I read somewhere that the shape should be `(num_chains, num_draws)` although I don’t recall where exactly I saw it. But when I tried the shape `(num_chains, num_draws)` in my code, it crashed. So I instead had to use the shape `(num_draws, num_chains)`. It would be great if someone could revise the documentation of `sample_smc` to clarify this point.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 25, 2024, 6:20pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/5 "2024-04-25T18:20:53Z")

</div>

That may actually be a bug / untested feature? I see the only test we have is for a single chain: [pymc/tests/smc/test\_smc.py at 6761c0c73ba07cc9dc51ec3adab7f1aa5f76b23d · pymc-devs/pymc · GitHub](https://github.com/pymc-devs/pymc/blob/6761c0c73ba07cc9dc51ec3adab7f1aa5f76b23d/tests/smc/test_smc.py#L160-L169)

Do you mind opening an issue to check if this is behaving as expected and/or update the docs in our github repository?

---

<div class="post-metadata">

**Author:** ![LongPham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/longpham/32/8059_2.png) [@LongPham](https://discourse.pymc.io/u/LongPham)\
**Post date:** [April 26, 2024, 2:01pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/6 "2024-04-26T14:01:35Z")

</div>

Sure. Let me open an issue on PyMC’s GitHub repository.

---

<div class="post-metadata">

**Author:** ![LongPham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/longpham/32/8059_2.png) [@LongPham](https://discourse.pymc.io/u/LongPham)\
**Post date:** [April 27, 2024, 6:58pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/7 "2024-04-27T18:58:24Z")

</div>

@ricardoV94 I’ve reported this issue on [GitHub](https://github.com/pymc-devs/pymc/issues/7283#issue-2267143549), asking for the clarification in the PyMC documentation and updating the unit test.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 27, 2024, 7:05pm UTC](https://discourse.pymc.io/t/what-is-the-correct-format-shape-of-the-initial-values-for-sequential-monte-carlo-smc/14267/8 "2024-04-27T19:05:13Z")

</div>

Thanks!
