Skip to main content

Python library for manipulating probabilistic automata.

Project description

Probabilistic Automata

Build Status Docs codecov PyPI version License: MIT

Python library for manipulating Probabilistic Automata. This library builds upon the dfa package.

Installation

If you just need to use probabilistic_automata, you can just run:

$ pip install probabilistic_automata

For developers, note that this project uses the poetry python package/dependency management tool. Please familarize yourself with it and then run:

$ poetry install

Usage

The probabilistic_automata library centers around the PDFA object which models a finite probabilistic transition system, e.g., a Markov Decision Process, as a DFA or Moore Machine over a product alphabet over the system's actions and the environment's stochastic action.

import probabilistic_automata as PA

def transition(state, composite_action):
    sys_action, env_action = composite_action
    return (state + sys_action + env_action) % 2

def env_dist(state, sys_action):
    """Based on state and the system action, what are the probabilities 
    of the environment's action."""

    return {0: 1/2, 1: 1/2}  # Always a coin flip.

noisy_parity = PA.pdfa(
    start=0,
    label=bool,
    inputs={0, 1},
    env_inputs={0, 1},
    outputs={0, 1},
    transition=transition,
    env_dist=env_dist,   # Equivalently, PA.uniform({0, 1}).
)

The support and transition probabilities can easily calculated:

assert noisy_parity.support(0, 0) == {0, 1}
assert noisy_parity.transition_probs(0, 0) == {0: 1/2, 1: 1/2}
assert noisy_parity.prob(start=0, action=0, end=0) == 1/2

Dict <-> PDFA

Note that pdfa provides helper functions for going from a dictionary based representation of a probabilistic transition system to a PDFA object and back.

import probabilistic_automata as PA

mapping = {
    "s1": (True, {
        'a': {'s1': 0.5, 's2': 0.5},
    }),
    "s2": (False, {
        'a': {'s1': 1},
    }),
}

start = "s1"
pdfa = PA.dict2pdfa(mapping=mapping, start=start)
assert pdfa.inputs == {'a'}

mapping2, start2 = PA.pdfa2dict(pdfa)
assert start == start2
assert mapping2 == mapping

DFA to PDFA

The probabilistic_automata library has two convenience methods for transforming a Deterministic Finite Automaton (dfa.DFA) into a PDFA.

  • The lift function simply creates a PDFA whose transitions are deterministic and match the original dfa.DFA.
import probabilistic_automata as PA
from dfa import DFA

parity = DFA(
    start=0,
    inputs={0, 1},
    label=bool,
    transition=lambda s, c: (s + c) & 1,
)

parity_pdfa = lift(parity)

assert pdfa.inputs == PARITY.inputs
assert pdfa.env_inputs == {None}
  • The randomize function takes a DFA and returns a PDFA modeling the actions of the DFA being selected uniformly at random.
pdfa = PA.randomize(PARITY)

assert pdfa.inputs == {None}
assert pdfa.env_inputs == PARITY.inputs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

probabilistic_automata-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

probabilistic_automata-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file probabilistic_automata-0.1.1.tar.gz.

File metadata

  • Download URL: probabilistic_automata-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.8.1 Linux/5.5.0-1-MANJARO

File hashes

Hashes for probabilistic_automata-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5fb0a09c3aa6749deed1b0a93189c9931ec1a9431c5d1c842214c763b404d113
MD5 30045db36a8b40be9ad60d79dbe5495b
BLAKE2b-256 49d3d9bb493ffffb8f42af11da8a537d27e9d17fdedff2613e0bc99a29826f88

See more details on using hashes here.

File details

Details for the file probabilistic_automata-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for probabilistic_automata-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 57ac436bc0164deae54b54176d4950c9df710285390f045fb27ce6c74e72d0f5
MD5 1d4e21f7042a8d063571d12122cc1c66
BLAKE2b-256 83287cbd4367ef7fcaa306c86b01a13bcdb60cc05abe44f87124b36a6388fb35

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page