Box-Pierce Test

The Box-Pierce Test (Box and Pierce, 1970) is the original portmanteau test, introduced to check the residuals of ARIMA models: if the model has captured the dependence, the residual autocorrelations are small and their squared sum behaves like a chi-squared variable.

The formula for the Box-Pierce Test starts from the sample autocorrelation \(\rho_{k}\) of the n returns \(r_{t}\) at lag k, the covariance of each return with the one k periods earlier divided by the variance:

\[\rho_{k} = \frac{\sum_{t=k + 1}^{n} \left(r_{t} - \operatorname{mean}\left(r\right)\right) \cdot \left(r_{t-k} - \operatorname{mean}\left(r\right)\right)}{\sum_{t=1}^{n} \left(r_{t} - \operatorname{mean}\left(r\right)\right)^{2}}\]

The statistic \(Q_{\text{BP}}\) is n times the sum of the squared autocorrelations up to lag h:

\[Q_{\text{BP}} = n \cdot \sum_{k=1}^{h} \rho_{k}^{2}\]

Under the null hypothesis \(Q_{\text{BP}}\) is chi-squared with h degrees of freedom. The weighted version of Fisher and Gallagher (2012) gives lag k the weight (h - k + 1) / h, so the first lags count most:

\[Q_{\text{BP},W} = n \cdot \sum_{k=1}^{h} \left(\frac{h - k + 1}{h}\right) \cdot \rho_{k}^{2}\]

Its p-value comes from the Gamma distribution with the mean and the variance of \(Q_{\text{BP} , W}\), whose shape and scale are:

\[\text{Shape} = \frac{3 \cdot (h + 1)^{2} \cdot h}{4 \cdot \left(2 \cdot h^{2} + 3 \cdot h + 1\right)}\] \[\text{Scale} = \frac{2 \cdot \left(2 \cdot h^{2} + 3 \cdot h + 1\right)}{3 \cdot h \cdot (h + 1)}\]

A large \(Q_{\text{BP}}\), with a p-value below the significance level, means the returns are autocorrelated. In short samples the statistic is smaller than its chi-squared distribution assumes, so the test rejects too rarely; the Ljung-Box Test corrects that and gives nearly the same value in long samples.

A portmanteau test asks whether a series is autocorrelated at any of the first h lags at once: the null hypothesis is that every autocorrelation up to lag h is zero, the alternative that at least one is not. Testing the lags one by one would find a significant one by chance among many; a portmanteau test sums the squared autocorrelations into one statistic instead. It serves both as a test of predictability (whether past returns say anything about the next one, as they would not for a random walk) and as a check of a fitted model, whose residuals should no longer be autocorrelated. A p-value below the chosen significance level (such as 0.05) rejects the absence of autocorrelation; a higher p-value means no autocorrelation is found up to that lag.

Also known as: Box-Pierce Q test, portmanteau test, weighted Box-Pierce test.

In the Finance Toolkit this is get_ljung_box_test with method="box_pierce", the same function as Ljung-Box Test.

Example · WMT, COST, TGT · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_ljung_box_test:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["WMT", "COST", "TGT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_ljung_box_test(method="box_pierce")

Which returns:

Using Your Own Data

get_ljung_box_test runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_ljung_box_test()

References

  • Paper Box, G.E.P. and Pierce, D.A. (1970). “Distribution of Residual Autocorrelations in Autoregressive-Integrated Moving Average Time Series Models.” Journal of the American Statistical Association, 65(332), 1509-1526.
  • Paper Fisher, T.J. and Gallagher, C.M. (2012). “New Weighted Portmanteau Statistics for Time Series Goodness of Fit Testing.” Journal of the American Statistical Association, 107(498), 777-787.
  • Library Referenced against statsmodels’ acorr_ljungbox (Ljung-Box Test, Box-Pierce Test)
Share