demo_gaussian.py 999 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright Jim Bosch 2010-2012.
  2. # Distributed under the Boost Software License, Version 1.0.
  3. # (See accompanying file LICENSE_1_0.txt or copy at
  4. # http://www.boost.org/LICENSE_1_0.txt)
  5. import numpy
  6. import gaussian
  7. mu = numpy.zeros(2, dtype=float)
  8. sigma = numpy.identity(2, dtype=float)
  9. sigma[0, 1] = 0.15
  10. sigma[1, 0] = 0.15
  11. g = gaussian.bivariate_gaussian(mu, sigma)
  12. r = numpy.linspace(-40, 40, 1001)
  13. x, y = numpy.meshgrid(r, r)
  14. z = g(x, y)
  15. s = z.sum() * (r[1] - r[0])**2
  16. print "sum (should be ~ 1):", s
  17. xc = (z * x).sum() / z.sum()
  18. print "x centroid (should be ~ %f): %f" % (mu[0], xc)
  19. yc = (z * y).sum() / z.sum()
  20. print "y centroid (should be ~ %f): %f" % (mu[1], yc)
  21. xx = (z * (x - xc)**2).sum() / z.sum()
  22. print "xx moment (should be ~ %f): %f" % (sigma[0,0], xx)
  23. yy = (z * (y - yc)**2).sum() / z.sum()
  24. print "yy moment (should be ~ %f): %f" % (sigma[1,1], yy)
  25. xy = 0.5 * (z * (x - xc) * (y - yc)).sum() / z.sum()
  26. print "xy moment (should be ~ %f): %f" % (sigma[0,1], xy)