def covfunc_se(amplitude, lengthscale, x1, x2=None, gradient=False): # Make sure that hyperparameters are scalars, not an array objects amplitude = utils.array_to_scalar(amplitude) lengthscale = utils.array_to_scalar(lengthscale) # Compute covariance matrix if x2 is None: x1 = gp_preprocess_inputs(x1) #x = inputs[0] # Compute variance vector N = np.shape(x1)[0] K = np.ones(N) np.multiply(K, amplitude**2, out=K) # Compute gradient w.r.t. lengthscale if gradient: # TODO: Use sparse matrices? gradient_lengthscale = np.zeros(N) else: (x1,x2) = gp_preprocess_inputs(x1,x2) x1 = x1 / (lengthscale) x2 = x2 / (lengthscale) # Compute distance matrix K = squared_distance(x1, x2) # Compute gradient partly if gradient: gradient_lengthscale = np.divide(K, lengthscale) # Compute covariance matrix gp_cov_se(K, overwrite=True) np.multiply(K, amplitude**2, out=K) # Compute gradient w.r.t. lengthscale if gradient: gradient_lengthscale *= K # Gradient w.r.t. amplitude if gradient: gradient_amplitude = K * (2 / amplitude) # Return values if gradient: print("se grad", gradient_amplitude, gradient_lengthscale) return (K, (gradient_amplitude, gradient_lengthscale)) else: return K
def covfunc_se(amplitude, lengthscale, x1, x2=None, gradient=False): # Make sure that hyperparameters are scalars, not an array objects amplitude = utils.array_to_scalar(amplitude) lengthscale = utils.array_to_scalar(lengthscale) # Compute covariance matrix if x2 is None: x1 = gp_preprocess_inputs(x1) #x = inputs[0] # Compute variance vector N = np.shape(x1)[0] K = np.ones(N) np.multiply(K, amplitude**2, out=K) # Compute gradient w.r.t. lengthscale if gradient: # TODO: Use sparse matrices? gradient_lengthscale = np.zeros(N) else: (x1, x2) = gp_preprocess_inputs(x1, x2) x1 = x1 / (lengthscale) x2 = x2 / (lengthscale) # Compute distance matrix K = squared_distance(x1, x2) # Compute gradient partly if gradient: gradient_lengthscale = np.divide(K, lengthscale) # Compute covariance matrix gp_cov_se(K, overwrite=True) np.multiply(K, amplitude**2, out=K) # Compute gradient w.r.t. lengthscale if gradient: gradient_lengthscale *= K # Gradient w.r.t. amplitude if gradient: gradient_amplitude = K * (2 / amplitude) # Return values if gradient: print("se grad", gradient_amplitude, gradient_lengthscale) return (K, (gradient_amplitude, gradient_lengthscale)) else: return K
def covfunc_delta(amplitude, x1, x2=None, gradient=False): # Make sure that amplitude is a scalar, not an array object amplitude = utils.array_to_scalar(amplitude) ## if gradient: ## gradient_amplitude = gradient[0] ## else: ## gradient_amplitude = [] ## inputs = gp_preprocess_inputs(*inputs) # Compute distance and covariance matrix if x2 is None: x1 = gp_preprocess_inputs(x1) # Only variance vector asked #x = inputs[0] N = np.shape(x1)[0] K = np.ones(N) * amplitude**2 else: (x1,x2) = gp_preprocess_inputs(x1,x2) # Full covariance matrix asked #x1 = inputs[0] #x2 = inputs[1] # Number of inputs x1 N1 = np.shape(x1)[0] # x1 == x2? if x1 is x2: delta = True # Delta covariance # # FIXME: Broadcasting doesn't work with sparse matrices, # so must use scalar multiplication K = gp_cov_delta(N1) * amplitude**2 #K = gp_cov_delta(N1).multiply(amplitude**2) else: delta = False # Number of inputs x2 N2 = np.shape(x2)[0] # Zero covariance if N1 > 0 and N2 > 0: K = sp.csc_matrix((N1,N2)) else: K = np.zeros((N1,N2)) # Gradient w.r.t. amplitude if gradient: # FIXME: Broadcasting doesn't work with sparse matrices, # so must use scalar multiplication gradient_amplitude = K*(2/amplitude) print("noise grad", gradient_amplitude) return (K, (gradient_amplitude,)) else: return K
def covfunc_delta(amplitude, x1, x2=None, gradient=False): # Make sure that amplitude is a scalar, not an array object amplitude = utils.array_to_scalar(amplitude) ## if gradient: ## gradient_amplitude = gradient[0] ## else: ## gradient_amplitude = [] ## inputs = gp_preprocess_inputs(*inputs) # Compute distance and covariance matrix if x2 is None: x1 = gp_preprocess_inputs(x1) # Only variance vector asked #x = inputs[0] N = np.shape(x1)[0] K = np.ones(N) * amplitude**2 else: (x1, x2) = gp_preprocess_inputs(x1, x2) # Full covariance matrix asked #x1 = inputs[0] #x2 = inputs[1] # Number of inputs x1 N1 = np.shape(x1)[0] # x1 == x2? if x1 is x2: delta = True # Delta covariance # # FIXME: Broadcasting doesn't work with sparse matrices, # so must use scalar multiplication K = gp_cov_delta(N1) * amplitude**2 #K = gp_cov_delta(N1).multiply(amplitude**2) else: delta = False # Number of inputs x2 N2 = np.shape(x2)[0] # Zero covariance if N1 > 0 and N2 > 0: K = sp.csc_matrix((N1, N2)) else: K = np.zeros((N1, N2)) # Gradient w.r.t. amplitude if gradient: # FIXME: Broadcasting doesn't work with sparse matrices, # so must use scalar multiplication gradient_amplitude = K * (2 / amplitude) print("noise grad", gradient_amplitude) return (K, (gradient_amplitude, )) else: return K
def covfunc_pp2(amplitude, lengthscale, x1, x2=None, gradient=False): # Make sure that hyperparameters are scalars, not an array objects amplitude = utils.array_to_scalar(amplitude) lengthscale = utils.array_to_scalar(lengthscale) #amplitude = theta[0] #lengthscale = theta[1] ## if gradient: ## gradient_amplitude = gradient[0] ## gradient_lengthscale = gradient[1] ## else: ## gradient_amplitude = [] ## gradient_lengthscale = [] ## inputs = gp_preprocess_inputs(*inputs) # Compute covariance matrix if x2 is None: x1 = gp_preprocess_inputs(x1) # Compute variance vector K = np.ones(np.shape(x)[:-1]) K *= amplitude**2 # Compute gradient w.r.t. lengthscale if gradient: gradient_lengthscale = np.zeros(np.shape(x1)[:-1]) else: (x1,x2) = gp_preprocess_inputs(x1,x2) # Compute (sparse) distance matrix if x1 is x2: x1 = x1 / (lengthscale) x2 = x1 D2 = distance.sparse_pdist(x1, 1.0, form="full", format="csc") else: x1 = x1 / (lengthscale) x2 = x2 / (lengthscale) D2 = distance.sparse_cdist(x1, x2, 1.0, format="csc") r = np.sqrt(D2.data) N1 = np.shape(x1)[0] N2 = np.shape(x2)[0] # Compute the covariances if gradient: (k, dk) = gp_cov_pp2(r, np.shape(x1)[-1], gradient=True) else: k = gp_cov_pp2(r, np.shape(x1)[-1]) k *= amplitude**2 # Compute gradient w.r.t. lengthscale if gradient: if N1 >= 1 and N2 >= 1: dk *= r * (-amplitude**2 / lengthscale) gradient_lengthscale = sp.csc_matrix((dk, D2.indices, D2.indptr), shape=(N1,N2)) else: gradient_lengthscale = np.empty((N1,N2)) # Form sparse covariance matrix if N1 >= 1 and N2 >= 1: ## K = sp.csc_matrix((k, ij), shape=(N1,N2)) K = sp.csc_matrix((k, D2.indices, D2.indptr), shape=(N1,N2)) else: K = np.empty((N1, N2)) #print(K.__class__) # Gradient w.r.t. amplitude if gradient: gradient_amplitude = K * (2 / amplitude) # Return values if gradient: print("pp2 grad", gradient_lengthscale) return (K, (gradient_amplitude, gradient_lengthscale)) else: return K
def covfunc_pp2(amplitude, lengthscale, x1, x2=None, gradient=False): # Make sure that hyperparameters are scalars, not an array objects amplitude = utils.array_to_scalar(amplitude) lengthscale = utils.array_to_scalar(lengthscale) #amplitude = theta[0] #lengthscale = theta[1] ## if gradient: ## gradient_amplitude = gradient[0] ## gradient_lengthscale = gradient[1] ## else: ## gradient_amplitude = [] ## gradient_lengthscale = [] ## inputs = gp_preprocess_inputs(*inputs) # Compute covariance matrix if x2 is None: x1 = gp_preprocess_inputs(x1) # Compute variance vector K = np.ones(np.shape(x)[:-1]) K *= amplitude**2 # Compute gradient w.r.t. lengthscale if gradient: gradient_lengthscale = np.zeros(np.shape(x1)[:-1]) else: (x1, x2) = gp_preprocess_inputs(x1, x2) # Compute (sparse) distance matrix if x1 is x2: x1 = x1 / (lengthscale) x2 = x1 D2 = distance.sparse_pdist(x1, 1.0, form="full", format="csc") else: x1 = x1 / (lengthscale) x2 = x2 / (lengthscale) D2 = distance.sparse_cdist(x1, x2, 1.0, format="csc") r = np.sqrt(D2.data) N1 = np.shape(x1)[0] N2 = np.shape(x2)[0] # Compute the covariances if gradient: (k, dk) = gp_cov_pp2(r, np.shape(x1)[-1], gradient=True) else: k = gp_cov_pp2(r, np.shape(x1)[-1]) k *= amplitude**2 # Compute gradient w.r.t. lengthscale if gradient: if N1 >= 1 and N2 >= 1: dk *= r * (-amplitude**2 / lengthscale) gradient_lengthscale = sp.csc_matrix( (dk, D2.indices, D2.indptr), shape=(N1, N2)) else: gradient_lengthscale = np.empty((N1, N2)) # Form sparse covariance matrix if N1 >= 1 and N2 >= 1: ## K = sp.csc_matrix((k, ij), shape=(N1,N2)) K = sp.csc_matrix((k, D2.indices, D2.indptr), shape=(N1, N2)) else: K = np.empty((N1, N2)) #print(K.__class__) # Gradient w.r.t. amplitude if gradient: gradient_amplitude = K * (2 / amplitude) # Return values if gradient: print("pp2 grad", gradient_lengthscale) return (K, (gradient_amplitude, gradient_lengthscale)) else: return K