Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

分位点回帰

分位点τ\tauにおける条件付分位関数を

Qτ(yiXi)=Fy1(τXi)Q_\tau(y_i | X_i) = F_y^{-1}(\tau | X_i)

と表す。ここでFy1(τXi)F_y^{-1}(\tau | X_i)yyにおいてXiX_iに条件づけられたyiy_iの分布関数である(Fy1(τXi)=inf{y:Fy(yXi)τ}F_y^{-1}(\tau | X_i) = \inf \{ y: F_y(y|X_i) \geq \tau \})。

例えばτ=0.1\tau = 0.1のとき、Qτ(yiXi)Q_\tau(y_i | X_i)yiy_iの下位10分位である。

標準的な回帰モデルは二乗誤差(yim(Xi))2(y_i - m(X_i))^2の和や期待値を最小化するようにモデルm(Xi)m(X_i)を学習して条件付き期待値E(yiXi)E(y_i|X_i)を予測する

\newcommand{\argmin} attempting to redefine \argmin; use \renewcommand

\newcommand{\argmin}{\mathop{\rm arg~min}\limits}
E(y_i|X_i) = \argmin_{m(X_i)}
E\big[ (y_i - m(X_i))^2 \big]

分位点回帰 (quantile regression)モデルはpinball lossρτ(yiq(Xi))\rho_{\tau}(y_i - q(X_i))の和や期待値を最小化するようにモデルq(Xi)q(X_i)を学習させ、条件付き分位関数Qτ(yiXi)=Fy1(τXi)Q_{\tau}(y_i|X_i) = F^{-1}_y(\tau|X_i)を予測する

Qτ(yiXi)=arg minq(Xi)E[ρτ(yiq(Xi))]Q_{\tau}(y_i|X_i) = \argmin_{q(X_i)} E\big[ \rho_{\tau}(y_i - q(X_i)) \big]

pinball lossは τ\tau-tiled absolute value function や 検定関数(check function)とも呼ばれる(グラフを描くとチェックマークに似てるため)

ρτ(x)=(τ1(x0))x\rho_{\tau} (x) = \big(\tau - \mathbb{1}(x \leq 0) \big) x

あるいは

ρτ(x)={(τ1)x if x0τx if x>0\rho_{\tau} (x) = \begin{cases} (\tau - 1) x & \text{ if } x \leq 0\\ \tau x & \text{ if } x > 0\\ \end{cases}

あるいは

ρτ(x)=τmax(x,0)+(τ1)min(x,0)\rho_{\tau} (x) = \tau \max(x, 0) + (\tau - 1) \min(-x, 0)

と書かれる

Source
import numpy as np
import matplotlib.pyplot as plt

def pinball_loss(x, tau):
    return (tau - 1 * (x <= 0)) * x

x = np.linspace(-1, 1, 100)
fig, axes = plt.subplots(figsize=[10, 2], ncols=3)
for i, tau in enumerate([0.1, 0.5, 0.9]):
    y = pinball_loss(x, tau=tau)
    axes[i].plot(x, y)
    if i == 0:
        axes[i].set(title=f"τ={tau}", xlabel=r"$x$", ylabel=r"$y = (\tau - 1(x <= 0)) x$")
    else:
        axes[i].set(title=f"τ={tau}", xlabel=r"$x$")
fig.show()
<Figure size 1000x200 with 3 Axes>

なお、pinball lossはτ=0.5\tau=0.5のとき

ρ0.5(x)={0.5x if x00.5x if x>0=12x\begin{align} \rho_{0.5} (x) &= \begin{cases} -0.5 x & \text{ if } x \leq 0\\ 0.5 x & \text{ if } x > 0\\ \end{cases} \\ &= \frac{1}{2} |x| \end{align}

と、絶対誤差と比例する形になる。

絶対誤差の和を目的関数にとった線形モデルは統計学においてleast absolute deviations (LAD) と呼ばれ、その解は条件付き中央値になる

median(yiXi)=Q0.5(yiXi)=arg minq(Xi)E[ρ0.5(yiq(Xi))]\text{median}(y_i|X_i) = Q_{0.5}(y_i|X_i) = \argmin_{q(X_i)} E\big[ \rho_{0.5}(y_i - q(X_i)) \big]
Source
import numpy as np
import matplotlib.pyplot as plt

def pinball_loss(x, tau):
    return (tau - 1 * (x <= 0)) * x

x = np.linspace(-3, 3, 100)
fig, ax = plt.subplots(figsize=[4, 3])
ax.plot(x, pinball_loss(x, tau=0.5), label=r"$\rho_{0.5}(x)$")
ax.plot(x, abs(x), label="|x|")
ax.legend()
fig.show()
<Figure size 400x300 with 1 Axes>

分位点回帰モデルの実践

LightGBMでのquantile regression

目的関数をbinball lossにすればいいだけなので他のアルゴリズムでも実行できる

Source
import numpy as np
import matplotlib.pyplot as plt

# create data
np.random.seed(0)
n = 1000
x = np.random.uniform(-3, 3, size=n)
X = x.reshape(-1, 1)
y = np.sin(x) + np.random.normal(scale=0.5, size=n)

fig, ax = plt.subplots()
ax.scatter(x, y, alpha=0.7)

# regression
x_plot = np.linspace(-3, 3, 500)
X_test = x_plot.reshape(-1, 1)
import lightgbm as lgb
taus = [0.1, 0.9]
colors = ["orange", "tomato"]
for i in range(2):
    model = lgb.LGBMRegressor(objective="quantile", alpha=taus[i], verbose=-1)
    model.fit(X, y)
    y_hat = model.predict(X_test)
    ax.plot(X_test, y_hat, color=colors[i], label=fr"$\tau = {taus[i]}$")
ax.set(xlabel="x", ylabel="y", title="Quantile Regression with LightGBM")
ax.legend()
fig.show()
<Figure size 640x480 with 1 Axes>
from sklearn.metrics import d2_pinball_score, make_scorer
d2_pinball_score_09 = make_scorer(d2_pinball_score, alpha=0.9)
d2_pinball_score_09(model, X, y)
0.48475107952573926