optimizing() returns bad estimation occasionally

429 views
Skip to first unread message

Xulong Wang

unread,
Dec 13, 2014, 12:41:13 PM12/13/14
to stan-...@googlegroups.com
Dear Stan group,

When running the optimizing function on my model, I occasionally came across this error message: Error evaluating model log probability: Non-finite gradient.

There is a big jump after this error message appears. From simulation data, log probability before this jump was already in the right range but suddenly jumped to a nonsense value.

If I put another run for the same data which returned problematic estimation, the optimization process can go through smoothly with reasonable result. So, it most likely to be an initialization problem?

I tried all three algorithms: Newton, BFGS, LBFGS. Newton is very slow and always returned bad estimation. Both BFGS and LBFGS returned reasonable estimation on most runs, but occasionally returned problematic estimate.

I can do a second round run on all the problematic estimations given that the output log likelihoods fall in a completely different range, so it is easy to tell. But this is little dumb. And I am afraid there might be other fundamental issues in the model, which made the problem.

I have attached the R code, stan code, as well as the relevant R data. Simply run the R code will catch the problem. The final log probability should be around -605. And you will get some around -12.

Thanks much,

Xulong

optimizingIssue.R
optimizingIssue.rdt
glmKS2ApoeNull.stan

Bob Carpenter

unread,
Dec 13, 2014, 3:00:49 PM12/13/14
to stan-...@googlegroups.com
The -12 log density will be a BETTER estimate than -605.

If your posterior is multimodal, we won't necessarily find the
global optimum. And if it's hierarchical, there may not be
a maximum on the posterior density, so the posterior mode doesn't
make sense.

You need kind of prior on your cut points. Otherwise,
the scale isn't going to be well identified (though the cauchy
priors on the coefficients will help a bit, they have very fat
tails).

- Bob

Xulong Wang

unread,
Dec 13, 2014, 7:40:38 PM12/13/14
to stan-...@googlegroups.com
Thanks Bob for your valuable points,

The -12 log density is indeed a better estimate mathematically. But I claimed -605 is the sensible one because this is based on a simulation data over 400 markers as you may see from the attached R data file. The log density curve with the 400 markers make sense if it were around -605. There are clear high values on the right markers, with a peak on the curve in the right position.

When I set init = 0 in the optimizing function, I will get the log density around -12 for all of the 400 markers. And there were no peaks.

I am trying to set a dirichlet prior on the cut, and also set parameters' initial values within the reasonable range. But I am afraid my model is indeed a little strange, and this optimizing function will just not work for me anyway...

Bob Carpenter

unread,
Dec 14, 2014, 4:05:52 PM12/14/14
to stan-...@googlegroups.com

> On Dec 13, 2014, at 7:40 PM, Xulong Wang <emai...@gmail.com> wrote:
>
> Thanks Bob for your valuable points,
>
> The -12 log density is indeed a better estimate mathematically. But I claimed -605 is the sensible one

Then you need to be doing something other than maximum likelihood
estimation.

> I am trying to set a dirichlet prior on the cut, and also set parameters' initial values within the reasonable range.

The Dirichlet density assumes its variable has
been declared a simplex --- it's constrained. Also,
Dirichlet variates aren't ordered.

You can just declare a normal prior on the cut points.
If you want them to be more constrained, you'll need
other declarations.

- Bob

Xulong Wang

unread,
Dec 15, 2014, 9:52:29 AM12/15/14
to
- Bob, before elaborating the details, I want to gladly report that the model is working, if not too early to say so. Here are what I did:

1. Declared primitive cut points as simplex, put dirichlet prior on them, declared ordered positive cut points from cumsum of the primitive cut points with a chosen scale factor (I used 10), also put the first cut point as 0. I think this should resolve the scalability issue.

2. Add an intercept parameter in the ordered categorical model with cauchy(0, 0.5) prior, given the fact that I fixed the first cut point at 0. Together with the last step, this should resolve the identificability issue.

3. Constrain the deviation parameter more, that is the sigmaU in the model. You pointed this out in your previous post, and I found out from my diagnosis sigmaU is really the key. When estimations go insane, the sigmaU values always went crazy, can be more than 100. Now, I constrained sigmaU lower as 0.001, upper as 1, and a little more stringent prior as half cauchy(0, 0.5). I believe the current constrain is reasonable if you also agree. I understand we can not put too much constrain on sigmaU, otherwise we will underestimate the random effect. I also have the multivariate normal primitive parameter z in the model that follows normal(0, 1), which is pretty broad. It is sigmaU * (CK * z) together that define the random effect. So I think the whole strategy should be fine theoretically.

4. Put user-defined initial values instead of the random. I put the optimizing() output from one marker as initial values of all other markers. This significantly decrease the estimating time.

I want to report the above strategies also resolve the problem as reported in another post of mine: Ordered categorical model with known covariance matrix not converging. It was the same model, same data, instead implemented by sampling. MCMC converged well, and results are similar with that of the optimizing method. When tested on simulated data, the peak was clear and correctly positioned in the WAIC plot.

Please let me know if you see anything problematic, and any other things I should do to check the sanity.

Best,

Xulong

Bob Carpenter

unread,
Dec 15, 2014, 12:57:19 PM12/15/14
to stan-...@googlegroups.com
For (3) we tend to favor unbounded priors for scale or sd parameters.
But there are lots of examples still in our manual with hard constraints.
You shouldn't have too much problem with hard constraints as long as
the posterior isn't jammed up against one of the bounds. I don't think
cauchy(0,0.5) is going to do much in the interval (0,1) --- you could
just leave it uniform. You should also be able to use the lower bound 0
rather than 0.001. Were you finding estimates unstable around 0 otherwise?

- Bob

> On Dec 15, 2014, at 9:52 AM, Xulong Wang <emai...@gmail.com> wrote:
>
> - Bob, before elaborating the details, I want to gladly report that the model is working, if not too early. This is what I did:
>
> 1. Declared primitive cut points as simplex, put dirichlet prior on them, declared ordered positive cut points from cumsum of the primitive cut points with a chosen scale factor (I used 10), also put the first cut point as 0. I think this should resolve the scalability issue.
>
> 2. Add an intercept parameter in the ordered categorical model with cauchy(0, 0.5) prior, given the fact that I fixed the first cut point at 0. Together with the last step, this should resolve the identificability issue.
>
> 3. Constrain the deviation parameter more, that is the sigmaU in the model. You pointed this out in your previous post, and I found out from my diagnosis sigmaU is really the key. When estimations go insane, the sigmaU values always went crazy, can be more than 100. Now, I constrained sigmaU lower as 0.001, upper as 1, and a little more stringent prior as half cauchy(0, 0.5). I believe the current constrain is reasonable if you also agree. I understand we can not put too much constrain on sigmaU, otherwise we will underestimate the random effect. I also have the multivariate normal primitive parameter z in the model that follows normal(0, 1), which is pretty broad. It is sigmaU * (CK * z) together that define the random effect. So I think the whole strategy should be fine theoretically.
>
> 4. Put user-defined initial values instead of the random. I put the optimizing() output from one marker as initial values of all other markers. This significantly decrease the estimating time.
>
> I want to report the above strategies also resolve the problem as reported in another post of mine: Ordered categorical model with known covariance matrix not converging. It was the same model, same data, instead implemented by sampling. MCMC converged well, and results are similar with that of the optimizing method. When tested on simulated data, the peak was clear and correctly positioned in the WAIC plot.
>
> Please let me know if you see anything problematic, and any other things I should do to check the sanity.
>
> Best,
>
> Xulong
>
> --
> 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/d/optout.

Reply all
Reply to author
Forward
0 new messages