Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for scipy.stats.norm.pdf?

Does anyone know of an alternative for scipy.stats.norm.pdf()? I'm hosting my python site on Google App Engine and Google doesn't support SciPy.

I've tried this function, but that didn't return the same results as scipy:

def normpdf(x, mu, sigma):
    u = (x-mu)/abs(sigma)
    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)
    return y

For example:

print scipy.stats.norm.pdf(20, 20, 10)
print normpdf(20, 20, 10)

print scipy.stats.norm.pdf(15, 20, 10)
print normpdf(15, 20, 10)

print scipy.stats.norm.pdf(10, 20, 10)
print normpdf(10, 20, 10)

Returns these values:

0.0398942280401
0.0398942280401

0.0352065326764
0.0146762663174

0.0241970724519
0.0146762663174
like image 461
Leon Avatar asked Dec 29 '11 14:12

Leon


1 Answers

You got tricked by pythons integer division arithmetics! Here is some working code:

from __future__ import division

import scipy.stats
from numpy import *

def normpdf(x, mu, sigma):
    u = (x-mu)/abs(sigma)
    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)
    return y


print scipy.stats.norm.pdf(20, 20, 10)
print normpdf(20, 20, 10)

print scipy.stats.norm.pdf(15, 20, 10)
print normpdf(15, 20, 10)

print scipy.stats.norm.pdf(10, 20, 10)
print normpdf(10, 20, 10)

Note the first line! Otherwise, you could convert each input variable to a float, e.g. by multiplying by 1.

like image 132
David Zwicker Avatar answered Sep 27 '22 16:09

David Zwicker