Cointegrating Regression
Calculate the cointegrating regression for every ordered pair of tickers: the long-run hedge ratio of one price on another, estimated efficiently and with standard errors that are valid when the prices are cointegrated.
The OLS slope of one cointegrated price on another is consistent, but its standard errors and t-statistics are not valid: the regression errors are autocorrelated and correlated with the shocks of the regressor. Three estimators correct this. Dynamic OLS adds leads and lags of the differenced regressor, Fully Modified OLS corrects the dependent series and the slope with the long-run covariances of the errors and the regressor’s shocks, and the Canonical Cointegrating Regression transforms both series before OLS. Their slope is the hedge ratio of a pairs trade, with a standard error to judge it by.
The formula for Dynamic OLS is as follows:
\[A_{t} = \alpha + \beta \cdot B_{t} + \sum_{j=-p}^{p} \gamma_{j} \cdot \Delta B_{t-j} + e_{t}\] \[\operatorname{cov}\left(\hat{\alpha}, \hat{\beta}\right) = \hat{\omega}^{2} \cdot (X' \cdot X)^{-1}\]fitted by OLS, with A the dependent and B the independent ticker’s column level (Adj Close by default), p = leads_lags, or the p from 0 to floor(T^(1/3)) that minimises the BIC on a shared sample, and hat(omega)^2 the Newey-West long-run variance of the residuals.
The moments shared by Fully Modified OLS and the Canonical Cointegrating Regression are as follows:
\[A_{t} = \alpha + \beta \cdot B_{t} + u_{t} \;\text{by OLS}\;\;\text{and}\;\; w_{t} = (\hat{u}_{t},\; \Delta B_{t})',\;\; t = 2 \ldots T,\;\; n = T - 1\] \[\hat{\Sigma} = \frac{\sum w_{t}w_{t}'}{n},\;\; \hat{\Lambda} = \frac{\sum_{s=1}^{l}\left(1 - \frac{s}{l + 1}\right) \cdot \sum_{t} w_{t-s}w_{t}'}{n},\;\; \text{with}\;\; l = \text{bandwidth}\] \[\hat{\Omega} = \hat{\Sigma} + \hat{\Lambda} + \hat{\Lambda}',\;\; \hat{\Delta} = \hat{\Sigma} + \hat{\Lambda}\;\;\text{and}\;\; \hat{\omega}_{11}._{2} = \hat{\omega}_{11} - \frac{\hat{\omega}_{12}^{2}}{\hat{\omega}_{22}}\]The formula for Fully Modified OLS is as follows:
\[A_{\text{plus},t} = A_{t} - \left(\frac{\hat{\omega}_{1,2}}{\hat{\omega}_{2,2}}\right) \cdot \Delta B_{t}\] \[\hat{\delta}_{\text{plus}} = \hat{\Delta}_{2,1} - \frac{\hat{\Delta}_{2,2} \cdot \hat{\omega}_{2,1}}{\hat{\omega}_{2,2}}\] \[\hat{\theta} = (Z' \cdot Z)^{-1} \cdot (Z' \cdot A_{\text{plus}} - n \cdot e_{2} \cdot \hat{\delta}_{\text{plus}})\] \[\operatorname{cov}\left(\hat{\theta}\right) = \omega_{c} \cdot (Z' \cdot Z)^{-1}\] \[\text{where}\;\; \omega_{c} = \hat{\omega}_{1,1} - \hat{\omega}_{1,2} \cdot \hat{\omega}_{2,2}^{-1} \cdot \hat{\omega}_{2,1}\]with hat(theta) = (hat(alpha), hat(beta))’, \(Z_{t} = (1 , B_{t})\), \(e_{2}\) = (0, 1)’ and hat(omega_{i,j}) and hat(Delta_{i,j}) the elements of the long-run and one-sided long-run covariances.
The formula for the Canonical Cointegrating Regression is as follows:
\[B_{\text{star},t} = B_{t} - G' \cdot w_{t}\] \[\text{where}\;\; G = \hat{\Sigma}^{-1} \cdot \hat{\Gamma}_{2}\] \[A_{\text{star},t} = A_{t} - H' \cdot w_{t}\] \[\text{where}\;\; H = G \cdot \hat{\beta}_{\text{OLS}} + \frac{e_{2} \cdot \hat{\omega}_{1,2}}{\hat{\omega}_{2,2}}\] \[\hat{\theta} = (Z'_{\text{star}} \cdot Z_{\text{star}})^{-1} \cdot Z'_{\text{star}} \cdot A_{\text{star}}\] \[\operatorname{cov}\left(\hat{\theta}\right) = \omega_{c} \cdot (Z'_{\text{star}} \cdot Z_{\text{star}})^{-1}\]with hat(Gamma_2) = (hat(Delta_{2,1}), hat(Delta_{2,2}))’, \(e_{2}\) = (0, 1)’, \(Z_{\text{star} , t} = (1 , B_{\text{star} , t})\) and hat(theta) = (hat(alpha), hat(beta))’, the OLS regression of \(A_{\text{star}}\) on \(Z_{\text{star}}\).
The slope’s p-value is two-sided from the standard normal distribution.
Also known as: DOLS, dynamic OLS, FMOLS, fully modified OLS, CCR, canonical cointegrating regression, long-run hedge ratio, cointegrating vector.
Example · KO, PEP · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_cointegrating_regression:
from financetoolkit import Toolkit
toolkit = Toolkit(
["KO", "PEP"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
regression = toolkit.econometrics.get_cointegrating_regression(method="fully_modified_ols")
regression[["Intercept", "Slope", "Slope Standard Error", "Slope P-Value"]]
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
What is the long-run relation between the share prices of Coca-Cola and PepsiCo from 2021 to 2025, by fully modified OLS, each regressed on the other?
Which returns:
| Item | Intercept | Slope | Slope Standard Error | Slope P-Value |
|---|---|---|---|---|
| ('KO', 'PEP') | 36.1507 | 0.1397 | 0.0691 | 0.0431 |
| ('PEP', 'KO') | 117.5590 | 0.4875 | 0.2437 | 0.0455 |
2 rows
Using Your Own Data
get_cointegrating_regression 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_cointegrating_regression()
Parameters
get_cointegrating_regression accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column to use. Defaults to “Adj Close”. | "Return" |
method | str | The estimator. Options are: “dynamic_ols” (Dynamic OLS), “fully_modified_ols” (Fully Modified OLS), “canonical_cointegrating_regression” (Canonical Cointegrating Regression). Defaults to “dynamic_ols”. | "dynamic_ols" |
leads_lags | int | The number of leads and of lags of the differenced regressor in “dynamic_ols”. Defaults to None, chosen by the BIC. | None, chosen by the BIC |
bandwidth | int | The truncation lag l of the long-run (co)variances. Defaults to the Schwert (1989) rule of thumb, capped at (n - 5) // 3 for short samples. | 0.02 |
include_benchmark | bool | Whether to include “Benchmark” among the tickers paired up. 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 |
References
- Paper Stock, J.H. and Watson, M.W. (1993). “A Simple Estimator of Cointegrating Vectors in Higher Order Integrated Systems.” Econometrica, 61(4), 783-820.
- Paper Phillips, P.C.B. and Hansen, B.E. (1990). “Statistical Inference in Instrumental Variables Regression with I(1) Processes.” Review of Economic Studies, 57(1), 99-125.
- Paper Park, J.Y. (1992). “Canonical Cointegrating Regressions.” Econometrica, 60(1), 119-143.
- Library Referenced against arch’s DynamicOLS (Dynamic OLS)
- Library Referenced against arch’s FullyModifiedOLS (Fully Modified OLS)
- Library Referenced against arch’s CanonicalCointegratingReg (Canonical Cointegrating Regression)