模拟干预措施的影响#

通过模拟干预措施的影响,我们可以回答诸如以下问题:

如果我干预 Y,变量 Z 会发生什么?

如何使用#

为了了解该方法如何工作,让我们生成一些数据

>>> import numpy as np, pandas as pd
>>> X = np.random.normal(loc=0, scale=1, size=1000)
>>> Y = 2*X + np.random.normal(loc=0, scale=1, size=1000)
>>> Z = 3*Y + np.random.normal(loc=0, scale=1, size=1000)
>>> training_data = pd.DataFrame(data=dict(X=X, Y=Y, Z=Z))

接下来,我们将因果关系建模为概率因果模型并将其拟合到数据

>>> import networkx as nx
>>> from dowhy import gcm
>>> causal_model = gcm.ProbabilisticCausalModel(nx.DiGraph([('X', 'Y'), ('Y', 'Z')])) # X -> Y -> Z
>>> gcm.auto.assign_causal_mechanisms(causal_model, training_data)
>>> gcm.fit(causal_model, training_data)

最后,让我们对 X 执行干预。在这里,我们明确地执行干预 \(do(X:=1)\)

>>> samples = gcm.interventional_samples(causal_model,
>>>                                      {'X': lambda x: 1},
>>>                                      num_samples_to_draw=1000)
>>> samples.head()
       X         Y          Z
    0  1  3.481467  12.475105
    1  1  1.282945   3.279435
    2  1  2.508717   7.907412
    3  1  2.077061   5.506252
    4  1  1.400568   6.097633

正如我们所见,X 现在固定在一个常数值 1。这被称为原子干预 (atomic intervention)。我们还可以执行位移干预 (shift intervention),即将随机变量 X 位移某个值

>>> samples = gcm.interventional_samples(causal_model,
>>>                                      {'X': lambda x: x + 0.5},
>>>                                      num_samples_to_draw=1000)
>>> samples.head()
              X         Y          Z
    0 -0.542813  0.031771   1.195391
    1  1.615089  2.156833   6.704683
    2  1.340949  1.910316   5.882468
    3  1.837919  4.360685  12.565738
    4  3.791410  8.361918  25.477725