--- title: "Line shopping and devigging in R" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Line shopping and devigging in R} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include=FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") ``` The Comets are playing the Owls. North lists the Comets at **−110** and South lists them at **+105**. Which book offers the higher payout for the same outcome? What would you need to believe about the Comets to take that price? The package includes both fictional books' quotes at one observation time. You can run the exercise offline after installation. A second game between the same teams and a point spread sit in the file alongside the moneyline. ## Find the right game and market ```{r} library(parlayapiR) fixture <- system.file("extdata", "moneyline-rehearsal.json", package = "parlayapiR", mustWork = TRUE) games <- jsonlite::fromJSON(fixture) game <- games[games$id == "rehearsal-1", ] stopifnot(nrow(game) == 1L) game[, c("id", "commence_time", "away_team", "home_team")] ``` Each book has a list of markets. Select the full-game moneyline (`h2h`) by key. North lists its spread first. South lists the two moneyline outcomes in the opposite order. ```{r} books <- game$bookmakers[[1]] teams <- c(game$home_team, game$away_team) quotes <- do.call(rbind, lapply(seq_len(nrow(books)), function(i) { markets <- books$markets[[i]] market <- markets[markets$key == "h2h", ] stopifnot(nrow(market) == 1L) sides <- market$outcomes[[1]] stopifnot(nrow(sides) == 2L, setequal(sides$name, teams)) data.frame(book = books$key[i], team = sides$name, american = sides$price) })) quotes$decimal <- pa_american_to_decimal(quotes$american) quotes ``` ## Compare the offers Take the greatest decimal price for each team. Joining the maximum back to the quotes retains every book in the event of a tie. ```{r} maxima <- aggregate(decimal ~ team, quotes, max) best <- merge(maxima, quotes, by = c("team", "decimal")) best[, c("team", "book", "american", "decimal")] ``` South offers **2.05** on the Comets. North offers about **1.91** on the Owls. At South's price, a winning one-unit Comets wager would return 2.05 units including the stake. ## Remove one book's margin Use both sides from the same book as a reference. At −110 on each side, North's implied probabilities add up to about 104.76%. The excess above 100% is the overround. The multiplicative method divides each implied probability by their sum. ```{r} north <- quotes[quotes$book == "north", ] pa_hold(north$american, odds_format = "american") fair <- pa_devig(north$american, odds_format = "american") reference <- data.frame(team = north$team, estimate = fair$fair_prob) reference ``` Both estimates are 50% because North priced the two sides equally. You can compare the additive and power methods by passing `method = "additive"` or `method = "power"` to `pa_devig()`. A probability inferred from one book is a modeling assumption. ## Test the assumption ```{r} comets_price <- maxima$decimal[maxima$team == "Comets"] north_estimate <- reference$estimate[reference$team == "Comets"] estimate <- c(north_estimate, 0.48) data.frame(estimate, expected_net_units = round(estimate * comets_price - 1, 3), quarter_kelly_fraction = round(pa_kelly(estimate, comets_price, fraction = 0.25), 4)) ``` At 50%, the estimated net return on one unit is +0.025 units. At 48%, it falls to −0.016. The break-even probability is `1 / 2.05`, about 48.78%. The last column shows how sensitive fractional Kelly is to that assumption. These two scenarios use invented prices and establish no betting advantage. ## Inspect a current basketball board [Open the public NBA preview](https://parlay-api.com/try) to inspect a small current sample without a key. Check the event ID, teams, book, market, priced outcome and available update time before comparing offers. An empty result tells you only what that request returned. For a metered account request, [open signup](https://parlay-api.com/signup?utm_source=r_package&utm_medium=vignette&utm_campaign=r_moneyline_report) and set `PARLAY_API_KEY` privately in your environment. Run the following code when you want to request the NBA moneyline board against your allowance. The explicit `odds_format` requests the units used in the exercise. ```{r, eval=FALSE} if (!nzchar(trimws(Sys.getenv("PARLAY_API_KEY")))) { stop("Set PARLAY_API_KEY in your private environment first.") } board <- pa_odds("basketball_nba", markets = "h2h", odds_format = "american", sandbox = FALSE) fields <- c("id", "away_team", "home_team") if (identical(board, list())) { message("This request returned no events.") } else if (is.data.frame(board) && all(fields %in% names(board))) { head(board[, fields, drop = FALSE]) } else { stop("Expected an array of events with IDs and team names.") } ``` Books and outcomes can be absent in a real response. For archive evaluation, check [historical coverage](https://parlay-api.com/historical-coverage), the desired sport and date, and your plan's access before making a keyed historical request.