You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running the tests locally, I see discrete_model.NegativeBinomial._score_nbin stands out as a hotspot. It is taking about 28 seconds, almost 7% of the runtime. More surprising is that the profiler is attributing almost all of that to _score_nbin itself and not to calls made from within the function. For the moment I am assuming that the cost is actually in the calls to scipy.special.digamma and that the reporting is misleading.
It turns out that there is a cython implementation of digamma reachable via cimport scipy.special.cython_special.psi as digamma. Supposedly. I haven't been able to get it to work, but someone with stronger cython-fu might have better luck.
cython is only really useful if we have expensive python loops. I assume in this case using cython would not improve much since a vectorized digamma and the cython digamma would take about the same time, i.e. the loop is already in C from using the special function.
special.digamma and special.psi are the same function IIUC.
_score_nbin does a lot of duplicate computation, which is expensive if digamma is expensive
You're right, so there's some low-hanging fruit there. It turns out I had already made that change in the branch that produced those profiling results, so it may still warrant looking at.
I just saw that score_obs wasn't rewritten to use the analytical formula
Good catch. I don't see that showing up in the >1s portion of the profile results.
special.digamma and special.psi are the same function IIUC.
Yes. The only reason this observation might have been useful is because special.cython_special doesn't export the "digamma" alias, so it might be easy to overlook. Sounds like it's worth de-duplicating the calls to digamma, but otherwise this is a non-issue until someone starts using this heavily.
I don't see that showing up in the >1s portion of the profile results.
score_obs might not be used much in the unit tests. It is only used for sandwich cov_types for now (AFAIR) and not every model is tested, given that it's mostly generic code (It looks like neg bin is tested for cluster robust).
Activity
josef-pkt commentedon Mar 27, 2017
In general, without having looked at the detail.
cython is only really useful if we have expensive python loops. I assume in this case using cython would not improve much since a vectorized digamma and the cython digamma would take about the same time, i.e. the loop is already in C from using the special function.
special.digamma and special.psi are the same function IIUC.
josef-pkt commentedon Mar 27, 2017
_score_nbindoes a lot of duplicate computation, which is expensive if digamma is expensivei.e. terms like
special.digamma(y + mu/alpha)should only be computed once by using temp variables.josef-pkt commentedon Mar 27, 2017
for benchmarking: a comparison with complex step derivative would be interesting.
I just saw that score_obs wasn't rewritten to use the analytical formula
score_obs(params).sum(0)should be the same asscore(params)at at least 1e-13 precision (I guess)jbrockmendel commentedon Mar 27, 2017
You're right, so there's some low-hanging fruit there. It turns out I had already made that change in the branch that produced those profiling results, so it may still warrant looking at.
Good catch. I don't see that showing up in the >1s portion of the profile results.
jbrockmendel commentedon Mar 27, 2017
Yes. The only reason this observation might have been useful is because special.cython_special doesn't export the "digamma" alias, so it might be easy to overlook. Sounds like it's worth de-duplicating the calls to digamma, but otherwise this is a non-issue until someone starts using this heavily.
josef-pkt commentedon Mar 27, 2017
score_obsmight not be used much in the unit tests. It is only used for sandwich cov_types for now (AFAIR) and not every model is tested, given that it's mostly generic code (It looks like neg bin is tested for cluster robust).