liveBook · Manning

Weighted average

As explained in Experimentation for Engineers

A weighted average is an average in which each value contributes in proportion to an assigned weight.

A weighted average combines values while giving each one influence proportional to its assigned weight. Larger weights pull the estimate closer to their measurements. section 6.2.1

Estimating CPU time

For measurements 1.54 at parameter 0.5 and 1.21 at parameter 0.0, an equal average is 0.5 × 1.54 + 0.5 × 1.21 = 1.365. For parameter 0.4, weights 0.8 and 0.2 produce 1.458, favoring the nearer measurement. section 6.2.1

Use weighted averages carefully:

  • Make weights sum to 1 so the result remains on the measurements’ scale. section 6.2.1
  • In proximity-based estimates, decrease a measurement’s weight as its distance from the query increases. 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

A NumPy implementation centers measurements, computes squared-exponential kernel weights, normalizes them, and forms a weighted sum. section 6.2.1

How Experimentation for Engineers covers it

How deep it goes
Defines normalization and proximity weighting, then connects them to squared-exponential kernels and a Gaussian-process implementation.
Examples
Works through CPU-time measurements at parameters 0.5 and 0.0, estimating values near 0.4 and 0.25.
What it assumes
Assumes measured parameter–outcome pairs and a query value; develops the difference between simple and weighted averages.
What you can do after
Apply it via NumPy to construct proximity-weighted estimates from measured parameter–outcome pairs.
How it connects
Connects weighted averages to Gaussian process regression, kernel functions, distance measures, and Bayesian optimization.

Also explained in

Compare how 5 books explain weighted average →

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.