Master Quantile Regression with Adaptive Lasso in Python using asgl
This article introduces quantile regression, explains its advantages over ordinary least squares, demonstrates its usefulness for high‑dimensional data, and provides a step‑by‑step Python tutorial using the asgl package with adaptive‑lasso penalization, complete code snippets, and practical tips.
What is Quantile Regression?
Quantile regression models the τ‑th conditional quantile of a response variable Y given predictors X. It solves
\min_{\beta}\;\sum_{i=1}^{n}\rho_{\tau}(y_i - x_i^{\top}\beta)where ρ_τ(u)=τ·u if u≥0 and (τ‑1)·u otherwise. By varying τ∈(0,1) the method provides estimates for any quantile, allowing analysis of the entire conditional distribution rather than only the mean.
Why use Quantile Regression instead of OLS?
Ordinary least squares (OLS) minimizes squared error and therefore estimates the conditional mean. When data contain outliers, heteroscedasticity, or skewed distributions, the mean can be misleading. Quantile regression fits separate models for lower, median, and upper parts of the distribution (e.g., τ=0.1, 0.5, 0.9), revealing how different quantiles respond to covariates.
High‑dimensional settings
In fields such as genomics or image analysis the number of predictors p often exceeds the number of observations n. Standard quantile‑regression tools lack built‑in penalization (Lasso, Group Lasso, etc.) for p≫n, which is required for variable selection and stable estimation.
The asgl Python package
asglprovides a unified, scikit‑learn‑compatible API for fitting linear, quantile, and logistic regression models with a wide range of penalties: Lasso, Ridge, Group Lasso, Sparse Group Lasso, Adaptive Lasso, and their adaptive variants. The implementation works efficiently for both low‑ and high‑dimensional data.
Installation
pip install asglExample: Median (τ=0.5) quantile regression with Adaptive Lasso
The script below generates synthetic data (n=100, p=200, 10 informative features), splits it, fits a penalized quantile regression model, and evaluates mean absolute error.
# Generate synthetic data
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
from asgl import Regressor
X, y = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
noise=0.1,
random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Define and train the quantile regression model with adaptive Lasso
model = Regressor(
model='qr', # quantile regression
penalization='alasso',# adaptive Lasso penalty
quantile=0.5 # median
)
model.fit(X_train, y_train)
# Predict and evaluate
pred = model.predict(X_test)
mae = mean_absolute_error(y_test, pred)
print(f'Mean Absolute Error: {mae:.3f}')Changing the quantile argument to any value in (0, 1) fits the corresponding quantile. The penalization='alasso' option first computes adaptive weights from an initial fit and then applies a weighted L1 penalty, which is effective for variable selection when p≫n.
Key capabilities of asgl
Scalability: Efficient algorithms (coordinate descent and proximal gradient) handle datasets with thousands of predictors.
Flexibility: Supports linear, quantile, and logistic models; multiple penalty families (Lasso, Ridge, Group Lasso, Sparse Group Lasso, Adaptive variants) via a single Regressor class.
Integration with scikit‑learn: The estimator follows the scikit‑learn interface, enabling use in pipelines, cross‑validation, and hyper‑parameter search.
Source code and documentation are available at:
https://github.com/alvaromc317/asgl
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Data Party THU
Official platform of Tsinghua Big Data Research Center, sharing the team's latest research, teaching updates, and big data news.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
