Skip to content

Stochastic Morphisms

Stochastic relations, kernels, and morphisms for probabilistic computation.

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: Morphism

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 Markov algebra

quivers.stochastic re-exports MarkovAlgebra and its singleton MARKOV, the algebra stochastic morphisms compose under, 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 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.