Reject statement stops the entire program instead of an iteration

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

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!

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)

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.

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.

2 Likes

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.

1 Like