Repository navigation
Description
This user is getting np.nan for the prsquared in MNLogit. It looks like the problem is in the mod_null model with only a constant regressor. In the call to fit, the third vector of params it evaluates is array([ -7.61152072e+09, -1.14406917e+09, 5.60530684e+07, 6.72625670e+07, 1.12107442e+07, 2.24214857e+07, 1.12107442e+07]).
Paraphrasing loglike here:
Xb = np.dot(self.exog, params)
prob = self.cdf(Xb)
logprob = np.log(prob)
return np.sum(self.wendog * logprob)
self.exog here is just a vector of ones, so we take evaluate cdf on the params:
eXB = np.column_stack((np.ones(len(Xb)), np.exp(Xb)))
cdf = eXb/eXb.sum(1)[:, None]
With this particular vector of params, np.exp(Xb) is array([ 0., 0., inf, inf, inf, inf, inf]), so we get a np.inf/np.inf and cdf returns array([[ 0., 0., nan, nan, nan, nan, nan]])
In this particular example, one of the endog outcomes is overwhelmingly more likely than the others. Aside from numerical issues which are addressable-albeit-ugly, is this class of model going to have robustness issues with highly imbalanced outcomes?
Before working around the overflow, I want to double-check a difference between the univariate and multivariate code. Logit.loglike and Probit.loglike are essentially identical:
q = 2*self.endog - 1
X = self.exog
return np.sum(np.log(self.cdf(q*np.dot(X, params))))
But MNLogit leaves out the q. Is the sign baked in to the MNLogit penalty somewhere else?
Activity
josef-pkt commentedon May 2, 2017
(without having looked at the details for this case)
e.g. maybe logcdf(...) would be more stable that log(cdf(...)).
Also, cdf has to be bound in interval [0, 1] and inf is out of bounds.
jbrockmendel commentedon May 2, 2017
I'm going to hold off on worrying about the overflow here until I understand the difference that makes the
qunnecessary here.In the mean-time to thank you (and Chad) for putting up with me, I'm putting together a PR that gets rid of the for-loop in
MNLogit.hessian.josef-pkt commentedon Sep 22, 2017
maybe related: llnull problems fixed for basic count models in #3921 when there are convergence problems
np.sum(np.log(self.cdf(q*np.dot(X, params))))the
qhere uses a special trick for binary models (I had to recently look at it). I think that does not work for MNLogit because of more than two choices.jbrockmendel commentedon Sep 22, 2017
Not sure if the llnull is related, but I'll take a look. FYI I've gotten the overflow issues fixed, but haven't gotten a good fix for underflows.
I've been holding off on pushing these and related changes upstream until the backlog of PRs gets resolved -- don't want to inundante you. Let me know if you'd prefer to plow ahead on the MNLogit area.
josef-pkt commentedon Sep 22, 2017
I might put MNLogit soon on my high priority list, given that I have been back in
discretefor some time.related #3923 for Logit if it's mainly an overflow issue
josef-pkt commentedon Sep 22, 2017
AFAICS we can rewrite the MNLogit cdf to avoid evaluating exp(xb)
also given that the main use in loglike is for logcdf we should define it as a separate method (same for Logit). Better cdf is still needed in score.
Note, specific bug fixes with unit tests are much easier to review and merge than large refactorings and cleanups.
josef-pkt commentedon Sep 22, 2017
AFAICS, underflow needs clipping to cdf > 0, (or the use of 0log0 = 0)
jbrockmendel commentedon Sep 23, 2017
The problem I'm referring to is floats that analytically are strictly positive but are rounded to zero by
np.float64. These generally show up when the outcome classes are sufficiently unbalanced, but that's a reasonably common use case that is worth getting right.Even (especially) if there is perfect separation, many users may be interested in prediction without identification. In that case, there may be better options than raising.
I'll take a look at my outstanding PRs and see if any of them can be split into smaller chunks. Even if its a small amount, it'd be nice to maintain forward momentum.
josef-pkt commentedon Sep 23, 2017
Do you have an example or test case for this?
I assume that it is related to the overflow that has some choice probabilities at essentially 1 at floating point precision, which forces the other the be numerically zero, even if they would be e-xx where xx is very large. The smallest number in double precision 1e-xx > 0 is small enough that it doesn't round down by itself for any real live sample, I guess.
I think this is computationally the same problem as perfect prediction or partial (perfect) separation
I completely agree here. I added an option to turn perfect separation into a warning instead of an exception in binary models exactly for this reason. We don't have perfect separation detection for MNLogit yet.
jbrockmendel commentedon Sep 24, 2017
The mailing list message that started this thread includes an excel doc Sample.xlsx
So this is pretty imbalanced. Groups 2 and 3 might be OK numerically, but we're going to have trouble by the time we get to 6,7,9.
So its FU, but not necessarily BAR. We may be able to salvage some information if, say,
eXb[0, 1 ] / eXb[1, 1]is (analytically) finite.The approach I've been taking is to subtract
Xb.max(1)before exponentiating. There are some corner cases I'll ignore for now.So we're still going to have problems with calculating the Hessian, but we're in a much better position than we were with
inf/inf.Gotta run, will follow up later.
jbrockmendel commentedon Sep 25, 2017
Looks like a lot of this actually got discussed in #3778. The upshot being that this normalization gets rid of the overflow, but the zeros here are the result of an underflow.
The zeros will still lead to
nans coming out inloglike(orloglikeobs), see discussion in #3778 for a workaround.But for
scoreandhessian, I haven't found a good way to get anything meaningful. In some cases we can get higher-precision floats, but that comes with its own set of problems.jbrockmendel commentedon Sep 25, 2017
The workaround I've been poking at is to combine the smaller groups and iterate on conditional probabilities. In this case:
So we've at least got something meaningful. But this has its own set of problems, including the fact that the next iteration still diverges.
josef-pkt commentedon Sep 25, 2017
A suggestion for a MNLogit bug-fix PR that should be relatively simple to merge, based on this, #3778. and related issues like 3924 for corner cases in discrete in general
(if we can identify perfect separation case in MNLogit, then we could warn or raise similar to Logit, but I don't know anything specific about MNLogit, and when parameters are not identified in that model.)
Given that I'm now mostly familiar with the issues, reviewing and helping should now be relatively easy for me.
warning: getting unit tests to run across versions for corner or "not nice" cases can get tedious (in my recent experience)
jbrockmendel commentedon Sep 25, 2017
These are good ideas. One more to add before I call it a day; we can discuss strategy tomorrow.
This needs double-checking, but I think in the following procedure can check for perfect separation. Sticking with the earlier example:
I had expected that
spatial.ConvexHull(cloud)would have ais_in_hullmethod or something, but the internet pointed me towardsDelaunay. A point is in the convex hull ofcloudiffhull.find_simplex(pt) >= 0(well actually, the post only gives one direction of theiff. I need to look at the docs to be sure)It turns out that
outsideis all False in this case, which suggests that no group is perfectly separated from group 1. It may still be that group2 is perfectly separated from group3, or ...The
Delaunaycall is pretty quick in this ~700 point example. I haven't looked at how it scales. I imagine that there are near-optimality algorithms using the convex hulls of random subsets.josef-pkt commentedon Sep 25, 2017
If there is a good, but computationally more complex check, then we might want to put it in post-estimation results to make it available to users, and eg. warn in summary instead of repeatedly computing it during optimization.
(Using ConvexHull or Delauay for boundary problems sounds interesting and might be also potentially useful for compositional (interior of simplex) data which has a very similar structure as MNLogit, except continuous.)