Q_STAT
This function computes the Ljung-Box portmanteau statistic from a sequence of autocorrelation estimates.
The statistic aggregates squared autocorrelations across lags to test whether serial correlation remains in a process:
Q = n(n+2) \sum_{k=1}^{m} \frac{\rho_k^2}{n-k}
It returns the cumulative Q-statistic and associated p-value at each lag.
Excel Usage
=Q_STAT(x, nobs)
x(list[list], required): Autocorrelation coefficients as a 2D range, typically excluding lag zero.nobs(int, required): Total number of observations in the underlying sample.
Returns (list[list]): 2D table with columns lag, q_stat, and p_value.
Example 1: Ljung-Box statistics from short ACF sequence
Inputs:
| x | nobs | ||
|---|---|---|---|
| 0.2 | 0.1 | 0.05 | 60 |
Excel formula:
=Q_STAT({0.2,0.1,0.05}, 60)
Expected output:
| Result | ||
|---|---|---|
| 1 | 2.52203 | 0.112266 |
| 2 | 3.16341 | 0.205624 |
| 3 | 3.32657 | 0.343962 |
Example 2: Ljung-Box statistics from moderate ACF sequence
Inputs:
| x | nobs | |||
|---|---|---|---|---|
| 0.35 | 0.22 | 0.11 | 0.04 | 120 |
Excel formula:
=Q_STAT({0.35,0.22,0.11,0.04}, 120)
Expected output:
| Result | ||
|---|---|---|
| 1 | 15.0706 | 0.000103564 |
| 2 | 21.0755 | 0.0000265167 |
| 3 | 22.5895 | 0.0000491731 |
| 4 | 22.7915 | 0.000139371 |
Example 3: Ljung-Box statistics with alternating autocorrelation signs
Inputs:
| x | nobs | |||
|---|---|---|---|---|
| 0.25 | -0.15 | 0.1 | -0.05 | 80 |
Excel formula:
=Q_STAT({0.25,-0.15,0.1,-0.05}, 80)
Expected output:
| Result | ||
|---|---|---|
| 1 | 5.18987 | 0.0227189 |
| 2 | 7.08218 | 0.0289817 |
| 3 | 7.93413 | 0.0473929 |
| 4 | 8.14992 | 0.0862383 |
Example 4: Ljung-Box statistics from five-lag autocorrelation input
Inputs:
| x | nobs | ||||
|---|---|---|---|---|---|
| 0.18 | 0.12 | 0.09 | 0.04 | 0.02 | 150 |
Excel formula:
=Q_STAT({0.18,0.12,0.09,0.04,0.02}, 150)
Expected output:
| Result | ||
|---|---|---|
| 1 | 4.95785 | 0.0259724 |
| 2 | 7.17623 | 0.0276504 |
| 3 | 8.43256 | 0.0378689 |
| 4 | 8.68242 | 0.0695466 |
| 5 | 8.74532 | 0.119664 |
Python Code
import numpy as np
from statsmodels.tsa.stattools import q_stat as sm_q_stat
def q_stat(x, nobs):
"""
Compute Ljung-Box Q statistics and p-values from autocorrelation coefficients.
See: https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.q_stat.html
This example function is provided as-is without any representation of accuracy.
Args:
x (list[list]): Autocorrelation coefficients as a 2D range, typically excluding lag zero.
nobs (int): Total number of observations in the underlying sample.
Returns:
list[list]: 2D table with columns lag, q_stat, and p_value.
"""
try:
def to1d(values):
if isinstance(values, list):
if all(isinstance(row, list) for row in values):
raw = [item for row in values for item in row]
else:
raw = values
else:
raw = [values]
out = []
for item in raw:
try:
out.append(float(item))
except (TypeError, ValueError):
continue
return out
if nobs <= 1:
return "Error: nobs must be greater than 1"
acf_vals = to1d(x)
if len(acf_vals) == 0:
return "Error: x must contain at least one numeric autocorrelation value"
q_vals, p_vals = sm_q_stat(np.asarray(acf_vals, dtype=float), nobs=nobs)
q_arr = np.asarray(q_vals, dtype=float)
p_arr = np.asarray(p_vals, dtype=float)
return [[lag + 1, float(q_arr[lag]), float(p_arr[lag])] for lag in range(len(q_arr))]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Autocorrelation coefficients as a 2D range, typically excluding lag zero.
Total number of observations in the underlying sample.