Lectures onType Theory
Chapter 175
Chapter 175Optional

Inference as Semantics-Preserving Program Transformation

Prerequisites. Direct starred prerequisites: chapter 174. No later core chapter depends on this route.

Take the model obs=Hcoin(1/2);score(if H then 3 else 1);return H. Its unnormalized meaning is the finite measure {tt3/2, ff1/2}, of total mass 2, and its posterior is {tt3/4, ff1/4}.

Now run an inference procedure on it. Draw two particles from the prior, weigh them by the score, and resample: with luck both particles are tt, and the procedure reports tt with frequency 1. With other luck it reports 3/4. The output is a random object, and no single run refutes or confirms anything. The question the chapter answers is not whether a run looks plausible but whether the procedure, as a transformation of representations, preserves the measure that the model denotes.

The obstruction is that the object preserved is not the one the procedure manipulates. A procedure manipulates particles, weights, and suspended computations; the model denotes a measure. A correctness statement must therefore say what measure a pile of weighted particles means, and then require every step of the procedure to leave that measure unchanged. Once meaning is a function from representations to measures, correctness becomes an equation that composes, and a large algorithm becomes a composite of small steps each of which satisfies it. This chapter builds the meaning functions, proves the equation for each step of a sequential Monte Carlo algorithm, and assembles them.

Convention 175.1 — Imported interface and what is not imported

From chapter 174 this chapter uses the quasi-Borel category, its cartesian closed structure (proposition 174.8, proposition 174.10), and the probability monad P with its laws (theorem 174.16, proposition 174.17); through it, the kernel interface PMK-bind of convention 172.27. No theorem about SFPC, its operational semantics, or the statistical powerdomain T of convention 174.20 is used or transported: the system of this chapter is Ścibior et al.’s inference representations, a separate card, and no statement crosses between the two without a proved translation. The measures manipulated here are unnormalized finite measures, not probability measures, because scoring changes total mass.

What a representation must mean

Definition 175.2 — Mass functions

For a countable set X let MassX be the set of finitely supported functions XR+, the finite measures with finite support. Its operations are returnMassx=δx,(m=Massf)(y)=xm(x)f(x)(y),flipMass={ff12,tt12},scoreMass(r)=rδ. The total mass is |m|=xm(x).

The three monad laws for Mass are the discrete instance of theorem 172.20, with the counting measure in place of a general measure and finite sums in place of integrals; example 172.21 carried out the identification.

Example 175.3 — The model's meaning, computed once

Reading obs through definition 175.2, [[obs]]=flipMass=λH.(scoreMass(if H then 3 else 1)=λ.δH)=123δtt+121δff={tt32, ff12}. Total mass 2; normalizing gives {tt3/4, ff1/4}. Every claim below about an inference procedure applied to obs is checked against these three exact rationals.

Definition 175.4 — Inference representation

A discrete inference representation is a sextuple T=(T,returnT,=T,flipT,scoreT,mT) consisting of a monadic interface (T,returnT,=T) on sets, elements flipTT2 and scoreT:R+T1, and a family of meaning functions mXT:TXMassX such that for all xX, aTX, rR+ and f:XTY, mT(returnTx)=δx,mT(a=Tf)=mT(a)=Massλx.mT(fx),mT(flipT)=flipMass,mT(scoreTr)=rδ. The monadic interface is not required to satisfy the monad laws, and mT is not required to be natural.

Example 175.5 — Enumeration

Let EnumX=List(R+×X), with returnx=[(1,x)],as=f=concat[scale(r,fx)(r,x)as],flip=[(12,ff),(12,tt)],scorer=[(r,)], where scale(r,bs) multiplies every weight in bs by r, and mEnum(as)(x)={r(r,x)as}. The four laws hold. For return and flip and score read the definitions. For bind, first note m(scale(r,bs))=rm(bs), by summing the scaled weights, and then m(as=f)(y)=definition(r,x)asm(scale(r,fx))(y)=scaling(r,x)asrm(fx)(y)=definition175.2(m(as)=Massλx.m(fx))(y), the last step because the inner sum groups the elements of as by their value component, which is exactly what m(as) records.

Example 175.6 — Weighted sampler terms

Let TermX be the binary trees generated by Return(r,x) with rR+ and Flip(tff,ttt). Put returnx=Return(1,x),scorer=Return(r,),flip=Flip(Return(1,ff),Return(1,tt)), and let t=f graft scale(r,fx) at each leaf Return(r,x). The meaning function is m(Return(r,x))=rδx,m(Flip(t0,t1))=12m(t0)+12m(t1). The bind law is proved by structural induction on t, using the auxiliary equation m(scale(r,t))=rm(t), itself an induction: at a leaf it is the definition, and at a node it is the linearity of the half-sum. At a leaf, m(Return(r,x)=f)=m(scale(r,fx))=rm(fx)=(rδx)=Massλx.m(fx); at a node, both sides split into halves and the induction hypothesis applies to each.

Definition 175.7 — Inference transformation

Let T and S be inference representations. A discrete inference transformation t:TS assigns to each set X a function tX:TXSX with mT(a)=mS(tX(a))for every aTX.

Lemma 175.8 — Transformations compose

Identities are inference transformations, and if t:TS and u:SU are inference transformations then so is xuX(tX(x)).

Proof of Lemma 175.8 — Transformations compose

Proof. mT(a)=mS(tXa)=mU(uX(tXa)). ◻

Example 175.9 — Aggregation, and a transformation that is not natural

Let aggrX merge list entries with equal value components by summing their weights. Then mEnum(aggrX(as))=mEnum(as), by induction on as: merging two entries (r,x) and (s,x) into (r+s,x) changes no value of the mass function. So aggr is an inference transformation. It is not a natural transformation: for the map g:{0,1}{}, aggregating before applying g and after applying g give lists of different lengths, and the square does not commute in Enum, although both sides have the same meaning. Naturality is not part of definition 175.7, and this is why.

Remark 175.10 — What the criterion does not promise

Definition 175.7 requires preservation of the mass function and nothing else. A transformation may preserve meaning and yet be useless: it may increase variance, run slower, or produce a population concentrated on one particle. Those are separate claims about convergence or cost, established by other arguments; the theorem of this chapter is exactly that the algorithm does not change what the program means.

Exercise 175.1

★☆☆ Represent obs in Enum, compute the resulting list, apply mEnum, and check the answer against example 175.3.

Exercise 175.2

★☆☆ Do the same in Term: display the tree and evaluate its meaning by the two clauses of example 175.6.

Exercise 175.3

★★☆ Exhibit a function EnumXEnumX that normalizes each list to total weight 1, and show that it is not an inference transformation by evaluating both sides of definition 175.7 on the list representing obs. State which quantity it destroys.

Importance weighting

Before assembling an algorithm, one step must be derived rather than postulated: why weights appear at all. They come from a change of measure on a one-step model.

Proposition 175.11 — Importance weighting on one step

Let μ and ν be finite measures with finite support on a countable set X with μ(x)>0 whenever ν(x)>0, and let w(x)=ν(x)/μ(x) for μ(x)>0. Then for every f:XMassY, ν=Massf=μ=Massλx.(w(x)f(x)). In particular, drawing from μ and weighting each draw by w represents ν.

Proof of Proposition 175.11 — Importance weighting on one step

Proof. Evaluate both sides at y: xν(x)f(x)(y)=x:μ(x)>0μ(x)ν(x)μ(x)f(x)(y), and the omitted terms have ν(x)=0 by hypothesis. ◻

Example 175.12 — The model, by importance weighting

Take μ=flipMass, the prior, and ν the unnormalized posterior of example 175.3. Then w(tt)=3/21/2=3 and w(ff)=1/21/2=1, which are the two scores of the model. Proposition 175.11 says that sampling H from the fair coin and attaching the weight 3 or 1 represents ν exactly; the weighted population [(3/2,tt),(1/2,ff)] has mEnum equal to {tt3/2,ff1/2}, which is example 175.3. The score construct of the model is the importance weight; nothing further is postulated.

Transformers: weights, populations, suspensions

Sequential Monte Carlo is assembled from three constructions applied to a base representation, and each is introduced by the single obstruction it removes.

Definition 175.13 — Inference transformer

An inference transformer F assigns an inference representation FT to every inference representation T, an inference transformation tmapFt:FTFS to every inference transformation t:TS, and an inference transformation liftT:TFT.

Definition 175.14 — The three transformers

Weighting. WTX=T(R+×X), with returnx=returnT(1,x); bind multiplies the accumulated weights; scoreWTr=returnT(r,); lifta=a=Tλx.returnT(1,x); and mWT(a)(x)={rmT(a)(r,x)rR+}, the weighted mass.

Populations. ListTX=T(ListX) with mListT(a)= the sum of the meanings of the entries, and PopT=WListT, so that PopTX=T(List(R+×X)): a computation in T producing a list of weighted particles. Its meaning function is mPopT(a)=particles(weight)(value), the total measure of the population, summed over the randomness of T.

Suspensions. Let SusTX=T({ReturnX}{Yield(SusTX)}), with scoreSusTr=returnT(Yield(lift(scoreTr))), so that a computation suspends after each score. Its two operations are advance, which runs one suspended step, and finish, which runs all of them; mSusT(a)=mT(finish(a)).

Lemma 175.15 — Suspension steps preserve meaning

advance:SusTSusT and finish:SusTT are inference transformations, and tmapSust is one whenever t is.

Proof of Lemma 175.15 — Suspension steps preserve meaning

Proof. By definition 175.14 the meaning of a suspended computation is the meaning of the computation obtained by running it to completion, so mSusT(advance(a))=mT(finish(advance(a)))=mT(finish(a))=mSusT(a), the middle equality because finish runs every remaining step, and running one step first leaves the same computation to run. For finish itself, the required equation is its definition. For tmapSust, the meaning of the transformed computation is computed by folding mSt over the suspension structure, and mSt=mT by hypothesis. ◻

Lemma 175.16 — Spawning preserves meaning

For n1, let sparknPopT1 be the population of n particles each carrying the value and the weight 1/n, and let spawn(n,a)=sparkn=λ.a. Then mPopT(sparkn)=δ and spawn(n,):PopTPopT is an inference transformation.

Proof of Lemma 175.16 — Spawning preserves meaning

Proof. The total measure of sparkn is n1nδ=δ, of mass 1. Hence m(spawn(n,a))=δ=Massλ.m(a)=m(a) by the left unit law of definition 175.2. ◻

Lemma 175.17 — The discrete weighted randomiser

Let xs be the finite weighted list [(r1,x1),,(rk,xk)], let w=iri, and let U be the uniform measure on [0,1). When w>0, put dwrand(xs,u)=Takexj for the unique j with 1wi<jri  u < 1wijri, and when w=0 define dwrand(xs,u)=Fail. Then w=0  dwrand(xs,)U=δFail,w>0  dwrand(xs,)U=iriwδTakexi. Consequently, for a representation T with an element fail of meaning 0, the discrete weighted sampler dwsample(xs)=scoreT(w)=λ.(sample=λu.case dwrand(xs,u)) has mT(dwsample(xs))=iriδxi.

Proof of Lemma 175.17 — The discrete weighted randomiser

Proof. The intervals [1wi<jri,1wijri) for j=1,,k partition [0,1) and have lengths rj/w, so the pushforward of U assigns rj/w to Takexj; when w=0 the function is constantly Fail. For the second claim, mT(dwsample(xs))=wiriwδxi=iriδxi, using the score law and the bind law of definition 175.4 and the first claim; the Fail branch contributes 0 because mT(fail)=0. ◻

Proposition 175.18 — Resampling preserves meaning

Let resample(n,a)=a=λxs.spawn(n,dwsamplePopT(xs)). Then resample(n,):PopTPopT is an inference transformation.

Proof of Proposition 175.18 — Resampling preserves meaning

Proof. Fix a population xs of total weight w. By lemma 175.17 the discrete weighted sampler on xs has meaning iriδxi, which is the meaning of the population xs; by lemma 175.16, spawning n copies of it leaves that meaning unchanged. The meaning function of PopT satisfies the bind law of definition 175.4, so replacing the population inside a by one of equal meaning leaves the meaning of the bind unchanged. ◻

Example 175.19 — Resampling the two particles

Take the ordered population [(3/2,tt),(1/2,ff)] of example 175.12, of total weight 2. The normalized cumulative bounds are 3/4 and 1, so dwrand sends u<3/4 to tt and 3/4u<1 to ff. Feeding the two uniforms 1/8 and 7/8 produces the ordered unweighted population [tt,ff], each particle carrying weight 1/2 after the spark, and total weight 2 after the score in dwsample. Its meaning is 2(12δtt+12δff)=δtt+δff, of total mass 2 — the same total mass as example 175.3, as proposition 175.18 requires, although the two measures differ: resampling preserves the mass function of the population as a random object, not the value of one draw.

Theorem 175.20 — The sequential Monte Carlo composite

Let T be a sampling representation and let n,k1. The composite smcn,k: (SusPop)T tmapSusspawn(n,) (SusPop)T (advancetmapSusresample(n,))k (SusPop)T finish PopT is an inference transformation.

Proof of Theorem 175.20 — The sequential Monte Carlo composite

Proof. Each factor is an inference transformation: spawning by lemma 175.16 and resampling by proposition 175.18, hence their images under the suspension transformer by lemma 175.15, which also covers advance and finish. A k-fold composite of inference transformations is one by lemma 175.8, and so is the whole composite. ◻

Remark 175.21 — Reading the theorem

Theorem 175.20 says that running the particle filter with n particles for k steps returns a population whose mass function is the meaning of the original program. It does not say that the population is a good approximation for any particular n — that is a convergence statement, not proved here — and it does not say that PopT is a monad: when T is a sampler representation, which is not commutative, the monad laws fail for PopT, which is why definition 175.4 does not require them. The proof used only the meaning laws, and this is exactly the generality the failure forces.

Remark 175.22 — Comparison cards, owning no theorem

Three further algorithms are treated in this chapter as comparison cards and as sources of problems only: the Metropolis method with its Green generalization, its trace-based variant, and resample-move sequential Monte Carlo. A reconstruction of the source’s Theorems 7.1 to 7.3 would introduce densities with respect to a dominating measure, Markov kernels on trace spaces, and support conditions, none of which appears above; that reconstruction is a separate development and is not claimed here. The implementations named in the literature, monad-bayes and the systems built on it, are used as laboratories and as sources of fixtures; none of them is an interchangeable proof artifact for theorem 175.20.

Exercise 175.4

★☆☆ Verify lemma 175.16 for n=3 by computing the meaning of spark3 directly, and state what goes wrong if the sparked weights are 1 rather than 1/n.

Exercise 175.5

★★☆ In lemma 175.17, compute dwrand(xs,u) for u=3/4 exactly, and show that changing the half-open intervals to closed ones would make the pushforward assign mass to two outcomes at once.

Exercise 175.6

★★☆ Let xs have total weight 0. Compute mT(dwsample(xs)) and show that proposition 175.18 still holds, identifying the hypothesis mT(fail)=0 where it is used.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 175.7, then exercise 175.8, then the practical project exercise 175.11.

Exercise 175.7

★☆☆ For obs, compute the unnormalized measure and posterior by exact enumeration and by importance weighting from the prior, and check that the two agree with example 175.3 and example 175.12.

Exercise 175.8

★★☆ Write the proof of proposition 175.18 for a population of two particles, displaying every use of the bind law of definition 175.4 and the exact rational weights at each stage.

Exercise 175.9

★★☆ Modify resample so that the score scoreT(w) inside dwsample is omitted. Compute the meaning of the resulting population for example 175.19 and identify the exact factor by which it differs from the correct value. State which line of the proof of lemma 175.17 the modification invalidates.

Exercise 175.10

★★☆ Give a two-score program in the fragment of section 175.1, display its representation in SusEnum, and compute advance once and finish, checking lemma 175.15 on it.

Exercise 175.11

★★★ Practical project.smc-meaning-checker Build a meaning-preservation checker for the finite fragment. It has three parts: an exact enumerator computing mEnum as a table of reduced rationals; an importance-weighting stage implementing proposition 175.11 from an explicitly supplied proposal; and a multinomial resampler implementing dwrand of lemma 175.17 driven by an explicit list of rationals in [0,1) instead of a random source.

The invariant to maintain is that every stage records both the population and its total weight, and that the checker compares the complete rational representation of the mass function before and after each stage, not merely its normalization.

The concrete result is the sequence of exact tables for obs=Hcoin(1/2); score(if H then 3 else 1); return H. The acceptance test is decidable and exact: exact enumeration and importance weighting must both print the unnormalized table {tt3/2, ff1/2} and the posterior {tt3/4, ff1/4}; the resampler applied to the ordered particles [(tt,3/2),(ff,1/2)] with the stream [1/8,7/8] must return the ordered unweighted population [tt,ff], compared as a list, in that order; and the checker must report the total weight 2 unchanged across the resampling stage. A large-sample display of frequencies is commentary only. This checker exercises the finite instance of theorem 175.20 and proves no part of it.

Search the book

Type to search the local edition.