Initialization failed after 100 attempts (NaN rejection)

1,470 views
Skip to first unread message

Dan Stowell

unread,
Jun 28, 2013, 5:30:51 AM6/28/13
to stan-...@googlegroups.com
Hi all,

The following model gets consistently rejected on initialisation:
https://gist.github.com/danstowell/5883463

"""
Informational Message: The parameter state is about to be Metropolis
rejected due to the following underlying, non-fatal (really) issue
(and please ignore that what comes next might say 'error'): Error in
function stan::prob::normal_log(N4stan5agrad3varE): Random variable[0]
is nan:0, but must not be nan!
If the problem persists across multiple draws, you might have a
problem with an initial state or a gradient somewhere.
If the problem does not persist, the resulting samples will still be
drawn from the posterior.
[...]

Initialization failed after 100 attempts. Try specifying initial
values, reducing ranges of constrained values, or reparameterizing the
model.
"""

I can't see any constraint issues. Specifying initial values doesn't
help, it leads to:

"""
Exception: Error in function
stan::prob::normal_log(N4stan5agrad3varE): Random variable[0] is
nan:0, but must not be nan!
Diagnostic information:
Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::domain_error>
>
std::exception::what: Error in function
stan::prob::normal_log(N4stan5agrad3varE): Random variable[0] is
nan:0, but must not be nan!
"""

Any clues apparent?

Thanks for any thoughts -
Dan
--
http://www.mcld.co.uk

Bob Carpenter

unread,
Jun 28, 2013, 2:51:47 PM6/28/13
to stan-...@googlegroups.com
The

On 6/28/13 5:30 AM, Dan Stowell wrote:
> Hi all,
>
> The following model gets consistently rejected on initialisation:
> https://gist.github.com/danstowell/5883463

Thanks for providign the code. One problem is the way you
start the model:

--------------------------------------------
model {
vector[D] newgen_means[N];
vector[D] newgen_stdvs[N];
vector[D] gen_means[N];
vector[D] gen_stdvs[N];

changeness ~ beta(0.6, 0.8);

// each datum is associated with its own generating gaussian
// Common prior for the brandnew
for (n in 1:N){
newgen_means[n] ~ normal(0.0, 100.0);
newgen_stdvs[n] ~ exponential(2.0);
}
-----------------------------------------

This won't work in Stan because you are using newgen_means[n] before
you define it. You declare the local variable, but don't give it
a value.

If you want to sample these values, they need to be declared
as parameters. And newgen_stdvs needs to be declared with the
constraint lower=0.

The ~ doesn't mean randomly generate the left-hand side from the
distribution on the right hand side. For instance, your first line
is equivalent to:

lp__ <- lp__ + normal_log(newgen_means[n], 0, 100);

where normal_log() is the built-in normal log density function.

- Bob



- Bob

Dan Stowell

unread,
Jun 29, 2013, 6:06:16 AM6/29/13
to stan-...@googlegroups.com
2013/6/28 Bob Carpenter <ca...@alias-i.com>:
Bob, thanks for that. I had initially specified those as parameters,
then moved them down to be local variables, without spotting the big
difference that makes wrt sampling. The code works now. (gist updated,
in case anyone's interested.)

Best
Dan
--
http://www.mcld.co.uk

Dan Stowell

unread,
Jul 19, 2013, 5:01:11 AM7/19/13
to stan-...@googlegroups.com
Hi all,

Here's another "initialization failed" issue - does the "location
parameter" message below give any clues about what's going on?

"""
Informational Message: The parameter state is about to be Metropolis
rejected due to the following underlying, non-fatal (really) issue
(and please ignore that what comes next might say 'error'): Error in
function stan::prob::normal_log(m): Location parameter (max size) is
256, but must be consistent, 1 or max=768
If the problem persists across multiple draws, you might have a
problem with an initial state or a gradient somewhere.
If the problem does not persist, the resulting samples will still be
drawn from the posterior.

Initialization failed after 100 attempts. Try specifying initial
values, reducing ranges of constrained values, or reparameterizing the
model.
"""

The context of this: it's the changepoint model I mentioned previously
in this thread, applied to a vector time-series. The difference I'm
trying to add is dependence between elements in a vector, because the
data I have is actually a stack of three smaller vectors for which we
can assume "common cause" for corresponding elements. Code is here:
<https://gist.github.com/danstowell/6037566>. The model runs if I set
the Nstack to 1 (equivalent to the old model).

My first suspect for the init fail was that the data might not fit
this more constrained model, but that doesn't seem to be the case
since I can make the priors very wide (large value of "newgen_stdvs"),
or initialise using the empirical means+stds, and the same error
occurs.

Thanks
Dan

Bob Carpenter

unread,
Jul 19, 2013, 1:31:30 PM7/19/13
to stan-...@googlegroups.com

On 7/19/13 5:01 AM, Dan Stowell wrote:
> Hi all,
>
> Here's another "initialization failed" issue - does the "location
> parameter" message below give any clues about what's going on?

Yes, "size" means vector size. So what's happening is that
differently shaped or sized multidimensional objects are
being passed into the normal distribution at some point,
one of which appears to be three times as long as
the other. Sure enough, here's the culprit:

data {
vector[D * Nstack] x[N]; // obsns

parameters {
vector[D] newgen_means[N];
vector<lower=0>[D] newgen_stdvs[N];

model {
...
x[1] ~ normal(newgen_means[1], newgen_stdvs[1]);

So what you need to do is declare:

data {
vector[D] x[N,Nstack];
...
model {
for (ns in 1:Nstack}
x[n,1] ~ normal(new_gen_means[1], newgen_stdvs[1]);

And the same thing for the x[n] case later.

This is just the use case we were imagining vectorizing so that you
could write what you wrote originally with the data type for
x:

data {
vector[D] x[N,Nstack];
...
model {
...
x[1] ~ normal(newgen_means[1], newgen_stdvs[1]);

I'm following up with a message about fixing our error messages.

Thanks for posting --- it's really helpful to see these messages
in the wild rather than when we're doing development and know what
to look for.

- Bob

Dan Stowell

unread,
Jul 19, 2013, 3:46:17 PM7/19/13
to stan-...@googlegroups.com
2013/7/19 Bob Carpenter <ca...@alias-i.com>:
Thanks for your answer. I can see now how the error message does kinda
say "something is the wrong shape" but only once you know that's what
it's saying (IMHO)! Model is back up and running again, thanks.

Dan
> --
> You received this message because you are subscribed to the Google Groups
> "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
http://www.mcld.co.uk
Reply all
Reply to author
Forward
0 new messages