基于 LiNGAM 的方法

从观测数据中估计线性非高斯无环模型。它假设因果模型中的噪声项是非高斯的。

causal-learn 提供了官方实现了一系列基于 LiNGAM 的方法(例如,基于 ICA 的 LiNGAM [1]、DirectLiNGAM [2]、VAR-LiNGAM [3]、RCD [4] 和 CAM-UV [5])。我们正在积极更新列表。

基于 ICA 的 LiNGAM

from causallearn.search.FCMBased import lingam
model = lingam.ICALiNGAM(random_state, max_iter)
model.fit(X)

print(model.causal_order_)
print(model.adjacency_matrix_)

参数

random_state: int, 可选 (默认值=None)。随机数生成器使用的种子。

max_iter: int, 可选 (默认值=1000)。FastICA 的最大迭代次数。

X: array-like, shape (n_samples, n_features)。训练数据,其中 n_samples 是样本数,n_features 是特征数。

返回值

model.causal_order_: array-like, shape (n_features)。拟合模型的因果顺序,其中 n_features 是特征数。

model.adjacency_matrix_: array-like, shape (n_features, n_features)。拟合模型的邻接矩阵 B,其中 n_features 是特征数。

DirectLiNGAM

from causallearn.search.FCMBased import lingam
model = lingam.DirectLiNGAM(random_state, prior_knowledge, apply_prior_knowledge_softly, measure)
model.fit(X)

print(model.causal_order_)
print(model.adjacency_matrix_)

参数

random_state: int, 可选 (默认值=None)。随机数生成器使用的种子。

prior_knowledge: array-like, shape (n_features, n_features), 可选 (默认值=None)。用于因果发现的先验知识,其中 n_features 是特征数。先验知识矩阵的元素定义如下:

  • 0: \(x_i\) 没有指向 \(x_j\) 的有向路径

  • 1: \(x_i\) 有指向 \(x_j\) 的有向路径

  • -1: 没有可用的先验知识来判断上述两种情况(0 或 1)是否成立。

apply_prior_knowledge_softly: boolean, 可选 (默认值=False)。如果为 True,则软应用先验知识。

measure: {‘pwling’, ‘kernel’}, 可选 (默认值=’pwling’)。用于评估独立性的度量:‘pwling’ 或 ‘kernel’。

X: array-like, shape (n_samples, n_features)。训练数据,其中 n_samples 是样本数,n_features 是特征数。

返回值

model.causal_order_: array-like, shape (n_features)。拟合模型的因果顺序,其中 n_features 是特征数。

model.adjacency_matrix_: array-like, shape (n_features, n_features)。拟合模型的邻接矩阵 B,其中 n_features 是特征数。

VAR-LiNGAM

from causallearn.search.FCMBased import lingam
model = lingam.VARLiNGAM(lags, criterion, prune, ar_coefs, lingam_model, random_state)
model.fit(X)

print(model.causal_order_)
print(model.adjacency_matrices_[0])
print(model.adjacency_matrices_[1])
print(model.residuals_)

参数

lags: int, 可选 (默认值=1)。滞后数。

criterion: {‘aic’, ‘fpe’, ‘hqic’, ‘bic’, None}, 可选 (默认值=’bic’)。在指定 ‘lags’ 范围内决定最佳滞后数的准则。如果 ‘criterion’ 为 None,则禁用搜索最佳滞后数。

prune: boolean, 可选 (默认值=False)。是否修剪邻接矩阵。

ar_coefs: array-like, 可选 (默认值=None)。AR 模型的系数。如果指定了 ‘ar_coefs’,则跳过估计 AR 模型。形状必须为 (‘lags’, n_features, n_features)。

lingam_model: lingam 对象,继承自 ‘lingam._BaseLiNGAM’,可选 (默认值=None)。用于因果发现的 LiNGAM 模型。如果为 None,则选择 DirectLiNGAM 算法。

random_state: int, 可选 (默认值=None)。‘random_state’ 是随机数生成器使用的种子。

X: array-like, shape (n_samples, n_features)。训练数据,其中 n_samples 是样本数,n_features 是特征数。

返回值

model.causal_order_: array-like, shape (n_features)。拟合模型的因果顺序,其中 n_features 是特征数。

model.adjacency_matrices_: array-like, shape (lags, n_features, n_features)。拟合模型的邻接矩阵,其中 n_features 是特征数。

model.residuals_: array-like, shape (n_samples)。回归的残差,其中 n_samples 是样本数。

RCD

from causallearn.search.FCMBased import lingam
model = lingam.RCD(max_explanatory_num, cor_alpha, ind_alpha, shapiro_alpha, MLHSICR, bw_method)
model.fit(X)

print(model.adjacency_matrix_)
print(model.ancestors_list_)

参数

max_explanatory_num: int, 可选 (默认值=2)。最大解释变量数。

cor_alpha: float, 可选 (默认值=0.01)。Pearson 相关性的 Alpha 水平。

ind_alpha: float, 可选 (默认值=0.01)。HSIC 的 Alpha 水平。

shapiro_alpha: float, 可选 (默认值=0.01)。Shapiro-Wilk 检验的 Alpha 水平。

MLHSICR: bool, 可选 (默认值=False)。如果为 True,对多元回归使用 MLHSICR;如果为 False,对多元回归使用 OLS。

bw_method: str, 可选 (默认值=’mdbs’)。用于计算 HSIC 带宽的方法。
  • ‘mdbs’: 样本之间的中位数距离。

  • ‘scott’: Scott 经验法则。

  • ‘silverman’: Silverman 经验法则。

X: array-like, shape (n_samples, n_features)。训练数据,其中 n_samples 是样本数,n_features 是特征数。

返回值

model.adjacency_matrix_: array-like, shape (n_features, n_features)。拟合模型的邻接矩阵 B,其中 n_features 是特征数。

model.ancestors_list_: array-like, shape (n_features)。因果祖先集合列表,其中 n_features 是特征数。

CAM-UV

from causallearn.search.FCMBased.lingam import CAMUV
P, U = CAMUV.execute(data, alpha, num_explanatory_vals)

for i, result in enumerate(P):
    if not len(result) == 0:
        print("child: " + str(i) + ",  parents: " + str(result))

for result in U:
    print(result)

参数

data: array-like, shape (n_samples, n_features)。训练数据,其中 n_samples 是样本数,n_features 是特征数。

alpha: 独立性检验的 alpha 水平。

num_explanatory_vals: 用于推断因果关系的最大变量数。这相当于论文中的 d。

返回值

P: P[i] 包含 Xi 的父节点的索引。

U: 具有 UCP 或 UBP 的变量对的索引。