Setting and notation¶
QVR phrases are interpreted in three categories: \(\mathcal V\)-matrices between finite sets, stochastic matrices between finite sets, and Markov kernels between standard Borel spaces. Programs that condition on data are interpreted as s-finite kernels.
0. Categories, functors, and monads¶
A category consists of objects, a set of morphisms \(f : A \to B\) for each pair of objects (the hom-set), an associative composition, and an identity morphism on each object (Mac Lane, 1998). We write \(g \circ f\) for "first \(f\), then \(g\)", and \(f ; g\) for the same composite in diagrammatic order. A functor is a map between categories that preserves composition and identities, and it is faithful when it is injective on each hom-set. A natural transformation between two functors is a family of morphisms, one per object, that commutes with the image of every morphism.
A monad on a category \(\mathcal C\) is a functor \(T : \mathcal C \to \mathcal C\) with a unit \(\eta_X : X \to T(X)\) and a multiplication \(\mathrm{m}_X : T(T(X)) \to T(X)\), natural in \(X\), that satisfy a left unit law, a right unit law, and an associativity law (Mac Lane, 1998). Moggi interprets effectful programs by morphisms \(X \to T(Y)\) (Moggi, 1991). These morphisms form the Kleisli category of \(T\), with the same objects as \(\mathcal C\), the unit \(\eta_X\) as identity on \(X\), and the composite of \(f : X \to T(Y)\) and \(g : Y \to T(Z)\) given by \(\mathrm{m}_Z \circ T(g) \circ f\). A monad on a category with products is strong when it has a natural map \(X \times T(Y) \to T(X \times Y)\) compatible with the unit and multiplication, and it is commutative when the two ways of pairing \(T(X) \times T(Y) \to T(X \times Y)\) agree, so that independent effects may be performed in either order (Kock, 1972).
A monoidal category has a tensor product \(\boxtimes\) of objects and of morphisms with a unit object, associative and unital up to isomorphisms that satisfy the coherence conditions (Mac Lane, 1998). It is symmetric when there is also a swap \(A \boxtimes B \to B \boxtimes A\) that squares to the identity. A Markov category is a symmetric monoidal category in which every object carries a copy map \(A \to A \boxtimes A\) and a discard map \(A \to I\), compatible with the tensor product. Kernels form Markov categories, and Fritz uses this structure to state conditional independence and conditionals without measure theory (Fritz, 2020).
For a monoidal category \(\mathbf W\), a \(\mathbf W\)-enriched category has an object of \(\mathbf W\) in place of each hom-set, with composition and identities given by morphisms of \(\mathbf W\) (Kelly, 1982).
1. Weights and their operations¶
Composition of finite weighted relations is computed with two operations on a set of weights.
Let \(\mathcal V = (V, \otimes, \bigoplus, \mathbf 1)\) be a set \(V\) of weights with a binary
product \(\otimes\), a reduction \(\bigoplus\) that combines any finite family of weights, and a unit
\(\mathbf 1\) for \(\otimes\). In the library, these are the methods tensor_op, join, and unit
of the class Algebra, which also gives a zero, a meet, and a
negation. The method name join does not mean that \(\bigoplus\) is a lattice join: it is the
sum for the sum-product instances and log-sum-exp for the log semiring.
The laws below hold for some instances and fail for others, and the results that need them say so. Semirings, t-norms, and change of base lists which laws each built-in instance satisfies.
- \(\mathcal V\) is a semiring when \(\bigoplus\) is an associative and commutative addition with a zero \(\mathbf 0\), \(\otimes\) is associative with unit \(\mathbf 1\), \(\otimes\) distributes over \(\bigoplus\) on both sides, and \(\mathbf 0\) annihilates: \(a \otimes \mathbf 0 = \mathbf 0 \otimes a = \mathbf 0\) (Goodman, 1999; Mohri, 2002). It is commutative when \(\otimes\) is.
-
A quantale is a complete lattice \(V\), that is, a partial order in which every subset has a least upper bound \(\bigvee\), with an associative product \(\otimes\) that distributes over arbitrary joins on both sides (Rosenthal, 1996):
\[ a \otimes \bigvee_{i \in I} b_i = \bigvee_{i \in I} (a \otimes b_i), \qquad \Bigl(\bigvee_{i \in I} a_i\Bigr) \otimes b = \bigvee_{i \in I} (a_i \otimes b). \]A quantale with a unit for \(\otimes\) is unital. When \(\bigoplus\) is the join \(\bigvee\), a unital quantale is a semiring whose addition is idempotent. - A t-norm is an associative, commutative, monotone operation on \([0, 1]\) with unit \(1\), and a t-conorm is the same with unit \(0\) (Klement et al., 2000). They generalize logical conjunction and disjunction. A t-norm paired with a t-conorm need not satisfy distributivity, and so need not be a semiring.
2. V-matrices¶
Let \(X\) and \(Y\) be finite sets. A \(\mathcal V\)-matrix (or \(\mathcal V\)-valued relation) from \(X\) to \(Y\) is a function
The composite of \(r : X \to Y\) and \(s : Y \to Z\) is the matrix product over \(\mathcal V\):
When \(\mathcal V\) is a semiring, this composition is associative, and the matrices \(1_X(x, x') = \mathbf 1\) for \(x = x'\) and \(\mathbf 0\) otherwise are identities. Finite sets and \(\mathcal V\)-matrices then form a category, which we write \(\mathbf{Mat}(\mathcal V)\) (Hofmann et al., 2014). For the Boolean semiring it is the category \(\mathbf{Rel}\) of relations between finite sets. When \(\mathcal V\) is a quantale, each hom-set \(V^{X \times Y}\) is a complete lattice under the pointwise order and composition preserves joins in each argument, so \(\mathbf{Mat}(\mathcal V)\) is a quantaloid, a category enriched in complete lattices (Rosenthal, 1996).
When the semiring \(\mathcal V\) is also commutative, \(\mathbf{Mat}(\mathcal V)\) is symmetric monoidal. The tensor of objects is the Cartesian product, and the tensor of matrices is the Kronecker product
Given the semiring laws, commutativity of \(\otimes\) is what makes \(\boxtimes\) a functor, that is, what makes \((r ; r') \boxtimes (s ; s') = (r \boxtimes s) ; (r' \boxtimes s')\).
If \(\mathcal V\) is not a semiring, the formula above still defines a composite, and the runtime computes it, but the composite need not be associative and the identity matrices need not be identities. Composition rules gives the levels of law that a rule may claim.
3. Standard Borel spaces and Markov kernels¶
A measurable space \((S, \Sigma)\) is a set \(S\) with a σ-algebra \(\Sigma\) of subsets, the events to which probabilities are assigned. It is a standard Borel space when it is isomorphic to a Borel subset of a Polish space, that is, of a separable, completely metrizable space. Every finite set with all subsets as events, every \(\mathbb R^n\), and every countable product of these is standard Borel (Kechris, 1995). We write \(\mathbf{SBor}\) for the category of standard Borel spaces and measurable functions.
A Markov kernel \(k\) from \(S\) to \(T\) assigns to each \(s \in S\) a probability measure \(k(s, -)\) on \(T\), such that \(s \mapsto k(s, B)\) is measurable for every event \(B\) of \(T\). On finite sets, a Markov kernel is a stochastic matrix. Kernels compose by integrating over the intermediate value, as in the Chapman–Kolmogorov equation:
A measurable function \(f : S \to T\) gives the deterministic kernel \(s \mapsto \delta_{f(s)}\), where \(\delta_t\) is the Dirac measure at \(t\).
The Giry monad \(\mathcal G\) sends a space \(S\) to the space \(\mathcal G(S)\) of probability measures on \(S\), with the least σ-algebra that makes each evaluation \(\mu \mapsto \mu(B)\) measurable (Giry, 1982). If \(S\) is standard Borel, so is \(\mathcal G(S)\) (Kechris, 1995), so \(\mathcal G\) is a monad on \(\mathbf{SBor}\). Its unit sends \(s\) to \(\delta_s\), and its multiplication averages a measure over measures. A Markov kernel from \(S\) to \(T\) is the same thing as a measurable map \(S \to \mathcal G(T)\), and the Kleisli composite of two such maps is the integral above, so Markov kernels are the morphisms of the Kleisli category of \(\mathcal G\) (Giry, 1982; Panangaden, 1999). The Giry monad is commutative: this is Fubini's theorem for probability measures.
Following Fritz, we write \(\mathbf{BorelStoch}\) for the category of standard Borel spaces and
Markov kernels, and \(\mathbf{FinStoch}\) for its full subcategory on finite sets, whose morphisms
are stochastic matrices (Fritz, 2020). Both are Markov categories. On a standard Borel
space every joint distribution has a measurable family of conditional distributions, which fails
for general measurable spaces (Fritz, 2020). On finite sets, \(\mathcal G\) restricts to
the distribution monad \(\mathcal D\), which
sends \(X\) to the probability vectors on \(X\). Since \(\mathcal D(X)\) is a simplex, an infinite set,
\(\mathcal D\) is not a monad on \(\mathbf{FinSet}\), but its Kleisli morphisms between finite sets
are exactly the stochastic matrices. The class FinStoch implements
\(\mathbf{FinStoch}\) as the Kleisli category of the class GiryMonad.
4. Finite and s-finite kernels¶
A program that conditions on data multiplies probabilities by likelihoods, so its denotation is a kernel into measures that need not have mass one. A kernel from \(S\) to \(T\) in this sense assigns to each \(s\) a measure \(k(s, -)\) on \(T\) with values in \([0, \infty]\), measurably in \(s\). It is finite when there is a single bound \(r < \infty\) with \(k(s, T) < r\) for every \(s\), and s-finite when it is a countable sum \(\sum_{i} k_i\) of finite kernels (Staton, 2017). A subprobability kernel has \(k(s, T) \le 1\) for every \(s\).
Staton shows that s-finite kernels compose by the same integral as Markov kernels, that this
composition is associative, and that iterated integrals against s-finite kernels may be taken in
either order (Staton, 2017). The last fact is what makes two independent sample or
score steps of a program commute. Kernels whose weight \(\exp(w)\) comes from an unbounded
score expression \(w\) are s-finite but in general not finite, which is why the finite kernels
alone do not suffice (Staton, 2017). Staton conjectures that s-finite kernels are not
the Kleisli category of a commutative monad on measurable spaces, so the pages that use them speak
of the category of s-finite kernels, not of a measure monad.
5. Fragments¶
QVR morphisms fall into three fragments, each interpreted in its own category.
| Fragment | Declared by | Category |
|---|---|---|
| Finite weighted | morphism with role=parameter or role=observed |
\(\mathbf{Mat}(\mathcal{V})\) |
| Finite stochastic | morphism with role=kernel between finite sets and no ~ Family initializer |
\(\mathbf{FinStoch}\) |
| Continuous | morphism with a ~ Family initializer and a continuous codomain, and object declarations of continuous spaces |
\(\mathbf{BorelStoch}\) |
The fragments are related by embeddings. Giving a finite set all its subsets as events makes it a
standard Borel space, and this inclusion \(\mathbf{FinSet} \hookrightarrow \mathbf{SBor}\) extends
to a full and faithful functor \(\mathbf{FinStoch} \hookrightarrow \mathbf{BorelStoch}\). A Boolean
matrix with exactly one true entry in each row is a function, and these functions embed into
\(\mathbf{FinStoch}\) as the deterministic kernels. A discretize declaration denotes a
kernel from a bounded interval to a finite set of bins, and an embed declaration denotes a
kernel in the opposite direction (Morphisms §5).
6. Environments¶
A semantic environment \(\rho\) is a partial function from identifiers to denotations. It has one part for each kind of name:
- \(\rho_{\mathrm{obj}}\) maps object names to finite sets.
- \(\rho_{\mathrm{spc}}\) maps space names to standard Borel spaces.
- \(\rho_{\mathrm{mor}}\) maps morphism names to \(\mathcal V\)-matrices, stochastic matrices, or Markov kernels.
- \(\rho_{\mathrm{cat}}\) maps the atoms of syntactic categories to their denotations in weighted deduction.
- \(\rho_{\mathrm{var}}\) maps runtime input parameters and variables bound earlier in a
programbody to points of their spaces (Programs as kernels).
We write \(\rho[x \mapsto v]\) for \(\rho\) extended with the binding of \(x\) to \(v\), and \(\llbracket \phi \rrbracket_{\rho}\) for the denotation of the phrase \(\phi\) in \(\rho\). We drop \(\rho\) when it is clear from the context.
For a declaration with a static telescope \(\Delta\), interpretation is relative to a static valuation \(\delta\in\llbracket\Delta\rrbracket\) before runtime values are assigned. An input telescope \(\Gamma_v(\delta)\) may depend on that valuation, as a tensor's shape depends on a static extent. Giving \(d\in\llbracket\Gamma_v(\delta)\rrbracket\) extends the runtime environment without performing an effect. The parameter semantics makes this dependence explicit for programs.
7. Typing judgments¶
A typing judgment \(\Gamma \vdash \phi : \tau\) states that in the typing context \(\Gamma\) the phrase \(\phi\) has type \(\tau\). The context \(\Gamma\) records, for each name in scope, its sort (object, space, or morphism with its domain and codomain), where \(\rho\) records its denotation. The compiler checks these judgments before it builds any runtime object, and the denotation \(\llbracket \phi \rrbracket_\rho\) is defined only for well-typed phrases. Typing and checking gives the judgments of the source checker and of the checker of the Quivers Indexed Effect Core (QIEC), the typed core language that QVR elaborates to.
Cited sources¶
Saunders Mac Lane. Categories for the Working Mathematician. Volume 5 of Graduate Texts in Mathematics. Springer, 2 edition, 1998. URL: https://link.springer.com/book/10.1007/978-1-4757-4721-8, doi:10.1007/978-1-4757-4721-8.
Eugenio Moggi. Notions of Computation and Monads. Information and Computation, 93(1):55–92, 1991. URL: https://www.sciencedirect.com/science/article/pii/0890540191900524, doi:10.1016/0890-5401(91)90052-4.
Anders Kock. Strong Functors and Monoidal Monads. Archiv der Mathematik, 23:113–120, 1972. URL: https://link.springer.com/article/10.1007/BF01304852, doi:10.1007/BF01304852.
Tobias Fritz. A Synthetic Approach to Markov Kernels, Conditional Independence and Theorems on Sufficient Statistics. Advances in Mathematics, 370:107239, 2020. URL: https://www.sciencedirect.com/science/article/pii/S0001870820302656, doi:10.1016/j.aim.2020.107239.
G. M. Kelly. Basic Concepts of Enriched Category Theory. Cambridge University Press, 1982. Reprinted in Reprints in Theory and Applications of Categories 10 (2005), 1–136. URL: https://www.tac.mta.ca/tac/reprints/articles/10/tr10abs.html.
Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.
Mehryar Mohri. Semiring Frameworks and Algorithms for Shortest-Distance Problems. Journal of Automata, Languages and Combinatorics, 7(3):321–350, 2002. doi:10.25596/jalc-2002-321.
K. I. Rosenthal. The Theory of Quantaloids. Chapman \\& Hall, 1 edition, 1996. URL: https://www.routledge.com/The-Theory-of-Quantaloids/Rosenthal/p/book/9780429157028.
Erich Peter Klement, Radko Mesiar, and Endre Pap. Triangular Norms. Trends in Logic. Springer, 2000. doi:10.1007/978-94-015-9540-7.
Dirk Hofmann, Gavin J. Seal, and Walter Tholen, editors. Monoidal Topology: A Categorical Approach to Order, Metric, and Topology. Cambridge University Press, 2014. doi:10.1017/CBO9781107517288.
Alexander S. Kechris. Classical Descriptive Set Theory. Graduate Texts in Mathematics. Springer, 1995. doi:10.1007/978-1-4612-4190-4.
Michèle Giry. A Categorical Approach to Probability Theory. In B. Banaschewski, editor, Categorical Aspects of Topology and Analysis, volume 915 of Lecture Notes in Mathematics, pages 68–85. Springer, 1982. URL: https://link.springer.com/chapter/10.1007/BFb0092872, doi:10.1007/BFb0092872.
Prakash Panangaden. The Category of Markov Kernels. Electronic Notes in Theoretical Computer Science, 22:171–187, 1999. doi:10.1016/S1571-0661(05)80602-4.
Sam Staton. Commutative Semantics for Probabilistic Programming. In Programming Languages and Systems (ESOP 2017), Lecture Notes in Computer Science, 855–879. Springer, 2017. doi:10.1007/978-3-662-54434-1_32.