Fit an Ordinary Least Squares (OLS) regression of dependent_ticker on independent_tickers, using the selected column (returns by default) of each ticker.

OLS estimates the coefficients that minimise the sum of squared residuals, i.e. the line (or plane) that best explains the dependent ticker from the independent tickers.

The formula is as follows:

\[\text{betâ} = (\text{X'X}) \text{⁻¹X'y},\;\; \text{with y the dependent ticker's column (Return by default) and X the independent tickers' column plus a constant column (add\_constant=True)}\] \[\operatorname{Cov}(\text{betâ}) = \text{sigmâ} ^{2} \cdot (\text{X'X}) \text{⁻¹},\;\; \text{with}\; \text{sigmâ} ^{2} = \text{e'e} / (n - k) (\text{cov\_type} = \text{"nonrobust"}) \text{;} \text{"HC0"} \;\text{to}\; \text{"HC3"},\;\; \text{"cluster" and "HAC" (Newey-West with maxlags) use statsmodels' sandwich estimators}\] \[t = \text{betâⱼ} / \operatorname{SE}(\text{betâⱼ}),\;\; \text{with p-values from a Student-t distribution with n - k degrees of freedom}\] \[R ^{2} = 1 - \text{e'e} / \operatorname{Σ}(\text{yₜ} - \operatorname{mean}(y)) ^{2}\]

Each coefficient is the expected change in dependent_ticker for a one-unit change in that independent ticker, holding the others fixed, and the intercept is what remains when all of them are zero. With returns this is the familiar way to estimate how strongly one asset moves with others (e.g. a beta against an index).

Under the classical assumptions (linearity, no perfect multicollinearity, homoskedastic and uncorrelated errors) OLS is the Best Linear Unbiased Estimator (Gauss-Markov). The coefficients stay unbiased when the errors are heteroskedastic, but the classical standard errors do not: check with get_breusch_pagan_test or get_white_test and, if needed, refit with a robust cov_type such as “HC1” (or “HC3” for small samples), or “HAC” when the errors are also autocorrelated.

For more information about the HAC covariance, see the following paper:

  • Newey, W.K. & West, K.D. (1987). “A Simple, Positive Semi-Definite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix.” Econometrica, 55(3), 703-708.

Also known as: linear regression, least squares regression.

Ordinary Least Squares (OLS) in Python or via the MCP

Install the Finance Toolkit with:

pip install financetoolkit -U

Then call get_ols as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["XOM", "USO"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

# XOM (the first ticker) is dependent; USO and the Benchmark are independent.
# dependent_ticker and independent_tickers choose others, cov_type='HC1' gives
# heteroskedasticity-robust and cov_type='HAC' (with maxlags) Newey-West standard errors.
toolkit.econometrics.get_ols(include_benchmark=True)

Or, after setting up the Finance Toolkit MCP, ask:

How sensitive were ExxonMobil's daily returns to those of the United States Oil Fund (USO) and the S&P 500 from 2021 to 2025, according to an OLS regression?

Which returns:

  Coefficient Std. Error t-Statistic P-Value
Intercept 0.0005 0.0004 1.5047 0.1327
USO 0.4878 0.0172 28.4214 0
Benchmark 0.366 0.0333 10.9991 0

Using Your Own Data

get_ols 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_ols()

Parameters

get_ols accepts the following parameters.

ParameterTypeDescriptionExample
dependent_tickerstr | NoneThe dependent (predicted) asset. Defaults to None, meaning the Toolkit instance’s first ticker."AAPL"
independent_tickersstr | list[str] | NoneThe independent (predictor) asset(s). Defaults to None, meaning every other ticker in the Toolkit instance besides dependent_ticker.["MSFT", "Benchmark"]
include_benchmarkboolWhether to include “Benchmark” in the default independent ticker(s) (has no effect when independent_tickers is given explicitly). Defaults to False.True
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column to regress on. Defaults to “Return”."Return"
add_constantboolWhether to include an intercept. Defaults to True.False
cov_typestrWhich covariance estimator to use for the standard errors – one of “nonrobust” (classical, assumes homoskedastic errors), “HC0”/”HC1”/”HC2”/”HC3” (heteroskedasticity-robust), “cluster” (cluster-robust, requires clusters) or “HAC” (Newey-West, heteroskedasticity-and-autocorrelation-consistent, uses maxlags). Use get_breusch_pagan_test/get_white_test to check for heteroskedasticity and get_ljung_box_test to check for autocorrelation first. Defaults to “nonrobust”."HC3"
clusterspd.Series | NoneThe cluster label for each observation (e.g. a coarser time bucket derived from the return index, to correct for within-period correlation), required when cov_type="cluster". Aligned to the regression’s own index before use, so it may be indexed by the full period index even though the regression drops periods with missing data. Defaults to None.None
maxlagsint | NoneThe maximum lag to include when estimating the HAC (Newey-West) covariance matrix, only used when cov_type="HAC". Defaults to None, meaning the Newey & West (1994) rule of thumb floor(4 * (n / 100)^(2/9)) with n the number of observations.5
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
Share