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()
make_term(parents, node)

Builds a term randomly.

Parameters:
  • parents (list) – Name of the parent nodes.

  • node (str) – Name of the node whose term should be made

Returns:

A term

Return type:

bn_testing.term.Term

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()
make_term(parents, node)

Builds a term randomly.

Parameters:
  • parents (list) – Name of the parent nodes.

  • node (str) – Name of the node whose term should be made

Returns:

A term

Return type:

bn_testing.term.Term

class bn_testing.conditionals.LinearConditional(coef_min=0.5, coef_max=1)

Bases: Conditional

Linear conditionals

make_term(parents, node)

Builds a term randomly.

Parameters:
  • parents (list) – Name of the parent nodes.

  • node (str) – Name of the node whose term should be made

Returns:

A term

Return type:

bn_testing.term.Term

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 monmial

  • with_log (bool) – Whether each monomial should be wrapped by log1p() expression

make_term(parents, node)

Builds a term randomly.

Parameters:
  • parents (list) – Name of the parent nodes.

  • node (str) – Name of the node whose term should be made

Returns:

A term

Return type:

bn_testing.term.Term

bn_testing.dags module

class bn_testing.dags.DAG

Bases: object

generate(random=None)
init(random)
make_dag()
mark_as_hidden(dag, nodes)

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:
  • p (float) – Erdös-Renyi probability

  • n_visible_nodes (int) – Number of visible nodes

  • n_hidden_nodes (int) – Number of hidden nodes

make_dag()
class bn_testing.dags.RandomizedDAG(n_visible_nodes, n_hidden_nodes=0)

Bases: DAG

Helper class to generate randomized DAGs.

Parameters:
  • n_visible_nodes (int) – The number of visible nodes

  • n_hidden_nodes (int) – The number of hidden nodes. Defaults to 0.

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.

Parameters:
  • node_from (str) – Name of node that gets the intervention

  • node_onto (str) – Variable whose change should be computed

  • value (float) – Value of intervention

  • n (int) – Sample size to approximate the expected values

Returns:

The average causal effect

Return type:

float

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).

Parameters:

n (int) – The sample size to estimate the variances

Returns:

The varsortability of the dag

Return type:

float

property edges
generate()

Generates the mod

property hidden_nodes
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.

Parameters:

filepath (str) – Path to a file where model has been written to with save()

Returns:

The loaded model

Return type:

bn_testing.models.BayesianNetwork

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 using conditionals.

  • 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 and modify_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:
  • n (int) – Number of observation to be created

  • exclude_hidden_nodes (bool) – If True, hidden nodes will be excluded. Defaults to True.

  • normalize (bool) – If true, each column in the resulting dataframe is divided by its standard deviation

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.Constant(parents, value)

Bases: Term

A constant term.

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 monmial

  • with_log (bool) – Whether each monomial should be wrapped by log1p() expression

apply(parents_mapping)

Applies the term onto the parents

property disp
class bn_testing.terms.Term(parents, term_fn=None, disp='')

Bases: object

A mathematical expression that transforms the parent nodes into the given node

Parameters:
  • parents (list) – List of parent nodes

  • term_fn (function) – Function that maps a dict where the parent names are the keys and pymc variables are the values to a pymc variable

  • disp (str) – String that should be shown when the term is displayed

apply(parents_mapping)

Applies the term onto the parents

property disp
get_vars_from_dict(parent_dict)

Flattens the dict of parents to a list with order specified in the constructor.

Parameters:

parent_dictdict where the keys are parent names and the values are variables.

Returns:

List of parent variables

Return type:

list

Module contents