# Credal Networks¶

## Model¶

pyAgrum.CredalNet
class pyAgrum.pyAgrum.CredalNet_double(*args)

Constructor used to create a CredalNet (step by step or with two BayesNet)

Available ructors:

CredalNet() -> CredalNet

CredalNet(src_min_num,src_max_den) -> CredalNet

CredalNet(src_min_num,src_max_den) -> CredalNet

Parameters: src_min_num – (str) the path to a BayesNet which contains lower probabilities. src_max_den – (str) the (optional) path to a BayesNet which contains upper probabilities. src_min_num – (pyAgrum.BayesNet) the BayesNet which contains lower probabilities. src_max_den – (pyAgrum.BayesNet) the (optional) BayesNet which contains upper probabilities.
addArc(self, tail, head)

Adds an arc between two nodes

Parameters: tail – the id of the tail node head (int) – the id of the head node gum.InvalidDirectedCircle – If any (directed) cycle is created by this arc gum.InvalidNode – If head or tail does not belong to the graph nodes gum.DuplicateElement – If one of the arc already exists
addVariable(self, name, card)
Parameters: name (str) – the name of the new variable card (int) – the domainSize of the new variable the id of the new node int
approximatedBinarization(self)

Approximate binarization.

Each bit has a lower and upper probability which is the lowest - resp. highest - over all vertices of the credal set. Enlarge the orignal credal sets and may induce huge imprecision.

Warning

Enlarge the orignal credal sets and therefor induce huge imprecision by propagation. Not recommended, use MCSampling or something else instead

bnToCredal(self, beta, oneNet, keepZeroes=False)

bnToCredal(self, beta, oneNet)

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.

Parameters: beta (double) – The beta used to perturbate the network oneNet (bool) – used as a flag. Set to True if one BayesNet if provided with counts, to False if two BayesNet are provided; one with probabilities (the lower net) and one with denominators over the first modalities (the upper net) keepZeroes (bool) – used as a flag as whether or not - respectively True or False - we keep zeroes as zeroes. Default is False, i.e. zeroes are not kept
computeCPTMinMax(self)

Used with binary networks to speed-up L2U inference.

Store the lower and upper probabilities of each node X over the ‘True’ modality.

credalNet_currentCpt(self)

Warning

Experimental function - Return type to be wrapped

Returns: a ant reference to the (up-to-date) CredalNet CPTs. tbw
credalNet_srcCpt(self)

Warning

Experimental function - Return type to be wrapped

Returns: a ant reference to the (up-to-date) CredalNet CPTs. tbw
currentNodeType(self, id)
Parameters: id (int) – The ant reference to the choosen NodeId the type of the choosen node in the (up-to-date) CredalNet __current_bn if any, __src_bn otherwise. pyAgrum.CredalNet
current_bn(self)
Returns: Returs a ant reference to the actual BayesNet (used as a DAG, it’s CPTs does not matter). pyAgrum.BayesNet
domainSize(self, id)
Parameters: id (int) – The id of the node The cardinality of the node int
epsilonMax(self)
Returns: a ant reference to the highest perturbation of the BayesNet provided as input for this CredalNet. double
epsilonMean(self)
Returns: a ant reference to the average perturbation of the BayesNet provided as input for this CredalNet. double
epsilonMin(self)
Returns: a ant reference to the lowest perturbation of the BayesNet provided as input for this CredalNet. double
fillConstraint(self, id, entry, lower, upper)

fillConstraint(self, id, ins, lower, upper)

Set the interval raints of a credal set of a given node (from an instantiation index)

Parameters: id (int) – The id of the node entry (int) – The index of the instantiation excluding the given node (only the parents are used to compute the index of the credal set) ins (pyAgrum.Instantiation) – The Instantiation lower (list) – The lower value for each probability in correct order upper (list) – The upper value for each probability in correct order

Warning

You need to call intervalToCredal when done filling all raints.

Warning

DOES change the BayesNet (s) associated to this credal net !

fillConstraints(self, id, lower, upper)

Set the interval raints of the credal sets of a given node (all instantiations)

Parameters: id (int) – The id of the node lower (list) – The lower value for each probability in correct order upper (list) – The upper value for each probability in correct order

Warning

You need to call intervalToCredal when done filling all raints.

Warning

DOES change the BayesNet (s) associated to this credal net !

get_CPT_max(self)

Warning

Experimental function - Return type to be wrapped

Returns: a ant reference to the upper probabilities of each node X over the ‘True’ modality tbw
get_CPT_min(self)

Warning

Experimental function - Return type to be wrapped

Returns: a ant reference to the lower probabilities of each node X over the ‘True’ modality tbw
hasComputedCPTMinMax(self)
Returns: True this CredalNet has called computeCPTMinMax() to speed-up inference with binary networks and L2U. bool
idmLearning(self, s=0, keepZeroes=False)

idmLearning(self, s=0) idmLearning(self)

Learns parameters from a BayesNet storing counts of events.

Use this method when using a single BayesNet storing counts of events. IDM model if s > 0, standard point probability if s = 0 (default value if none precised).

Parameters: s (int) – the IDM parameter. keepZeroes (bool) – used as a flag as whether or not - respectively True or False - we keep zeroes as zeroes. Default is False, i.e. zeroes are not kept.
instantiation(self, id)

Get an Instantiation from a node id, usefull to fill the raints of the network.

bnet accessors / shortcuts.

Parameters: id (int) – the id of the node we want an instantiation from the instantiation pyAgrum.Instantiation
intervalToCredal(self)

Computes the vertices of each credal set according to their interval definition (uses lrs).

Use this method when using two BayesNet, one with lower probabilities and one with upper probabilities.

intervalToCredalWithFiles(self)

Warning

Deprecated : use intervalToCredal (lrsWrapper with no input / output files needed).

Computes the vertices of each credal set according to their interval definition (uses lrs).

Use this method when using a single BayesNet storing counts of events.

isSeparatelySpecified(self)
Returns: True if this CredalNet is separately and interval specified, False otherwise. bool
lagrangeNormalization(self)

Normalize counts of a BayesNet storing counts of each events such that no probability is 0.

Use this method when using a single BayesNet storing counts of events. Lagrange normalization. This call is irreversible and modify counts stored by __src_bn.

Doest not performs computations of the parameters but keeps normalized counts of events only. Call idmLearning to compute the probabilities (with any parameter value).

nodeType(self, id)
Parameters: id (int) – the ant reference to the choosen NodeId the type of the choosen node in the (up-to-date) CredalNet in __src_bn. pyAgrum.CredalNet
saveBNsMinMax(self, min_path, max_path)

If this CredalNet was built over a perturbed BayesNet, one can save the intervals as two BayesNet.

to call after bnToCredal(GUM_SCALAR beta) save a BN with lower probabilities and a BN with upper ones

Parameters: min_path (str) – the path to save the BayesNet which contains the lower probabilities of each node X. max_path (str) – the path to save the BayesNet which contains the upper probabilities of each node X.
setCPT(self, id, entry, cpt)

setCPT(self, id, ins, cpt)

Warning

(experimental function) - Parameters to be wrapped

Set the vertices of one credal set of a given node (any instantiation index)

Parameters: id (int) – the Id of the node entry (int) – the index of the instantiation (from 0 to K - 1) excluding the given node (only the parents are used to compute the index of the credal set) ins (pyAgrum.Instantiation) – the Instantiation (only the parents matter to find the credal set index) cpt (tbw) – the vertices of every credal set (for each instantiation of the parents)

Warning

DOES not change the BayesNet(s) associated to this credal net !

setCPTs(self, id, cpt)

Warning

(experimental function) - Parameters to be wrapped

Set the vertices of the credal sets (all of the conditionals) of a given node

Parameters: id (int) – the NodeId of the node cpt (tbw) – the vertices of every credal set (for each instantiation of the parents)

Warning

DOES not change the BayesNet (s) associated to this credal net !

src_bn(self)
Returns: Returns a ant reference to the original BayesNet (used as a DAG, it’s CPTs does not matter). pyAgrum.BayesNet

## Inference¶

pyAgrum.CNMonteCarloSampling
class pyAgrum.pyAgrum.CNMonteCarloSampling_double(credalNet)

Class used for inferences in credal networks with Monte Carlo sampling algorithm.

Available ructors:

CNMonteCarloSampling(cn) -> CNMonteCarloSampling
Parameters: cn (pyAgrum.CredalNet) – a Credal network
asIApproximationSchemeConfiguration(self)
Returns: asIApproximationSchemeConfiguration
currentTime(self)
Returns: get the current running time in second (double) double
dynamicExpMax(self, varName)

Get the upper dynamic expectation of a given variable prefix.

Parameters: varName (str) – the variable name prefix which upper expectation we want. a ant reference to the variable upper expectation over all time steps. double
dynamicExpMin(self, varName)

Get the lower dynamic expectation of a given variable prefix.

Parameters: varName (str) – the variable name prefix which lower expectation we want. a ant reference to the variable lower expectation over all time steps. double
epsilon(self)
Returns: the value of epsilon double
history(self)
Returns: the scheme history tuple gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
insertEvidenceFile(self, path)

Insert evidence from file.

Parameters: path (str) – the path to the evidence file.
insertModalsFile(self, path)

Insert variables modalities from file to compute expectations.

Parameters: path (str) – The path to the modalities file.
makeInference(self)

Starts the inference.

marginalMax(self, id)

marginalMax(self, name) -> Vector

Get the upper marginals of a given node id.

Parameters: id (int) – the node id which upper marginals we want. varName (str) – the variable name which upper marginals we want. a ant reference to this node upper marginals. list gum.IndexError – If the node does not belong to the Credal network
marginalMin(self, id)

marginalMin(self, name) -> Vector

Get the lower marginals of a given node id.

Parameters: id (int) – the node id which lower marginals we want. varName (str) – the variable name which lower marginals we want. a ant reference to this node lower marginals. list gum.IndexError – If the node does not belong to the Credal network
maxIter(self)
Returns: the criterion on number of iterations int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrIterations(self)
Returns: the number of iterations int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – If p<1
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – If eps<0
setMaxIter(self, max)
Parameters: max (int) – the maximum number of iteration gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters: rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters: p (int) – number of samples between 2 stopping gum.OutOfLowerBound – If p<1
setRepetitiveInd(self, flag)
Parameters: flag (bool) – True if repetitive independence is to be used, false otherwise. Only usefull with dynamic networks.
setVerbosity(self, v)
Parameters: v (bool) – verbosity
verbosity(self)
Returns: True if the verbosity is enabled bool
pyAgrum.CNLoopyPropagation
class pyAgrum.pyAgrum.CNLoopyPropagation_double(cnet)

Class used for inferences in credal networks with Loopy Propagation algorithm.

Available ructors:

CNLoopyPropagation(cn) -> CNLoopyPropagation
Parameters: cn (pyAgrum.CredalNet) – a Credal network
currentTime(self)
Returns: get the current running time in second (double) double
dynamicExpMax(self, varName)

Get the upper dynamic expectation of a given variable prefix.

Parameters: varName (str) – the variable name prefix which upper expectation we want. a ant reference to the variable upper expectation over all time steps. double
dynamicExpMin(self, varName)

Get the lower dynamic expectation of a given variable prefix.

Parameters: varName (str) – the variable name prefix which lower expectation we want. a ant reference to the variable lower expectation over all time steps. double
epsilon(self)
Returns: the value of epsilon double
eraseAllEvidence(self)

Erase all inference related data to perform another one.

You need to insert evidence again if needed but modalities are kept. You can insert new ones by using the appropriate method which will delete the old ones.

history(self)
Returns: the scheme history tuple gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
inferenceType(self, inft)

inferenceType(self) -> pyAgrum.credal::CNLoopyPropagation ::InferenceType

Returns: the inference type int
insertEvidenceFile(self, path)

Insert evidence from file.

Parameters: path (str) – the path to the evidence file.
insertModalsFile(self, path)

Insert variables modalities from file to compute expectations.

Parameters: path (str) – The path to the modalities file.
makeInference(self)

Starts the inference.

marginalMax(self, id)

marginalMax(self, name) -> Vector

Get the upper marginals of a given node id.

Parameters: id (int) – the node id which upper marginals we want. varName (str) – the variable name which upper marginals we want. a ant reference to this node upper marginals. list gum.IndexError – If the node does not belong to the Credal network
marginalMin(self, id)

marginalMin(self, name) -> Vector

Get the lower marginals of a given node id.

Parameters: id (int) – the node id which lower marginals we want. varName (str) – the variable name which lower marginals we want. a ant reference to this node lower marginals. list gum.IndexError – If the node does not belong to the Credal network
maxIter(self)
Returns: the criterion on number of iterations int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrIterations(self)
Returns: the number of iterations int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – If p<1
saveInference(self, path)

Saves marginals.

Parameters: path (str) – The path to the file to save marginals.
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – If eps<0
setMaxIter(self, max)
Parameters: max (int) – the maximum number of iteration gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters: rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters: p (int) – number of samples between 2 stopping gum.OutOfLowerBound – If p<1
setRepetitiveInd(self, flag)
Parameters: flag (bool) – True if repetitive independence is to be used, false otherwise. Only usefull with dynamic networks.
setVerbosity(self, v)
Parameters: v (bool) – verbosity
verbosity(self)
Returns: True if the verbosity is enabled bool