Search code examples
pythonnumpyprobability

Calculate moments (mean, variance) of a distribution in Python


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:

def mean(x, counts):
    return np.sum(x*counts) / np.sum(counts)

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.


Solution

  • You could use the moment function from SciPy. It calculates the n-th central moment of your data.

    You could also define your own function, which could look something like this:

    def nmoment(x, counts, c, n):
        return np.sum(counts*(x-c)**n) / np.sum(counts)
    

    In that function, c is meant to be the point around which the moment is taken, and n is the order. So to get the variance, you could do nmoment(x, counts, np.average(x, weights=counts), 2).