2. Stochastic relations¶
A Markov kernel between finite sets \(X\) and \(Y\) is a row-stochastic matrix: row \(x\) is the conditional distribution \(p(y \mid x)\). Kernels compose by the matrix product under sum and product, which sums over the intermediate value:
Finite sets with Markov kernels form the category FinStoch (Fritz, 2020). Build kernels with the MARKOV algebra, which is the sum-product semiring on row-stochastic matrices, compose them, discretize continuous families onto a grid, condition a kernel on evidence about its output, and query probabilities and expectations (Bishop, 2006).
Setup¶
import torch
from quivers.core.objects import FinSet
from quivers.core.morphisms import identity, observed
from quivers.stochastic import (
MARKOV,
stochastic,
DiscretizedNormal,
DiscretizedBeta,
condition,
prob,
marginal_prob,
expectation,
)
from quivers.program import Program
Creating stochastic morphisms¶
stochastic creates a learnable kernel, a StochasticMorphism:
Its parameters are unconstrained logits, and each row of the tensor is their softmax (with the temperature argument, default 1), so every row sums to one:
Row \(x\) holds \(p(y \mid x)\):
A kernel with fixed entries is an observed morphism under MARKOV:
Z = FinSet(name="Output", cardinality=2)
data = torch.tensor([
[0.7, 0.3],
[0.4, 0.6],
[0.5, 0.5],
[0.2, 0.8],
])
kern_obs = observed(Y, Z, data, algebra=MARKOV)
Composition of Markov kernels¶
composed = kern >> kern_obs
print(composed.tensor.shape) # [3, 2]
print(composed.tensor.sum(dim=1)) # all 1.0
The composite is again row-stochastic. A Program wraps it for training:
Discretized distributions¶
A model with discrete and continuous variables can discretize a continuous family onto a finite grid. DiscretizedNormal evaluates a Normal density at the centers of equal bins spanning [low, high] and normalizes the values to a probability vector. Its mean and log scale are learnable, one pair for each element of the domain:
Z_bin = FinSet(name="DiscretizedValue", cardinality=10)
Unit = FinSet(name="Unit", cardinality=1)
disc_normal = DiscretizedNormal(Unit, Z_bin, low=0.0, high=1.0)
print(disc_normal.tensor.shape) # [1, 10]
print(disc_normal.tensor.sum(dim=-1)) # ~[1.0]
This is a kernel from a set with one element to the grid. The mass of each bin is the normalized density at its center, not the integral of the density over the bin. DiscretizedBeta does the same for a Beta density:
Discretized families compose with other kernels, for instance with a learnable kernel from the grid to a coarser set:
Output = FinSet(name="Output", cardinality=3)
kern_to_output = stochastic(Z_bin, Output)
combined = disc_normal >> kern_to_output
print(combined.tensor.shape) # [1, 3]
print(combined.tensor.sum(dim=-1)) # ~[1.0]
Observations and conditioning¶
Given nonnegative evidence \(e\) over the codomain, condition reweights each row of the kernel and renormalizes it:
Evidence given as a likelihood vector over the values of \(Y\), rather than as an observed value, is virtual (likelihood) evidence (Pearl, 1988):
evidence = torch.tensor([0.1, 0.4, 0.3, 0.2])
conditioned = condition(kern, evidence)
print(conditioned.tensor.shape) # [3, 4]
print(conditioned.tensor.sum(dim=-1)) # ~[1, 1, 1]
An observed value \(Y = 2\) is the vector that is one at index 2 and zero elsewhere:
Each row with positive evidence mass becomes a distribution on \(Y\) concentrated on the support of the evidence. A row with zero evidence mass has no conditional distribution, and the implementation, which clamps the denominator, leaves it all zeros.
Conditioning the output does not update a distribution over the input \(X\). With a prior \(p(x)\), the posterior over the input is proportional to \(p(x) \sum_y f(x, y)\, e(y)\), and the row normalization above discards exactly these row totals. Bayes inversion in chapter 6 computes the posterior over the input from the original kernel and the prior.
Probabilities and expectations¶
prob reads entries of a kernel at pairs of domain and codomain indices:
x_idx = torch.tensor([0, 1, 2])
y_idx = torch.tensor([3, 1, 0])
print(prob(kern, x_idx, y_idx)) # shape [3]
marginal_prob computes \(p(y) = \frac{1}{|X|} \sum_x f(x, y)\), the marginal of the codomain under a uniform distribution on the domain, at the requested indices. It takes no prior argument. For another prior, compose a kernel from a set with one element that holds the prior with the kernel, and read the row of the composite.
expectation computes \(\mathbb E[v(Y) \mid X = x]\) of a real function \(v\) on the codomain, for each element \(x\) of the domain:
Multiple stages¶
# X -> Y -> Z
kern1 = stochastic(X, Y)
kern2 = stochastic(Y, Z)
composed = kern1 >> kern2
print(composed.tensor.shape) # [3, 2]
print(composed.tensor.sum(dim=-1)) # ~[1, 1, 1]
# Marginal of Z under uniform X
z_idx = torch.tensor([0, 1])
print(marginal_prob(composed, z_idx)) # shape [2], sums to 1
Since sum and product form a semiring, composition under MARKOV is associative, unlike the PRODUCT_FUZZY composition of chapter 1, and a chain of kernels may be bracketed either way.
Next¶
Chapter 3 builds probabilistic programs over continuous spaces.
Cited sources¶
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.
Christopher M. Bishop. Pattern Recognition and Machine Learning. Information Science and Statistics. Springer, 2006. ISBN 9780387310732. URL: https://link.springer.com/book/9780387310732.
Judea Pearl. Probabilistic Reasoning in Intelligent Systems: Networks of Plausible Inference. Morgan Kaufmann, 1988. doi:10.1016/C2009-0-27609-4.