# Reject statement stops the entire program instead of an iteration

**URL:** <https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713>\
**Category:** Modeling\
**Tags:** techniques\
**Created:** [August 28, 2020, 11:33am UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713 "2020-08-28T11:33:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwguo](https://avatars.discourse-cdn.com/v4/letter/w/7993a0/32.png) [@wwguo](https://discourse.mc-stan.org/u/wwguo)\
**Post date:** [August 28, 2020, 11:33am UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713/1 "2020-08-28T11:33:24Z")

</div>

I write a function named gram\_schmidt and have a `reject` statement in it for detecting whether eigenvalues are legal. When I run the stan code by pystan in Jupyter, sometimes the `reject` works as expected ( just stops the iteration) and sometimes it stops the program, like the last Exception in the following pystan feedbacks, and a RuntimeError shown in Jupyter.

```
Rejecting initial value:
  Gradient evaluated at the initial value is not finite.
  Stan can't start sampling from this initial value.
Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: Exception: gram_schmidt: Eigen values must not be negative; (in 'symx-r-init-pdf.stan' at line 70)
  (in 'symx-r-init-pdf.stan' at line 398)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: Exception: gram_schmidt: Eigen values must not be negative; (in 'symx-r-init-pdf.stan' at line 70)
  (in 'symx-r-init-pdf.stan' at line 398)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: Exception: gram_schmidt: Eigen values must not be negative; (in 'symx-r-init-pdf.stan' at line 70)
  (in 'symx-r-init-pdf.stan' at line 398)

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: Exception: gram_schmidt: Eigen values must not be negative; (in 'symx-r-init-pdf.stan' at line 70)
  (in 'symx-r-init-pdf.stan' at line 398)

Exception: Exception: gram_schmidt: Eigen values must not be negative; (in 'symx-r-init-pdf.stan' at line 70)
  (in 'symx-r-init-pdf.stan' at line 398)

```

Is it an expected behavior of the `reject` statement? I use pystan 2.19.1.1

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [August 28, 2020, 1:35pm UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713/2 "2020-08-28T13:35:12Z")

</div>

That looks like normal behavior.

What’s happening is that Stan is trying to initialize and is unable to. It tries 100 times before stopping. You can see that by the repeated rejection messages.

There are two things you can try.

1. Change the initialization radius. By default, each unconstrained parameter is independently drawn from a uniform -2, 2 distribution. Depending on the parameterization, that could be too wide, so trying something smaller might work.

2. The better thing to do is to put bounds on the parameters so more of the parameter space does not reject. Depending on what you’re doing, it may be easy or hard. If you’re able to write a condition to reject, you should be able to code the parameters so it doesn’t reject.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![wwguo](https://avatars.discourse-cdn.com/v4/letter/w/7993a0/32.png) [@wwguo](https://discourse.mc-stan.org/u/wwguo)\
**Post date:** [August 28, 2020, 2:01pm UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713/3 "2020-08-28T14:01:37Z")

</div>

Thank you for detailed explanation, but I think it might not be a problem related to limit of 100 times.

If it reaches the limit, the program will feedback like:

```
Initialization between (-2, 2) failed after 100 attempts. 
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.

```

But what I get in Jupyter is:

```
RuntimeError Traceback (most recent call last)
<ipython-input-90-dc25a5a438be> in <module>
      2 iter=200, chains=1, n_jobs=1, check_hmc_diagnostics=True,
      3 #control = {'max_treedepth': 12}, #'adapt_delta': 0.9},
----> 4 verbose=True) #, save_warmup=False

/usr/local/lib/python3.7/site-packages/pystan/model.py in sampling(self, data, pars, chains, iter, warmup, thin, seed, init, sample_file, diagnostic_file, verbose, algorithm, control, n_jobs, **kwargs)
    811 call_sampler_args = izip(itertools.repeat(data), args_list, itertools.repeat(pars))
    812 call_sampler_star = self.module._call_sampler_star
--> 813 ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
    814 samples = [smpl for _, smpl in ret_and_samples]
    815 

/usr/local/lib/python3.7/site-packages/pystan/model.py in _map_parallel(function, args, n_jobs)
     88 pool.join()
     89 else:
---> 90 map_result = list(map(function, args))
     91 return map_result
     92 

stanfit4anon_model_2951b16f4a2e62f91b26939530921dbe_2406916259490443607.pyx in stanfit4anon_model_2951b16f4a2e62f91b26939530921dbe_2406916259490443607._call_sampler_star()

stanfit4anon_model_2951b16f4a2e62f91b26939530921dbe_2406916259490443607.pyx in stanfit4anon_model_2951b16f4a2e62f91b26939530921dbe_2406916259490443607._call_sampler()

RuntimeError: Exception: Exception: gram_schmidt: Eigen values must not be negative; (in 'symx-r-init-pdf.stan' at line 70)
  (in 'symx-r-init-pdf.stan' at line 398)

```

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [August 28, 2020, 2:28pm UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713/4 "2020-08-28T14:28:38Z")

</div>

Yes. Thanks for adding the actual error message.

Really hard to tell without seeing the program. Do you have it wrapped in `map_rect`. If so, maybe the exception isn’t being handled properly there (in Stan or Math)… but that’s just speculation without seeing the actual program.

---

<div class="post-metadata">

**Author:** ![wwguo](https://avatars.discourse-cdn.com/v4/letter/w/7993a0/32.png) [@wwguo](https://discourse.mc-stan.org/u/wwguo)\
**Post date:** [August 28, 2020, 2:49pm UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713/5 "2020-08-28T14:49:29Z")

</div>

Sorry for not putting complete errors before. My function is so complicate that I want to skip it and hope those rejecting infos could tell the story, since those info are the whole rejecting exceptions, from the same function and the same code line.

I will try to generate a concise example that can reproduce the error. Again, sorry for posting vague info.

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [August 31, 2020, 6:46pm UTC](https://discourse.mc-stan.org/t/reject-statement-stops-the-entire-program-instead-of-an-iteration/17713/7 "2020-08-31T18:46:19Z")

</div>

> [@wwguo](#):
>
> I will try to generate a concise example that can reproduce the error. Again, sorry for posting vague info.

Thanks! And no need to apologize. If you’re able to get a smaller working example, the community can help with fixing the issue or identifying a bug.
