# Initial evaluation of model at starting point failed!

**URL:** <https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994>\
**Category:** v5\
**Tags:** sampling\
**Created:** [July 23, 2022, 2:41pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994 "2022-07-23T14:41:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![VadimSokolov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vadimsokolov/32/5425_2.png) [@VadimSokolov](https://discourse.pymc.io/u/VadimSokolov)\
**Post date:** [July 23, 2022, 2:41pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/1 "2022-07-23T14:41:29Z")

</div>

Hello,  
have a variable phi = pm.Uniform(“phi”, 0.1,1). However, when I sample from the model, the initial value for phi is negative which leads to undefined likelihood and the failure. The full code/error is below. Not sure why I get a negative number, when the support for my distribution is strictly positive.

```python
with pm.Model(coords={"predictors": X.columns.values}) as beta_reg:
    beta = pm.Normal("beta", 0,1, dims="predictors")
    beta0 = pm.Normal("beta0", 1, 1)
    phi = pm.Uniform("phi", 0.1,1)
    mu = pm.Deterministic("mu",pm.math.invlogit(beta0 + at.dot(X.values, beta)))
    a = phi*mu
    b = (1.0 - mu)*phi
    ratio = pm.Beta("ratio", alpha = a,beta = b, observed=y.values)
    pm.sample()

---------------------------------------------------------------------------
SamplingError Traceback (most recent call last)
Input In [196], in <cell line: 1>()
      7 b = (1.0 - mu)*phi
      8 ratio = pm.Beta("ratio", alpha = a,beta = b, observed=y.values)
----> 9 pm.sample()

File ~/miniconda3/lib/python3.9/site-packages/pymc/sampling.py:558, in sample(draws, step, init, n_init, initvals, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, callback, jitter_max_retries, return_inferencedata, idata_kwargs, mp_ctx, **kwargs)
    556 # One final check that shapes and logps at the starting points are okay.
    557 for ip in initial_points:
--> 558 model.check_start_vals(ip)
    559 _check_start_shape(model, ip)
    561 sample_args = {
    562 "draws": draws,
    563 "step": step,
   (...)
    573 "discard_tuned_samples": discard_tuned_samples,
    574 }

File ~/miniconda3/lib/python3.9/site-packages/pymc/model.py:1794, in Model.check_start_vals(self, start)
   1791 initial_eval = self.point_logps(point=elem)
   1793 if not all(np.isfinite(v) for v in initial_eval.values()):
-> 1794 raise SamplingError(
   1795 "Initial evaluation of model at starting point failed!\n"
   1796 f"Starting values:\n{elem}\n\n"
   1797 f"Initial evaluation results:\n{initial_eval}"
   1798 )

SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'beta': array([ 0.16049949, -0.22746862, -0.69406021, -0.89434199, -0.96045462,
        0.81442739, 0.65041207]), 'beta0': array(0.10526258), 'phi_interval__': array(-0.30358915)}

Initial evaluation results:
{'beta': -8.12, 'beta0': -1.32, 'phi': -1.41, 'ratio': inf}

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [July 23, 2022, 3:45pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/2 "2022-07-23T15:45:44Z")

</div>

Welcome!

> [@VadimSokolov](#):
>
> the initial value for phi is negative

The initial value for _the transformed version of phi_, which PyMC uses internally because phi is bound to the interval [0.1, 1], is negative. But as you can see from the initial evaluation results, the initial value of `phi` is fine, it’s the value of the observe `ratio` that is the problem. I suspect that either `a` or `b` are negative or that your observed data includes values outside the interval [0, 1] (which is where the beta distribution has support).

---

<div class="post-metadata">

**Author:** ![VadimSokolov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vadimsokolov/32/5425_2.png) [@VadimSokolov](https://discourse.pymc.io/u/VadimSokolov)\
**Post date:** [July 23, 2022, 4:30pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/3 "2022-07-23T16:30:19Z")

</div>

The reason I thought it was the phi parameter, is because, when I set a = 1 it works fine, but, when I set a = phi, I get the error. Value of mu is between 0 and 1, it is output of invlogit.

---

<div class="post-metadata">

**Author:** ![VadimSokolov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vadimsokolov/32/5425_2.png) [@VadimSokolov](https://discourse.pymc.io/u/VadimSokolov)\
**Post date:** [July 23, 2022, 4:35pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/4 "2022-07-23T16:35:57Z")

</div>

I think what causes it is that some of my y values are 0. I guess I need to change the model to allow zeros. Something like zero-inflated model Beta model. Although, not sure how to add the zero-inflated component.

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [August 15, 2023, 1:48pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/5 "2023-08-15T13:48:49Z")

</div>

Sorry, I didn’t get it. The error says:

```plaintext
Initial evaluation results:
{'beta': -8.12, 'beta0': -1.32, 'phi': -1.41, 'ratio': inf}

```

where ‘phi’ equals to -1.41, which is negative. So, there is something wrong

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [August 15, 2023, 3:06pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/6 "2023-08-15T15:06:29Z")

</div>

My guess is that the quantity `beta0 + X @ beta` is very negative, which makes the output of the sigmoid zero, so `a` is zero. What is the scale of the `X` data? If it’s quite large, you will run into this problem. It’s always a good idea to scale your input data.

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [August 16, 2023, 2:49am UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/7 "2023-08-16T02:49:48Z")

</div>

I probably realized why I was confused as well as VadimSokolov at the beginning.

Here:

```auto
Initial evaluation results:

```

are the logp values, not the values of the variables. It would be better to modify the messaging in pymc to make it more clear.

---

<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:** [August 16, 2023, 5:34am UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/8 "2023-08-16T05:34:54Z")

</div>

I think the message has been tweaked already in recent versions of PyMC? Which one are you using?

---

<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:** [August 16, 2023, 5:36am UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/9 "2023-08-16T05:36:21Z")

</div>

> <https://github.com/pymc-devs/pymc/pull/6632>
>
> \*\*What is this PR about?\*\*
> This PR aims to solve issue \[6629\](https://github.co…m/pymc-devs/pymc/issues/6629). When we check if the starting values for MCMC do not cause the relevant log probability to evaluate to something invalid and it does, the sampling error currently does not explicitly specify that the initial evaluations correspond to the values of the log-probability at starting values of corresponding parameters. This has created some confusion. In this PR, we specify that the initial evaluations correspond to the logp values.
> 
> \*\*Checklist\*\*
> + \[x\] Explain important implementation details 👆
> + \[x\] Make sure that \[the pre-commit linting/style checks pass\](https://docs.pymc.io/en/latest/contributing/python\_style.html).
> + \[x\] Link relevant issues (preferably in \[nice commit messages\](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html))
> + \[\] Are the changes covered by tests and docstrings?
> + \[\] Fill out the short summary sections 👇
> 
> \## Major / Breaking Changes
> \- ...
> 
> \## New features
> \- ...
> 
> \## Bugfixes
> \- ...
> 
> \## Documentation
> \- ...
> 
> \## Maintenance
> \- ...

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [August 16, 2023, 2:24pm UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/10 "2023-08-16T14:24:27Z")

</div>

That’s strange, because I installed one of the latest v.5.7.2

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [August 17, 2023, 3:09am UTC](https://discourse.pymc.io/t/initial-evaluation-of-model-at-starting-point-failed/9994/11 "2023-08-17T03:09:54Z")

</div>

@ricardoV94, my initial message was motivated by the original post, but I check my version and indeed it’s been changed to the correct one

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/c/c1e5785968e93160933c91dcb59b5b821eff518b.png)

Thanks a lot!
