KiTE.calibration_models

Additional Callibration Models

 1"""
 2Additional Callibration Models
 3"""
 4
 5from KiTE.validation import check_attributes
 6import numpy as np
 7from sklearn.kernel_ridge import KernelRidge
 8from sklearn.gaussian_process.kernels import pairwise_kernels
 9
10
11class KRR_calibration:
12    def __init__(self):
13        self.model = "KRR"
14
15    def fit(self, X, p, Y, use_y=True, kernel_function="rbf", **kwargs):
16        check_attributes(X, Y)
17        self.use_y = use_y
18        Xp = np.concatenate((X, p[:, np.newaxis]), axis=1) if self.use_y else X.copy()
19        self.model = KernelRidge(kernel=kernel_function, **kwargs)
20        observed_bias = Y - p
21        self.model.fit(Xp, observed_bias)
22        return self.model
23
24    def predict(self, X, p, mode="prob"):
25        Xp = np.concatenate((X, p[:, np.newaxis]), axis=1) if self.use_y else X.copy()
26
27        if mode == "bias":
28            return self.model.predict(Xp)
29        elif mode == "prob":
30            return self.model.predict(Xp) + p.flatten()
31        else:
32            raise ValueError(f"Mode {mode} is not defined.")
33
34
35class EWF_calibration:
36    def __init__(self):
37        self.model = "EWF"
38
39    def fit(self, X, p, Y, kernel_function="rbf", **kwargs):
40        check_attributes(X, Y)
41        self.Xp = np.concatenate((X, p[:, np.newaxis]), axis=1)
42        self.bias = Y - p
43        self.kernel_function = kernel_function
44        self.kwargs = kwargs
45
46    def predict(self, Xtest, ptest, mode="prob"):
47        Xtestp = np.concatenate((Xtest, ptest[:, np.newaxis]), axis=1)
48        K = pairwise_kernels(
49            self.Xp, Xtestp, metric=self.kernel_function, **self.kwargs
50        )
51        bias = np.sum(self.bias.flatten() * K.T, axis=1) / np.sum(K.T, axis=1)
52        if mode == "bias":
53            return bias
54        elif mode == "prob":
55            return bias + ptest.flatten()
56        else:
57            raise ValueError(f"Mode {mode} is not defined.")
class KRR_calibration:
12class KRR_calibration:
13    def __init__(self):
14        self.model = "KRR"
15
16    def fit(self, X, p, Y, use_y=True, kernel_function="rbf", **kwargs):
17        check_attributes(X, Y)
18        self.use_y = use_y
19        Xp = np.concatenate((X, p[:, np.newaxis]), axis=1) if self.use_y else X.copy()
20        self.model = KernelRidge(kernel=kernel_function, **kwargs)
21        observed_bias = Y - p
22        self.model.fit(Xp, observed_bias)
23        return self.model
24
25    def predict(self, X, p, mode="prob"):
26        Xp = np.concatenate((X, p[:, np.newaxis]), axis=1) if self.use_y else X.copy()
27
28        if mode == "bias":
29            return self.model.predict(Xp)
30        elif mode == "prob":
31            return self.model.predict(Xp) + p.flatten()
32        else:
33            raise ValueError(f"Mode {mode} is not defined.")
KRR_calibration()
13    def __init__(self):
14        self.model = "KRR"
def fit(self, X, p, Y, use_y=True, kernel_function='rbf', **kwargs):
16    def fit(self, X, p, Y, use_y=True, kernel_function="rbf", **kwargs):
17        check_attributes(X, Y)
18        self.use_y = use_y
19        Xp = np.concatenate((X, p[:, np.newaxis]), axis=1) if self.use_y else X.copy()
20        self.model = KernelRidge(kernel=kernel_function, **kwargs)
21        observed_bias = Y - p
22        self.model.fit(Xp, observed_bias)
23        return self.model
def predict(self, X, p, mode='prob'):
25    def predict(self, X, p, mode="prob"):
26        Xp = np.concatenate((X, p[:, np.newaxis]), axis=1) if self.use_y else X.copy()
27
28        if mode == "bias":
29            return self.model.predict(Xp)
30        elif mode == "prob":
31            return self.model.predict(Xp) + p.flatten()
32        else:
33            raise ValueError(f"Mode {mode} is not defined.")
class EWF_calibration:
36class EWF_calibration:
37    def __init__(self):
38        self.model = "EWF"
39
40    def fit(self, X, p, Y, kernel_function="rbf", **kwargs):
41        check_attributes(X, Y)
42        self.Xp = np.concatenate((X, p[:, np.newaxis]), axis=1)
43        self.bias = Y - p
44        self.kernel_function = kernel_function
45        self.kwargs = kwargs
46
47    def predict(self, Xtest, ptest, mode="prob"):
48        Xtestp = np.concatenate((Xtest, ptest[:, np.newaxis]), axis=1)
49        K = pairwise_kernels(
50            self.Xp, Xtestp, metric=self.kernel_function, **self.kwargs
51        )
52        bias = np.sum(self.bias.flatten() * K.T, axis=1) / np.sum(K.T, axis=1)
53        if mode == "bias":
54            return bias
55        elif mode == "prob":
56            return bias + ptest.flatten()
57        else:
58            raise ValueError(f"Mode {mode} is not defined.")
EWF_calibration()
37    def __init__(self):
38        self.model = "EWF"
def fit(self, X, p, Y, kernel_function='rbf', **kwargs):
40    def fit(self, X, p, Y, kernel_function="rbf", **kwargs):
41        check_attributes(X, Y)
42        self.Xp = np.concatenate((X, p[:, np.newaxis]), axis=1)
43        self.bias = Y - p
44        self.kernel_function = kernel_function
45        self.kwargs = kwargs
def predict(self, Xtest, ptest, mode='prob'):
47    def predict(self, Xtest, ptest, mode="prob"):
48        Xtestp = np.concatenate((Xtest, ptest[:, np.newaxis]), axis=1)
49        K = pairwise_kernels(
50            self.Xp, Xtestp, metric=self.kernel_function, **self.kwargs
51        )
52        bias = np.sum(self.bias.flatten() * K.T, axis=1) / np.sum(K.T, axis=1)
53        if mode == "bias":
54            return bias
55        elif mode == "prob":
56            return bias + ptest.flatten()
57        else:
58            raise ValueError(f"Mode {mode} is not defined.")