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:
by default, a variable is a
pyagrum.RangeVariableusing the default domain size (second argument of the functions).with
a[10], the variable is apyagrum.RangeVariableusing 10 as domain size (from 0 to 9)with
a[3,7], the variable is apyagrum.RangeVariableusing the integer values from 3 to 7.with
a[1,3.14,5,6.2], the variable is apyagrum.DiscretizedVariableusing the given ticks (at least 3 values).with
a[1:6.15:9], the variable is apyagrum.DiscretizedVariableusing the ticks from 1 to 6.15 in 9 steps (the domain size is 9).with
a+[1,3.14,5,6.2], the variable is apyagrum.DiscretizedVariableusing the given ticks (at least 3 values), empirical.with
a+[1:6.15:9], the variable is apyagrum.DiscretizedVariableusing the ticks from 1 to 6.15 in 9 steps (the domain size is 9), empirical.with
a{top|middle|bottom}, the variable is apyagrum.LabelizedVariableusing the given labels (here :top,middleandbottom).with
a{-1|5|0|3}, the variable is apyagrum.IntegerVariableusing the sorted given values.with
a{-0.5|5.01|0|3.1415}, the variable is apyagrum.NumericalDiscreteVariableusing the sorted given values.with
a{-0.5:3.1415:5}, the variable is apyagrum.NumericalDiscreteVariableusing the values from -0.5 to 3.1415 in 5 steps (the domain size is 5).
Create a variable from its fast syntax description (e.g. |
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->Xand 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 (seepyagrum.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).
Write a name as in a fast description: between single quotes (with its quotes doubled) if |
|
Tell whether the name of a variable must be quoted to be read back as is by |
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.
Create a Bayesian network with a dot-like syntax which specifies: |
|
Create a Markov random field with a modified dot-like syntax which specifies: |
|
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>]
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 descriptiondefaultDomain (
str) – the domain of the variables without one, for the graphical models
Methods
Tell why the description is not valid for a syntax. |
|
Tell whether the description is valid for a syntax. |
|
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.validSyntaxesgives the syntaxes for which a description is valid.
Attributes
The fast syntaxes: the kinds of objects that a fast description can build (see |
|
The fast syntaxes: the kinds of objects that a fast description can build (see |
|
The fast syntaxes: the kinds of objects that a fast description can build (see |
|
The fast syntaxes: the kinds of objects that a fast description can build (see |
|
The fast syntaxes: the kinds of objects that a fast description can build (see |
|
The fast syntaxes: the kinds of objects that a fast description can build (see |
|
The fast syntaxes: the kinds of objects that a fast description can build (see |