Conformal Prediction (CP) は予測区間(prediction intervals)を算出するためのフレームワーク。
予測の残差から予測の幅を算出する。
DMLのcross-fittingのように、train setでは残差の予測モデルをfitせず、分けておいたcalibration setで残差の学習を行う。
前提¶
問題設定¶
個の訓練サンプルがあるとし、予測対象のサンプルもあるとする。 両方のデータは 交換可能(exchangeable) であると仮定する(例えばi.i.d.であるとする)。
が含まれると思われる marginal distribution-free prediction interval を構築したい。
exchangeability¶
サンプルが任意の同時分布から得られたものであり、サンプルの順列を変えても変わらないこと。i.i.d.よりは弱い仮定。
例えばサンプルが3つあるとして、 と は同じ同時分布を持つということ(Exchangeable random variables - Wikipedia)。
Conformal Regression¶
まず、訓練データを2つに分割する
training set:
calibration set:
任意の回帰アルゴリズムを用いて、回帰モデルを訓練する
calibration setで残差の絶対値を計算する
所与の水準のもとで、絶対残差の経験分布の分位点を計算する
新しく与えられた点での予測区間は
info - Unknown Directive
info - Unknown DirectiveAdaptiveではないConformal Predictionはどのデータ点$X$においても同じQuantile(区間の幅が固定)
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()
Adaptive Conformal Prediction¶
区間の幅を可変にしたものの総称?
Conformalized quantile regression¶
locally adaptive