Core methods written from scratch¶
pyconformal does not only implement conformal methods. Where a conformal method needs a model, a decomposition or a special quantile, pyconformal has its own version, written for speed and checked against the reference library. So no method needs scikit-learn, statsmodels, statsforecast or quantile-forest.
| Use it when | You want these models on their own, or want to know what runs under pyconformal's conformal methods. |
| Guarantee | None by themselves: these are models and building blocks. The conformal methods on top carry the guarantees. |
| In the library | QuantileForest, pyconformal.point_models (Ridge, KNeighbors, ExtraTrees, linear_quantile_regression), stl, mstl, NadarayaWatsonQuantile, MarkovSwitchingAR, and internal kernels (isotonic regression, reservoir, typed and weighted quantiles) |
| Example needs | pyconformal with numba (pip install "pyconformal[fast]") |
What is implemented, and how fast¶
| Core method | Call it as | Instead of | Speed-up | Same results? | Used by |
|---|---|---|---|---|---|
| Quantile regression forest | QuantileForest(n_estimators=100).fit(X, y).predict(X, quantiles=[...]) |
quantile-forest (Zillow) | fit 6x to 9x, predict 1.6x to 4x (16 cores) | same accuracy (pinball loss) | CQR, CTI, CIR, SPCI, EnCQR, EnsCQR, CQACP |
| Ridge regression | Ridge(alpha=1.0).fit(X, y).predict(X) |
scikit-learn Ridge |
1.4x to 9x | yes, to 1e-14 | HQR-WACI |
| Nearest neighbours | KNeighbors(n_neighbors=10).fit(X, y).predict(X) |
scikit-learn KNeighborsRegressor |
1.7x to 9x | yes, to 2e-15 | HQR-WACI |
| Extremely randomised trees | ExtraTrees(n_estimators=50, max_depth=8).fit(X, y).predict(X) |
scikit-learn ExtraTreesRegressor |
2.7x to 4x (one core), 1.4x to 1.7x (all cores) | same method, own random draws | HQR-WACI |
| Linear quantile regression | linear_quantile_regression(Z, y, quantiles) |
statsmodels QuantReg; scikit-learn QuantileRegressor |
11x to 21x; 9x to 61x | statsmodels' iterations, to 1e-9 to 2e-7; scikit-learn's exact solution differs by up to the iteration's tolerance | HQR-WACI |
| STL and MSTL decomposition | stl(y, period), mstl(y, periods=[24, 168]) |
statsforecast MSTL; R forecast::mstl; statsmodels STL |
6x to 7x at equal settings (about 200x against statsforecast's defaults); 5x; CP-TSD 5x faster than on statsmodels' STL | yes, to R to 14 decimal places | CP-TSD |
| Isotonic regression (pool adjacent violators) | inside VennAbers |
venn-abers | Venn-Abers up to 188x | yes, identical outputs | Venn-Abers, cross Venn-Abers |
| Nadaraya-Watson quantile regression | NadarayaWatsonQuantile().fit(X, y).predict(X, quantiles=[...]) |
the KOWCPI authors' research code (not packaged) | KOWCPI | ||
| Markov-switching regression (EM, forward filter) | MarkovSwitchingAR().fit(X, y), .predict(X), .update(X, y) |
the conformal forecasting study's code | CPTC 9x faster with numba than without, 10x against the study | yes, to 1e-12 | CPTC |
| Echo state network (reservoir) | inside ResCP |
the study's code | ResCP 3x | identical | ResCP |
| R's type-1 and ggdist's weighted type-7 quantiles, NumPy's quantile methods on sorted windows | inside the trackers | R, ggdist, NumPy | the trackers 2x to 112x against the study | to the last digits | Wang-Hyndman methods, ECI, DDCI and other trackers |
The benchmarks behind each row are in benchmarks/ (speed_forest.py, speed_point_models.py, speed_mstl.py,
speed_venn_abers.py, speed_places31.py) with their outputs next to them.
Minimal examples¶
Copy, paste, run.
import numpy as np
from pyconformal import QuantileForest
from pyconformal.point_models import ExtraTrees, KNeighbors, Ridge, linear_quantile_regression
rng = np.random.default_rng(0)
X = rng.normal(size=(2000, 8))
y = X[:, 0] - 2 * X[:, 1] + np.sin(3 * X[:, 2]) + rng.normal(0, 0.3 + 0.3 * np.abs(X[:, 3]), 2000)
X_train, y_train, X_test, y_test = X[:1600], y[:1600], X[1600:], y[1600:]
forest = QuantileForest(n_estimators=100, random_state=0).fit(X_train, y_train)
q = forest.predict(X_test, quantiles=[0.05, 0.5, 0.95]) # (400, 3)
print("forest 90% band covers", np.mean((y_test >= q[:, 0]) & (y_test <= q[:, 2])))
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(1600), X_train[:, :4]]) # a column of ones for the intercept
print(linear_quantile_regression(Z, y_train, [0.05, 0.5, 0.95]).shape) # (3, 5): one row per level
Decomposition, kernel quantiles and regimes:
import numpy as np
from pyconformal import MarkovSwitchingAR, NadarayaWatsonQuantile, mstl
rng = np.random.default_rng(1)
t = np.arange(24 * 7 * 8)
y = 0.01 * t + 3 * np.sin(2 * np.pi * t / 24) + 2 * np.sin(2 * np.pi * t / 168) + rng.normal(0, 0.3, t.size)
parts = mstl(y, periods=[24, 168]) # day and week in hourly data
print(parts.trend.shape, parts.seasonal[24].shape, parts.remainder.shape)
X = rng.normal(size=(800, 3))
yy = X[:, 0] + (0.2 + np.abs(X[:, 1])) * rng.normal(size=800)
nw = NadarayaWatsonQuantile().fit(X[:600], yy[:600]) # bandwidth chosen by AIC
print(nw.predict(X[600:605], quantiles=[0.1, 0.9]).shape) # (5, 2)
regime = np.where((np.arange(800) // 200) % 2 == 0, 0.3, 2.0) # two noise regimes
z = np.cumsum(rng.normal(size=800)) * 0.05 + regime * rng.normal(size=800)
L = np.column_stack([z[2:-1], z[1:-2], z[:-3]]) # three lags
ms = MarkovSwitchingAR().fit(L[:600], z[3:603])
probs, state_forecasts, forecast = ms.predict(L[600:601])
print("regimes", ms.K, "state probabilities", np.round(probs[0], 3))
More¶
- Point models: ridge, nearest neighbours, extra trees and linear quantile regression in detail
- Quantile forest · STL and MSTL · Venn-Abers
- Full argument list: API reference