causalexplain.estimators.notears package#

Submodules#

class LBFGSBScipy(params)[source]#

Bases: Optimizer

Wrap L-BFGS-B algorithm, using scipy routines.

Courtesy: Arthur Mensch’s gist https://gist.github.com/arthurmensch/c55ac413868550f89225a0b9212aa4cd

__init__(params)[source]#
step(closure)[source]#

Performs a single optimization step.

Parameters:

closure (callable) – A closure that reevaluates the model and returns the loss.

main()[source]#
notears_linear(X, lambda1, loss_type, max_iter=100, h_tol=1e-08, rho_max=1e+16, w_threshold=0.3)[source]#

Solve NOTEARS for linear models with an augmented Lagrangian.

class LocallyConnected(num_linear, input_features, output_features, bias=True)[source]#

Bases: Module

Local linear layer, i.e. Conv1dLocal() with filter size 1.

Parameters:
  • num_linear – num of local linear layers, i.e.

  • in_features – m1

  • out_features – m2

  • bias – whether to include bias or not

Shape:
  • Input: [n, d, m1]

  • Output: [n, d, m2]

weight#

[d, m1, m2]

bias#

[d, m2]

__init__(num_linear, input_features, output_features, bias=True)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

reset_parameters()[source]#
forward(input)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

extra_repr()[source]#

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

main()[source]#
least_squares_loss(W, data, cov, d, n)[source]#
least_squares_loss_grad(W, data, cov, d, n)[source]#
least_squares_loss_cov(W, data, cov, d, n)[source]#
least_squares_loss_cov_grad(W, data, cov, d, n)[source]#
run(variant, data, loss, loss_grad, **kwargs)[source]#
notears_standard(data, loss, loss_grad, c=0.25, r=10.0, e=1e-08, rnd_W_init=False, output_all_progress=False, verbose=False, max_iter=None)[source]#

Runs NOTEARS algorithm.

Parameters:
  • data (np.array) – n x d data matrix with n samples, d variables

  • c (float) – minimum rate of progress, c in (0,1)

  • r (float) – penalty growth rate, r > 1

  • e (float) – optimation accuracy, e > 0 (acyclicity stopping criteria)

  • loss (function) – loss function

  • loss_grad (function) – gradient of the loss function

  • rnd_W_init (bool) – initialize W to std. normal random matrix, rather than zero matrix

  • output_all_progress (bool) – return all intermediate values of W, rather than

  • value (just the final)

  • verbose (bool) – print optimization information

Returns:

{ ‘h’: acyclicity of output,

’loss’: loss of output, ‘W’: resulting optimized adjacency matrix}

Return type:

dict

class NotearsMLP(dims, bias=True)[source]#

Bases: Module

__init__(dims, bias=True)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

h_func()[source]#

Constrain 2-norm-squared of fc1 weights along m1 dim to be a DAG

l2_reg()[source]#

Take 2-norm-squared of all parameters

fc1_l1_reg()[source]#

Take l1 norm of fc1 weight

fc1_to_adj()[source]#

Get W from fc1 weights, take 2-norm over m1 dim

class NotearsSobolev(d, k)[source]#

Bases: Module

__init__(d, k)[source]#

d: num variables k: num expansion of each variable

sobolev_basis(x)[source]#
forward(x)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

h_func()[source]#
l2_reg()[source]#
fc1_l1_reg()[source]#
fc1_to_adj()[source]#
squared_loss(output, target)[source]#
dual_ascent_step(model, X, lambda1, lambda2, rho, alpha, h, rho_max)[source]#

Perform one step of dual ascent in augmented Lagrangian.

notears_nonlinear(model, X, lambda1=0.0, lambda2=0.0, max_iter=100, h_tol=1e-08, rho_max=1e+16, w_threshold=0.3)[source]#
main()[source]#

NOTEARS

  1. Original code from xunzheng/notears

class NOTEARS(name, loss=<function least_squares_loss>, loss_grad=<function least_squares_loss_grad>, c=0.25, r=10.0, e=1e-08, rnd_W_init=False, verbose=False)[source]#

Bases: object

__init__(name, loss=<function least_squares_loss>, loss_grad=<function least_squares_loss_grad>, c=0.25, r=10.0, e=1e-08, rnd_W_init=False, verbose=False)[source]#
notears_standard(data, return_all_progress=False, max_iter=None)[source]#

Runs NOTEARS algorithm.

Parameters:
  • data (np.array) – n x d data matrix with n samples, d variables

  • c (float) – minimum rate of progress, c in (0,1)

  • r (float) – penalty growth rate, r > 1

  • e (float) – optimation accuracy, e > 0 (acyclicity stopping criteria)

  • loss (function) – loss function

  • loss_grad (function) – gradient of the loss function

  • rnd_W_init (bool) – initialize W to std. normal random matrix, rather than zero matrix

  • output_all_progress (bool) – return all intermediate values of W, rather than

  • value (just the final)

Returns:

{ ‘h’: acyclicity of output,

’loss’: loss of output, ‘W’: resulting optimized adjacency matrix}

Return type:

dict

fit(X, **kwargs)[source]#
predict(ref_graph=None, threshold=0.1)[source]#
fit_predict(train, test, ref_graph=None, threshold=0.1, **kwargs)[source]#
main(dataset_name, input_path='/Users/renero/phd/data/', output_path='/Users/renero/phd/output/', save=False)[source]#
class TraceExpm(*args, **kwargs)[source]#

Bases: Function

static forward(ctx, input)[source]#

Compute the trace of the matrix exponential.

static backward(ctx, grad_output)[source]#

Compute gradients for the trace-expm operation.

main()[source]#
set_random_seed(seed)[source]#
is_dag(W)[source]#
simulate_dag(d, s0, graph_type)[source]#

Simulate random DAG with some expected number of edges.

Parameters:
  • d (int) – num of nodes

  • s0 (int) – expected num of edges

  • graph_type (str) – ER, SF, BP

Returns:

[d, d] binary adj matrix of DAG

Return type:

B (np.ndarray)

simulate_parameter(B, w_ranges=((-2.0, -0.5), (0.5, 2.0)))[source]#

Simulate SEM parameters for a DAG.

Parameters:
  • B (np.ndarray) – [d, d] binary adj matrix of DAG

  • w_ranges (tuple) – disjoint weight ranges

Returns:

[d, d] weighted adj matrix of DAG

Return type:

W (np.ndarray)

simulate_linear_sem(W, n, sem_type, noise_scale=None)[source]#

Simulate samples from linear SEM with specified type of noise.

For uniform, noise z ~ uniform(-a, a), where a = noise_scale.

Parameters:
  • W (np.ndarray) – [d, d] weighted adj matrix of DAG

  • n (int) – num of samples, n=inf mimics population risk

  • sem_type (str) – gauss, exp, gumbel, uniform, logistic, poisson

  • noise_scale (np.ndarray) – scale parameter of additive noise, default all ones

Returns:

[n, d] sample matrix, [d, d] if n=inf

Return type:

X (np.ndarray)

simulate_nonlinear_sem(B, n, sem_type, noise_scale=None)[source]#

Simulate samples from nonlinear SEM.

Parameters:
  • B (np.ndarray) – [d, d] binary adj matrix of DAG

  • n (int) – num of samples

  • sem_type (str) – mlp, mim, gp, gp-add

  • noise_scale (np.ndarray) – scale parameter of additive noise, default all ones

Returns:

[n, d] sample matrix

Return type:

X (np.ndarray)

count_accuracy(B_true, B_est)[source]#

Compute various accuracy metrics for B_est.

true positive = predicted association exists in condition in correct direction reverse = predicted association exists in condition in opposite direction false positive = predicted association does not exist in condition

Parameters:
  • B_true (np.ndarray) – [d, d] ground truth graph, {0, 1}

  • B_est (np.ndarray) – [d, d] estimate, {0, 1, -1}, -1 is undirected edge in CPDAG

Returns:

(reverse + false positive) / prediction positive tpr: (true positive) / condition positive fpr: (reverse + false positive) / condition negative shd: undirected extra + undirected missing + reverse nnz: prediction positive

Return type:

fdr

threshold_output(W, desired_edges=None, verbose=False)[source]#
generate_random_dag(num_nodes, num_edges, probabilistic=False, edge_coefficient_range=[0.5, 2.0])[source]#
simulate_from_dag_lg(tam, n_sample, mean=0, variance=1)[source]#
compare_graphs_undirected(true_graph, estimated_graph)[source]#
compare_graphs_precision(x)[source]#
compare_graphs_recall(x)[source]#
compare_graphs_specificity(x)[source]#

Module contents#