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))