Code: Select all
def simulate_dataset(N, seed):
"""
N: Sample size
"""
np.random.seed(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
# -------------------------
# Root variables
# -------------------------
# W ~ Bernoulli(0.5)
W = np.random.binomial(1, 0.5, N)
# B ~ Bernoulli(0.4)
B = np.random.binomial(1, 0.4, N)
# -------------------------
# A depends on B
# -------------------------
# P(A=1 | B)
# P(A=1∣B=1)=0.8
# P(A=1∣B=0)=0.2
p_A = np.where(B == 1, 0.8, 0.2)
A = np.random.binomial(1, p_A)
# -------------------------
# X depends on W
# -------------------------
# P(X=1 | W)
p_X = np.where(W == 1, 0.75, 0.25)
X = np.random.binomial(1, p_X)
# -------------------------
# T depends on X, W, A, B
# -------------------------
def sigmoid(z):
return 1 / (1 + np.exp(-z))
logit_T = (
-1.0
+ 1.5 * X
+ 1.0 * W
+ 0.8 * A
+ 1.2 * B
)
p_T = sigmoid(logit_T)
T = np.random.binomial(1, p_T)
# -------------------------
# Create dataframe
# -------------------------
df = pd.DataFrame({
"W": W,
"B": B,
"A": A,
"X": X,
"T": T
})
return dfCould you please show me how to use SMILE to do effect estimation. I want to calculate the following P(T=1|do(X=1),A=1) - P(T=1|do(X=0),A=1). P(T=1|do(X=1),A=1) means the observe A=1 and intervene variable X with value 1, check the probability of T=1.
I know that to intervene X, I can use the following code. Please note that I converted value of X into X___0 and X___1. Other variables are similar.
Code: Select all
SMILE_network.set_controlled_value("X", "X___0")
SMILE_network.update_beliefs()
#print_all_posteriors(SMILE_network)
pre_intervention_val = get_posterior(SMILE_network, "T", "T___1")
print(f"SMILE pre_intervention_val: P(T|T___1)={pre_intervention_val}")
SMILE_network.clear_controlled_value("X")
SMILE_network.set_controlled_value("X", "X___1")
SMILE_network.update_beliefs()
#print_all_posteriors(SMILE_network)
post_intervention_val = get_posterior(SMILE_network, "T", "T___1")
print(f"SMILE post_intervention_val: P(T|T___1)={post_intervention_val}")
eff_estimation = post_intervention_val - pre_intervention_val
print(f"=================> SMILE effect estimation: {eff_estimation}")Could you please show me how to do effect estimation by intervening X with the condition of observe 'A' equals 'A___1', so that I can calculate P(T=1|do(X=1),A=1) - P(T=1|do(X=0),A=1)? Thanks.