x <- 1:3
y <- 4:6
w <- 3:1
#---- Transforming generalized means ----
# Calculate the geometric mean as an arithmetic mean and
# harmonic mean by transmuting the weights
geometric_mean(x)
arithmetic_mean(x, transmute_weights(0, 1)(x))
harmonic_mean(x, transmute_weights(0, -1)(x))
# Transmuting the weights for a harmonic mean into those
# for an arithmetic mean is the same as using weights w / x
all.equal(transmute_weights(-1, 1)(x, w), scale_weights(w / x))
# Transmuting the weights for an arithmetic mean into those
# for a harmonic mean is the same as using weights w * x
all.equal(transmute_weights(1, -1)(x, w), scale_weights(w * x))
# Works for nested means, too
w1 <- 3:1
w2 <- 1:3
fisher_mean(x, w1, w2)
arithmetic_mean(x, nested_transmute(0, c(1, -1), 1)(x, w1, w2))
arithmetic_mean(x, nested_transmute2(0, c(1, -1), 1)(x, w1, w2))
# Note that nested_transmute() has an invariance property
# not shared by nested_transmute2()
all.equal(
nested_transmute(0, c(1, -1), 1)(x, w1, w2),
transmute_weights(2, 1)(
x, nested_transmute(0, c(1, -1), 2)(x, w1, w2)
)
)
all.equal(
nested_transmute2(0, c(1, -1), 1)(x, w1, w2),
transmute_weights(2, 1)(
x, nested_transmute2(0, c(1, -1), 2)(x, w1, w2)
)
)
#---- Monotonicity ----
# Transmuted weights increase when x is small and decrease
# when x is large if r < s
transmute_weights(0, 1)(x, w) > scale_weights(w)
# The opposite happens when r > s
transmute_weights(1, 0)(x, w) > scale_weights(w)
#---- Percent-change contributions ----
# Transmuted weights can be used to calculate percent-change
# contributions for, e.g., a geometric price index
transmute_weights(0, 1)(x) * (x - 1)
geometric_contributions(x) # the more convenient way
#---- Basket representation of a price index ----
# Any generalized-mean index can be represented as a basket-style
# index by transmuting the weights, which is how some authors
# define a price index (e.g., Sydsaeter et al., 2005, p. 174)
p1 <- 2:6
p0 <- 1:5
qs <- transmute_weights(-1, 1)(p1 / p0) / p0
all.equal(harmonic_mean(p1 / p0), sum(p1 * qs) / sum(p0 * qs))
Run the code above in your browser using DataLab