title: Calculate moments (mean, variance) of distribution in python
body: <p>I have two arrays. x is the independent variable, and counts is the number of counts of x occurring, like a histogram. I know I can calculate the mean by defining a function:</p>
  
  <pre><code>def mean(x,counts):
      return np.sum(x*counts) / np.sum(counts)
  </code></pre>
  
  <p>Is there a general function I can use to calculate each moment from the distribution defined by x and counts? I would also like to compute the variance.</p>