Last active
August 26, 2018 04:31
-
-
Save usptact/7852b2971918daf23f333e0573a74514 to your computer and use it in GitHub Desktop.
Agentmodels, WebPPL: Monty Hall Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remove each element in array ys from array xs | |
var remove = function(xs, ys) { | |
return _.without.apply(null, [xs].concat(ys)); | |
}; | |
var doors = [1, 2, 3]; | |
// Monty chooses a door that is neither Alice's door | |
// nor the prize door | |
var monty = function(aliceDoor, prizeDoor) { | |
return Infer({ | |
model() { | |
var door = uniformDraw(doors); | |
condition(door != aliceDoor); | |
condition(door != prizeDoor); | |
return door; | |
}}); | |
}; | |
var actions = ['switch', 'stay']; | |
// If Alice switches, she randomly chooses a door that is | |
// neither the one Monty showed nor her previous door | |
var transition = function(state, action) { | |
if (action === 'switch') { | |
return { | |
prizeDoor: state.prizeDoor, | |
montyDoor: state.montyDoor, | |
aliceDoor: uniformDraw(remove(doors,[state.aliceDoor,state.montyDoor])) | |
}; | |
} else { | |
return state; | |
} | |
}; | |
// Utility is high (say 10) if Alice's door matches the | |
// prize door, 0 otherwise. | |
var utility = function(state) { | |
if (state.aliceDoor === state.prizeDoor) | |
return 10; | |
else | |
return 0; | |
}; | |
var sampleState = function() { | |
var aliceDoor = uniformDraw(doors); | |
var prizeDoor = uniformDraw(doors); | |
return { | |
aliceDoor, | |
prizeDoor, | |
montyDoor: sample(monty(aliceDoor, prizeDoor)) | |
} | |
} | |
var agent = function() { | |
var action = uniformDraw(actions); | |
var expectedUtility = function(action){ | |
return expectation(Infer({ | |
model() { | |
var state = sampleState(); | |
return utility(transition(state, action)); | |
}})); | |
}; | |
factor(expectedUtility(action)); | |
return { action }; | |
}; | |
viz(Infer({ model: agent })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment