I was learning causal inference and discovery these days and have suffered from this question for a long time.

From my understanding of the literature, It seems causal inference is quite different from traditional machine learning. For traditional machine learning, once the model is trained, and given a set of X, the model directly predicts the value of Y.

However, for the causal inference, the model answers if X1 changes from 1 to 2, for example, it will return the causal effects on Y.

So how can I answer the prediction question using causal inference?

Here are some simulated data using this graph:

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')

enter image description here

# 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

enter image description here

The problem is:

How to make predictions on Y when X = 10, Z = 20 pretending you only know the causal graph but not the detailed causal function?

I have tried using Microsoft Causia to identify the causal graph. And also the causal inference, but they are not traditional prediction problems.

  • 2
    $\begingroup$ Cross post It's not encouraged to post the same question on both, sorry if my comment over on StackOverflow wasn't clear in that regard. $\endgroup$
    – Scriddie
    Commented Dec 29, 2023 at 12:03

1 Answer 1


The DAG is a nonparametric device which tells you which variables influence other variables in a causal manner. Whether the effect is linear or nonlinear is not specified by the DAG. The DAG is typically formulated through expert knowledge of the data. If you have reason to suspect a nonlinear relationship, then you can fit a nonlinear model. If your actual data were as shown in the OP with an obvious sinusoidal shape, then it would be a good idea to fit a model using sinusoidal parameters. The plot in the OP doesn't make a lot of sense to me since it combines X, Y and Z values all on the y-axis. A more revealing plot would be a 3 dimensional one.

Something like this:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Scatter plot for 3D data
ax.scatter(df['X'], df['Z'], df['Y'], c='r', marker='o')

ax.set_xlabel('X Label')
ax.set_ylabel('Z Label')
ax.set_zlabel('Y Label')


pseudo-3D plot of points on a sinusoidal curve

To make the predictions requested (x=10, y=20) we can proceed like this:

from sklearn.linear_model import LinearRegression
# Adding sine and cosine transformations of the predictors to the DataFrame
df['sin_X'] = np.sin(df['X'])
df['cos_Z'] = np.cos(df['Z'])

# Fit a linear regression model using the transformed predictors
model = LinearRegression()
model.fit(df[['sin_X', 'cos_Z']], df['Y'])
prediction = model.predict([[np.sin(10), np.cos(20)]])

which produces -0.1257 as compared to -0.149808 in the actual data.

  • $\begingroup$ Well, it is a nice choice to observe and transfer it to linear regression. However, what if you cannot observe an apparent structure form? It is common when we have a lot of variables, for example, 10 variables. Then we are not able to plot a 10 dimension plots and observed the structure form. $\endgroup$
    – Wenyao Leo
    Commented Jan 1 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.