# Bayesian Regressor: Sampling Error: "Bad Initial Energy"

**URL:** <https://discourse.pymc.io/t/bayesian-regressor-sampling-error-bad-initial-energy/5249>\
**Category:** Questions\
**Created:** [June 10, 2020, 1:00pm UTC](https://discourse.pymc.io/t/bayesian-regressor-sampling-error-bad-initial-energy/5249 "2020-06-10T13:00:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pymc3\_newbie](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@pymc3\_newbie](https://discourse.pymc.io/u/pymc3_newbie)\
**Post date:** [June 10, 2020, 1:00pm UTC](https://discourse.pymc.io/t/bayesian-regressor-sampling-error-bad-initial-energy/5249/1 "2020-06-10T13:00:06Z")

</div>

Regards,

I am trying to create a _Bayesian Linear Regression_ model with one independent variable. However, when I try to sample my model using the NUTS sampler, I get the following error: “Sampling Error: Bad Initial Energy”.

The code below shows the approach I followed to build my model:

```
basic_model = pm.Model()

with basic_model:

   alpha = pm.Gamma('alpha', mu=alpha_mean, sigma=alpha_std, shape=(1))
   beta = pm.Beta('beta', mu=beta_mean, sigma=beta_std, shape=(1))
   sigma = pm.HalfNormal('sigma', tau=1)

   mu = alpha + beta*model_input

   Y_obs = pm.Beta('Y_obs', mu=mu, sigma=sigma, observed=model_output)

```

`with basic_model:`

```
   trace = pm.sample(500, cores=1, init='adapt_diag')

```

I had pre-calculated the inputs to the model as follows:

```
# Slice out Variables
X = bayesian_regressor_frame['X']
Y = bayesian_regressor_frame['Y']
X = X.values
Y = Y.values
model_input = theano.shared(X)
model_output = theano.shared(Y)

# Calculate Parameters
alpha_mean = Rolling_OLS_params['alpha'].mean()
alpha_std = Rolling_OLS_params['alpha'].std()
beta_mean = Rolling_OLS_params['beta'].mean()
beta_std = Rolling_OLS_params['beta'].std()

```

I have taken the liberty to attach the input files that were used to build the model. Where am I going wrong?

[bayesian\_regressor\_frame.csv](https://discourse.pymc.io/uploads/short-url/3aXsCwI0pU6vDUlPseSjq9Ntg6G.csv) (113.2 KB) [Rolling\_OLS\_params.csv](https://discourse.pymc.io/uploads/short-url/8SYiefJoGCaSBwFqj1FLJtTPMzA.csv) (102.8 KB)

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [June 11, 2020, 8:29am UTC](https://discourse.pymc.io/t/bayesian-regressor-sampling-error-bad-initial-energy/5249/2 "2020-06-11T08:29:12Z")

</div>

Hi,  
There are various reasons why `Bad initial energy` can happen. Have you tried [the solutions in our FAQ](https://discourse.pymc.io/t/frequently-asked-questions/74/5)?  
A few quick suggestions related to this:

- Make sure your data are standardized somehow if they are on a wide scale.
- Make sure your priors are not too wide, i.e informative enough – here are [good practices on choosing priors](https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations). In general, I wouldn’t advice setting your priors with the data’s mean and std. First, this can force the sampler to start in a region of low curvature. Second, you’re using the data twice: once to set the priors, and another time to update them. However, the priors encode the scientific information you have _before_ seeing any data.
- Make sure there are no inappropriate NaNs somewhere – you’ll find more details in the linked FAQ.
- Finally, you don’t have to specify `shape` when the random variable is one-dimensional.

Hope this helps 🖖

---

<div class="post-metadata">

**Author:** ![pymc3\_newbie](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@pymc3\_newbie](https://discourse.pymc.io/u/pymc3_newbie)\
**Post date:** [June 11, 2020, 9:34am UTC](https://discourse.pymc.io/t/bayesian-regressor-sampling-error-bad-initial-energy/5249/3 "2020-06-11T09:34:53Z")

</div>

Thanks for the pointers…

I believe that the problem has to do with my priors. I tried the datasets using canned models from the high-level “pycm3\_models” module and the sampling proceeded smoothly. So, the problem must be my priors.

Will go through all the links you sent.
