Conformal Regression#

Conformal Prediction (CP) は予測区間(prediction intervals)を算出するためのフレームワーク。

予測の残差から予測の幅を算出する。

DMLのcross-fittingのように、train setでは残差の予測モデルをfitせず、分けておいたcalibration setで残差の学習を行う。

前提#

問題設定#

n個の訓練サンプル{(Xi,Yi)}i=1nがあるとし、予測対象のサンプル(Xn+1,Yn+1)もあるとする。 両方のデータ{(Xi,Yi)}i=1n+1交換可能(exchangeable) であると仮定する(例えばi.i.d.であるとする)。

Yn+1が含まれると思われる marginal distribution-free prediction interval C(Xn+1Rを構築したい。

exchangeability#

サンプル(Xi,Yi)が任意の同時分布PXYから得られたものであり、サンプルの順列を変えても変わらないこと。i.i.d.よりは弱い仮定。

例えばサンプルが3つあるとして、(X1,Y1),(X2,Y2),(X3,Y3)(X2,Y2),(X1,Y1),(X3,Y3) は同じ同時分布を持つということ(Exchangeable random variables - Wikipedia)。

Conformal Regression#

まず、訓練データを2つに分割する

  • training set: {(Xi,Yi):iI1}

  • calibration set: {(Xi,Yi):iI2}

任意の回帰アルゴリズムAを用いて、回帰モデルを訓練する

μ^(x):=A({(Xi,Yi):iI1})

calibration setで残差の絶対値を計算する

Ri=|Yiμ^(Xi)|,iI2

所与の水準αのもとで、絶対残差の経験分布の分位点を計算する

Q1α(R,I2):=(1α)(1+1/|I2|)-th empirical quantile of {Ri:iI2}

新しく与えられた点Xn+1での予測区間は

C(Xn+1)=[μ^(Xn+1)Q1α(R,I2),μ^(Xn+1)+Q1α(R,I2)]
Hide code cell source
# generate data
import numpy as np
import matplotlib.pyplot as plt
n = 1000
np.random.seed(0)
x = np.random.uniform(low=0, high=1, size=n)
y = x**3 + 2 * np.exp(-6 * (x - 0.3)**2)
y = y + np.random.normal(loc=0, scale=x * 0.3, size=n)

from sklearn.model_selection import train_test_split
X = x.reshape(-1, 1)
X_train, X_cal, y_train, y_cal = train_test_split(X, y, test_size=0.33, random_state=42)

# calculate residual
from lightgbm import LGBMRegressor
model = LGBMRegressor(verbose=-1)
model.fit(X_train, y_train)
abs_residuals = np.abs(y_cal - model.predict(X_cal))

# aclculate quantile
quantile = np.quantile(abs_residuals, q=0.9)

# plot
x_range = np.linspace(x.min(), x.max(), 100)
y_pred = model.predict(x_range.reshape(-1, 1))

fig, ax = plt.subplots(figsize=[6, 3])
ax.scatter(x, y, alpha=0.3, color="gray")
ax.plot(x_range, y_pred, alpha=0.9, color="black")
ax.plot(x_range, y_pred + quantile, alpha=0.9, color="steelblue")
ax.plot(x_range, y_pred - quantile, alpha=0.9, color="steelblue")
fig.show()
/usr/local/lib/python3.10/site-packages/sklearn/utils/validation.py:2739: UserWarning: X does not have valid feature names, but LGBMRegressor was fitted with feature names
  warnings.warn(
/usr/local/lib/python3.10/site-packages/sklearn/utils/validation.py:2739: UserWarning: X does not have valid feature names, but LGBMRegressor was fitted with feature names
  warnings.warn(
../../_images/225a136461fd8320e3037f128eef7bc6719f60caa9a5ecd5cebd607163d0c1e3.png

Adaptive Conformal Prediction#

区間の幅を可変にしたものの総称?

Conformalized quantile regression#

locally adaptive

Romano et al. (2019). Conformalized quantile regression.

Seedat et al. (2023, April). Improving adaptive conformal prediction using self-supervised learning.

参考#