Skip to main content

A simple python package to correct the issue of quantile crossing in probabilistic forecasting

Project description

: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(),
)

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quantile_glasses-0.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quantile_glasses-0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file quantile_glasses-0.1.tar.gz.

File metadata

  • Download URL: quantile_glasses-0.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for quantile_glasses-0.1.tar.gz
Algorithm Hash digest
SHA256 efa7d7e4d13c7caae62779315c61a404cc323ef107febbe4c3caa99e1730c26f
MD5 a8cf260b08a79128d3631dd90cc2303c
BLAKE2b-256 68f85decfd78af29db6a8cf856fc0988a554ff880b950ca6371c804b914f1077

See more details on using hashes here.

File details

Details for the file quantile_glasses-0.1-py3-none-any.whl.

File metadata

  • Download URL: quantile_glasses-0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for quantile_glasses-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dab015a6fd209fa6322c15c98779721fd7cc3f1736b08b855e791d06994cea45
MD5 b16763903bf8337a32bc46754939ba63
BLAKE2b-256 d6d8952f66fd95c1ca83896b1a4e081dbcc07d5abdcd0a708585c07f078d6a79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page