Skip to contents

Calculate a weighted Lehmer mean.

Usage

lehmer_mean(r)

contraharmonic_mean(x, w = NULL, na.rm = FALSE)

Arguments

r

A finite number giving the order of the Lehmer mean.

x

A strictly positive numeric vector.

w

A strictly positive numeric vector of weights, the same length as x. The default is to equally weight each element of x.

na.rm

Should missing values in x and w be removed? By default missing values in x or w return a missing value.

Value

lehmer_mean() returns a function:

function(x, w = NULL, na.rm = FALSE){...}

This computes the Lehmer mean of order r of x with weights w.

contraharmonic_mean() returns a numeric value for the Lehmer mean of order 2.

Details

The function lehmer_mean() returns a function to compute the Lehmer mean of order r of x with weights w, which is calculated as the arithmetic mean of x with weights \(wx^{r-1}\). This is also called the counter-harmonic mean or generalized anti-harmonic mean. See Bullen (2003, p. 245) for a definition, or https://en.wikipedia.org/wiki/Lehmer_mean.

The Lehmer mean of order 2 is sometimes called the contraharmonic (or anti-harmonic) mean. The function contraharmonic_mean() simply calls lehmer_mean(2)(). Like the generalized mean, the contraharmonic mean is the solution to an optimal prediction problem: choose \(m\) to minimize \(\sum_{i = 1}^{n} w_{i} \left(\frac{x_{i}}{m} - 1 \right)^2\). The Lehmer mean of order -1 has a similar interpretation, replacing \(\frac{x_{i}}{m}\) with \(\frac{m}{x_{i}}\), and together these bound the harmonic and arithmetic means.

The Lehmer mean is an alternative to the generalized mean that generalizes the Pythagorean means. The function lehmer_mean(1)() is identical to arithmetic_mean(), lehmer_mean(0)() is identical to harmonic_mean(), and lehmer_mean(0.5)() is identical to geometric_mean() with two values and no weights. See von der Lippe (2015) for more details on the use of these means for making price indexes.

Note

lehmer_mean() can be defined on the extended real line, so that r = -Inf / Inf returns min()/max(), to agree with the definition in, e.g., Bullen (2003). This is not implemented, and r must be finite.

References

Bullen, P. S. (2003). Handbook of Means and Their Inequalities. Springer Science+Business Media.

Lehmer, D. H. (1971). On the Compounding of Certain Means. Journal of Mathematical Analysis and Applications, 36(1): 183-200.

von der Lippe, P. (2015). Generalized Statistical Means and New Price Index Formulas, Notes on some unexplored index formulas, their interpretations and generalizations. Munich Personal RePEc Archive paper no. 64952.

See also

Examples

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))
#> [1] TRUE
all.equal(lehmer_mean(0)(x, w), harmonic_mean(x, w))
#> [1] TRUE
all.equal(lehmer_mean(0.5)(x), geometric_mean(x))
#> [1] TRUE

#---- 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)
#> [1] TRUE

# The reverse is true when r > 1

lehmer_mean(3)(x, w) > generalized_mean(3)(x, w)
#> [1] TRUE

# This implies the contraharmonic mean is larger than the quadratic
# mean, and therefore the Pythagorean means

contraharmonic_mean(x, w) > arithmetic_mean(x, w)
#> [1] TRUE
contraharmonic_mean(x, w) > geometric_mean(x, w)
#> [1] TRUE
contraharmonic_mean(x, w) > harmonic_mean(x, w)
#> [1] TRUE

# ... and the logarithmic mean

contraharmonic_mean(2:3) > logmean(2, 3)
#> [1] TRUE

# 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)
#> [1] 2.818182
contraharmonic_mean(x, w)
#> [1] 2.818182

#---- 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)
#> [1] 2.818182
lehmer_mean(s)(x, w * x^(r - 1) / x^(s - 1))
#> [1] 2.818182

# 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)
#> [1] 2.818182
generalized_mean(s)(x, transmute_weights(1, s)(x, w * x^(r - 1)))
#> [1] 2.818182

# ... and vice versa

lehmer_mean(r)(x, transmute_weights(s, 1)(x, w) / x^(r - 1))
#> [1] 2.568312
generalized_mean(s)(x, w)
#> [1] 2.568312

#---- 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)
#> [1] 0.1818182 1.6363636