1717import numpy as np
1818from scipy .stats import gaussian_kde
1919from scipy .integrate import quad
20- import GPy
20+ from GPy .kern import RBF
21+ from GPy .models import GPRegression
22+ from GPy .core .parameterization .priors import Gamma
23+ from GPy .inference .mcmc import HMC
2124
2225def find_modal (samples , linspace_num : int = 1000 ):
2326
@@ -43,29 +46,29 @@ def gp_model(X: np.array,
4346 is_mcmc : bool = False ,
4447 num_samples : int = 1000 ,
4548 hmc_iters : int = 2 ,
46- linspace_num : int = 1000 ) -> GPy . models . GPRegression :
49+ linspace_num : int = 1000 ) -> GPRegression :
4750
4851 dim = X .shape [- 1 ]
4952
5053 if is_mcmc :
5154
5255 factor = 10.0 # TODO: clever factor for numerical issues in HMC train
5356
54- kern = GPy . kern . RBF (input_dim = dim , ARD = True )
55- model = GPy . models . GPRegression (factor * X ,y ,kernel = kern .copy ())
57+ kern = RBF (input_dim = dim , ARD = True )
58+ model = GPRegression (factor * X ,y ,kernel = kern .copy ())
5659
5760 # TODO: automate prior for RBF variance
58- model .kern .variance .set_prior (GPy . priors . Gamma .from_EV (0.1 ,0.1 ),warning = False )
61+ model .kern .variance .set_prior (Gamma .from_EV (0.1 ,0.1 ),warning = False )
5962
6063 lengthscales = {}
6164 for i in range (dim ):
6265 kde = gaussian_kde (X [:,i ])
6366 mean = quad (lambda x : x * kde .pdf (x ), a = - np .inf , b = np .inf )[0 ]
6467 var = quad (lambda x : x ** 2 * kde .pdf (x ), a = - np .inf , b = np .inf )[0 ] - mean ** 2
6568 lengthscales [i ] = np .sqrt (var )
66- model .kern .lengthscale [[i ]].set_prior (GPy . priors . Gamma .from_EV (lengthscales [i ],lengthscales [i ]/ 2 ),warning = False ) # data variance as length scale
69+ model .kern .lengthscale [[i ]].set_prior (Gamma .from_EV (lengthscales [i ],lengthscales [i ]/ 2 ),warning = False ) # data variance as length scale
6770
68- hmc = GPy . inference . mcmc . HMC (model )
71+ hmc = HMC (model )
6972 samples = hmc .sample (num_samples = num_samples ,hmc_iters = hmc_iters )
7073
7174 modals = {}
@@ -74,8 +77,8 @@ def gp_model(X: np.array,
7477 if modal is not None :
7578 modals [i ] = modal
7679
77- kern = GPy . kern . RBF (input_dim = dim , ARD = True )
78- model = GPy . models . GPRegression (X ,y ,kernel = kern .copy ())
80+ kern = RBF (input_dim = dim , ARD = True )
81+ model = GPRegression (X ,y ,kernel = kern .copy ())
7982
8083 if (0 in modals ) and (dim - 1 in modals ):
8184 model .rbf .variance = modals [0 ]/ factor ** 2
@@ -91,8 +94,8 @@ def gp_model(X: np.array,
9194
9295 else :
9396
94- kern = GPy . kern . RBF (input_dim = dim , ARD = True )
95- model = GPy . models . GPRegression (X ,y ,kernel = kern .copy ())
97+ kern = RBF (input_dim = dim , ARD = True )
98+ model = GPRegression (X ,y ,kernel = kern .copy ())
9699
97100 # model.optimize_restarts(num_restarts=10,optimizer='lbfgs',verbose=False)
98101 model .optimize (optimizer = 'lbfgs' ,messages = False )
0 commit comments