Quantile Regression
Fit a Quantile Regression of dependent_ticker on independent_tickers at quantile tau.
Where OLS estimates how the average (conditional mean) of the dependent ticker depends on the independent tickers, Quantile Regression estimates how a chosen quantile of it does. It minimises an asymmetric (“pinball”) loss that is smallest at the conditional tau-quantile.
The formula is as follows:
\[\operatorname{betâ}(\text{τ}) = \text{argmin}\; \text{Σ} (\text{τ} \cdot \max(\text{uₜ},\; 0) + (1 - \text{τ}) \cdot \max(- \text{uₜ},\; 0)),\;\; \text{with}\; \text{uₜ} = \text{yₜ} - \text{xₜ'beta} \;\text{and}\; \text{τ} = \text{tau} (0.5 \;\text{by default})\]-
y is the dependent ticker’s
column(Return by default) and X the independent tickers’columnplus a constant (add_constant=True) -
SE(β̂): statsmodels’ kernel sandwich estimator (Epanechnikov kernel, Hall-Sheather bandwidth), or, when
n_bootstrap> 0, the standard deviation (ddof=1) of β̂ overn_bootstrapresampled-pairs fits
With tau = 0.5 this is the median regression, which is far less sensitive to outliers than OLS. Fitting at several quantiles (e.g. 0.05, 0.5 and 0.95) shows how the whole distribution of returns responds to the independent tickers, for example whether an asset’s sensitivity to the market is larger in sell-offs (low quantiles) than in normal periods, which is directly relevant for downside risk.
Comparing the coefficients at a low tau with those at tau=0.5, or with the betas from get_ols, shows whether the sensitivity to the market rises in sell-offs. The table has no p-values: a coefficient more than about two standard errors from zero is significant at the 5% level.
For more information about the method, see the following paper:
- Koenker, R., & Bassett, G. (1978). “Regression Quantiles.” Econometrica, 46(1), 33-50.
Also known as: QR.
Quantile Regression in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_quantile_regression as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["BAC", "KRE"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
# BAC (the first ticker) is dependent; KRE and the Benchmark are independent.
# tau=0.05 describes how the worst 5% of days respond.
toolkit.econometrics.get_quantile_regression(include_benchmark=True, tau=0.05)
Or, after setting up the Finance Toolkit MCP, ask:
How did Bank of America's worst 5% of daily returns respond to those of the SPDR S&P Regional Banking ETF (KRE) and the S&P 500 from 2021 to 2025, according to a quantile regression?
Which returns:
| Coefficient | Std. Error | |
|---|---|---|
| Intercept | -0.0146 | 0.0006 |
| KRE | 0.5271 | 0.0484 |
| Benchmark | 0.3965 | 0.1133 |
Using Your Own Data
get_quantile_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_quantile_regression()
Parameters
get_quantile_regression accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
dependent_ticker | str | None | The dependent (predicted) asset. Defaults to None, meaning the Toolkit instance’s first ticker. | "AAPL" |
independent_tickers | str | list[str] | None | The independent (predictor) asset(s). Defaults to None, meaning every other ticker in the Toolkit instance besides dependent_ticker. | ["MSFT", "Benchmark"] |
include_benchmark | bool | Whether to include “Benchmark” in the default independent ticker(s) (has no effect when independent_tickers is given explicitly). Defaults to False. | True |
tau | float | The quantile to fit, in (0, 1). Defaults to 0.5 (the median). | 0.1 |
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column to regress on. Defaults to “Return”. | "Return" |
add_constant | bool | Whether to include an intercept. Defaults to True. | False |
n_bootstrap | int | The number of bootstrap resamples used for coefficient standard errors, overriding statsmodels’ default analytic (kernel density-based) standard errors. Defaults to 0 (use the analytic standard errors). | 500 |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |