Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ and the request cost before using your key.
## Start with an offline tutorial

[Rebuild what was known at a decision time](tutorials/point-in-time/README.md)
replays late observations and withdrawals using base R. Try six cutoffs in
your browser or run the same example offline.
compares the price a backtest would assign with the quote the analyst had received.
Try eight cutoffs in your browser, inspect the [illustrated timeline](https://jacobiusmakes.github.io/parlayapi-notebooks/point-in-time-replay/#compare-eight-decision-times), or run the fictional replay offline.

[Choose the right fixture for a two-sided report](tutorials/fixture-report/README.md)
uses two games between the same teams. Download the base-R exercise to see
Expand Down
14 changes: 12 additions & 2 deletions tutorials/point-in-time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

At 14:00:04, an analyst records the home team's price as **1.90**. The archive later shows a **2.10** update stamped 14:00:03. Was 1.90 a mistake? Check when the update arrived. The analyst received it at 14:00:06, two seconds after the decision. Giving the analyst 2.10 in a backtest introduces look-ahead bias.

[Try the six decision times in your browser][interactive]. The interactive version loads R and Shiny in your browser and uses the six fictional messages below. For the offline version, download [replay.R](replay.R) and run `Rscript replay.R` with base R.
[Compare the two clocks at eight decision times in your browser][interactive]. The interactive version loads R and Shiny in your browser and uses the six fictional messages below. For the offline version, download [replay.R](replay.R) and run `Rscript replay.R` with base R.

| Selection | Source updated (UTC) | Received (UTC) | Decimal price |
|---|---|---|---:|
Expand All @@ -13,6 +13,16 @@ At 14:00:04, an analyst records the home team's price as **1.90**. The archive l
| Home | 14:00:08 | 14:00:09 | Withdrawn |
| Home | 14:00:10 | 14:00:11 | 1.97 |

Three cutoffs show how a research result changes when the backtest treats a source update as immediately available.

| Decision time (UTC) | Received by the analyst | Source clock alone |
|---|---|---|
| 14:00:04 | 1.90 | 2.10 |
| 14:00:08 | 2.10 | Withdrawn |
| 14:00:10 | Withdrawn | 1.97 |

The withdrawal took one second to reach the analyst. So did the next quote. Even a message that removes a price can change which decision a backtest would have allowed.

Each selection here belongs to the same fictional event, bookmaker, period and moneyline market. In a real log, identify that entire stream before comparing records. Both clocks in this example share a UTC time base. The receipt timestamp records when the observation became available to this analyst.

For a worked example with two games between the same teams, try [choosing the right fixture for a two-sided report](../fixture-report/README.md). An editor has several away prices in hand and must decide which one belongs in the report.
Expand Down Expand Up @@ -104,6 +114,6 @@ To repeat this replay on a real feed, save each quote's source time and the time

For work with a real feed, record the two timestamp definitions and the selection identifiers in the [ParlayAPI evaluation lab](https://parlay-api.com/tools/evaluation-lab?utm_source=github&utm_medium=r_tutorial&utm_campaign=point_in_time_replay). ParlayAPI maintains this tutorial. The six teaching messages are fictional. The optional capture reads the public NBA preview.

[interactive]: https://shinylive.io/r/app/#code=NobwRAdghgtgpmAXGKAHVA6ASmANGAYwHsIAXOMpMAGwEsAjAJykYE8AKAZwAtaJWAlAB0InIgFdGBOOyFhGcVNSitsc3AAJqRAlGoaAvBoAqWAKoBRYRBHaA5hoA8AWg1wAHrCVwA%2BvfbW4rROrgBm1EEAJgAKUHYyIhoapHGcACQwUHxcpKzUcIYacpnuzgDutJGk3IgALAAcCjAA3JmMdnyIAExNGlDipETNqFCRkXx2iAAMGgCMTeqJSRrcs7JgAOq8BNwaAI7iROQro32MjLQAbnCRAPxyArhLSajrWIrKrH0aobQEpLQSHoVkR4M5yLANDASHA8nw4BgNABZIjXZLcAqROAEWicQEQZK0eDJIgaThwAplba7VAXaTogpQaDUVicUgnSIaBTSK43DAPJ4E5bMcZEABCA0GonW5OIEEi6iKYAAItjcfjCcT2GZjABhYR4Z7LY0mpI7Ih-OCcQrk0gAOVgVvYBHYU00tU0ADZNAB2TQATk0s1mjyNpvDEcjLygbLgU3Ws1q0ymiEVLrkUymioztWzYCmnrzUx9Rf9eeDDwEoaFUZN5Py-xuhQ9Gj4dAgBSMpks1eN3C67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ukMbOFy7UdMOU7dKOyBGViLJOXAT9qHELDXkmpwPavNeaKqAW%2FQa0Wxn7HHl1oHEF3WrPJRavfFeOKtY5l9ApwDp6Pxz1i88DGIXgcc8su0qPNeHR6AAnUU8X7q%2BB8WObrge0a2FCDmKwHiZcTLyqukHnxpg92B9lfiGMs%2FEF2ptuh6N0pcPbe3J%2BdvY8Tzdb4oIaOlsryMrpNOM4YniRkE5BduQMxJB4xu0dakv0yrUeauUUnBBqU6Uic8wtHYOvGHE%2Bs7aBmGFQKrqFg%2F0yHWRQUR2AriFftozpvWA3ZP8%2FrSr42pNCs2w06rtRN7iQe4J6Co%2F%2B4ambmVd3l5d%2B8L%2F61YvfqAZuLrys%2FBUe7pMFfgJ1YYwuG2y7NHUfh1AWYeKjd8zhXgFc9PTwL8B787g%3D&jz
41 changes: 29 additions & 12 deletions tutorials/point-in-time/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,50 @@ log <- example_log()
ui <- fluidPage(
tags$main(style = "max-width:48rem;margin:2rem auto;padding:0 1rem",
h1("Which quote had arrived?"),
p("Replay a fictional home-team moneyline. Move the decision time to see which price the analyst had received."),
p("A fictional basketball game has six quote messages. Pick a decision time to compare the home price the analyst had received with the price a backtest would assign using source timestamps alone."),
radioButtons("second", "Decision time (UTC)",
choices = setNames(c(0, 4, 6, 7, 9, 11),
paste0("14:00:", c("00", "04", "06", "07", "09", "11"))),
choices = setNames(c(0, 4, 6, 7, 8, 9, 10, 11),
paste0("14:00:", c("00", "04", "06", "07", "08", "09", "10", "11"))),
selected = 4, inline = TRUE),
h2(`aria-live` = "polite", textOutput("answer", inline = TRUE)),
tags$div(`aria-live` = "polite", `aria-atomic` = "true",
fluidRow(
column(6, h2("Received by the analyst"), p(strong(textOutput("answer", inline = TRUE)))),
column(6, h2("Source clock alone"), p(strong(textOutput("source_answer", inline = TRUE))))
)
),
p(textOutput("explanation", inline = TRUE)),
h3("Messages received by the decision time"),
tableOutput("arrivals")
tags$div(style = "overflow-x:auto;max-width:100%", role = "region",
`aria-label` = "Received quote messages", tabindex = "0",
tableOutput("arrivals")),
p("Try this with an actual game. ",
a("Inspect the public NBA preview",
href = "https://parlay-api.com/try?utm_source=shinylive&utm_medium=r_tutorial&utm_campaign=r_clock_comparison",
target = "_blank", rel = "noopener noreferrer"),
" and record when your client receives the response. The market timestamp in the response is a separate observation.")
)
)
server <- function(input, output, session) {
cutoff <- reactive(example_start() + as.integer(input$second))
result <- reactive(quote_at(log, "home", cutoff()))
output$answer <- renderText({
r <- result()
if (r$state == "quoted") return(sprintf("Home price: %.2f", r$price))
if (r$state == "withdrawn") return("Home quote withdrawn")
"No home quote received"
})
display_quote <- function(row) {
switch(row$state,
quoted = sprintf("%.2f decimal", row$price),
unseen = "No quote yet",
withdrawn = "Withdrawn",
conflict = "Conflicting messages"
)
}
output$answer <- renderText(display_quote(quote_at(log, "home", cutoff())))
output$source_answer <- renderText(display_quote(quote_at(log, "home", cutoff(), clock = "updated")))
output$explanation <- renderText({
switch(input$second,
"0" = "The log contains later quotes. None had arrived when this decision was made.",
"4" = "The 2.10 update was created at 14:00:03. It arrives two seconds after this decision.",
"6" = "The 2.10 update arrives exactly at the cutoff and is now eligible.",
"7" = "A delayed 1.85 message just arrived. Its source update is older than the 2.10 update.",
"8" = "The source stamped a withdrawal at 14:00:08. The analyst still saw 2.10 until the message arrived one second later.",
"9" = "The source withdrew the quote. Carrying 2.10 forward would invent an available price.",
"10" = "The source stamped a new 1.97 quote at 14:00:10. The analyst still saw a withdrawal until 14:00:11.",
"11" = "The source issued a new quote after the withdrawal."
)
})
Expand Down
5 changes: 3 additions & 2 deletions tutorials/point-in-time/replay.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ example_log <- function() {
)
}

quote_at <- function(log, stream, cutoff) {
quote_at <- function(log, stream, cutoff, clock = c("received", "updated")) {
clock <- match.arg(clock)
stopifnot(is.data.frame(log),
all(c("stream", "updated", "received", "state", "price") %in% names(log)),
is.character(stream), length(stream) == 1L, !is.na(stream), nzchar(stream),
Expand All @@ -28,7 +29,7 @@ quote_at <- function(log, stream, cutoff) {
stopifnot(all(is.finite(log$price[quoted]) & log$price[quoted] > 1),
all(is.na(log$price[!quoted])))

known <- log[log$stream == stream & log$received <= cutoff, , drop = FALSE]
known <- log[log$stream == stream & log[[clock]] <= cutoff, , drop = FALSE]
result <- data.frame(stream = stream, cutoff = cutoff, state = "unseen",
updated = as.POSIXct(NA_real_, origin = "1970-01-01", tz = "UTC"),
price = NA_real_, stringsAsFactors = FALSE)
Expand Down
9 changes: 9 additions & 0 deletions tutorials/point-in-time/test_replay.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ bad$received[1] <- bad$updated[1] - 1
stopifnot(inherits(try(at(4, bad), silent = TRUE), "try-error"))
bad$received[1] <- as.POSIXct(NA)
stopifnot(inherits(try(at(4, bad), silent = TRUE), "try-error"))
# Filtering by source time is a retrospective view. Receipt time is the default.
from_source <- function(s, rows = log) quote_at(rows, "home", example_start() + s, clock = "updated")
stopifnot(from_source(0)$state == "unseen", from_source(4)$price == 2.10,
at(8)$price == 2.10, from_source(8)$state == "withdrawn",
at(10)$state == "withdrawn", from_source(10)$price == 1.97,
from_source(6)$price == at(6)$price, from_source(7)$price == 2.10,
from_source(9)$state == at(9)$state, from_source(11)$price == at(11)$price,
from_source(4, conflicting)$state == "conflict",
is.na(from_source(4, conflicting)$price))
cat("Point-in-time replay checks passed.\n")
Loading