code_junkie

Best way to write a Python function that integrates a gaussian

Nov 14th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. def make_gauss(N, sigma, mu):
  2. return (lambda x: N/(sigma * (2*numpy.pi)**.5) *
  3. numpy.e ** (-(x-mu)**2/(2 * sigma**2)))
  4.  
  5. quad(make_gauss(N=10, sigma=2, mu=0), -inf, inf)
  6.  
  7. quad(gen_gauss, -inf, inf, (10,2,0))
  8.  
  9. def make_gauss(N, sigma, mu):
  10. k = N / (sigma * math.sqrt(2*math.pi))
  11. s = -1.0 / (2 * sigma * sigma)
  12. def f(x):
  13. return k * math.exp(s * (x - mu)*(x - mu))
  14. return f
  15.  
  16. import scipy.special
  17. help(scipy.special.erf)
  18.  
  19. from scipy.integrate import quad
  20. from math import pi, exp
  21. mean = 0
  22. sd = 1
  23. quad(lambda x: 1 / ( sd * ( 2 * pi ) ** 0.5 ) * exp( x ** 2 / (-2 * sd ** 2) ), 0, inf )
Add Comment
Please, Sign In to add comment