Lexical-Uncertainty RSA¶
Overview¶
Suppose a speaker says Some of the students passed. A listener may infer that some but not all passed, even when some is compatible with every student passing. This Rational Speech Acts (RSA) model compares some with all while allowing the speaker to reason with different meanings for some. At \(\alpha=1\), the listener assigns probability 0.7041 to the world in which some but not all students passed.
QVR source¶
lexical_listener averages the speaker's probabilities across lexica
for each world, then multiplies by the world prior and normalizes.
View the complete QVR source.
# Lexical-Uncertainty Rational Speech Acts
#
# The specificity-implicature example of Bergen, Levy & Goodman (2016),
# section 4.4 and Figure 5, in the known-world form of equations (34)
# to (37):
#
# (34) L0(w | u, L) ∝ L(u, w) P(w)
# (35) U1(u | w, L) = log L0(w | u, L) - c(u)
# (36) S1(u | w, L) ∝ exp(alpha * U1(u | w, L))
# (37) L1(w | u) ∝ P(w) * sum_L P(L) S1(u | w, L)
#
# The paper writes the rationality parameter as lambda; it is alpha
# here. The listener marginalizes over lexica at L1 only: each S1
# reasons about an L0 with one fixed lexicon, as equation (37) requires.
#
# Worlds, with the uniform prior P(w) = 1/2 of Figure 5:
# 0 = all of the students passed (the paper's ∀)
# 1 = some but not all of the students passed (the paper's ∃¬∀)
#
# Utterances, with the Figure 5 costs c(some) = c(all) = 1, c(null) = 5:
# 0 = "some"
# 1 = "all"
# 2 = the null utterance, true in both worlds
#
# Lexica, the enrichment of [[some]] = {∀, ∃¬∀}, [[all]] = {∀} given in
# section 4.4, each with the null utterance and uniform P(L) = 1/3:
# 0 = "some" is true in both worlds (the paper's L1)
# 1 = "some" means some-but-not-all (the paper's L2)
# 2 = "some" means all (the paper's L3)
#
# Section 4.3 lists four ways to keep (34) and (35) well defined and
# adopts the null utterance. The null utterance is present here, and a
# false cell is also set to 10^-6, which is the second of those four
# options. The reference machine's log rejects 0, and a zero cell would
# give an infinite utility whose gradient in alpha is undefined. A
# false cell enters S1 as (10^-6)^alpha, so the relaxation's effect on
# L1 shrinks with alpha: at alpha = 1 it moves L1(all | "some") from
# 0.295874 (exact zeros) to 0.295864; at alpha = 0.5 the gap is 8e-4.
#
# The program puts a log-normal prior on alpha (alpha = 1, the Figure 5
# value, is its median) and conditions on two observations of one
# exchange: whether the speaker said "some" (heard_some, scored by
# marginalizing the six lexicon-world states under P(L) P(w)) and which
# world the listener chose on hearing "some" (listener_choice, scored
# under L1(. | "some")). With heard_some = 1, their product is
# P(w, u = "some") under the generative model the L1 listener inverts.
#
# pragmatic_speaker traverses the alternatives to call literal_listener,
# and lexical_speakers traverses the lexica to call pragmatic_speaker.
index Evidence = Answered | Predict
family Interpretation(e : Evidence) : Type
constructor Chose : Int -> Interpretation(Answered)
constructor Unanswered : Interpretation(Predict)
instance random : Random
instance score : Score
define interpret[e : Evidence](response : Interpretation(e), p_all : Real, p_not_all : Real) : Int !{random, score} =
let listener = Categorical(probs=[p_all, p_not_all])
case response motive (t : Evidence) => Int
Chose(world) =>
perform score.add(log_prob(listener, world))
return world
Unanswered =>
let world <- perform random.sample[Int](site("interpretation"), listener)
return world
define literal_listener(meaning : Real[Fin 2], world_prior : Real[Fin 2]) : Real[Fin 2] !{} =
let masses = map([0, 1], world -> meaning[world] * world_prior[world])
let normalizer = fold(masses, 0.0, total -> mass -> total + mass)
return map(masses, mass -> mass / normalizer)
define utterance_utility(listeners : Real[Fin 3, Fin 2], costs : Real[Fin 3], world : Int, utterance : Int, alpha : Real) : Real !{} =
let listener = listeners[utterance]
return alpha * (log(listener[world]) - costs[utterance])
define speaker_at_world(listeners : Real[Fin 3, Fin 2], costs : Real[Fin 3], world : Int, alpha : Real) : Real[Fin 3] !{} =
let utilities <- traverse(
[0, 1, 2],
utterance -> utterance_utility(listeners, costs, world, utterance, alpha),
)
let log_normalizer = logsumexp_over(utilities, utility -> utility)
return map(utilities, utility -> exp(utility - log_normalizer))
define pragmatic_speaker(some_meaning : Real[Fin 2], all_meaning : Real[Fin 2], world_prior : Real[Fin 2], alpha : Real) : Real[Fin 2, Fin 3] !{} =
let meanings = [some_meaning, all_meaning, [1.0, 1.0]]
let costs = [1.0, 1.0, 5.0]
let listeners <- traverse(meanings, meaning -> literal_listener(meaning, world_prior))
let speaker <- traverse([0, 1], world -> speaker_at_world(listeners, costs, world, alpha))
return speaker
define some_probability(speaker : Real[Fin 2, Fin 3], world : Int) : Real !{} =
let utterance_distribution = speaker[world]
return utterance_distribution[0]
define some_probabilities(speaker : Real[Fin 2, Fin 3]) : Real[Fin 2] !{} =
let probabilities <- traverse([0, 1], world -> some_probability(speaker, world))
return probabilities
define lexical_speakers(alpha : Real, epsilon : Real) : Real[Fin 3, Fin 2] !{} =
let world_prior = [0.5, 0.5]
let all_meaning = [1.0, epsilon]
let some_meanings = [[1.0, 1.0], [epsilon, 1.0], [1.0, epsilon]]
let speakers <- traverse(
some_meanings,
some_meaning -> pragmatic_speaker(some_meaning, all_meaning, world_prior, alpha),
)
let probabilities <- traverse(speakers, speaker -> some_probabilities(speaker))
return probabilities
define lexical_listener(alpha : Real, epsilon : Real) : Real[Fin 2] !{} =
let speakers <- lexical_speakers(alpha, epsilon)
let likelihoods = map(
[0, 1],
world -> fold(
speakers,
0.0,
total -> speaker -> total + speaker[world],
) / real(length(speakers)),
)
let masses = map(likelihoods, likelihood -> 0.5 * likelihood)
let normalizer = fold(masses, 0.0, total -> mass -> total + mass)
return map(masses, mass -> mass / normalizer)
define state_likelihood(speakers : Real[Fin 3, Fin 2], lexicon_index : Int[Fin 6], world_index : Int[Fin 6], state : Int) : Real !{} =
let speaker = speakers[lexicon_index[state]]
return speaker[world_index[state]]
object CommunicationState : FinSet 6
object Event : FinSet 1
object Posterior : Real 2
program lexical_uncertainty_rsa : Event -> Posterior
let raw_alpha <- sample Normal(loc=0.0, scale=1.0)
let alpha = exp(raw_alpha)
let epsilon = 0.000001
let posterior <- lexical_listener(alpha, epsilon)
let p_all = posterior[0]
let p_not_all = posterior[1]
let choice = int(listener_choice)
let response <- interpret[Answered](
(Chose(choice) : Interpretation(Answered)),
p_all,
p_not_all,
)
let speakers <- lexical_speakers(alpha, epsilon)
let states = [0, 1, 2, 3, 4, 5]
let lexicon_index = [0, 0, 1, 1, 2, 2]
let world_index = [0, 1, 0, 1, 0, 1]
let speaker_some <- traverse(
states,
state -> state_likelihood(speakers, lexicon_index, world_index, state),
)
let state_prior = map(states, state -> 1.0 / real(length(states)))
marginalize state : CommunicationState <- Categorical(probs=state_prior) [reduction=logsumexp]
let p_some = speaker_some[state]
observe heard_some : Event <- Bernoulli(probs=p_some)
return posterior
export lexical_uncertainty_rsa
Understanding the model¶
The two worlds have equal prior probability. In world 0 all students passed, and in world 1 some but not all passed. The three equiprobable lexica license some in both worlds, only in world 1, or only in world 0. In every lexicon, all is true in world 0 and the null utterance is true in both worlds.
Given a lexicon \(L\) and an utterance \(u\), the literal listener \(L_0\) weights each world by its prior and the truth weight assigned by \(L\). The program uses a weight of \(1\) for true entries and \(10^{-6}\) for false entries, so the pragmatic speaker can take the log of every literal-listener probability. Given \(w\) and \(L\), that speaker chooses among some, all, and the null utterance with probability proportional to \(\exp(\alpha [\log L_0(w\mid u,L)-c(u)])\). The costs \(c(u)\) are \(1\), \(1\), and \(5\), respectively.
The pragmatic listener conditions on hearing some. The unnormalized mass for each world is
where \(S_1\) is the pragmatic speaker. Dividing \(m_w\) by \(m_0+m_1\) gives the probability that the listener assigns to world \(w\).
Walkthrough¶
Compute the speaker for each lexicon¶
lexical_speakers uses traverse to call pragmatic_speaker with each
candidate meaning for some. Within each call, the speaker normalizes
over the three utterances separately for each world. The resulting table
keeps the probability of some in each of the six lexicon–world pairs.
Score an observed exchange¶
The exported program draws raw_alpha from a standard Normal and sets
\(\alpha=\exp(\texttt{raw\_alpha})\). The Answered case of interpret
scores the observed world under the pragmatic listener, and the
marginalize block scores whether the speaker said some by summing over
the six lexicon–world pairs. When the utterance was some, the product of
these two likelihood terms is \(m_w\) for the chosen world.
Try it¶
Run this from the repository root. Replay raw_alpha = 0 so the speaker
has \(\alpha=1\), then check the returned probabilities and the log joint
against the six pair masses computed directly in probability space.
from math import exp, fsum, isclose, log, pi
from pathlib import Path
from quivers.dsl import Compiler, parse
from quivers.qiec import run_program
source = Path("docs/examples/qiec/lexical-uncertainty-rsa.qvr").read_text()
module = Compiler(parse(source)).qiec_module
run = run_program(
module,
"lexical_uncertainty_rsa",
data={"listener_choice": 0.0, "heard_some": [True]},
sites={"raw_alpha": 0.0},
)
epsilon = 1e-6
true_mass = 1.0 / (1.0 + epsilon)
false_mass = epsilon / (1.0 + epsilon)
all_literal = (true_mass, false_mass)
some_literal = ((0.5, 0.5), (false_mass, true_mass), (true_mass, false_mass))
# At alpha = 1, divide all speaker weights by exp(-1).
null_weight = 0.5 * exp(-4.0)
world_masses = tuple(
fsum(row[w] / (row[w] + all_literal[w] + null_weight) for row in some_literal)
/ 6.0
for w in range(2)
)
expected_posterior = tuple(mass / fsum(world_masses) for mass in world_masses)
expected_log_joint = log(world_masses[0]) - 0.5 * log(2.0 * pi)
for actual, expected in zip(run.value, expected_posterior, strict=True):
assert isclose(float(actual), expected, abs_tol=1e-12)
assert isclose(float(run.log_joint), expected_log_joint, abs_tol=1e-12)
print(f"all: {float(run.value[0]):.4f}, some but not all: {float(run.value[1]):.4f}")