-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_gradable_original.wppl
75 lines (60 loc) · 1.85 KB
/
rsa_gradable_original.wppl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This code is from http://gscontras.github.io/ESSLLI-2016/chapters/3-semantics.html
///fold:
var marginalize = function(dist, key){
return Infer({method: "enumerate"}, function(){
return sample(dist)[key];
})
}
///
var book = {
"prices": [2, 6, 10, 14, 18, 22, 26, 30],
"probabilities": [1, 2, 3, 4, 4, 3, 2, 1]
};
var statePrior = function() {
return categorical(book.probabilities, book.prices);
};
var thetaPrior = function() {
return uniformDraw(book.prices);
};
var alpha = 1; // rationality parameter
var utterances = ["expensive", ""];
var cost = {
"expensive": 1,
"": 0
};
var utterancePrior = function() {
var uttProbs = map(function(u) {return Math.exp(-cost[u]) }, utterances);
return categorical(uttProbs, utterances);
};
var meaning = function(utterance, price, theta) {
if (utterance == "expensive") {
return price >= theta;
} else {
return true;
}
};
var literalListener = cache(function(utterance, theta, item) {
return Infer({method: "enumerate"}, function() {
var price = statePrior();
condition(meaning(utterance, price, theta))
return price;
});
});
var speaker = cache(function(price, theta, item) {
return Infer({method: "enumerate"}, function() {
var utterance = utterancePrior();
factor( alpha * literalListener(utterance, theta, item).score(price) );
return utterance;
});
});
var pragmaticListener = function(utterance, item) {
return Infer({method: "enumerate"}, function() {
var price = statePrior();
var theta = thetaPrior();
factor(speaker(price, theta, item).score(utterance));
return { price: price, theta: theta };
});
};
var expensiveBook = pragmaticListener("expensive", "book");
viz.auto(marginalize(expensiveBook, "price"));
viz.auto(marginalize(expensiveBook, "theta"));