Backdoor Criterion
Since version 10
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BackdoorCriterionExample.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.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BayesServer.HelpSamples
{
public static class BackdoorCriterionExample
{
public static void Main()
{
// This example uses Causal AI (Causal Inference)
// to calculate potential adjustment sets
// required to calculate P(Y | Do(X)) in the presence of
// unobserved confounders, using the
// Backdoor Criterion algorithm.
// ** IMPORTANT **
// If you want to actually perform the adjustment (inference)
// the BackdoorInference class can automatically determine
// an appropriate adjustment set, making the following code
// unecessary.
// This code is useful when you want to enumerate the possible
// adjustment sets.
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 criterion = new BackdoorCriterion(network);
// Method 1
{
var inf = new BackdoorInference(network);
var query = new Table(y);
inf.QueryDistributions.Add(query);
// Set one or more interventions
inf.Evidence.SetState(x.States[0], null, InterventionType.Do);
// you could also set non-intervention evidence here
var options = new BackdoorCriterionOptions();
var output = (BackdoorCriterionOutput)criterion.Identify(inf.Evidence, query, options);
Console.WriteLine("Method 1");
PrintAdjustmentSets(output.Sets);
}
Console.WriteLine();
// Method 2
{
var treatments = new List<CausalNode>();
// add one or more treatments
treatments.Add(new CausalNode(x.Node));
var outcomes = new List<CausalNode>();
// add one or more outcomes
outcomes.Add(new CausalNode(y.Node));
// you can add nodes to nonTreatmentEvidence if required, for
// nodes that have normal (non-intervention) evidence set
var nonTreatmentEvidence = new List<CausalNode>();
var options = new BackdoorCriterionOptions();
var output = (BackdoorCriterionOutput)criterion.Identify(treatments, outcomes, nonTreatmentEvidence, options);
Console.WriteLine("Method 2");
PrintAdjustmentSets(output.Sets);
}
}
private static void PrintAdjustmentSets(IList<AdjustmentSet> adjustmentSets)
{
Console.WriteLine($"Adjustment sets found = {adjustmentSets.Count}");
foreach (var adjustmentSet in adjustmentSets)
{
Console.WriteLine($" {{{string.Join(',', adjustmentSet.Nodes.Select(n => n.Node.Name))}}}");
}
}
}
}