[SciPy-User] speed of logpdf functions in scipy.stats
Brian Blais
bblais at gmail.com
Fri Mar 27 19:28:05 EDT 2015
Hello,
I was playing with some MCMC methods which require logpdf for
different distributions, and thought "hey! I can use the
scipy.stats.distributions!". Then, when I tested them, they seemed
slow. Upon comparison, I noticed a huge speed difference between
these functions and my own not-so-cleverly written python only
functions. Is there a way to get better performance? Am I doing
something silly here, and creating unneeded objects somewhere? The
simplest code which shows the issue is below.
thanks,
bb
--
-----------------
bblais at gmail.com
http://web.bryant.edu/~bblais
import numpy as np
from scipy.stats import distributions as D
def lognormalpdf(x,mn,sig):
# 1/sqrt(2*pi*sigma^2)*exp(-x^2/2/sigma^2)
return -0.5*log(2*np.pi*sig**2)- (x-mn)**2/sig**2/2.0
x=np.random.rand(5)
print lognormalpdf(x,0,1)
print D.norm.logpdf(x,0,1)
x=np.random.rand(15000)
%timeit y=lognormalpdf(x,0,1)
# 10000 loops, best of 3: 66.9 µs per loop
%timeit y=D.norm.logpdf(x,0,1)
# 1000 loops, best of 3: 727 µs per loop
More information about the SciPy-User
mailing list