fast syntax

Quick specification of discrete random variables

aGrUM/pyAgrum offers a so-called fast syntax that allows to quickly and compactly specify prototypes of graphical models.

The type of the (discrete) random variables can be specifiy with different syntaxes:

fastVariable()

Create a variable from its fast syntax description (e.g. a{yes|no}, a[3,7]).

Names between quotes

A name may be written between single quotes, in every fast syntax (variables, graphical models and graphs): every character is then part of the name, a doubled quote standing for a quote.

pyagrum.fastBN("'Visit Asia'{yes|no}->Dyspnoea<-'X-ray'[3];'l''eau'")
pyagrum.fastID("'my state'->*'my decision'->$'my utility'")
pyagrum.fastMRF("'a--b'--C")
pyagrum.fastDiGraph("'Visit Asia'->X")
pyagrum.fastPAG("'A'o->'B'")
  • A quote opens a quoted name only at the beginning of a name: O'Brien->X and the labels {l'eau|x} are unchanged.

  • Without quotes, the name of a variable may contain spaces (Visit Asia->X), but not -, ;, [, {… The name of a node of a graph contains neither space, '<' nor '>'.

  • In a quoted name, '12' is a name, not a node id; 'X+'[0,1,2] is not empirical, 'X'+[0,1,2] is.

  • toFast() quotes a name only if it would not be read back as is otherwise (see pyagrum.quoteFastName).

Warning

The files saved by pyAgrum (.jgum, .bgum, .bifxml, pickle…) write the variables in fast syntax. A name which needs quotes, such as X-ray (or the names X[0] of the variables of a pyagrum.ktbn.KTBN), is written 'X-ray'[3]: pyAgrum 3.3.2 or older read it with the quotes in the name. The simple names are written as before.

In the .jgum and .bgum files and the pickles, a variable which the fast syntax does not read back exactly (the labels {10|9} of a pyagrum.LabelizedVariable, a description…) is written as a JSON object (see JGUM / BGUM Format Reference).

quoteFastName()

Write a name as in a fast description: between single quotes (with its quotes doubled) if pyagrum.fastNameNeedsQuotes, as is otherwise.

fastNameNeedsQuotes()

Tell whether the name of a variable must be quoted to be read back as is by pyagrum.fastVariable and by the fast syntax of the graphical models (pyagrum.fastBN, pyagrum.fastID, pyagrum.fastMRF).

Quick specification of (randomly parameterized) graphical models

Using the fast syntax, one can quickly specify a Bayesian network (arcs ->), an influence diagram (arcs, with the prefixes * and $ for the decision and utility nodes) or a Markov random field (the variables of a factor separated by --). The fast syntax specifies the variables and the structure of the graphical model, not its parameters (CPTs, factors, utilities): they are randomly generated. When a node appears several times, its first occurrence gives its variable and, in an influence diagram, its kind.

pyagrum.fastBN("A[10]->B<-C{top|middle|bottom};B->D")

Note

  • If the dot-like string contains such a specification more than once for a variable, the first specification will be used.

  • the CPTs are randomly generated.

fastBN()

Create a Bayesian network with a dot-like syntax which specifies:

fastMRF()

Create a Markov random field with a modified dot-like syntax which specifies:

fastID()

Create an Influence Diagram with a modified dot-like syntax which specifies:

Checking a description

A description may be valid for several fast syntaxes: 'A->B' is a directed, a mixed and a marked graph, a Bayesian network and an influence diagram, 'A--B' only a Markov random field. pyagrum.checkFastSyntax gives the verdict for every syntax (this is how pyagrum.fastGraph chooses the type of graph it builds).

>>> pyagrum.checkFastSyntax("A{yes|no}->Visit Asia").validSyntaxes()
[<FastSyntax.BayesNet: 4>, <FastSyntax.InfluenceDiagram: 5>]

checkFastSyntax()

Check a fast description for every fast syntax.

class pyagrum.FastSyntaxCheck(*args)

The verdict on a fast description for every fast syntax (see pyagrum.checkFastSyntax).

A FastSyntaxCheck is true if the description is valid for at least one syntax.

FastSyntaxCheck(description, defaultDomain=’[2]’) -> FastSyntaxCheck

Parameters:
  • description (str) – the fast description

  • defaultDomain (str) – the domain of the variables without one, for the graphical models

Methods

description()

error()

Tell why the description is not valid for a syntax.

isValid()

Tell whether the description is valid for a syntax.

validSyntaxes()

The syntaxes for which the description is valid.

class pyagrum.FastSyntax(value)

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

The members are the graphs (UndiGraph, DiGraph, MixedGraph, MarkedGraph) and the graphical models (BayesNet, InfluenceDiagram, MarkovRandomField). pyagrum.FastSyntaxCheck.validSyntaxes gives the syntaxes for which a description is valid.

Attributes

BayesNet

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

DiGraph

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

InfluenceDiagram

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

MarkedGraph

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

MarkovRandomField

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

MixedGraph

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).

UndiGraph

The fast syntaxes: the kinds of objects that a fast description can build (see pyagrum.checkFastSyntax).