# Non-centered hierarchical model with constraints

**URL:** <https://discourse.pymc.io/t/non-centered-hierarchical-model-with-constraints/371>\
**Category:** Questions\
**Created:** [September 27, 2017, 11:46am UTC](https://discourse.pymc.io/t/non-centered-hierarchical-model-with-constraints/371 "2017-09-27T11:46:25Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![Olaf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/olaf/32/2896_2.png) [@Olaf](https://discourse.pymc.io/u/Olaf)\
**Post date:** [September 30, 2020, 11:38pm UTC](https://discourse.pymc.io/t/non-centered-hierarchical-model-with-constraints/371/13 "2020-09-30T23:38:38Z")

</div>

I try to model standard deviation, which obviously must be positive, but in the centered parameterization I get a handful of divergences. I tried to naively re-parameterize with `sigma = pm.Deterministic('sigma', global_sigma + offset * global_sigma_sd)`, and got a `Bad Initial Energy` error when sampling.  
then in order to put a lower bound on the deterministic variable I tried with `sigma.clip(0.0, np.inf)` and `tt.switch(sigma<0, 0.0, sigma)`, but again I always get a `Bad Initial Energy` error when sampling.  
When instead of a 0-lower bound I tried _1e-15_, with clip I get:

> ValueError: Mass matrix contains zeros on the diagonal.

and with _tt.switch_:

> The chain contains only diverging samples. The model is probably misspecified.

I tested with a latent variable:

```auto
offset = pm.Normal('offset', mu=0, sigma=1.0)
b_offset = pm.Bound(pm.Normal, lower=0.0)('bound_offset', mu=global_sigma + offset, sigma=1)
sigma = pm.Deterministic('sigma', b_offset * global_sigma_sd)

```

which gave no divergences and took 2-4 times longer to sample, but `a + b * c != (a + b) * c`, so that doesn’t seem like a good idea.  
How can this be solved? Is the best option to transform the data, e.g. square demeaned data and log-transform and then model mu instead of sigma?  
This is related to [this ongoing struggle](https://discourse.pymc.io/t/model-comparison-how-to-constrain-a-model-to-positive-mu-differences/5742).

---

_[View the full topic](https://discourse.pymc.io/t/non-centered-hierarchical-model-with-constraints/371)._
