Cards

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
alice_chooses_card_model <- '
var alice_chooses_card = function() {
   var card = sample(Categorical({ vs: [1,2,3] }))
   return card
}
var dist = Infer(alice_chooses_card)
expectation(dist)
'
alice_chooses_card_E <- rwebppl::webppl(alice_chooses_card_model)
using webppl version: main v0.9.15-276a71d /Users/jacobzimmerman/ucsd/fyp/memo-sandbox/.pixi/envs/default/lib/R/library/rwebppl/js/webppl
show(alice_chooses_card_E)
[1] 2

Basic conditioning

library(tidyverse)
basic_inference_model <- '
var model = function() {
   // uniform prior of true value, discretized for parity with memo
   var value = randomInteger(101) / 100; // uniform(0, 1)

   // someone has observed that value is >.5
   condition(value > .5)
   
   // return the estimated probability of each possible value
   return value
}
var dist = Infer(model)
dist

// alternatively, output the expected value:
// expectation(dist)
'
posterior <- rwebppl::webppl(basic_inference_model) |>
   arrange(support)

# extra: could compute E of posterior manually like this
# posteriorE <- sum(as.numeric(posterior$prob) * as.numeric(posterior$support))
# posteriorE

posterior |>
   # fill in missing support (HACK: need to round support values to ensure compatibility when calling `complete`)
   mutate(support = round(support, 3)) |>
   complete(support = seq(0, 1, by = 0.01) |> round(3), fill = list(prob = 0)) |>
   ggplot() + geom_line(aes(x=support, y=prob)) + lims(x=c(0,1), y=c(0, NA))