MOMENT

This function computes the central moment of specified integer order for numeric data.

For observations x_1, \ldots, x_n with mean \bar{x}, the order-k central moment is:

m_k = \frac{1}{n}\sum_{i=1}^{n}(x_i-\bar{x})^k

Lower moments describe key distribution shape features: order 2 relates to spread, order 3 to asymmetry, and order 4 to tail behavior.

Excel Usage

=MOMENT(data, order)
  • data (list[list], required): 2D array of numeric values. Non-numeric values are ignored.
  • order (int, required): Order of the moment to calculate (must be a positive integer).

Returns (float): The nth moment about the mean, or error message (str) if input is invalid.

Example 1: Second moment (variance) of a column vector

Inputs:

data order
1 2
2
3
4
5

Excel formula:

=MOMENT({1;2;3;4;5}, 2)

Expected output:

2

Example 2: Third moment (skewness) of a row vector

Inputs:

data order
1 2 3 4 5 3

Excel formula:

=MOMENT({1,2,3,4,5}, 3)

Expected output:

0

Example 3: Fourth moment (kurtosis) of a matrix

Inputs:

data order
1 2 3 4
4 5 6

Excel formula:

=MOMENT({1,2,3;4,5,6}, 4)

Expected output:

14.7292

Example 4: First moment of a column vector (always zero)

Inputs:

data order
10 1
20
30

Excel formula:

=MOMENT({10;20;30}, 1)

Expected output:

0

Python Code

from scipy.stats import moment as scipy_moment
import math

def moment(data, order):
    """
    Calculates the nth moment about the mean for a sample.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.moment.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): 2D array of numeric values. Non-numeric values are ignored.
        order (int): Order of the moment to calculate (must be a positive integer).

    Returns:
        float: The nth moment about the mean, or error message (str) if input is invalid.
    """
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    try:
      data = to2d(data)

      if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
        return "Error: Invalid input: data must be a 2D list."

      flat = []
      for row in data:
        for x in row:
          try:
            val = float(x)
            if math.isfinite(val):
              flat.append(val)
          except (TypeError, ValueError):
            continue

      if len(flat) < 2:
        return "Error: Invalid input: data must contain at least two numeric values."

      if not isinstance(order, (int, float)) or int(order) != order or order < 1:
        return "Error: Invalid input: order must be a positive integer."

      result = scipy_moment(flat, order=int(order), axis=None, nan_policy='omit')
      return float(result)
    except Exception as e:
      return f"Error: {str(e)}"

Online Calculator

2D array of numeric values. Non-numeric values are ignored.
Order of the moment to calculate (must be a positive integer).