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

Ernesto Adorio ernesto.adorio@gmail....
Wed Mar 14 06:51:06 CDT 2012


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.

Regards,
Ernesto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120314/ae9a9052/attachment.html 


More information about the SciPy-User mailing list