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")
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency for returns (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
within_period | bool | Whether to calculate the test within the specified period or for the entire period. Thus whether to look at the test within a specific year (if period = ‘yearly’) or look at the entirety of all years. Defaults to False. Note that this requires intraday data when period = ‘daily’, since a single day only nests observations when intraday data was fetched. | True |
lags | int | The number of lags to test for autocorrelation up to. Defaults to 10. | 20 |
include_benchmark | bool | Whether to include “Benchmark” among the assets tested. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
weighted | bool | Whether to calculate the weighted test of Fisher and Gallagher (2012). Defaults to False. | False |
method | str | The statistic. Options are: “ljung_box” (Ljung-Box Test), “box_pierce” (Box-Pierce Test). Defaults to “ljung_box”. | "ljung_box" |
Which returns:
| Item | WMT | COST | TGT |
|---|---|---|---|
| Box-Pierce Statistic | 12.8624 | 11.3191 | 11.9286 |
| P-Value | 0.2315 | 0.3332 | 0.2899 |
2 rows
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)