causalexplain.estimators.notears package#
Submodules#
- class LBFGSBScipy(params)[source]#
Bases:
OptimizerWrap L-BFGS-B algorithm, using scipy routines.
Courtesy: Arthur Mensch’s gist https://gist.github.com/arthurmensch/c55ac413868550f89225a0b9212aa4cd
- 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:
ModuleLocal 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.
- 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- 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:
- 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class NotearsSobolev(d, k)[source]#
Bases:
Module- 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- 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]#
NOTEARS
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:
- main(dataset_name, input_path='/Users/renero/phd/data/', output_path='/Users/renero/phd/output/', save=False)[source]#
- 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:
- 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.
- 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