Skip to content

Instantly share code, notes, and snippets.

@mrcaseb
Created September 19, 2022 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrcaseb/8763e9c7ec94a03293e54976c57baa28 to your computer and use it in GitHub Desktop.
Save mrcaseb/8763e9c7ec94a03293e54976c57baa28 to your computer and use it in GitHub Desktop.
#' Remove Bookmaker’s Vig from a Vector of Probablities
#' @description This function implements the iterative power method to adjust
#' implied probabilities suggested by Vovk and Zhadanov (2009) and Clarke (2016),
#' where bookmakers’ implied probabilities are raised to a fixed power, which
#' never produces bookmaker or fair probabilities outside the 0-1 range
#' (upper limit can be changed) and allows for the favorite long-shot bias.
#' @param probs A vector of probabilities from which to remove the bookmaker’s
#' vig. The probabilities will be adjusted to sum up to `sum_to`.
#' @param overround_limit Allowed tolerance of the sum of adjusted probabilities
#' from the value `sum_to`.
#' @param verbose If `TRUE`, will print current error to track number of iterations.
#' @param sum_to The function will adjust the probabilities of param `probs` to
#' sum up to this value.
#' @seealso The related paper <http://dx.doi.org/10.11648/j.ajss.20170506.12>
#' @export
#' @return Vector of type `double`
#' @examples
#' remove_vig_power(c(0.675, 0.375), verbose = TRUE)
remove_vig_power <- function(probs,
overround_limit = 1e-5,
verbose = FALSE,
sum_to = 1) {
n <- length(probs)
pi <- sum(probs) / sum_to # booksum
error <- abs(pi - 1) # overround
# to check how many iterations were necessary
if (isTRUE(verbose)) cli::cli_alert_info("overround: {.val {error}}")
if (error <= overround_limit) {
return(probs)
}
k <- log(n) / log(n / pi)
new_probs <- probs^k
remove_vig_power(
new_probs,
overround_limit = overround_limit,
verbose = verbose,
sum_to = sum_to
)
}
remove_vig_power(c(0.675, 0.375), verbose = TRUE)
#> ℹ overround: 0.05
#> ℹ overround: 0.00336613043091871
#> ℹ overround: 0.000232916595995114
#> ℹ overround: 1.61467426247341e-05
#> ℹ overround: 1.11950456083143e-06
#> [1] 0.6537594 0.3462418
# Gist URL https://gist.github.com/8763e9c7ec94a03293e54976c57baa28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment