Skip to content

_score_nbin optimization #3582

Closed
Closed

Description

@jbrockmendel

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.

Activity

  1. josef-pkt commented on Mar 27, 2017

    @josef-pkt
    Member

    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.

  2. josef-pkt commented on Mar 27, 2017

    @josef-pkt
    Member

    _score_nbin does a lot of duplicate computation, which is expensive if digamma is expensive

    i.e. terms like special.digamma(y + mu/alpha) should only be computed once by using temp variables.

            if Q: # nb1
                dparams = exog*mu/alpha*(np.log(1/(alpha + 1)) +
                           special.digamma(y + mu/alpha) -
                           special.digamma(mu/alpha))
                dalpha = ((alpha*(y - mu*np.log(1/(alpha + 1)) -
                                  mu*(special.digamma(y + mu/alpha) -
                                  special.digamma(mu/alpha) + 1)) -
                           mu*(np.log(1/(alpha + 1)) +
                               special.digamma(y + mu/alpha) -
                               special.digamma(mu/alpha)))/
                           (alpha**2*(alpha + 1))).sum()
    
  3. josef-pkt commented on Mar 27, 2017

    @josef-pkt
    Member

    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

        def score_obs(self, params):
            sc = approx_fprime_cs(params, self.loglikeobs)
            return sc
    

    score_obs(params).sum(0) should be the same as score(params) at at least 1e-13 precision (I guess)

  4. jbrockmendel commented on Mar 27, 2017

    @jbrockmendel
    ContributorAuthor

    _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.

  5. jbrockmendel commented on Mar 27, 2017

    @jbrockmendel
    ContributorAuthor

    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.

  6. josef-pkt commented on Mar 27, 2017

    @josef-pkt
    Member

    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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions