Skip to content

Referent choice and pronoun judgments

Overview

A sentence introduces one of two discourse referents, \(a\) or \(b\). The pronoun either resolves to that referent or picks a fixed alternative \(c\). The QVR program stores the introduced referent in a named state instance. Two choice instances weight the readings with separate logits. The handler for the store runs inside the two choices, so each lexical reading supplies its own value to the pronoun.

QVR source

In the complete QVR source, sentence selects and writes the referent, then selects the pronoun reading and reads back the stored value. The weighted handler resumes both alternatives and sums their answers. The exported judgment program observes one Bernoulli response at the resulting probability.

object Question : FinSet 1
object JudgedProbability : Real 1

effect Choice
    select_reading : Unit -> Bool

effect Referent
    read : Unit -> Bool
    write : Bool -> Unit

instance lexical : Choice
instance pronoun : Choice
instance referent : Referent

handler weighted for Choice : Real -> Real with (probability : Real) [coverage=total, forwards=none, implementation=authored]
    return answer =>
        return answer
    select_reading resumes omega =>
        let first <- resume(true)
        let second <- resume(false)
        return probability * first + (1.0 - probability) * second

handler thread for Referent : Real -> Real with (current : Bool) [coverage=total, forwards=none, implementation=authored]
    return answer =>
        return answer
    read resumes 1 =>
        resume(current)
    write(value : Bool) resumes 1 =>
        resume() with (value)

define sentence() : Real !{lexical, pronoun, referent} =
    let chosen <- perform lexical.select_reading()
    perform referent.write(chosen)
    let use_stored <- perform pronoun.select_reading()
    let stored <- perform referent.read()
    if stored then
        if use_stored then
            return 0.9
        else
            return 0.2
    else
        if use_stored then
            return 0.6
        else
            return 0.3

define truth_probability(lexical_logit : Real, pronoun_logit : Real) : Real !{} =
    handle lexical with weighted(sigmoid(lexical_logit)) in
        handle pronoun with weighted(sigmoid(pronoun_logit)) in
            handle referent with thread(false) in
                sentence()

program judgment(lexical_logit : Real, pronoun_logit : Real) : Question -> JudgedProbability
    let probability <- truth_probability(lexical_logit, pronoun_logit)
    observe truth <- Bernoulli(probs=probability)
    return probability

export judgment

Understanding the model

Likelihood

Let \(p=\operatorname{sigmoid}(\theta)\) be the prior probability of introducing \(a\) and \(\lambda=\operatorname{sigmoid}(\phi)\) the prior probability of resolving to the stored referent. The observation model uses \(m(a,a)=0.9\), \(m(a,c)=0.2\), \(m(b,b)=0.6\), and \(m(b,c)=0.3\) as conditional probabilities of a positive judgment. For \(x\in\{a,b\}\), write \(A_x=\lambda\,m(x,x)+(1-\lambda)\,m(x,c)\). Then the probability of a positive judgment is \(Z=pA_a+(1-p)A_b\).

For a positive judgment, the lexical gradient is

\[ \frac{\partial\log Z}{\partial\theta} = \frac{p A_a}{Z}-p = \Pr(x=a\mid \mathrm{true})-p. \]

For the pronoun logit, the corresponding derivative is the posterior probability of the stored reading minus \(\lambda\). For a negative judgment the same identities hold with \(m\) replaced by \(1-m\). In either case the identities follow by differentiating the Bernoulli prior masses before summing the four products.

Walkthrough

The reference machine makes one lexical request, two pronoun requests, two writes and four reads before it scores the judgment. The four reads have lexical and pronoun resumption paths \((0,0)\), \((0,1)\), \((1,0)\) and \((1,1)\). Discharge removes the handled requests and preserves the address of the single outer score. The ordinary Pyro target executes the source helper and exposes one observed site, truth. Stan emits the discharged probability calculation and the Bernoulli observation.

At \(p=1/3\) and \(\lambda=3/4\), the returned probability is \(71/120\) for either observed judgment. The gradients of the observation's log likelihood with respect to the lexical and pronoun logits are \((16/213,39/284)\) for a positive judgment and \((-16/147,-39/196)\) for a negative judgment.

Try it

Run this from the repository root. The finite sum over readings gives an independent likelihood and computes both posterior counts directly. The reference and discharged programs also agree on the observation score address.

import torch

from quivers.continuous.tensor_backend import TensorBackend
from quivers.dsl import load
from quivers.qiec.builtins import SCORE_ADD
from quivers.qiec.discharge import discharge_module
from quivers.qiec.program_runtime import run_program

module = load("docs/examples/source/referent_judgments.qvr").qiec
assert module is not None
compiled = discharge_module(module)
assert compiled.obstacle("truth_probability") is None
response_prob = torch.tensor([[0.9, 0.2], [0.6, 0.3]], dtype=torch.float64)

for answer in (False, True):
    logits = torch.tensor([0.4, -0.6], dtype=torch.float64, requires_grad=True)
    p, resolution = logits.detach().sigmoid().unbind()
    referents = torch.stack((p, 1 - p))
    pronouns = torch.stack((resolution, 1 - resolution))
    joint = referents[:, None] * pronouns[None, :] * (
        response_prob if answer else 1 - response_prob
    )
    evidence = joint.sum()
    expected_gradient = torch.stack(
        (joint[0, :].sum() / evidence - p, joint[:, 0].sum() / evidence - resolution)
    )
    score_addresses = []
    for evaluator_module in (module, compiled.module):
        run = run_program(
            evaluator_module,
            "judgment",
            data={"lexical_logit": logits[0], "pronoun_logit": logits[1], "truth": answer},
            sites={},
            distributions=TensorBackend(),
        )
        (gradient,) = torch.autograd.grad(run.log_joint, logits)
        torch.testing.assert_close(run.log_joint.detach(), evidence.log())
        torch.testing.assert_close(gradient, expected_gradient)
        addresses = tuple(
            event.detail["address"]
            for event in run.result.trace
            if event.event == "operation.requested"
            and event.detail["operation"] == str(SCORE_ADD)
        )
        assert len(addresses) == 1
        score_addresses.append(addresses)
    assert score_addresses[0] == score_addresses[1]