Ordinary Least Squares (OLS)
Fit an Ordinary Least Squares (OLS) regression of dependent_ticker on independent_tickers.
Also known as: linear regression, least squares regression.
For more information about the method, see regression_model.get_ols.
Ordinary Least Squares (OLS) in Python
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_ols as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(["AMZN", "TSLA"], api_key="FINANCIAL_MODELING_PREP_KEY")
# AMZN (the first ticker) is dependent; TSLA is independent
toolkit.econometrics.get_ols(period="quarterly")
# Or, override which ticker is dependent/independent, and pull in Benchmark too
toolkit.econometrics.get_ols(
dependent_ticker="AMZN",
independent_tickers=["TSLA", "Benchmark"],
period="quarterly",
cov_type="HC1",
)
# Or, simply opt every default independent ticker (here, just TSLA) into
# including Benchmark too:
toolkit.econometrics.get_ols(period="quarterly", include_benchmark=True)
# Or, use Newey-West (HAC) standard errors for time-series regressions
# where errors may be both heteroskedastic and autocorrelated:
toolkit.econometrics.get_ols(period="quarterly", cov_type="HAC", maxlags=4)
Which returns:
(for the first call, AMZN regressed on TSLA)
| Coefficient | Std. Error | t-Statistic | P-Value | |
|---|---|---|---|---|
| Intercept | 0.0134 | 0.026 | 0.5143 | 0.6119 |
| TSLA | 0.2479 | 0.0817 | 3.0331 | 0.0059 |
Parameters
The function accepts the following parameters:
- dependent_ticker (str | None, optional): The dependent (predicted) asset. Defaults to None, meaning the Toolkit instance’s first ticker.
- independent_tickers (str | list[str] | None, optional): The independent
(predictor) asset(s). Defaults to None, meaning every other ticker in
the Toolkit instance besides
dependent_ticker. - include_benchmark (bool, optional): Whether to include “Benchmark” in the default independent ticker(s) (has no effect when independent_tickers is given explicitly). Defaults to False.
- 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.
- cov_type (str, optional): Which 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, requiresmaxlags). Useget_breusch_pagan_test/get_white_testto check for heteroskedasticity andget_ljung_box_testto check for autocorrelation first. Defaults to “nonrobust”. - clusters (pd.Series | None, optional): The 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. - maxlags (int | None, optional): The maximum lag to include when estimating the
HAC (Newey-West) covariance matrix, required when
cov_type="HAC". A common rule of thumb isfloor(4 * (n / 100)^(2/9))(Newey & West, 1994). Defaults to None. - rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.