The shop has the same setup as the question: 10 servers, each holding its own region's orders, 1,000 orders in all. Start with the mean.
Predict
Each server reports the mean of its own orders. Is the average of the 10 means the mean of all 1,000 orders?
import numpy as nprng = np.random.default_rng(8)# orders on each of 10 servers, 1,000 in allsizes = [40, 50, 60, 70, 90, 100, 110, 130, 150, 200]nodes = [np.round(rng.lognormal(np.log(40 + 6 * i), 0.6, s)).astype(int) + 5 for i, s in enumerate(sizes)]mean_of_means = np.mean([x.mean() for x in nodes])sums, counts = sum(int(x.sum()) for x in nodes), sum(len(x) for x in nodes)print(f"mean of the 10 means: {mean_of_means:.2f}")print(f"sum over count: {sums / counts:.2f} (all at once: {np.concatenate(nodes).mean():.2f})")
mean of the 10 means: 84.86sum over count: 93.26 (all at once: 93.26)
The busy servers here also sell pricier orders, so the average of means comes out $8.40 low. The fix is to send a summary instead of an answer: each server sends its sum and its count, and the coordinator adds them up. Agarwal and coauthors call this mergeability: two summaries of two data sets merge into one summary of their union, with the same guarantees. MapReduce builds it in as the combiner, a function that does partial merging before the data is sent over the network.
In the editor, write pooled_mean(parts). It prints the mean of three servers, one of them empty.
Pick an answer above to see the rest
Instructions
1
Write pooled_mean(parts): each part is one server's (sum, count). Return the mean of every value on every server.