bn_testing package
Submodules
bn_testing.conditionals module
- class bn_testing.conditionals.Conditional
Bases:
object
Base class of conditional distributions
- init(random)
- make_noise()
- make_source()
- class bn_testing.conditionals.ConstantConditional(value)
Bases:
Conditional
A conditional yielding constant values. Used in intervensions and to compute causal effects.
- make_noise()
- make_source()
- class bn_testing.conditionals.LinearConditional(coef_min=0.5, coef_max=1)
Bases:
Conditional
Linear conditionals
- class bn_testing.conditionals.PolynomialConditional(min_terms=1, max_terms=5, max_degree_add=10, with_tanh=True, with_log=False)
Bases:
Conditional
Conditional that builds randomized polynomial terms
- Parameters:
min_terms (int) – Minimal number of terms in a generated polynomial
min_terms – Maximal number of terms in a generated polynomial
max_degree_add (int) – Number to add on the number of variables in a monomial to get its largest possible degree
with_tanh (bool) – Whether
numpy.tanh()
should be applied onto the monmialwith_log (bool) – Whether each monomial should be wrapped by
log1p()
expression
bn_testing.dags module
- class bn_testing.dags.DAG
Bases:
object
- generate(random=None)
- init(random)
- make_dag()
Sets for the stated nodes in the dag the node attribute
is_hidden
to True.- Params networkx.DiGraph dag:
The DAG whose nodes should be hidden
- Params list nodes:
List of nodes in the DAG
- Returns:
The same DAG where the stated nodes have the additional attribute
is_hidden
set to True- Return type:
networkx.DiGraph
- static show(dag)
- class bn_testing.dags.ErdosReny(n_visible_nodes=None, p=0.1, n_hidden_nodes=0)
Bases:
RandomizedDAG
A Directed a acyclic graph generated with the Erdos-Reny model.
- Parameters:
- make_dag()
- class bn_testing.dags.RandomizedDAG(n_visible_nodes, n_hidden_nodes=0)
Bases:
DAG
Helper class to generate randomized DAGs.
- Parameters:
- generate(random)
- property n_nodes
- property nodes
- class bn_testing.dags.ScaleFree(n_visible_nodes=None, alpha=0.4, beta=0.5, gamma=0.1, n_hidden_nodes=0)
Bases:
RandomizedDAG
Generates a scale free DAG.
- make_dag()
bn_testing.helpers module
Helpers
- bn_testing.helpers.abslog(x)
- bn_testing.helpers.sigmoid(x)
bn_testing.models module
- class bn_testing.models.BayesianNetwork(dag=None, conditionals=None, random_state=None)
Bases:
object
- Parameters:
dag (bn_testing.dags.DAG) – A DAG generation method
conditionals (bn_testing.conditionals.Conditionals) – A conditional type
random_state (int) – A random state
- compute_average_causal_effect(node_from=None, node_onto=None, value=None, n=1000)
Computes the average causal effect of a node that has a certain value onto another node.
- compute_varsortability(n=1000)
Computes the varsortability of the graphical model (see [Reisach et al.](https://arxiv.org/abs/2102.13647)), a number between 0 and 1 that measures how easy the graph structure can be read of the marginal variances (the larger, the easier).
- property edges
- generate()
Generates the mod
- is_source(node)
Checks if the given node is a source node, i.e., has no parents.
- Args:
node (str): Name of a node in the dag
- Returns:
bool: True if node is a source, false otherwise
- static load(filepath)
Loads the model from a file.
- modify_inner_node(node, term=None, noise=None, conditionals=None)
Modifies the term and the noise of a given node.
- Parameters:
node (str) – Name of the node. Cannot be a source node
term (bn_testing.terms.Term) – Optional, a new term. If set to
None
, a term is generated randomly usingconditionals
.noise (aesara.tensor.var.TensorVariable) – Optional, a new noise. If set to
None
, a noise is generated using the conditional.conditionals (bn_testing.conditionals.Conditionals) – A conditional. If set to
None
, then term and noise must be given.
- modify_node(node, conditionals=None)
General function to modify the distribution at a node using a conditional.
If more control over the modification is needed, the functions
modify_inner_node()
for inner nodes andmodify_source_node()
for source nodes can be used respectively.
- modify_source_node(node, distribution=None, conditionals=None)
Modifies the distribution of a source node.
- Parameters:
node (str) – Name of the node. Cannot be a source node
distribution (aesara.tensor.var.TensorVariable) – Optional, a new distribution. If set to
None
, a source distribution is generated using the conditional.conditionals (bn_testing.conditionals.Conditionals) – A conditional. If set to
None
, then distribution must be given.
- property nodes
- sample(n, nodes=None, normalize=False, exclude_hidden_nodes=True)
Samples n many identic and independent observations from the Bayesian network.
- Parameters:
- Returns:
Dataframe in which the variables are columns and the observations are rows
- Return type:
pandas.DataFrame:
- save(filepath)
Saves the model to the specified file
- Parameters:
filepath (str) – Path to a file where model should be written to
- show()
Visualizes the generated DAG.
- property visible_nodes
bn_testing.terms module
- class bn_testing.terms.Linear(parents, coefs, intercept=0)
Bases:
Term
A linear weighted sum of the parent variables
- Parameters:
parents (list) – List of parent nodes
coefs (numpy.ndarray) – Array of coefficients, size must equal the number of parents
intercept (float) – intercept of the linear transformation
- apply(parents_mapping)
Applies the term onto the parents
- property disp
- class bn_testing.terms.Polynomial(parents, exponents, coefs, intercept=0, with_tanh=False, with_log=False)
Bases:
Term
A multivariate polynomial in the parents variables.
- Parameters:
parents (list) – List of parent nodes
exponents (numpy.ndarray) – Array holding the exponents of the parent variables. Values can be negative.
coefs (numpy.ndarray) – Array holding the coeficients for each monomial
intercept (float) – The intercept of the polynomial
with_tanh (bool) – Whether
numpy.tanh()
should be applied onto the monmialwith_log (bool) – Whether each monomial should be wrapped by
log1p()
expression
- apply(parents_mapping)
Applies the term onto the parents
- property disp