f = (zs) => {
let batches = [];
for (let i = 0; i < zs.length; i++) {
let z = zs[i];
batches.push({ size: 1, mean: z });
while (batches.length > 1) {
let b = batches[batches.length - 2];
let c = batches[batches.length - 1];
if (b.mean < c.mean) break;
b.mean = (b.mean * b.size + c.mean * c.size) / (b.size + c.size);
b.size = b.size + c.size;
batches.pop();
}
}
let xs = [];
for (const batch of batches)
for (let i = 0; i < batch.size; i++) xs.push(batch.mean);
return xs;
}