Bayesian Nets
Bayesian nets can be used for general, causal analysis or in combination with Entropy Pooling for causal and predictive views / stress-testing. Methods for storing Entropy Pooling views and computing appropriately weighted Entropy Pooling posterior probabilities are integrated into the BayesianNet class, making it straightforward to perform these complex analyses.
The entropy_pooling_probability method accepts options for a particular Entropy
Pooling computation. Besides the control parameters that are available for the
Entropy Pooling class, these options can contain parameters for the
compute_posterior_probability method, i.e., heuristic and verbose parameters.
Finally, it is possible to specify that the intermediate Entropy Pooling
probabilities are returned using the 'return_all' key:
post_prob, intermediate_probs = bnet.entropy_pooling_probability(R, options={'return_all': True})
- class BayesianNet(name=None)
Bayesian net class.
- Parameters:
name (
str) – Name of the Bayesian net.
- add_edge(parent, child)
Method for adding an edge between two nodes in the Bayesian net.
- Parameters:
parent (
str) – Name of parent node.child (
str) – Name of child node.
- Raises:
ValueError – If parent or child node has not been defined.
- add_node(name, states)
Method for adding a node to the Bayesian net.
- Parameters:
name (
str) – Name of the new node.states (
List[str]) – A list of possible states for the new node.
- Raises:
TypeError – If name or one of the states is not a string.
TypeError – If states is not a list.
ValueError – If states is empty.
- build()
Method for building the Bayesian net.
- entropy_pooling_probability(R, p=None, observations=None, **kwargs)
Method for computing Entropy Pooling probabilities given the leaf nodes specification.
- Parameters:
R (
DataFrame) – P&L / risk factor simulation with shape (S, I).p (
ndarray) – Prior probability vector with shape (S,) for the S scenarios in R. Set to np.ones(S) / S by default.observations (
dict) – Dictionary containing observations.kwargs (
dict) – Entropy Pooling options.
- Return type:
Union[ndarray,Tuple[ndarray,ndarray]]- Returns:
Vector with shape (S,) containing weighted posterior probabilities or a tuple containing the weighted posterior probabilities as well as a matrix with shape (S, num_joint_states) containing the unweighted posterior probabilities for the individual joint states if return_all is set to True.
- Raises:
ValueError – If p is not a vector of shape (S,) containing strictly positive elements that sum to 1.
ValueError – If no view is specified for some node and state.
ValueError – If observations contains a node which is not a part of the network.
ValueError – If a state is invalid for a particular node.
- get_entropy_pooling_views()
Method for getting the current Entropy Pooling views dictionary.
- Return type:
dict- Returns:
Current Entropy Pooling views dictionary.
- Raises:
AttributeError – If no Entropy Pooling views have been specified.
- get_parent(node=None)
Method for getting the parent(s) of node(s).
- Parameters:
node (
str) – Name of the node that parents should be extracted for. If given None (default), parents of all nodes will be returned.- Return type:
Union[DataFrame,dict]- Returns:
Parent(s) of node(s).
- Raises:
ValueError – If the node is not a part of the network.
- get_probability(node=None)
Method for extracting the current probability table(s) of node(s).
- Parameters:
node (
str) – Name of the node that the probability table should be extracted for. If given None (default), probability tables of all nodes are returned.- Return type:
Union[DataFrame,dict]- Returns:
Current probability table(s) of the node(s).
- Raises:
ValueError – If the node does not exist or the probability table has not been initialized.
- initialize_probability_tables()
Method for initializing probability tables.
- joint_probability(observations=None)
Method for computing joint probabilities of the leaf nodes.
- Parameters:
observations (
dict) – Dictionary containing observations.- Return type:
DataFrame- Returns:
pd.DataFrame containing the joint probabilities of the leaf nodes.
- Raises:
ValueError – If a node is not part of the network.
ValueError – If a state is invalid for a particular node.
- marginal_probability(observations=None)
Method for computing marginal probabilities.
- Parameters:
observations (
dict) – Dictionary containing observations.- Return type:
dict[DataFrame]- Returns:
Dictionary of pd.DataFrames containing the probabilities of each node.
- Raises:
ValueError – If a node is not a part of the network.
ValueError – If a state is invalid for a particular node.
- plot(figsize=None)
Method for plotting the Bayesian net.
- Parameters:
figsize (
Tuple) – Figure size of the plot. Default: (9, 6).- Raises:
ValueError – If figsize is not a tuple of length two containing integers or floats.
- set_entropy_pooling_view(node, state, view_lists)
Method for setting Entropy Pooling views for a particular leaf node and state.
- Parameters:
node (
str) – Name of the leaf node.state (
str) – Name of the state.view_lists (
list) – List of lists containing views. See documentation for Entropy Pooling add_view and add_rank_view.
- Raises:
ValueError – If node is not a leaf node.
ValueError – If state does not exist for the node.
ValueError – If the first index of a list in view_lists is not in {‘regular’, ‘rank’}.
- set_probability(node, probability)
Method for setting the probability of a node.
- Parameters:
node (
str) – Name of the node that probabilities are set for.probability (
Union[List,ndarray]) – Probability table containing the (conditional) state probabilities.
- Raises:
ValueError – If node is not part of the network.
ValueError – If the probability tables have not been initialized.
ValueError – If any probabilities are negative.
ValueError – If the elements in a row sum to more than 1.
ValueError – If any given bounds do not lie in the interval [0, 1].
ValueError – If probability contains a list that does not have exactly 2 elements.
ValueError – If probability contains a list where the lower bound is greater than the upper bound.
ValueError – If the lower bounds sum to more than 1, or the upper bounds sum to less than 1.
ValueError – If the dimensionality of probability is different from the initialized probability table.