Stochastic Quantale

The [0,1] quantale for probabilistic morphisms.

quantale

Sum-product composition quantale for stochastic matrices.

Implements the MarkovQuantale which governs composition of finite stochastic matrices via sum-product (matrix multiplication).

MarkovQuantale

Bases: Quantale

Sum-product composition for stochastic matrices.

Implements the composition rule of FinStoch:

(g ∘ f)(a, c) = Σ_b f(a, b) · g(b, c)

This is standard matrix multiplication. Formally:

⊗ = product:     a ⊗ b = a · b
⋁ = sum:         ⋁_i x_i = Σ_i x_i
⋀ = product:     ⋀_i x_i = Π_i x_i
¬ = complement:  ¬a = 1 - a
I = 1.0
⊥ = 0.0
Note

This is not a true quantale in the lattice-theoretic sense because Σ is not idempotent (a ⋁ a = 2a ≠ a). It is a commutative semiring ([0,∞), +, ×, 0, 1) whose composition formula matches the quantale interface. The important property is that composition of row-stochastic matrices yields row-stochastic matrices.

tensor_op

tensor_op(a: Tensor, b: Tensor) -> Tensor

Pointwise product.

Source code in src/quivers/stochastic/quantale.py
44
45
46
def tensor_op(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    """Pointwise product."""
    return a * b

join

join(t: Tensor, dim: int | tuple[int, ...]) -> Tensor

Summation (marginalization in probability).

Source code in src/quivers/stochastic/quantale.py
48
49
50
51
52
53
def join(self, t: torch.Tensor, dim: int | tuple[int, ...]) -> torch.Tensor:
    """Summation (marginalization in probability)."""
    if isinstance(dim, int):
        dim = (dim,)

    return t.sum(dim=dim)

meet

meet(t: Tensor, dim: int | tuple[int, ...]) -> Tensor

Product (joint probability under independence).

Source code in src/quivers/stochastic/quantale.py
55
56
57
58
59
60
61
62
63
64
65
def meet(self, t: torch.Tensor, dim: int | tuple[int, ...]) -> torch.Tensor:
    """Product (joint probability under independence)."""
    if isinstance(dim, int):
        dim = (dim,)

    result = t

    for d in sorted(dim, reverse=True):
        result = result.prod(dim=d)

    return result

negate

negate(t: Tensor) -> Tensor

Complement: ¬p = 1 - p.

Source code in src/quivers/stochastic/quantale.py
67
68
69
def negate(self, t: torch.Tensor) -> torch.Tensor:
    """Complement: ¬p = 1 - p."""
    return 1.0 - t

compose

compose(m: Tensor, n: Tensor, n_contract: int) -> Tensor

Sum-product composition (matrix multiplication).

Computes: result[d..., c...] = Σ_{s...} m[d..., s...] · n[s..., c...]

Uses the default quantale compose, which expands + tensor_op + join. For 2D tensors this is equivalent to torch.mm.

PARAMETER DESCRIPTION
m

Left stochastic matrix of shape (domain, shared).

TYPE: Tensor

n

Right stochastic matrix of shape (shared, codomain).

TYPE: Tensor

n_contract

Number of shared dimensions.

TYPE: int

RETURNS DESCRIPTION
Tensor

Composed matrix of shape (domain, codomain).

Source code in src/quivers/stochastic/quantale.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def compose(
    self,
    m: torch.Tensor,
    n: torch.Tensor,
    n_contract: int,
) -> torch.Tensor:
    """Sum-product composition (matrix multiplication).

    Computes: result[d..., c...] = Σ_{s...} m[d..., s...] · n[s..., c...]

    Uses the default quantale compose, which expands + tensor_op + join.
    For 2D tensors this is equivalent to torch.mm.

    Parameters
    ----------
    m : torch.Tensor
        Left stochastic matrix of shape (*domain, *shared).
    n : torch.Tensor
        Right stochastic matrix of shape (*shared, *codomain).
    n_contract : int
        Number of shared dimensions.

    Returns
    -------
    torch.Tensor
        Composed matrix of shape (*domain, *codomain).
    """
    return super().compose(m, n, n_contract)