How to estimate conditional interventional effect using SMILE on a categorical graph?

The engine.
Post Reply
BayesFusionUser123
Posts: 24
Joined: Tue Jun 10, 2025 3:51 pm

How to estimate conditional interventional effect using SMILE on a categorical graph?

Post by BayesFusionUser123 »

I have a causal graph with the following edges : W -> X, X -> T, W -> T, A -> T, B -> T, B -> A. All of the node, W, X, A, B, T are binary categorical variable with value 0 and 1. I have simulated the dataset in the following way:

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 df

Could 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.
shooltz[BayesFusion]
Site Admin
Posts: 1497
Joined: Mon Nov 26, 2007 5:51 pm

Re: How to estimate conditional interventional effect using SMILE on a categorical graph?

Post by shooltz[BayesFusion] »

You need to set A to A1 with set_evidence once at the start of your program.

Code: Select all

SMILE_network.set_evidence("A", "A___1")
Also, you don't need to clear_controlled_value before using set_controlled_value with new outcome.
Post Reply