Skip to content

Instantly share code, notes, and snippets.

@usptact usptact/zoo

Last active Nov 20, 2018
What would you like to do?
// 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:[0],[1],[2]};
var g = Infer({method: 'MCMC', samples: 1000000, burn: 5000}, model);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.