:eyeglasses: :eyeglasses: :eyeglasses:
quantile-glasses
A simple python package to correct the issue of quantile crossing in probabilistic forecasting. Kind of like wearing glasses if you can't see well!
The LinearQuantileGlasses class allows you to correct your probabilistic forecast and remove quantile crossing.
It works by applying linear optimization, imposing a constraint that quantile crossing does not happen while minimizing pinball loss on the training set.
Installation
Just run
pip install quantile-glasses
You might encounter a few difficulties installing dependencies for cvxpy. If that happens to be the case, install cvxpy without dependencies first by running:
pip install cvxpy --no-deps
You can then selectively install the dependencies for that package one at a time. Note that cvxpy can work without most dependencies as long as you select a solver that you have installed. See the getting started guide below for an example.
Getting started
import pandas as pd
import numpy as np
from quantile_glasses import LinearQuantileGlasses
# simulate an independent and dependent variable for a regression problem
x = pd.Series(np.sin(np.linspace(0, 4 * np.pi, 1100)))
y = 5 * x + np.random.randn(len(x))
# define quantiles
quantiles = np.linspace(0.05, 0.95, 5)
# create a simple quantile forecast by binning the data
y_quant = pd.DataFrame(
{
q: y.groupby(pd.cut(x, 10), observed=True).transform(lambda x: x.quantile(q))
for q in quantiles
}
)
# split data
x_train, x_test = y_quant.iloc[:1000].copy(), y_quant.iloc[-100:].copy()
y_train, y_test = y.iloc[:1000].copy(), y.iloc[-100:].copy()
# artificially switch around the order of some quantiles
quant_switch = x_test.sample(10).index
for i in quant_switch:
x_test.loc[i] = x_test.loc[i].sort_values(ascending=False).values
# apply correction to prediction
qlg = LinearQuantileGlasses()
y_quant_corr = qlg.fit_predict(x_train, y_train, x_test, solver="CLARABEL")
print("Pinball loss with quantile crossing", qlg.pinball_loss(x_test, y_test).mean())
print(
"Pinball loss without quantile crossing",
qlg.pinball_loss(y_quant_corr, y_test).mean(),
)
Release files for quantile-glasses 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| quantile_glasses-0.1.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| quantile_glasses-0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:12.8 kB
Release files / quantile_glasses-0.1.tar.gz
| Download URL | quantile_glasses-0.1.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efa7d7e4d13c7caae62779315c61a404cc323ef107febbe4c3caa99e1730c26f
|
|
BLAKE2b-256 checksum How to use checksums |
68f85decfd78af29db6a8cf856fc0988a554ff880b950ca6371c804b914f1077
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / quantile_glasses-0.1-py3-none-any.whl
| Download URL | quantile_glasses-0.1-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dab015a6fd209fa6322c15c98779721fd7cc3f1736b08b855e791d06994cea45
|
|
BLAKE2b-256 checksum How to use checksums |
d6d8952f66fd95c1ca83896b1a4e081dbcc07d5abdcd0a708585c07f078d6a79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|