Alternative for scipy.stats.norm.pdf?

Does anyone know of an alternative for scipy.stats.norm.pdf ()? I host my python site on Google App Engine and Google does not support SciPy.

I tried this function, but it did not 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 instance:

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
+5
source share
2 answers

You tricked the arithmetic of integer division of pythons! Here is the 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)

Pay attention to the first line! Otherwise, you can convert each input variable to float, for example. by multiplying by1.

+9
source

2 exp , u int. , , u float, :

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

( mu sigma, , )

+6

All Articles