Skip to content

Commit

Permalink
Added hybrid CSWD index
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Aug 6, 2024
1 parent 6553c19 commit 94c2224
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gpindex
Title: Generalized Price and Quantity Indexes
Version: 0.6.1.9002
Version: 0.6.1.9003
Authors@R: c(
person("Steve", "Martin", role = c("aut", "cre", "cph"),
email = "[email protected]",
Expand Down
49 changes: 36 additions & 13 deletions R/price_indexes.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pythagorean_index <- function(r) {
"Carli", "Dutot", "Laspeyres",
"Palgrave", "Drobisch", "Unnamed",
"Walsh1", "MarshallEdgeworth", "GearyKhamis",
"Lowe", "Young"
"Lowe", "Young", "HybridCSWD"
)
)
gen_mean <- generalized_mean(r)
Expand Down Expand Up @@ -60,6 +60,9 @@ pythagorean_index <- function(r) {
},
Young = function(p1, p0, pb, qb, na.rm = FALSE) {
gen_mean(p1 / p0, weights(pb, qb), na.rm)
},
HybridCSWD = function(p1, p0, na.rm = FALSE) {
gen_mean(p1 / p0, weights(p1, p0), na.rm)
}
)
}
Expand Down Expand Up @@ -90,6 +93,7 @@ pythagorean_index <- function(r) {
#' - Rao
#' - Lowe
#' - Young
#' - Hybrid-CSWD
#'
#' The weights need not sum to 1, as this normalization isn't always
#' appropriate (i.e., for the Vartia-I weights).
Expand All @@ -103,15 +107,29 @@ pythagorean_index <- function(r) {
#'
#' @note
#' Naming for the indexes and weights generally follows the CPI manual (2020),
#' Balk (2008), and Selvanathan and Rao (1994). In several cases two or more
#' names correspond to the same weights (e.g., Paasche and Palgrave, or
#' Sato-Vartia and Vartia-II). The calculations are given in the examples.
#' Balk (2008), von der Lippe (2007), and Selvanathan and Rao (1994). In several
#' cases two or more names correspond to the same weights (e.g., Paasche and
#' Palgrave, or Sato-Vartia and Vartia-II). The calculations are given in the
#' examples.
#'
#' @seealso
#' [update_weights()] for price-updating weights.
#'
#' [quantity_index()] to remap the arguments in these functions for a
#' quantity index.
#'
#' @references
#' Balk, B. M. (2008). *Price and Quantity Index Numbers*.
#' Cambridge University Press.
#'
#' IMF, ILO, Eurostat, UNECE, OECD, and World Bank. (2020).
#' *Consumer Price Index Manual: Concepts and Methods*.
#' International Monetary Fund.
#'
#' von der Lippe, P. (2007). *Index Theory and Price Statistics*. Peter Lang.
#'
#' Selvanathan, E. A. and Rao, D. S. P. (1994).
#' *Index Numbers: A Stochastic Approach*. MacMillan.
#'
#' @examples
#' p0 <- price6[[2]]
Expand Down Expand Up @@ -227,6 +245,10 @@ pythagorean_index <- function(r) {
#' # Young
#'
#' all.equal(index_weights("Young")(pb, qb), pb * qb)
#'
#' # Hybrid CSWD (to approximate a CSWD index)
#'
#' all.equal(index_weights("HybridCSWD")(p1, p0), sqrt(p0 / p1))
#'
#' @family price index functions
#' @export
Expand All @@ -239,7 +261,7 @@ index_weights <- function(
"Walsh1", "Walsh2", "MarshallEdgeworth",
"GearyKhamis", "Vartia1", "MontgomeryVartia",
"Vartia2", "SatoVartia", "Theil", "Rao",
"Lowe", "Young"
"Lowe", "Young", "HybridCSWD"
)) {
switch(match.arg(type),
Carli = ,
Expand Down Expand Up @@ -293,7 +315,8 @@ index_weights <- function(
w0 <- scale_weights(p0 * q0)
w1 <- scale_weights(p1 * q1)
w0 * w1 / (w0 + w1)
}
},
HybridCSWD = function(p1, p0) sqrt(p0 / p1)
)
}

Expand All @@ -318,6 +341,7 @@ index_weights <- function(
#' - Geary-Khamis
#' - Lowe
#' - Young
#' - Hybrid-CSWD
#' - **Geometric indexes**
#' - Jevons
#' - Geometric Laspeyres (or Jöhr)
Expand Down Expand Up @@ -348,11 +372,12 @@ index_weights <- function(
#' functions.
#'
#' In addition to these indexes, there are also functions for calculating a
#' variety of indexes not based on generalized means. The Fisher index is the
#' variety of indexes based on nested generalized means. The Fisher index is the
#' geometric mean of the arithmetic Laspeyres and Paasche indexes; the Harmonic
#' Laspeyres Paasche index is the harmonic analog of the Fisher index (8054 on
#' Fisher's list). The Carruthers-Sellwood-Ward-Dalen and
#' Carruthers-Sellwood-Ward-Dalen-Balk indexes are sample analogs of the Fisher
#' Laspeyres Paasche (or Harmonic Paasche Laspeyres) index is the harmonic
#' analog of the Fisher index (8054 on Fisher's list). The
#' Carruthers-Sellwood-Ward-Dalen and Carruthers-Sellwood-Ward-Dalen-Balk
#' indexes are sample analogs of the Fisher
#' index; the Balk-Walsh index is the sample analog of the Walsh index. The AG
#' mean index is the arithmetic or geometric mean of the geometric and
#' arithmetic Laspeyres indexes, weighted by the elasticity of substitution.
Expand Down Expand Up @@ -463,9 +488,7 @@ index_weights <- function(
#'
#' all.equal(
#' scale_weights(index_weights("HybridLaspeyres")(p1, q0)),
#' scale_weights(
#' transmute_weights(1, -1)(p1 / p0, index_weights("Laspeyres")(p0, q0))
#' )
#' transmute_weights(1, -1)(p1 / p0, index_weights("Laspeyres")(p0, q0))
#' )
#'
#' # This strategy can be used to make more exotic indexes, like the
Expand Down
28 changes: 24 additions & 4 deletions man/index_weights.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/price_indexes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions tests/Examples/gpindex-Ex.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,12 @@ Warning in back_period(period) :
>
> all.equal(index_weights("Young")(pb, qb), pb * qb)
[1] TRUE
>
>
> # Hybrid CSWD (to approximate a CSWD index)
>
> all.equal(index_weights("HybridCSWD")(p1, p0), sqrt(p0 / p1))
[1] TRUE
>
>
>
>
Expand Down Expand Up @@ -1188,9 +1193,7 @@ t5 1.6720 1.0712 1.2477 0.9801 1.1850 1.2540 1.2678 0.9770
>
> all.equal(
+ scale_weights(index_weights("HybridLaspeyres")(p1, q0)),
+ scale_weights(
+ transmute_weights(1, -1)(p1 / p0, index_weights("Laspeyres")(p0, q0))
+ )
+ transmute_weights(1, -1)(p1 / p0, index_weights("Laspeyres")(p0, q0))
+ )
[1] TRUE
>
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-price-indexes.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ test_that("arithmetic indexes work", {
sum(p1 * qb) / sum(p0 * qb))
expect_equal(arithmetic_index("Young")(p1, p0, pb, qb),
weighted.mean(p1 / p0, pb * qb / sum(pb * qb)))
expect_equal(arithmetic_index("HybridCSWD")(p1, p0),
sum(sqrt(p1 / p0)) / sum(sqrt(p0 / p1)))
})

test_that("geometric indexes work", {
Expand Down

0 comments on commit 94c2224

Please sign in to comment.