Instrumental Variables (2SLS)
Fit an Instrumental Variables regression via Two-Stage Least Squares (2SLS) of dependent_ticker on endogenous_ticker, instrumented by instrument_tickers.
Also known as: IV, 2SLS, IV-2SLS.
For more information about the method, and why plain OLS on an asset suspected of reverse-causality/omitted-confounder bias against another asset is unreliable, see causal_inference_model.get_iv_2sls.
A natural use case in a multi-asset return panel: suppose endogenous_ticker’s return is suspected to be simultaneously determined together with dependent_ticker’s return (e.g. two closely related assets that react to each other intraday, or one asset’s return partly reflects news about the other) – plain OLS of one on the other is then biased. An instrument_tickers asset that moves endogenous_ticker for reasons unrelated to dependent_ticker’s own error term (e.g. a supplier/peer whose moves affect endogenous_ticker but only reach dependent_ticker, if at all, THROUGH endogenous_ticker) allows recovering a cleaner estimate of the causal pass-through.
Instrumental Variables (2SLS) in Python
get_iv_2sls is part of the Econometrics module of the open-source Finance Toolkit. Install it with:
pip install financetoolkit -U
Then call get_iv_2sls as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")
toolkit.econometrics.get_iv_2sls("AAPL", "MSFT", "Benchmark", period="weekly")
Which returns:
| Coefficient | Std. Error | t-Statistic | P-Value | |
|---|---|---|---|---|
| Intercept | 0.0006 | 0.0025 | 0.2336 | 0.8156 |
| MSFT | 1.1453 | 0.0813 | 14.0938 | 0.0000 |
Parameters
get_iv_2sls accepts the following parameters:
- dependent_ticker (str): The dependent (predicted) asset.
- endogenous_ticker (str | list[str]): The endogenous regressor asset(s) – suspected correlated with the error term.
- instrument_tickers (str | list[str]): The excluded instrument asset(s),
correlated with
endogenous_tickerbut assumed uncorrelated with the error term. Must supply at least as many instruments as endogenous regressors. - exogenous_tickers (str | list[str] | None, optional): Other, non-instrumented control asset(s) included as-is in both stages. Defaults to None.
- period (str, optional): The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”.
- column (str, optional): The historical data column to regress on. Defaults to “Return”.
- add_constant (bool, optional): Whether to include an intercept. Defaults to True.
- rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.