Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ brute-force reference or a closed-form value and has a regression test.
9 reverse-machine "explosions" are now 3-state reverse ε-machines, and finite/infinite now agrees
with `reverse_epsilon_machine_is_finite` on every topology.

### Canonical NFAs as generators and presentations

- `canonical_rfsa_hmm`, `maximized_prime_atomaton_hmm` and `atomaton_hmm` (new module
`sofic.generators.canonical_generators`; also `EpsilonMachine` / `BidirectionalEpsilonMachine` methods) build the
stochastic analogues of the canonical RFSA, the maximized prime átomaton and the átomaton as generators of a
process with a finite bidirectional machine. The RFSA analogue's states are the forward causal states whose
reverse beliefs are vertices of their convex hull (future morphs that are not mixtures of the others); the
prime-átomaton analogue is the same construction in reverse time on the forward beliefs of the reverse causal
states, time-reversed back. Both are linear programs and generally non-unifilar.
- `generative_model` offers both as candidates (`canonical RFSA generator`, `maximized prime átomaton
generator`) and `coupled_search` starts from them. On the binary 3-state dataset the prime-átomaton analogue
compresses the reverse machine on 112 of 620 processes and is the best generator known on 14, lowering the
best upper bound on `C_g` on three (e.g. 1.2809 to 1.0994).
- `SoficShift.canonical_rfsa_presentation`, `maximized_prime_atomaton_presentation` and `atomaton_presentation`
(new module `sofic.shifts.canonical_presentations`) present a sofic shift by the canonical NFAs of its factor
language, trimmed to the essential states.

### Generator verification

- `GenerativeModelResult` records the returned generator's word-process deviation from the source
Expand Down
48 changes: 48 additions & 0 deletions docs/generators/canonical_generators.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Canonical NFAs as generators
============================

.. currentmodule:: sofic.generators.canonical_generators

A regular language has several canonical automata :cite:`Myers2015`: the minimal DFA, the átomaton
:cite:`BrzozowskiTamm2014`, the canonical residual finite-state automaton (RFSA) :cite:`Denis2002`, and the
maximized prime átomaton :cite:`MaarandTamm2022`. For a stationary process with a finite bidirectional
ε-machine :cite:`Ellison2009`, each has a stochastic analogue that generates the process:

============================== ===================================================== ======================
canonical NFA generator states
============================== ===================================================== ======================
minimal DFA forward ε-machine :math:`S^+`
átomaton reverse ε-machine run forward (:func:`atomaton_hmm`) :math:`S^-`
canonical RFSA :func:`canonical_rfsa_hmm` at most :math:`|S^+|`
maximized prime átomaton :func:`maximized_prime_atomaton_hmm` at most :math:`|S^-|`
============================== ===================================================== ======================

A forward causal state's future morph is a mixture of the futures given the reverse causal states, which have
disjoint supports, with weights :math:`R_{s^+} = \Pr(S^- \mid S^+ = s^+)`. A morph that is not a mixture of the
other morphs -- the convex analogue of a prime residual, as in probabilistic residual automata
:cite:`Esposito2002` -- is therefore a vertex of :math:`\operatorname{conv}\{R_{s^+}\}`. The canonical-RFSA
analogue is the generator on those vertices: every causal state is a mixture of them, and the vertices'
transitions follow. The maximized-prime-átomaton analogue is the same construction applied to the reversed
process, on the vertices of the forward beliefs :math:`\operatorname{conv}\{Q_{s^-}\}`, time-reversed back. Both
are linear programs; both are in general not unifilar. The identification with the canonical NFAs is an
analogy and is not from the literature.

They are generators, so their state entropies are upper bounds on the generative complexity :cite:`LohrAy2009`,
and :func:`~sofic.generators.generative_search.generative_model` offers them as candidates and as starts of
its coupled search. For random-parameter ε-machines the RFSA analogue rarely differs from the ε-machine
(it needs the process rank to fall below :math:`|S^+|`); the prime-átomaton analogue often compresses a large
reverse machine to a few vertices, and on binary three-state ε-machines it is sometimes the smallest generator
known.

.. code-block:: python

from sofic.generators import maximized_prime_atomaton_hmm

prime = maximized_prime_atomaton_hmm(machine) # a MealyHMM on the extreme reverse causal states
prime.to_sofic_shift() # its support presents the same shift

The topological counterparts for sofic shifts are in :mod:`sofic.shifts.canonical_presentations`.

.. autofunction:: canonical_rfsa_hmm
.. autofunction:: maximized_prime_atomaton_hmm
.. autofunction:: atomaton_hmm
1 change: 1 addition & 0 deletions docs/generators/generators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Computational mechanics
predictive_rate_distortion
symbolic_hmm
generative_models
canonical_generators
directional_flow
alternative_complexity
wheeler_epsilon
Expand Down
22 changes: 22 additions & 0 deletions docs/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -1703,3 +1703,25 @@ @article{mccormick1976computability
year = {1976},
doi = {10.1007/BF01580665},
}

@inproceedings{Esposito2002,
author = {Esposito, Yann and Lemay, Aur{\'e}lien and Denis, Fran{\c{c}}ois and Dupont, Pierre},
title = {Learning Probabilistic Residual Finite State Automata},
booktitle = {Grammatical Inference: Algorithms and Applications (ICGI 2002)},
series = {Lecture Notes in Computer Science},
volume = {2484},
pages = {77--91},
publisher = {Springer},
year = {2002},
doi = {10.1007/3-540-45790-9_7},
}

@article{Myers2015,
author = {Myers, Robert S. R. and Ad{\'a}mek, Ji{\v r}{\'\i} and Milius, Stefan and Urbat, Henning},
title = {Coalgebraic Constructions of Canonical Nondeterministic Automata},
journal = {Theoretical Computer Science},
volume = {604},
pages = {81--101},
year = {2015},
doi = {10.1016/j.tcs.2015.03.035},
}
26 changes: 26 additions & 0 deletions docs/shifts/canonical_presentations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Canonical NFA presentations
===========================

.. currentmodule:: sofic.shifts.canonical_presentations

A sofic shift is determined by its factor language, which is factorial and extendable :cite:`LindMarcus1995`.
Any automaton for that language with every state accepting presents the shift once trimmed to the states on
bi-infinite paths, so the canonical NFAs of the factor language -- the átomaton :cite:`BrzozowskiTamm2014`, the
canonical residual finite-state automaton :cite:`Denis2002` and the maximized prime átomaton
:cite:`MaarandTamm2022` -- give canonical, generally non-resolving presentations beside the Fischer and Krieger
covers. The residual-side states are follower sets that are not unions of other follower sets.

.. code-block:: python

shift = machine.to_sofic_shift()
shift.canonical_rfsa_presentation()
shift.maximized_prime_atomaton_presentation()
shift.atomaton_presentation()

The stochastic analogues, which replace unions of residuals by mixtures of future morphs, are in
:mod:`sofic.generators.canonical_generators`.

.. autofunction:: canonical_rfsa_presentation
.. autofunction:: maximized_prime_atomaton_presentation
.. autofunction:: atomaton_presentation
.. autofunction:: factor_language_nfa
1 change: 1 addition & 0 deletions docs/shifts/shifts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type, Sofic shifts, Dyck shifts, topological Markov chains, and covers
textile
dyck_enumeration
covers
canonical_presentations
wheeler
4 changes: 4 additions & 0 deletions sofic/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sofic.generators.bidirectional_epsilon_machine import BidirectionalEpsilonMachine
from sofic.generators.block_convergence import BlockConvergenceDiagram, BlockConvergenceEstimates
from sofic.generators.block_entropy import BlockEntropyDiagram, BlockEntropyEstimates
from sofic.generators.canonical_generators import atomaton_hmm, canonical_rfsa_hmm, maximized_prime_atomaton_hmm
from sofic.generators.channel_coding import FiniteStateChannel
from sofic.generators.channel_measures import (
channel_statistical_complexity,
Expand Down Expand Up @@ -89,6 +90,9 @@
"generative_model",
"generative_model_from_bidirectional",
"minimal_functional_generator",
"atomaton_hmm",
"canonical_rfsa_hmm",
"maximized_prime_atomaton_hmm",
"minimal_wyner_model",
"ultimate_generative_model",
"mu1_brute_2x2",
Expand Down
18 changes: 18 additions & 0 deletions sofic/generators/bidirectional_epsilon_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,24 @@ def minimal_functional_generator(self, **kwargs: Any) -> Any:

return minimal_functional_generator(self, **kwargs)

def canonical_rfsa_hmm(self, **kwargs: Any) -> Any:
"""Canonical-RFSA analogue; see :func:`~sofic.generators.canonical_generators.canonical_rfsa_hmm`."""
from sofic.generators.canonical_generators import canonical_rfsa_hmm

return canonical_rfsa_hmm(self, **kwargs)

def maximized_prime_atomaton_hmm(self, **kwargs: Any) -> Any:
"""Prime-átomaton analogue; see :func:`~sofic.generators.canonical_generators.maximized_prime_atomaton_hmm`."""
from sofic.generators.canonical_generators import maximized_prime_atomaton_hmm

return maximized_prime_atomaton_hmm(self, **kwargs)

def atomaton_hmm(self, **kwargs: Any) -> Any:
"""Átomaton analogue (reverse ε-machine run forward); see :func:`~sofic.generators.canonical_generators.atomaton_hmm`."""
from sofic.generators.canonical_generators import atomaton_hmm

return atomaton_hmm(self, **kwargs)

def wyner_common_information(self, **kwargs: Any) -> float:
"""Wyner common information C[S+ : S-] = C[past : future] (no generator requirement)."""
from sofic.generators.minimal_generative_model import wyner_common_information
Expand Down
111 changes: 111 additions & 0 deletions sofic/generators/canonical_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
r"""Stochastic analogues of the canonical NFAs of a regular language, as generators of a process.

Regular languages have several canonical automata :cite:`Myers2015`: the minimal DFA, the átomaton
:cite:`BrzozowskiTamm2014`, the canonical residual finite-state automaton (RFSA) :cite:`Denis2002`, and its dual,
the maximized prime átomaton :cite:`MaarandTamm2022`. For a stationary process with a finite bidirectional
ε-machine :cite:`Ellison2009` the first two have standard analogues, and the last two have analogues built from
extreme points of belief polytopes:

* minimal DFA -- the forward ε-machine, states :math:`S^+`;
* átomaton (reverse of the minimal DFA of the reverse language) -- the reverse ε-machine run forward,
states :math:`S^-` (:func:`atomaton_hmm`);
* canonical RFSA (prime residuals) -- the forward causal states whose future morph is not a mixture of the
others' (:func:`canonical_rfsa_hmm`);
* maximized prime átomaton (reverse of the canonical RFSA of the reverse language) -- the same construction in
reverse time, reversed back (:func:`maximized_prime_atomaton_hmm`).

A forward causal state :math:`s^+` has future morph :math:`\sum_b R_{s^+}(b) \Pr(\text{future} \mid S^- = b)`
with :math:`R_{s^+} = \Pr(S^- \mid S^+ = s^+)`, and the futures given distinct reverse causal states have
disjoint supports. So the morph of :math:`s^+` is not a nonnegative combination of the other morphs exactly when
:math:`R_{s^+}` is a vertex of :math:`\operatorname{conv}\{R_{s^+}\}`: this is the convex analogue of a prime
residual, as in probabilistic residual automata :cite:`Esposito2002`. Writing every row as
:math:`R_t = \sum_e C_{te} R_e`, the HMM on the vertices with joint transition matrices :math:`(T^{(x)} C)_{E,
\cdot}` emits the future morph of each vertex, so it generates the process with at most :math:`|S^+|` states. It
is in general not unifilar. The prime-átomaton analogue uses the forward beliefs
:math:`Q_{s^-} = \Pr(S^+ \mid S^- = s^-)` of the reverse causal states instead: it is a generator of the
reversed process, time-reversed with its stationary law, with at most :math:`|S^-|` states.

These are generators, so their state entropies bound the generative complexity :math:`C_g` of
:cite:`LohrAy2009` from above; no minimality is claimed. Identifying them with the canonical NFAs is an analogy
(extreme points of a cone in place of prime residuals) and is not from the literature.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

import numpy as np

from sofic.generators.mealy import MealyHMM

if TYPE_CHECKING:
from sofic.generators.bidirectional_epsilon_machine import BidirectionalEpsilonMachine
from sofic.generators.coupled_generators import _GeneratorProblem

__all__ = ["atomaton_hmm", "canonical_rfsa_hmm", "maximized_prime_atomaton_hmm"]


def _problem(machine: Any) -> _GeneratorProblem:
from sofic.generators.bidirectional_epsilon_machine import BidirectionalEpsilonMachine
from sofic.generators.coupled_generators import _GeneratorProblem
from sofic.generators.epsilon_machine import EpsilonMachine

if not isinstance(machine, BidirectionalEpsilonMachine):
forward = machine if isinstance(machine, EpsilonMachine) else EpsilonMachine.from_hmm(machine)
machine = BidirectionalEpsilonMachine.from_forward(forward)
return _GeneratorProblem(machine)


def _labels(problem: _GeneratorProblem, side: int) -> list[Any]:
return sorted({pair[side] for pair in problem.lifted.pairs}, key=repr)


def _as_hmm(problem: _GeneratorProblem, pi: np.ndarray, K: np.ndarray, names: list[Any], cutoff: float) -> MealyHMM:
hmm = MealyHMM(observation_alphabet=frozenset(problem.symbols))
for name in names:
hmm.graph.add_state(name)
for w, x, v in zip(*np.nonzero(cutoff < K), strict=True):
hmm.add_transition(names[w], names[v], problem.symbols[x], float(K[w, x, v]))
total = float(pi.sum())
hmm.initial_distribution = {name: float(p) / total for name, p in zip(names, pi, strict=True)}
hmm.validate_stochastic()
return hmm


def canonical_rfsa_hmm(
machine: EpsilonMachine | BidirectionalEpsilonMachine, *, tol: float = 1e-7, cutoff: float = 1e-13
) -> MealyHMM:
"""Canonical-RFSA analogue: the generator on the forward causal states with extreme future morphs.

States are named by those causal states; the initial distribution is stationary. ``tol`` is the L1
tolerance of the extreme-point linear programs. See the module docstring.
"""
problem = _problem(machine)
(pi, _, _, K), E = problem.residual_seed(tol=tol)
names = _labels(problem, 0)
return _as_hmm(problem, pi, K, [names[e] for e in E], cutoff)


def maximized_prime_atomaton_hmm(
machine: EpsilonMachine | BidirectionalEpsilonMachine, *, tol: float = 1e-7, cutoff: float = 1e-13
) -> MealyHMM:
"""Maximized-prime-átomaton analogue: the generator on the reverse causal states with extreme past morphs.

States are named by those reverse causal states; the initial distribution is stationary. See the module
docstring.
"""
problem = _problem(machine)
(pi, _, _, K), E = problem.prime_atomaton_seed(tol=tol)
names = _labels(problem, 1)
return _as_hmm(problem, pi, K, [names[e] for e in E], cutoff)


def atomaton_hmm(machine: EpsilonMachine | BidirectionalEpsilonMachine, *, cutoff: float = 1e-13) -> MealyHMM:
"""Átomaton analogue: the reverse ε-machine run forward in time (a co-unifilar generator on :math:`S^-`)."""
problem = _problem(machine)
pi, _, _, K = problem.reverse_seed()
return _as_hmm(problem, pi, K, _labels(problem, 1), cutoff)


if TYPE_CHECKING:
from sofic.generators.epsilon_machine import EpsilonMachine
Loading
Loading