Skip to content

Instantly share code, notes, and snippets.

@dylnb
Last active May 16, 2019 02:22
Show Gist options
  • Save dylnb/299a686763fe243c01001cff3b409ede to your computer and use it in GitHub Desktop.
Save dylnb/299a686763fe243c01001cff3b409ede to your computer and use it in GitHub Desktop.
var people = {
"heights": [2, 6, 10, 14, 18, 22, 26, 30],
"probabilities": [1, 2, 3, 4, 4, 3, 2, 1]
};
// generate a state of the form [john's height, bill's height]
var statePrior = function() {
return [categorical(people.probabilities, people.heights),categorical(people.probabilities, people.heights)];
};
var thetaPrior = function() {
return uniformDraw(people.heights);
};
var utterances = ["asTall", "asShort", "tall", "short", ""];
var cost = {
"asTall": 1,
"asShort": 2,
"tall" : 1,
"short" : 2,
"": 0
};
var utterancePrior = function() {
return uniformDraw(utterances);
};
var meaning = function(utterance, state, thetaTall, thetaShort) {
utterance == "" ? true :
utterance == "tall" ? state[0] >= thetaTall :
utterance == "short" ? state[0] <= thetaShort :
state[0] == state[1] // you don't have the height in mind
// state[0] == 18 // you have the height in mind
};
var QUDs = ["tall?","short?","height?"]
var QUDprior = function(){
return uniformDraw(QUDs)
}
var QUDfns = {
"tall?" : function(state,thetaTall,thetaShort){return state[0]>=thetaTall},
"short?" : function(state,thetaTall,thetaShort){return state[0]<=thetaShort},
"height?" : function(state,thetaTall,thetaShort){return state[0]}
}
var literalListener = cache(function(utterance,thetaTall,thetaShort, QUD) {
return Infer({method: "enumerate"}, function() {
var state = statePrior();
var QUDfn = QUDfns[QUD]
condition(meaning(utterance, state, thetaTall,thetaShort))
return QUDfn(state,thetaTall,thetaShort);
});
});
var speaker = cache(function(state,thetaTall,thetaShort, QUD, alpha) {
return Infer({method: "enumerate"}, function() {
var utterance = utterancePrior();
var QUDfn = QUDfns[QUD]
factor( alpha * (literalListener(utterance,thetaTall,thetaShort, QUD).score(QUDfn(state,thetaTall,thetaShort))
- cost[utterance]));
return utterance;
});
});
var pragmaticListener = function(utterance, alpha) {
return Infer({method: "enumerate"}, function() {
var state = statePrior();
var thetaTall = thetaPrior();
var thetaShort = thetaPrior();
var QUD = QUDprior();
factor(speaker(state,thetaTall,thetaShort, QUD, alpha).score(utterance));
return { state: state[0] , qud: QUD};
});
};
var diff = function(i) {
var t = expectation(marginalize(pragmaticListener("asTall", i), "state"));
var s = expectation(marginalize(pragmaticListener("asShort", i), "state"));
return t - s;
}
map(function(i) {display("alpha = " + i); display(diff(i))}, [1,2,3,4,5,6,7,8,9,10])
// alpha = 1
// 3.552713678800501e-15
// alpha = 2
// 5.329070518200751e-15
// alpha = 3
// 1.7763568394002505e-15
// alpha = 4
// 1.7763568394002505e-15
// alpha = 5
// -1.0658141036401503e-14
// alpha = 6
// -8.881784197001252e-15
// alpha = 7
// 1.2434497875801753e-14
// alpha = 8
// -1.7763568394002505e-14
// alpha = 9
// -2.3092638912203256e-14
// alpha = 10
// 3.907985046680551e-14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment