HillClimbing#

class causalbase.search.HillClimbing(criterion='bdeu', ess=1.0, expected_parents=1.0, prior='bernoulli', penalty_discount=1.0, alpha_w=None, tabu_length=0, max_tabu_moves=0, n_restarts=0, perturbation=1, time_limit=None, random_state=None, n_jobs=1)[source]#

Learn a Bayesian network by hill climbing, or tabu search, over DAGs.

The search is hill_climbing() on the score criterion names: from the empty graph, or from initial_graph in fit(), apply the arc addition, deletion or reversal of largest gain until none improves, optionally as a tabu search and with random restarts, as bnlearn’s hc and tabu.

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.

tabu_lengthint, default=0

With a positive value, tabu search: the best move is applied even if it loses score, unless its DAG is among the last tabu_length visited.

max_tabu_movesint, default=0

Tabu search stops after this many moves without improving its best DAG.

n_restartsint, default=0

Climbs after the first, each from the best DAG so far perturbed by perturbation random moves.

perturbationint, default=1

Random moves before each restart.

time_limitfloat or None, default=None

Wall-clock budget in seconds.

random_stateint or None, default=None

Seeds the perturbations; None draws a fresh seed.

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

Moves applied, perturbations excluded.

trace_list of tuple

(seconds, moves, score) each time the best score improved.

Examples

>>> from causalbase import HillClimbing
>>> from causalbase.datasets import load_asia
>>> model = HillClimbing(n_restarts=2, random_state=0).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.