这几天在学习因果推理和发现,被这个问题困扰了好久。
从我对文献的理解来看,因果推理似乎与传统的机器学习有很大不同。对于传统的机器学习,一旦模型训练完毕,给定一组 X,模型直接预测 Y 的值。
但是,对于因果推断,模型会回答如果X1从1变为2,例如,它将返回对Y的因果影响。
那么我如何使用因果推理来回答预测问题?
以下是使用该图的一些模拟数据:
import networkx as nx
import matplotlib.pyplot as plt
# Create a directed graph
G = nx.DiGraph()
# Add nodes X, Y, and Z
G.add_nodes_from(['X', 'Y', 'Z'])
# Add edges representing causal relationships
G.add_edge('X', 'Y')
G.add_edge('Z', 'Y')
# Draw the graph
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, with_labels=True, node_color='lightblue', node_size=500, font_size=12, edge_color='gray')
plt.title('Causal Graph')
plt.show()
# Create the nonlinear relationships:
import numpy as np
import pandas as pd
# Generate X values
X = np.linspace(0, 10, 100)
# Generate Z values
Z = np.linspace(10, 20, len(X))
# Generate Y values using a non-linear relationship with X
Y = np.sin(X) + np.cos(Z) + np.random.normal(0, 0.1, len(X))
# Combine X, Z, Y into one pandas frame
# Combine X, Z, Y into one pandas DataFrame
df = pd.DataFrame({'X': X, 'Z': Z, 'Y': Y})
# Print the DataFrame
print(df)
XZY 0 0.00000 10.00000 -0.781419 1 0.10101 10.10101 -0.691126 2 0.20202 10.20202 -0.603684 3 0.30303 10.30303 -0.418206 4 0.40404 10.40404 -0.087543 ……………… 95 9.59596 19.59596 0.748085 96 9.69697 19.69697 0.455545 97 9.79798 19.79798 0.365235 98 9.89899 19.89899 0.023566 99 10.00000 20.00000 -0.193462
[100 行 x 3 列]
# plot the data
import matplotlib.pyplot as plt
# Plot X, Y, and Z
plt.plot(df['X'], label='X')
plt.plot(df['Y'], label='Y')
plt.plot(df['Z'], label='Z')
# Add labels and legend
plt.xlabel('Index')
plt.ylabel('Value')
plt.legend()
# Show the plot
plt.show()
问题是:
假设你只知道因果图而不知道详细的因果函数,如何在X = 10,Z = 20时对Y进行预测?
我尝试使用 microsoft causia 来识别因果图。还有因果推理,但它们不是预测问题。
[评论:您通常会在 CrossValidated 上获得有关因果关系问题的更多答案]
你的问题指出了关于因果结构的一个重要且经常被误解的观点。因果结构包含哪个变量影响另一个变量,但不包含它们“如何”影响。为此,您不仅需要因果图,还需要完整的结构因果模型 (SCM)。 SCM 包含每个变量根据其他变量和外生项的函数定义。 因果图可以告诉你,某个变量 X 可能不是其他变量 Y 的原因,在这种情况下,无论 X 的值如何,你都知道预测 Y 不会发生变化。但是,如果 X 是 Y 的原因,如果不知道函数,就无法知道如何正确预测某些 X 的 Y。
总而言之,因果结构并不能解决函数估计的问题,也不能解决在给定未见值的情况下进行预测时需要进行外推的问题。