# Why doesn't pm.Potential prevent SamplingError from invalid Gamma parameters?

**URL:** <https://discourse.pymc.io/t/why-doesnt-pm-potential-prevent-samplingerror-from-invalid-gamma-parameters/17010>\
**Category:** v5\
**Tags:** modeling\
**Created:** [May 17, 2025, 1:47am UTC](https://discourse.pymc.io/t/why-doesnt-pm-potential-prevent-samplingerror-from-invalid-gamma-parameters/17010 "2025-05-17T01:47:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shoh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/shoh/32/9175_2.png) [@shoh](https://discourse.pymc.io/u/shoh)\
**Post date:** [May 17, 2025, 1:47am UTC](https://discourse.pymc.io/t/why-doesnt-pm-potential-prevent-samplingerror-from-invalid-gamma-parameters/17010/1 "2025-05-17T01:47:06Z")

</div>

Hello!  
I’m running into a problem when using `pm.Gamma.dist(..., beta=k / predicted_spectra)` inside a custom model. Sometimes, `predicted_spectra` contains negative values, which makes the `beta` parameter invalid and causes this error:

> `SamplingError: Initial evaluation of model at starting point failed!`

To work around it, I added a `pm.Potential` to enforce positivity:  
pm.Potential(“positive\_spectra”, pm.math.switch(predicted\_spectra \> 0, 0, -np.inf))  
However, this doesn’t seem to prevent the error — the model still fails to initialize. Here’s a minimal version of my code:

```
def run_analysis(observed_spectra, time_axis, frequency_axis, betas, predictor_list):
  F, T = observed_spectra.shape
  N = len(predictor_list)
  k = 10
  coords = {"T": time_axis, "P": predictor_list, "F": frequency_axis}
  with pm.Model(coords=coords) as random_walk_model:
      sigma = pm.Uniform("sigma", lower=1.0, upper=10.0)
      noise = pm.GaussianRandomWalk("noise", mu=0, sigma=sigma, shape=(N, T), dims=("P", "T"))
      predictors = pm.Deterministic("predictors", pm.math.sigmoid(noise))
      predicted_spectra = pm.math.dot(betas, predictors)
      pm.Potential("positive_spectra", pm.math.switch(predicted_spectra > 0, 0, -np.inf))
      log_likelihood_by_time = pm.logp(pm.Gamma.dist(alpha=k, beta=k / predicted_spectra),observed_spectra).sum(axis=0)
      pm.Potential("log_likelihood_by_time", log_likelihood_by_time)
      idata = pm.sample()
  return idata

```

My question is, why doesn’t the `pm.Potential("positive_spectra", ...)` prevent the `SamplingError` from happening at model initialization?  
Thank you and sorry for a long question..

---

<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:** [May 17, 2025, 9:36pm UTC](https://discourse.pymc.io/t/why-doesnt-pm-potential-prevent-samplingerror-from-invalid-gamma-parameters/17010/2 "2025-05-17T21:36:31Z")

</div>

Potential doesn’t influence the initial point, you can set initval of the relevant variables for that instead
