Skip to content

Stochastic Morphisms

A StochasticMorphism is a Markov kernel between finite sets, stored as a stochastic matrix whose rows are output distributions: nonnegative entries, each row summing to one (FinStoch) (Fritz, 2020). Composition averages the next transition over the intermediate values and gives the distribution of the output given the initial input. stochastic(domain, codomain) builds a learnable one whose rows are a softmax of free parameters.

morphisms

Stochastic morphisms: learnable Markov kernels via softmax.

Provides StochasticMorphism for row-stochastic linear maps and the stochastic() factory function.

CategoricalMorphism module-attribute

CategoricalMorphism = StochasticMorphism

An alias of StochasticMorphism named for its categorical rows.

StochasticMorphism

StochasticMorphism(domain: SetObject, codomain: SetObject, temperature: float = 1.0)

Bases: FiniteMorphism

A learnable Markov kernel backed by softmax-normalized parameters.

Stores unconstrained real-valued logits and applies softmax along the codomain dimensions to produce a row-stochastic tensor (each fiber over a domain element sums to 1).

This is a morphism in FinStoch: a stochastic map A → B.

PARAMETER DESCRIPTION
domain

Source object.

TYPE: SetObject

codomain

Target object.

TYPE: SetObject

temperature

Softmax temperature. Lower values → sharper distributions. Default 1.0.

TYPE: float DEFAULT: 1.0

Examples:

>>> A = FinSet(name="A", cardinality=3)
>>> B = FinSet(name="B", cardinality=4)
>>> f = StochasticMorphism(A, B)
>>> t = f.tensor  # shape (3, 4), rows sum to 1
>>> t.sum(dim=-1)  # tensor([1., 1., 1.])
Source code in src/quivers/stochastic/morphisms.py
def __init__(
    self,
    domain: SetObject,
    codomain: SetObject,
    temperature: float = 1.0,
) -> None:
    super().__init__(domain, codomain, algebra=MARKOV)
    self._temperature = temperature

    shape = self.tensor_shape
    self._module = _MorphismModule()
    logits = nn.Parameter(torch.randn(shape) * 0.1)
    self._module.register_parameter("logits", logits)

logits property

logits: Parameter

Unconstrained logit parameters.

tensor property

tensor: Tensor

Row-stochastic tensor via softmax over codomain dimensions.

RETURNS DESCRIPTION
Tensor

Tensor of shape (*domain.shape, *codomain.shape) where each codomain fiber sums to 1.

module

module() -> Module

Return the nn.Module holding this morphism's parameters.

RETURNS DESCRIPTION
Module

The parameter container.

Source code in src/quivers/stochastic/morphisms.py
def module(self) -> nn.Module:
    """Return the ``nn.Module`` holding this morphism's parameters.

    Returns
    -------
    nn.Module
        The parameter container.
    """
    return self._module

stochastic

stochastic(domain: SetObject, codomain: SetObject, temperature: float = 1.0) -> StochasticMorphism

Create a learnable stochastic morphism (Markov kernel).

PARAMETER DESCRIPTION
domain

Source object.

TYPE: SetObject

codomain

Target object.

TYPE: SetObject

temperature

Softmax temperature.

TYPE: float DEFAULT: 1.0

RETURNS DESCRIPTION
StochasticMorphism

A learnable row-stochastic morphism.

Source code in src/quivers/stochastic/morphisms.py
def stochastic(
    domain: SetObject,
    codomain: SetObject,
    temperature: float = 1.0,
) -> StochasticMorphism:
    """Create a learnable stochastic morphism (Markov kernel).

    Parameters
    ----------
    domain : SetObject
        Source object.
    codomain : SetObject
        Target object.
    temperature : float
        Softmax temperature.

    Returns
    -------
    StochasticMorphism
        A learnable row-stochastic morphism.
    """
    return StochasticMorphism(domain, codomain, temperature=temperature)

The sum-product semiring

quivers.stochastic re-exports MarkovAlgebra and its singleton MARKOV, the Algebra under which stochastic morphisms compose (the sum-product semiring), from quivers.core.algebras.

Stochastic morphisms: Markov kernels over finite sets.

This module provides the probabilistic layer for quivers, implementing the category FinStoch of finite sets and stochastic maps (Markov kernels). This is the Kleisli category of the Giry monad restricted to finite sets, where:

  • Objects are finite sets (same as the base category).
  • Morphisms A -> B are stochastic matrices: tensors of shape (|A|, |B|) whose rows sum to 1.
  • Composition is standard matrix multiplication.
  • Identity is the Kronecker delta.

The package also re-exports MarkovAlgebra and its singleton MARKOV from quivers.core.algebras, the algebra these morphisms compose under.

Submodules

morphisms : StochasticMorphism, CategoricalMorphism families : Discretized distribution families transforms : condition, mix, factor, normalize queries : prob, marginal_prob, expectation giry : GiryMonad, FinStoch categories : Category types including atoms, slashes, products, unit, modals semiring : Chart semiring abstractions (LogProb, Viterbi, Boolean, Counting) schema : Composable rule schemas (functors CategorySystem -> RuleSystem) rules : Rule systems and the CCG / Lambek presets span : Span-based CKY components (LexicalAxiom, BinarySpanDeduction, etc.) parsers, ccg, lambek : Chart parsers inside : The inside algorithm over a rule system agenda : The agenda-driven weighted-deduction engine indexed : Charts indexed on the argument positions rule joins look up joins : Worst-case optimal joins and variable elimination for rule bodies planning : Plans for queries over rule presentations forest : Packed forests of derivations and folds over them recursion : Linear cyclic systems solved through the algebra's closure tries : Tries of relations and the intersection step of Generic Join productions : A grammar's productions grouped by left-hand side effect_lifts : Effect-lifted rule schemas for deduction systems deduction : Abstract weighted deductive system framework and its operations stdlib : Pre-registered deduction systems

MARKOV module-attribute

MARKOV = MarkovAlgebra()

The MarkovAlgebra singleton, composing stochastic matrices.

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.