# logarithmic_means: Logarithmic means In gpindex: Generalized Price and Quantity Indexes

## Description

Calculate a generalized logarithmic mean / extended mean.

## Usage

 ```1 2 3 4 5``` ```mean_extended(r, s) logmean_generalized(r) logmean(a, b, tol = .Machine\$double.eps^0.5) ```

## Arguments

 `r, s` A finite number giving the order of the generalized logarithmic mean / extended mean. `a, b` A strictly positive numeric vector. `tol` The tolerance used to determine if `a == b`.

## Details

The function `mean_extended()` returns a function to compute the extended mean of `a` and `b` of orders `r` and `s`. See Bullen (2003, p. 393) for a definition. This is also called the difference mean, Stolarsky mean, or extended mean-value mean.

The function `logmean_generalized()` returns a function to compute the generalized logarithmic mean of `a` and `b` of order `r`. See Bullen (2003, p. 385) for a definition, or https://en.wikipedia.org/wiki/Stolarsky_mean. The generalized logarithmic mean is a special case of the extended mean, corresponding to `mean_extended(r, 1)()`, but is more commonly used for price indexes.

The function `logmean()` returns the ordinary logarithmic mean, and corresponds to `logmean_generalized(1)()`.

Both `a` and `b` should be strictly positive. This is not enforced, but the results may not make sense when the generalized logarithmic mean / extended mean is not defined.

By definition, the generalized logarithmic mean / extended mean of `a` and `b` is `a` when `a == b`. The `tol` argument is used to test equality by checking if `abs(a - b) <= tol`. The default value is the same as in `all.equal()`. Setting `tol = 0` will test for exact equality, but can give misleading results when `a` and `b` are computed values.

## Value

`logmean_generalized()` and `mean_extended()` return a function:

`function(a, b, tol = .Machine\$double.eps^0.5)`.

`logmean()` returns a numeric vector the same length as `max(length(a), length(b))`.

## Warning

Passing very small values for `r` or `s` can give misleading results, and warning is given whenever `abs(r)` or `abs(s)` is sufficiently small. Similarly, values for `r` and `s` that are very close in value, but not equal, can give misleading results. In general, `r` and `s` should not be computed values.

## Note

`logmean_generalized()` can be defined on the extended real line, so that `r = -Inf/Inf` returns `pmin()`/`pmax()`, to agree with the definition in, e.g., Bullen (2003). This is not implemented, and `r` must be finite as in the original formulation by Stolarsky (1975).

## References

Balk, B. M. (2008). Price and Quantity Index Numbers. Cambridge University Press.

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

Stolarsky, K. B. (1975). Generalizations of the Logarithmic Mean. Mathematics Magazine, 48(2): 87-92.

`mean_generalized` for the generalized mean.
`weights_transmute` uses the extended mean to turn an r-generalized mean into an s-generalized mean.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77``` ```# Make some data x <- 8:5 y <- 1:4 # The arithmetic and geometric means are special cases of the generalized # logarithmic mean all.equal(logmean_generalized(2)(x, y), (x + y) / 2) all.equal(logmean_generalized(-1)(x, y), sqrt(x * y)) # The logarithmic mean lies between the arithmetic and geometric means # because the generalized logarithmic mean is increasing in r all(logmean(x, y) < (x + y) / 2) & all(logmean(x, y) > sqrt(x * y)) # The harmonic mean cannot be expressed as a logarithmic mean, but can be # expressed as an extended mean all.equal(mean_extended(-2, -1)(x, y), 2 / (1 / x + 1 / y)) # The quadratic mean is also a type of extended mean all.equal(mean_extended(2, 4)(x, y), sqrt(x^2 / 2 + y^2 / 2)) # As are heronian and centroidal means all.equal(mean_extended(0.5, 1.5)(x, y), (x + sqrt(x * y) + y) / 3) all.equal(mean_extended(2, 3)(x, y), 2 / 3 * (x^2 + x * y + y^2) / (x + y)) #-------------------- # The logarithmic mean can be approximated as a convex combination of the # arithmetic and geometric means that gives more weight to the geometric mean approx1 <- 1 / 3 * (x + y) / 2 + 2 / 3 * sqrt(x * y) approx2 <- ((x + y) / 2)^(1 / 3) * (sqrt(x * y))^(2 / 3) approx1 - logmean(x, y) # always a positive approximation error approx2 - logmean(x, y) # a negative approximation error in this case # A better approximation correction <- (log(x / y) / pi)^4 / 32 approx1 / (1 + correction) - logmean(x, y) #-------------------- # A useful identity for turning an additive change into a proportionate # change all.equal(logmean(x, y) * log(x / y), x - y) # Works for other orders, too r <- 2 all.equal(logmean_generalized(r)(x, y)^(r - 1) * (r * (x - y)), (x^r - y^r)) # Some other identities all.equal(logmean_generalized(-2)(1, 2), (mean_harmonic(1:2) * mean_geometric(1:2)^2)^(1 / 3)) all.equal(logmean_generalized(0.5)(1, 2), (mean_arithmetic(1:2) + mean_geometric(1:2)) / 2) all.equal(logmean(1, 2), mean_geometric(1:2)^2 * logmean(1, 1/2)) #-------------------- # Logarithmic means can be represented as integrals logmean(2, 3) stats::integrate(function(t) 2^(1 - t) * 3^t, 0, 1)\$value 1 / stats::integrate(function(t) 1 / (2 * (1 - t) + 3 * t), 0, 1)\$value ```