Craig Jolley
07-03-2012, 01:06 AM
Hi...
I'd noticed a problem with my code where sometimes root finding newt() would just stall without ever converging. After spending some time poking around in the NR routines, I found that the offending piece of code was the main loop in lnsrch(), which never terminated because alam had taken on a value of nan. This happened because the gradient being passed to lnsrch() by newt() had some very large components, which led to a value of -inf for the slope, by which point alam didn't have a chance of taking on a reasonable value.
FWIW, my code was examining the behavior of a model with randomly-chosen parameters; it isn't too surprising that some of them will be hopelessly pathological, and I would expect the root finding to fail in this case. To make that failure a little more graceful, I added the following in the loop:
if (alam != alam) {
throw("Pathological value of alam in lnsrch.");
}
This is a bit of a kluge (putting a sanity check further upstream would be better) but it seems to work for now. I'm mostly posting this in case anyone else runs into the same problem.
I'd noticed a problem with my code where sometimes root finding newt() would just stall without ever converging. After spending some time poking around in the NR routines, I found that the offending piece of code was the main loop in lnsrch(), which never terminated because alam had taken on a value of nan. This happened because the gradient being passed to lnsrch() by newt() had some very large components, which led to a value of -inf for the slope, by which point alam didn't have a chance of taking on a reasonable value.
FWIW, my code was examining the behavior of a model with randomly-chosen parameters; it isn't too surprising that some of them will be hopelessly pathological, and I would expect the root finding to fail in this case. To make that failure a little more graceful, I added the following in the loop:
if (alam != alam) {
throw("Pathological value of alam in lnsrch.");
}
This is a bit of a kluge (putting a sanity check further upstream would be better) but it seems to work for now. I'm mostly posting this in case anyone else runs into the same problem.