Credal Network
Credal networks are probabilistic graphical models based on imprecise probability. Credal networks can be regarded as an extension of Bayesian networks, where credal sets replace probability mass functions in the specification of the local models for the network variables given their parents. As a Bayesian network defines a joint probability mass function over its variables, a credal network defines a joint credal set (from Wikipedia).
Tutorial
Reference
CN Model
- class pyagrum.CredalNet(*args)
Constructor used to create a CredalNet (step by step or with two BayesNet)
- CredalNet() -> CredalNet
default constructor
CredalNet(src_min_num,src_max_den) -> CredalNet
- Parameters:
src_min_num (
strorpyagrum.BayesNet) – The path to a BayesNet or the BN itself which contains lower probabilities.src_max_den (
strorpyagrum.BayesNet) – The (optional) path to a BayesNet or the BN itself which contains upper probabilities.
Scientific notes
A credal network [1] replaces each conditional probability table of a Bayesian network by credal sets: for each variable \(X_i\) and each configuration \(\pi_i\) of its parents, a closed convex set \(K(X_i \mid \pi_i)\) of distributions, given in pyAgrum by its vertices. The local sets are separately specified (one per parent configuration) and the network represents their strong extension
the convex hull of the joint distributions of all the Bayesian networks whose conditional distributions are taken in
the local sets. An inference computes, for each value \(x\) of a variable, the lower and upper posteriors
\(\min_{P \in K} P(x \mid e)\) and \(\max_{P \in K} P(x \mid e)\); they are reached on combinations of
vertices of the local sets [1], which is what pyagrum.CNMonteCarloSampling exploits.
Building the credal sets. pyAgrum offers three ways, all of which store the vertices of each local set.
From two Bayesian networks giving lower and upper probabilities \(l_k \leq p_k \leq u_k\):
intervalToCredalenumerates the vertices of \(\{p : l \leq p \leq u, \sum_k p_k = 1\}\).From a Bayesian network of counts,
idmLearning(s)applies the imprecise Dirichlet model [2]: with \(n_k\) the count of the value \(k\) for a parent configuration and \(N = \sum_k n_k\), the set is \(p_k \in [n_k/(N+s),\, (n_k+s)/(N+s)]\), whose vertices are \((n + s\,e_k)/(N+s)\). In pyAgrum \(s\) is an integer and \(s = 0\) (the default) gives the precise frequencies. WithkeepZeroes, a value never observed keeps a null probability, and a configuration where a single value was observed stays precise.lagrangeNormalizationadds 1 to all the counts of the configurations that contain a zero.bnToCredal(beta, ...)makes a Bayesian network imprecise as in [3]: each distribution \(p\) becomes \(p_k \in [(1-\varepsilon)\,p_k,\ (1-\varepsilon)\,p_k + \varepsilon]\) (a linear-vacuous mixture, whose vertices put the mass \(\varepsilon\) on one value), with \(\varepsilon = \beta^{\ln(N+1)}\) where \(N\) is the number of cases observed for the parent configuration: the more data, the less imprecision; \(\beta = 0\) keeps the network precise, \(\beta = 1\) or \(N = 0\) makes the distribution vacuous.epsilonMin,epsilonMaxandepsilonMeansummarize the \(\varepsilon\) used.
Approximate binarization. pyagrum.CNLoopyPropagation needs binary variables. approximatedBinarization
replaces each variable with \(d > 2\) values by \(\lceil \log_2 d \rceil\) binary variables (its bits
X-b0, X-b1…): a bit has as parents the bits of the parents of the variable and its own preceding bits, and
its credal set is the interval between the lowest and the highest probability of the bit over the vertices of the
original set. Indicator variables X-v0, X-v1… give the probabilities of the values of \(X\). This is
the binarization recalled in [4], without the exact reformulation proposed there: the
binarized network represents more distributions than the original one, so the bounds computed on it are an outer
approximation (wider intervals), which can be crude.
Methods
Adds an arc between two nodes |
|
Approximate binarization. |
|
Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of point probabilities and then computes each vertex of each credal set. |
|
Used with binary networks to speed-up L2U inference. |
|
The credal sets of the nodes, up to date (after |
|
The credal sets of the nodes, as given when the credal net was built or set. |
|
Set the interval constraints of a credal set of a given node (from an instantiation index) |
|
Set the interval constraints of the credal sets of a given node (all instantiations) |
|
The upper probabilities of the value 1 of each binary node. |
|
The lower probabilities of the value 1 of each binary node. |
|
Check whether the binary CPT min/max values have been computed. |
|
Learns parameters from a BayesNet storing counts of events. |
|
Get an Instantiation from a node id, useful to fill the constraints of the network. |
|
Computes the vertices of each credal set according to their interval definition (uses lrs). |
|
Laplace smoothing of the counts of a BayesNet storing counts of events, such that no probability is 0. |
|
If this CredalNet was built over a perturbed BayesNet, one can save the intervals as two BayesNet. |
|
Set the vertices of one credal set of a given node. |
|
Set the vertices of the credal sets (all of the conditionals) of a given node. |
|
Attributes
int([x]) -> integer int(x, base=10) -> integer |
|
int([x]) -> integer int(x, base=10) -> integer |
|
int([x]) -> integer int(x, base=10) -> integer |
|
int([x]) -> integer int(x, base=10) -> integer |
CN Inference
- class pyagrum.CNMonteCarloSampling(credalNet)
Class used for inferences in credal networks with Monte Carlo sampling algorithm.
- CNMonteCarloSampling(cn) -> CNMonteCarloSampling
- Parameters:
cn (
pyagrum.CredalNet) – a credal network
By default, the sampling stops when a period finds no new bound (epsilon is 1000 times the machine epsilon) or after
pyagrum.config['CNMontecarlo','maxTime']seconds (60 unless configured), the samples being drawn by periods ofpyagrum.config['CNMontecarlo','periodSize'](1000) samples: the rate and the maximum number of iterations are disabled. These criteria can be changed; an epsilon below 1000 times the machine epsilon raisespyagrum.OutOfBounds, since the result would then depend on the number of threads.- Parameters:
credalNet (
CredalNet)
Scientific notes
CNMonteCarloSampling explores the strong extension of a credal network (see pyagrum.CredalNet) by sampling its
vertices, as in [3]. Since the lower and upper posteriors are reached on combinations of
vertices of the local credal sets [1], each sample is such a combination: for every variable and
every configuration of its parents, one vertex of the local set is drawn uniformly and independently. The resulting
Bayesian network is solved exactly (by pyagrum.LazyPropagation, with the evidence), and the bounds are updated:
over the \(N\) sampled networks \(P_k\) (a network in which the evidence is impossible is skipped). Each \(P_k\) belongs to the strong extension, so the bounds are an inner approximation: the interval always lies inside the exact one and grows towards it with the number of samples. It is not a statistical estimate: there is no error bar, and an extreme combination of vertices that is unlikely under the uniform drawing (when the optimum needs a precise vertex in many local sets) may be reached late, or never.
The stopping criterion. The samples are drawn by periods of periodSize (1000 by default, section
CNMontecarlo of pyagrum.config, as maxTime) samples. After each period, \(\varepsilon\) is the largest
change of a lower or upper bound, over all the values of all the variables, during the period; the sampling stops when
\(\varepsilon\) is at most epsilon, by default \(1000\) times the machine epsilon (about
\(2.2 \times 10^{-13}\)): a whole period has found no new bound. A smaller epsilon is refused, since a change
below this tolerance is rounding. The sampling also stops after maxTime (60 seconds by default);
minEpsilonRate and maxIter are disabled by default. Each sample is drawn from its own random generator, seeded
by the seed of the inference and the index of the sample, so the result does not depend on the number of threads.
Dynamic networks. For a dynamic credal network whose variables are named X_0, X_1, … X_T, the
default drawing is independent for each time slice (the dynamic strong extension of [3]).
With setRepetitiveInd(True), the slices of a variable whose tables have the same size share their drawing (one
drawing for the slice 0, one for the transition, reused by the later slices): the dynamic repetitive extension, a
subset of the strong one, which assumes a precise but ill-known transition model that does not change over time.
Methods
Return the credal network used by this inference engine. |
|
Add evidence for a variable in the credal network. |
|
Disable epsilon as a stopping criterion. |
|
Disable max iterations as a stopping criterion. |
|
Disable max time as a stopping criterion. |
|
Disable a min epsilon rate as a stopping criterion. |
|
Get the upper dynamic expectation of a given variable prefix. |
|
Get the lower dynamic expectation of a given variable prefix. |
|
Enable epsilon as a stopping criterion. |
|
Enable max iterations as a stopping criterion. |
|
Enable max time as a stopping criterion. |
|
Enable a min epsilon rate as a stopping criterion. |
|
Erase all inference related data to perform another one. |
|
The error at the start of each period of the last run: one value every |
|
Insert evidence from file. |
|
Insert variables modalities from file to compute expectations. |
|
Starts the inference. |
|
Get the upper marginals of a given node id. |
|
Get the lower marginals of a given node id. |
|
Set the threshold of the epsilon stopping criterion (and enable it). |
|
Erase all the evidences and apply addEvidence(key,value) for every pairs in evidces. |
|
- class pyagrum.CNLoopyPropagation(credalNet)
Class used for inferences in credal networks with Loopy Propagation algorithm.
- CNLoopyPropagation(cn) -> CNLoopyPropagation
- Parameters:
cn (
pyagrum.CredalNet) – a Credal network
The default stopping criteria (epsilon, rate, maximum number of iterations, period size) and verbosity are the keys of the section
CNLoopyofpyagrum.config(epsilon 1e-8, rate 1e-10, 100 iterations unless configured); there is no time limit by default.An impossible evidence (no message can be computed from it) makes
makeInference()raisepyagrum.IncompatibleEvidence.- Parameters:
credalNet (
CredalNet)
Scientific notes
CNLoopyPropagation implements L2U [5], the loopy version of 2U [6]. 2U
adapts Pearl’s propagation to credal networks with binary variables and computes exactly the lower and upper
posteriors of a polytree; L2U iterates its messages on a network with loops, as loopy belief propagation does (see
pyagrum.LoopyBeliefPropagation), and its bounds are then approximate, with no guarantee of being inner or outer. It
requires a separately specified binary credal network whose interval bounds have been prepared
(CredalNet.computeBinaryCPTMinMax); a network with non-binary variables is first binarized by
CredalNet.approximatedBinarization, which adds the outer approximation of the binarization to the one of the loops
[4].
The messages. With binary variables, a distribution is a single number, and every message is an interval:
a message \(\pi\) from a parent \(U\) to \(X\) bounds \(P(U = 1)\) given the evidence on the side of \(U\);
a message \(\Lambda\) from a child to \(X\) bounds the likelihood ratio \(\lambda(1)/\lambda(0)\) of the evidence on the side of the child; the ratio of \(X\) is the product of those of its children (lower bounds together, upper bounds together).
The bounds of \(\pi(X)\) are obtained by enumerating the combinations of the extreme values of the parents’ messages, with the lower (resp. upper) conditional probabilities \(P(X = 1 \mid u)\); the messages to the parents are computed in the same way. The posterior of \(X\) combines the two intervals, monotonically:
and \(P(X = 0 \mid e)\) follows by complement. Only hard evidence is accepted (an observed variable has \(\Lambda = \infty\) for the value 1, \(0\) for the value 0). An impossible evidence leaves both bounds of a message undefined, and the inference stops with an error.
Initialization and schedule. The \(\pi\) intervals are first propagated in a topological order and every
\(\Lambda\) is 1, so that without evidence the first values are the forward bounds. By default
(nodeToNeighbours), only the nodes whose information changed send messages at the next iteration; the other
orders send messages along all the arcs, in a fixed (ordered) or a new random (randomOrder) order at each
iteration.
The stopping criterion. After each iteration, \(\varepsilon\) is the largest change of a lower or upper
posterior over all the variables. The propagation stops when no node has a new message to send (nodeToNeighbours),
or with the criteria of the approximation scheme: \(\varepsilon\) at most epsilon (\(10^{-8}\) by
default), relative change of \(\varepsilon\) between two iterations below minEpsilonRate (\(10^{-10}\)),
maxIter (100) iterations; these defaults are the keys of the section CNLoopy of pyagrum.config, and there is
no time limit by default. With nodeToNeighbours, \(\varepsilon\) may decrease non monotonically (an iteration
where the active nodes hardly change, followed by larger changes), so that epsilon can be reached before the
convergence. On a network with loops, the convergence of L2U is not proved, although it is usually observed
[4].
Jaime Shinsuke Ide and Fabio Gagliardi Cozman. Approximate algorithms for credal networks with binary variables. International Journal of Approximate Reasoning, 48(1):275–296, 2008. URL: https://www.sciencedirect.com/science/article/pii/S0888613X07001351, doi:10.1016/j.ijar.2007.09.003.
Enrico Fagiuoli and Marco Zaffalon. 2U: an exact interval propagation algorithm for polytrees with binary variables. Artificial Intelligence, 106(1):77–107, 1998. URL: https://www.sciencedirect.com/science/article/pii/S0004370298000897, doi:10.1016/S0004-3702(98)00089-7.
Methods
Return the credal network used by this inference engine. |
|
Add evidence for a variable in the credal network. |
|
Disable epsilon as a stopping criterion. |
|
Disable max iterations as a stopping criterion. |
|
Disable max time as a stopping criterion. |
|
Disable a min epsilon rate as a stopping criterion. |
|
Get the upper dynamic expectation of a given variable prefix. |
|
Get the lower dynamic expectation of a given variable prefix. |
|
Enable epsilon as a stopping criterion. |
|
Enable max iterations as a stopping criterion. |
|
Enable max time as a stopping criterion. |
|
Enable a min epsilon rate as a stopping criterion. |
|
Erase all inference related data to perform another one. |
|
The error at the start of each period of the last run: one value every |
|
Insert evidence from file. |
|
Insert variables modalities from file to compute expectations. |
|
Starts the inference. |
|
Get the upper marginals of a given node id. |
|
Get the lower marginals of a given node id. |
|
Saves the marginals (lower and upper probabilities) in a text file. |
|
Erase all the evidences and apply addEvidence(key,value) for every pairs in evidces. |
|
Attributes
int([x]) -> integer int(x, base=10) -> integer |
|
int([x]) -> integer int(x, base=10) -> integer |
|
int([x]) -> integer int(x, base=10) -> integer |