liveBook · Manning

Kernel function

As explained in Experimentation for Engineers

A kernel function assigns a similarity or weight to an observation based on its distance from another input.

A kernel function assigns weight to each measurement according to its distance from a query input. In Gaussian process regression, nearby observations influence CPU-time estimates more strongly than distant ones. The common squared exponential kernel uses distance and a length scale, sigma, to control smoothness. section 6.2.1

Practical properties include:

  • Normalize the kernel weights before computing a weighted average; otherwise, their total can inflate the estimate. section 6.2.1
  • Increase sigma for a smoother surrogate surface; select it from measurements with leave-one-out cross-validation when needed. section 6.2.1

Estimating CPU time

With parameters [0.5, 0.0], measurements [1.52, 1.21], and sigma = 0.25, kernel-weighted estimates at queries 0.25 and 0.4 are 1.365 and 1.448. section 6.2.1

Listing 6.3 GPR with a weighted average
class GPR2:
    def __init__(self, parameters, measurements, sigma):        
        self.x = parameters
        self.y = np.array(measurements)
        self.sigma = sigma
        
        self.mean_y = self.y.mean()
        self.y -= self.mean_y                        #1
        
    def kernel(self, x1, x2):                        #2
        distance_squared = ((x1-x2)**2).sum()
        return np.exp( -distance_squared/(2*self.sigma**2) )
 
    def estimate(self, query_parameter):
        weights = [
            self.kernel(x, query_parameter)
            for x in self.x
        ]                                            #3
        weights = np.array(weights)
        weights = weights / weights.sum()            #4
        return self.mean_y + weights @ self.y        #5

Inspect a NumPy-style implementation that computes squared distances, builds kernel weights, normalizes them, and applies the weighted measurement residuals. section 6.2.1

How Experimentation for Engineers covers it

How deep it goes
Defines distance-based weighting, implements squared exponential kernels and normalization, but defers leave-one-out cross-validation details.
Examples
Uses CPU-time measurements at parameters 0.5 and 0.0, evaluates queries 0.25 and 0.4, and plots surrogate curves.
What it assumes
Assumes weighted averages and absolute distance, while introducing sigma as a smoothness hyperparameter.
What you can do after
Apply it via NumPy to normalize distance-based weights and estimate query values from measured parameters.
How it connects
Connects Gaussian process regression, Bayesian optimization, weighted averages, length scales, and leave-one-out cross-validation.

Also explained in

Compare how 4 books explain kernel function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Experimentation for Engineers. The section links point to the parts of the book it draws on.