File size: 1,304 Bytes
fc329a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Local scaling from training residuals (expected to fail)."""
import numpy as np
from .base import ConformalResult
from ._split_quantile import split_conformal_quantile
from ._knn_sigma import knn_sigma_hat


def trainres_conformal(
    R_cal: np.ndarray,
    R_test: np.ndarray,
    alpha: float,
    U_cal: np.ndarray,
    U_test: np.ndarray,
    R_train: np.ndarray,
    U_train: np.ndarray,
    k: int = 20,
) -> ConformalResult:
    """Locally-normalized conformal using training residuals for scale.

    Args:
        R_cal: calibration residuals (n_cal,)
        R_test: test residuals (n_test,)
        alpha: miscoverage level
        U_cal: calibration predictions (n_cal, K)
        U_test: test predictions (n_test, K)
        R_train: training residuals (n_train,)
        U_train: training predictions (n_train, K)
        k: kNN neighbors for scale estimation

    Returns:
        ConformalResult (expected to be miscalibrated).
    """
    sigma_hat_cal = knn_sigma_hat(U_train, R_train, U_cal, k=k)
    S_cal = R_cal / sigma_hat_cal
    q = split_conformal_quantile(S_cal, alpha)

    sigma_hat_test = knn_sigma_hat(U_train, R_train, U_test, k=k)
    radius = sigma_hat_test * q
    covered = R_test <= radius

    return ConformalResult(covered=covered, radius=radius, threshold=q)