x <- 1:3
y <- 4:6
w <- 3:1
# Factor the harmonic mean by chaining the calculation.
harmonic_mean(x * y, w)
harmonic_mean(x, w) * harmonic_mean(y, factor_weights(-1)(x, w))
# The common case of an arithmetic mean.
arithmetic_mean(x * y, w)
arithmetic_mean(x, w) * arithmetic_mean(y, update_weights(x, w))
# In cases where x and y have the same order, Chebyshev's
# inequality implies that the chained calculation is too small.
arithmetic_mean(x * y, w) >
arithmetic_mean(x, w) * arithmetic_mean(y, w)
Run the code above in your browser using DataLab