Skip to content

Point models: ridge, nearest neighbours, extra trees, linear quantile regression

pyconformal's own fast versions of the point models its methods use, so that no method needs scikit-learn or statsmodels for its defaults.

Use it when A method needs a point model or a linear quantile regression (HQRWACI uses all four), or you want these without scikit-learn or statsmodels.
Guarantee None by themselves: they are point models. The conformal methods built on them carry the guarantees.
In the library pyconformal.point_models: Ridge, KNeighbors, ExtraTrees, linear_quantile_regression
Example needs pyconformal with numba (pip install "pyconformal[fast]")

What each one is

  • Ridge(alpha=1.0): ridge regression with an unpenalised intercept, solved by Cholesky on centred data. The same predictions as scikit-learn's Ridge (to about 1e-14).
  • KNeighbors(n_neighbors=10): the mean outcome of the k nearest training rows by Euclidean distance. One matrix product per block of rows, then a compiled pass that keeps the k smallest. The same predictions as scikit-learn's KNeighborsRegressor.
  • ExtraTrees(n_estimators=100, max_depth=None): extremely randomised trees (Geurts, Ernst and Wehenkel 2006): at each node one uniform random threshold per feature and the best of them by squared-error reduction, no bootstrap; grown and evaluated in parallel. The method is scikit-learn's ExtraTreesRegressor, but the random draws are pyconformal's own, so the trees are not the same trees.
  • linear_quantile_regression(Z, y, quantiles): linear quantile regression by the iteratively reweighted least squares of statsmodels' QuantReg (same start, weights and stopping rule), compiled and run in parallel over levels. Coefficients agree with statsmodels' to the iteration's tolerance (1e-9 to 1e-7 in the benchmark below). Without numba it falls back to a NumPy version that repeats statsmodels' arithmetic exactly.

Minimal example

import numpy as np
from pyconformal.point_models import ExtraTrees, KNeighbors, Ridge, linear_quantile_regression

rng = np.random.default_rng(0)
X = rng.normal(size=(1000, 8))
y = X[:, 0] - 2 * X[:, 1] + np.sin(3 * X[:, 2]) + rng.normal(0, 0.3 + 0.3 * np.abs(X[:, 3]), 1000)
X_train, y_train, X_test, y_test = X[:800], y[:800], X[800:], y[800:]

for name, model in [("ridge", Ridge(alpha=1.0)), ("nearest neighbours", KNeighbors(n_neighbors=10)),
                    ("extra trees", ExtraTrees(n_estimators=100, max_depth=8, random_state=0))]:
    pred = model.fit(X_train, y_train).predict(X_test)
    print(f"{name:18s} RMSE {np.sqrt(np.mean((pred - y_test) ** 2)):.3f}")

Z = np.column_stack([np.ones(800), X_train[:, :4]])                 # a column of ones for the intercept
coef = linear_quantile_regression(Z, y_train, [0.05, 0.5, 0.95])     # one row of coefficients per level
print(coef.shape)                                                    # (3, 5)

Speed

Fit and predict (400 test rows), best of several runs, against scikit-learn 1.9 and statsmodels 0.15 on 2 cores (benchmarks/speed_point_models.py, output in benchmarks/output_speed_point_models.txt). "Difference" is the largest difference in the predictions or coefficients.

Model Rows x features pyconformal Reference Speed-up Difference
Ridge vs Ridge 600 x 12 0.08 ms 0.76 ms 9.2x 3e-15
2500 x 24 0.31 ms 1.00 ms 3.2x 4e-15
3500 x 48 1.21 ms 1.73 ms 1.4x 1e-14
KNeighbors vs KNeighborsRegressor 600 x 12 0.60 ms 5.22 ms 8.7x 1e-15
2500 x 24 1.85 ms 3.16 ms 1.7x 2e-15
3500 x 48 3.84 ms 6.40 ms 1.7x 2e-15
ExtraTrees vs ExtraTreesRegressor (50 trees, depth 8, one core) 600 x 12 15.7 ms 63.0 ms 4.0x own random draws
2500 x 24 74.4 ms 212 ms 2.9x own random draws
3500 x 48 179 ms 478 ms 2.7x own random draws
ExtraTrees vs ExtraTreesRegressor (all cores) 3500 x 48 186 ms 269 ms 1.4x own random draws
linear_quantile_regression vs QuantReg, 22 levels 200 x 3 7.0 ms 150 ms 21x 1e-9
500 x 3 13.3 ms 141 ms 11x 2e-7
linear_quantile_regression vs scikit-learn QuantileRegressor (HiGHS), 22 levels 200 x 3 20 ms 183 ms 9.1x scikit-learn solves the linear program exactly; the iterations stop at their tolerance
500 x 3 11.8 ms 352 ms 30x
2000 x 3 55 ms 3.35 s 61x

In HQRWACI they make the whole method 11x faster on an hourly series than with scikit-learn's models and statsmodels (benchmarks/output_speed_places31.txt), and 6.8x faster than the study's code over four series, with the study's rcWIS. See places 31 to 66.

More