Skip to main content

Backdoor Adjustment

Since version 10

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BackdoorAdjustmentExample.cs" company="Bayes Server">
// Copyright (C) Bayes Server. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using BayesServer.Causal;
using BayesServer.Causal.Identification;
using BayesServer.Causal.Inference;
using BayesServer.Inference;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BayesServer.HelpSamples
public static class BackdoorAdjustmentExample
public static void Main()
// This example uses Causal AI (Causal Inference)
// to calculate P(Y | Do(X)) in the presence of
// unobserved confounders, using the
// Backdoor Adjustment algorithm.

var network = new Network();

// TODO download the network from the Bayes Server User Interface (or Bayes Server Online)
// and adjust the following path
network.Load(@"Backdoor Collider.bayes");

var x = network.Variables["X", true];
var y = network.Variables["Y", true];
var z = network.Variables["Z", true];
var a = network.Variables["A", true];

var inf = new BackdoorInference(network);
var qopt = new BackdoorQueryOptions();
var qout = new BackdoorQueryOutput();

var query = new Table(y);

// Set one or more interventions

inf.Evidence.SetState(x.States[0], null, InterventionType.Do);

// you could also set non-intervention evidence here

// Method 1
// let the algorithm determine an appropriate adjustment set

Debug.Assert(qopt.AdjustmentSetOverride == null);

inf.Query(qopt, qout);

Console.WriteLine("Method 1");


// Method 2
// supply your own adjustment set (e.g. using the BackdoorCriterion class)

qopt.AdjustmentSetOverride = new AdjustmentSet(
new AdjustmentSetNode(z.Node),
new AdjustmentSetNode(a.Node));

inf.Query(qopt, qout);

Console.WriteLine("Method 2");