Skip to content

Instantly share code, notes, and snippets.

@usptact
Last active November 20, 2018 10:41
Show Gist options
  • Save usptact/263f63f0af1bc41fb7d7ddc7e5e6123c to your computer and use it in GitHub Desktop.
Save usptact/263f63f0af1bc41fb7d7ddc7e5e6123c to your computer and use it in GitHub Desktop.
//
// Zoo: Infer prevalence of 3 animals after a zoo visit
//
// Observed: 3 lions, 2 tigers and 1 bear
//
// Questions:
// - Prevalence of each species
// - Probability of seeing a bear next time
//
var animals = ['lion', 'tiger', 'bear']
var observations = ['lion', 'lion', 'lion', 'tiger', 'tiger', 'bear']
var model = function() {
// set Dirichlet prior
var a = [uniform(0,5), uniform(0,5), uniform(0,5)]
var p = dirichlet(Vector(a))
// set Categorical distribution
var cdist = Categorical({ps: p, vs: animals})
// observe every data point
var obsFn = function(x) {observe(cdist, x)}
mapData({data: observations}, obsFn)
// get animal prevalencies
return {p_lion: p.data[0], p_tiger:p.data[1], p_bear:p.data[2]};
}
var g = Infer({method: 'MCMC', samples: 1000000, burn: 5000}, model);
viz.marginals(g);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment