ExactSearch#

class causalbase.exact.ExactSearch(criterion='bdeu', ess=1.0, expected_parents=1.0, prior='bernoulli', penalty_discount=1.0, alpha_w=None, max_parents=None, time_limit=None, n_jobs=1)[source]#

Learn the Bayesian network of highest score, or the best one under a parent limit.

The search is exact_search() on the score criterion names: dynamic programming over variable subsets without a parent limit (some 25 variables at most), otherwise an integer program solved with SCIP, seeded with a topological order of the network GES learns, or of initial_graph in fit().

Parameters:
criterion{“bdeu”, “bic”, “gaussian_bic”, “bge”}, default=”bdeu”

The score: BDeu or BIC over categorical data, or the BIC or BGe of a linear Gaussian model over continuous data.

essfloat, default=1.0

Equivalent sample size: of the BDeu prior, or of BGe’s prior on the mean. Also the prior of the parameters behind score() on categorical data.

expected_parentsfloat or None, default=1.0

Expected in-degree under the structure prior, for every criterion; see FamilyScore. None is a uniform prior.

prior{“bernoulli”, “tetrad”}, default=”bernoulli”

The structure prior: the Bernoulli prior of FamilyScore, or, for BDeu only, Tetrad 7.6.5’s, with expected_parents as Tetrad’s structure prior (see BDeuScore).

penalty_discountfloat, default=1.0

Multiplier of the penalty of the two BIC criteria; larger values give sparser structures.

alpha_wfloat or None, default=None

Degrees of freedom of BGe’s Wishart prior, above n_vars + 1; None is n_vars + 2. See BGeScore.

max_parentsint or None, default=None

Most parents of a node; None is no limit.

time_limitfloat or None, default=None

With max_parents, SCIP’s time limit in seconds; the result is then the best network SCIP found.

n_jobsint, default=1

Threads of the score’s compiled kernels; -1 is every thread numba has. The result does not depend on it.

Attributes:
dag_DAG

The learned network.

cpdag_PDAG

Its Markov equivalence class.

score_float

The score of dag_ under criterion, with the structure prior chosen (with prior="tetrad", the value Tetrad reports).

adjacency_pandas.DataFrame

pandas.DataFrame: The learned CPDAG as a named adjacency matrix.

n_features_in_int

Number of variables.

feature_names_in_ndarray of str

Variable names.

n_iter_int

Always 0.

trace_list of tuple

(seconds, 0, score) of the optimum.

Examples

>>> from causalbase import ExactSearch
>>> from causalbase.datasets import load_asia
>>> model = ExactSearch().fit(load_asia().data)
>>> model.cpdag_
PDAG(num_nodes=8, directed=..., undirected=...)

Methods

fit(X[, y, initial_graph])

Learn the structure of X.

get_params([deep])

Return the estimator's hyperparameters by name.

score(X[, y])

Return the mean log likelihood per row of X under the learned network.

set_params(**params)

Set hyperparameters by name and return the estimator.

property adjacency_#

pandas.DataFrame: The learned CPDAG as a named adjacency matrix.

fit(X, y=None, initial_graph=None)#

Learn the structure of X.

Parameters:
XDiscreteDataset, ContinuousDataset, pandas.DataFrame or array-like

Of shape (n_rows, n_vars). For the discrete criteria every distinct value of a column is a level; for "gaussian_bic" and "bge" the columns are real-valued.

yNone

Ignored.

initial_graphDAG or PDAG, optional

Where the search starts, over the columns of X: the graph itself for the searches over graphs and equivalence classes, a topological order of it for the searches over orders, and the first solution of the integer program for ExactSearch with a parent limit (the dynamic programming has no starting point). A PDAG stands for a DAG it extends to. By default each estimator’s own start, which its docstring gives.

Returns:
self
Raises:
ValueError

If X has no rows, initial_graph has another number of nodes, or a hyperparameter is out of range (including prior="tetrad" with a criterion other than "bdeu").

get_params(deep=True)#

Return the estimator’s hyperparameters by name.

score(X, y=None)#

Return the mean log likelihood per row of X under the learned network.

The parameters are learned from the training data: by a BDeu prior of equivalent sample size ess for categorical data, by maximum likelihood for continuous data. Higher is better, so a scikit-learn parameter search can select the hyperparameters on held-out rows.

Parameters:
XDiscreteDataset, ContinuousDataset, pandas.DataFrame or array-like

Rows over the training variables, with no level the training data lacks.

yNone

Ignored.

Returns:
float
Raises:
ValueError

If X has other variables, or a level the training data lacks.

set_params(**params)#

Set hyperparameters by name and return the estimator.