x <- 2:3
w <- c(0.25, 0.75)
#---- The Pythagorean means are special cases of the Lehmer mean ----
all.equal(lehmer_mean(1)(x, w), arithmetic_mean(x, w))
all.equal(lehmer_mean(0)(x, w), harmonic_mean(x, w))
all.equal(lehmer_mean(0.5)(x), geometric_mean(x))
#---- Comparing Lehmer means and generalized means ----
# When r < 1, the generalized mean is larger than the corresponding
# Lehmer mean
lehmer_mean(-1)(x, w) < generalized_mean(-1)(x, w)
# The reverse is true when r > 1
lehmer_mean(3)(x, w) > generalized_mean(3)(x, w)
# This implies the contraharmonic mean is larger than the quadratic
# mean, and therefore the Pythagorean means
contraharmonic_mean(x, w) > arithmetic_mean(x, w)
contraharmonic_mean(x, w) > geometric_mean(x, w)
contraharmonic_mean(x, w) > harmonic_mean(x, w)
# ... and the logarithmic mean
contraharmonic_mean(2:3) > logmean(2, 3)
# The difference between the arithmetic mean and contraharmonic mean
# is proportional to the variance of x
weighted_var <- function(x, w) {
arithmetic_mean((x - arithmetic_mean(x, w))^2, w)
}
arithmetic_mean(x, w) + weighted_var(x, w) / arithmetic_mean(x, w)
contraharmonic_mean(x, w)
#---- Changing the order of the mean ----
# It is easy to modify the weights to turn a Lehmer mean of order r
# into a Lehmer mean of order s because the Lehmer mean can be
# expressed as an arithmetic mean
r <- 2
s <- -3
lehmer_mean(r)(x, w)
lehmer_mean(s)(x, w * x^(r - 1) / x^(s - 1))
# The weights can also be modified to turn a Lehmer mean of order r
# into a generalized mean of order s
lehmer_mean(r)(x, w)
generalized_mean(s)(x, transmute_weights(1, s)(x, w * x^(r - 1)))
# ... and vice versa
lehmer_mean(r)(x, transmute_weights(s, 1)(x, w) / x^(r - 1))
generalized_mean(s)(x, w)
#---- Percent-change contributions ----
# Percent-change contributions for a price index based on the Lehmer
# mean are easy to calculate
scale_weights(w * x^(r - 1)) * (x - 1)
Run the code above in your browser using DataLab