Skip to content

Instantly share code, notes, and snippets.

@usptact
Last active September 15, 2018 04:43
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 usptact/cc6b906aaf3470bda65bd45d6fb6c9d9 to your computer and use it in GitHub Desktop.
Save usptact/cc6b906aaf3470bda65bd45d6fb6c9d9 to your computer and use it in GitHub Desktop.
// 4 dice: 4, 6, 8 and 12 sides
// each dice is perfect
var dice = [
Categorical(
{
ps: [1/4, 1/4, 1/4, 1/4],
vs: [0, 1, 2, 3]
}
),
Categorical(
{
ps: [1/6, 1/6, 1/6, 1/6, 1/6, 1/6],
vs: [0, 1, 2, 3, 4, 5]
}
),
Categorical(
{
ps: [1/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8],
vs: [0, 1, 2, 3, 4, 5, 6, 7]
}
),
Categorical(
{
ps: [1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12],
vs: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
}
)
]
var model_1 = function() {
// pick one of four dice
var sel = sample(Categorical({ps: [1/4, 1/4, 1/4, 1/4], vs: [0, 1, 2, 3]}))
// roll the picked dice twice
var roll1 = sample(dice[sel])
var roll2 = sample(dice[sel])
// two rolls must return the same face
condition(roll1 == roll2)
// roll for the last time
var roll3 = sample(dice[sel])
condition(roll3 == roll2)
// probability of third roll being the same as the first two
return sel
}
var model_2 = function() {
// pick one of four dice
var sel = sample(Categorical({ps: [1/4, 1/4, 1/4, 1/4], vs: [0, 1, 2, 3]}))
// roll the picked dice twice
var roll1 = sample(dice[sel])
var roll2 = sample(dice[sel])
// two rolls must return the same face
condition(roll1 == roll2)
// roll for the last time
var roll3 = sample(dice[sel])
// prob that third roll is the same as the first two
var same = (roll3 == roll2 && roll3 == roll1)
return same
}
display("Probabilities selecting each dice:")
var posterior_1 = Infer({method: 'rejection', samples: 10000}, model_1)
display(posterior_1)
display("Probability of third roll same as the first two")
var posterior_2 = Infer({method: 'rejection', samples: 10000}, model_2)
display(posterior_2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment