fake_data_for_learning.fake_data_for_learning module

Main module.

class fake_data_for_learning.fake_data_for_learning.BayesianNodeRV(name, cpt, values=None, parent_names=[])[source]

Bases: object

Sample-able random variable corresponding to node of a discrete Bayesian network.

Parameters
  • name (string) – Node variable name

  • cpt (numpy.array) – (Conditional) probability array. NB: We depart from normal convention on the assignment of the array dimensions. In this class, the node variable (aka dependent, not conditioned on) corresponds to the LAST componend of array indexing. E.g. For a 2-d array cpt, this means that the ROWS must sum to 1, not the columns, as is otherwise standard. This choice is to make cpt definition in numpy more human-readable.

  • values (list, optional) – list of values random variable will take. Default is [0, cpt.shape[-1]) if values are given, use sklearn.preprocessing.LabelEncoder(). NOTE: the sklearn label encoder sorts values in lexicographic order. To ensure compatibility with the conditional probability table, given values must also be in lexicographic order.

  • parent_names (list, optional) – list of parent node random variable names. Default is empty list, i.e. no parents

get_internal_value(sample_value)[source]

Translate SampleValue representation to natural number consistent with conditional probability table definition.

Parameters

sample_value (SampleValue) –

Returns

res – Internal (integer) representation of external value

Return type

int

get_probability_table(parent_values={})[source]

Get probability table.

Parameters

parent_values (dict, empty or {'name1': SampleValue(..), 'name2': ...}) – Values of parent nodes to get relevant 1-d submatrix of the (conditional) probability table

pmf(value, parent_values={})[source]

Probability mass function

Parameters
  • value (int or str) – Must be contained in the class attribute values

  • parent_values (dict of SampleValue) – Required in case of conditional random variable

Returns

Return type

float

rvs(parent_values={}, size=1, seed=None)[source]

Generate random variates from the bayesian node.

Parameters
  • parent_values (dict) – Dict with parent values, possibly empty

  • size (int) – Number of random samples to draw

  • seed (int) – Seed for numpy.random

Returns

rvs – Random variates of given size.

Return type

ndarray or scalar

class fake_data_for_learning.fake_data_for_learning.FakeDataBayesianNetwork(*args)[source]

Bases: object

Sample-able Bayesian network comprised up of BayesianNetworkRV’s

Parameters
  • args (tuple of BayesianNetworkRV's (BNRVs)) – BayesianNetworkRV’s that make up the Bayesian network

  • Attributes ((Other)) –

  • ------------------ –

  • node_names (list of strings) – Node variable names of the BNRVs

  • adjacency_matrix (numpy array) – Adjacency matrix of the Bayesian network’s graph

  • eve_node_names (list of strings) – Node variable names without parents

calc_adjacency_matrix()[source]
draw_graph()[source]
get_ancestral_sample()[source]

Ancestral sampling from the Bayesian network.

get_expected_cpt_dims(parent_idxs, child_value_length)[source]

Return expected dimension of the conditional probability table. Note that the tuple ordering depends on the ordering of the parent ordering for each non-orphan bayesian node random variable.

Parameters
  • parent_idxs (list of int) – Indices of parent nodes in creation of Bayesian network

  • child_value_length (int) – Cardinality of values of child node

Returns

Return type

tuple

get_graph()[source]
get_node(node_name)[source]
get_ordered_samples()[source]
static get_parent_idx(child_idx, adjacency_matrix)[source]

Return list of index positions of parents of node at child_idx in adjacency matrix

get_topological_ordering()[source]
static name_in_list(name, l)[source]

Return 1 if name is in list l, else 0.

pmf(sample)[source]

Probabilty mass function for bayesian network.

Parameters

sample (pandas.Series) – Series of sampled values

Returns

res – Probability mass function evaluated at sample

Return type

float

rvs(size=1)[source]

Ancestrally sampled values from the Bayesian network.

class fake_data_for_learning.fake_data_for_learning.SampleValue(value, label_encoder=None)[source]

Bases: object

Container for parent values when sampling from conditional probability distributions.

If the sample values are non-default–i.e. other than natural numbers corresponding to the number of possible variable states–then a label encoder is required.

static possible_default_value(x)[source]

Check conditions that rule-out a default (i.e. natural number) value.