[SciPy-User] speed up a python function using scipy constructs

josef.pktd@gmai... josef.pktd@gmai...
Wed Mar 14 07:20:47 CDT 2012


On Wed, Mar 14, 2012 at 7:51 AM, Ernesto Adorio
<ernesto.adorio@gmail.com> wrote:
> Hi,
>
> The following function is a pure python implmentation of the multinomial
> logistic regression
> log likelihood function.
>
> <pre>
> def negmloglik(Betas, X, Y,  m, reflevel=0):
>     """
>     log likelihood for polytomous regression or mlogit.
>     Betas - estimated coefficients, as a SINGLE array!
>     Y values are coded from 0 to ncategories - 1
>
>     Betas matrix
>             b[0][0] + b[0][1]+ b[0][2]+ ... + b[[0][D-1]
>             b[1][0] + b[1][1]+ b[1][2]+ ... + b[[1][D-1]
>                         ...
>             b[ncategories-1][0] + b[ncategories-1][1]+ b[ncategories-1][2]
>              .... + ... + b[[ncategories - 1][D-1]
>
>             Stored in one array! The beta   coefficients for each level
>             are stored with indices in range(level*D , level *D + D)
>     X,Y   data X matrix and integer response Y vector with values
>             from 0 to maxlevel=ncategories-1
>     m - number of categories in Y vector. each value of  ylevel in Y must be
> in the
>             interval [0, ncategories) or 0 <= ylevel < m
>     reflevel - reference level, default code: 0
>     """
>
>     n  = len(X[0]) # number of coefficients per level.
>     L  = 0
>     for (xrow, ylevel) in zip(X,Y):
>         h   = [0.0] * m
>         denom = 0.0
>         for k in range(m):
>                  if k == reflevel:
>                     denom += 1
>                  else:
>                     sa = k * n
>                     v = sum([(x * b) for (x,b) in zip(xrow, Betas[sa: sa +
> n])])
>                     h[k] = v
>                     denom += exp(v)
>         deltaL = h[ylevel] - log(denom)
>         L += deltaL
>     return -2 * L
> </pre>
>
> I am wondering if there are Scipy/Numpy constructs which can speed up the
> above Python implementation?
> Rewrite if necessary.

Maybe it helps to look at our implementation in statsmodels
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/discrete/discrete_model.py#L1091

I didn't read your loop to see if it is the same.

Cheers,
Josef

>
> Regards,
> Ernesto
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-User mailing list