using BayesServer.Inference;
using BayesServer.Inference.RelevanceTree;
using BayesServer.Optimization;
using BayesServer.Optimization.Genetic;
using BayesServer.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BayesServer.HelpSamples
{
public static class CausalInferenceExample
{
public static void Main()
{
var network = new Network();
network.Load(@"Causal Inference Simple.bayes");
var drug = network.Variables["Drug", true];
var drugTrue = drug.States["True", true];
var drugFalse = drug.States["False", true];
var recovered = network.Variables["Recovered", true];
var recoveredTrue = recovered.States["True", true];
var factory = new RelevanceTreeInferenceFactory();
var inference = factory.CreateInferenceEngine(network);
var queryOptions = factory.CreateQueryOptions();
var queryOutput = factory.CreateQueryOutput();
var queryRecovered = new Table(recovered);
inference.QueryDistributions.Add(queryRecovered);
{
Console.WriteLine("Non-causal version (incorrect)...");
inference.Evidence.SetState(drugTrue);
inference.Query(queryOptions, queryOutput);
var pRecoveredGivenDrugTrue = queryRecovered[recoveredTrue];
inference.Evidence.SetState(drugFalse);
inference.Query(queryOptions, queryOutput);
var pRecoveredGivenDrugFalse = queryRecovered[recoveredTrue];
var effectivenessNonCausal = pRecoveredGivenDrugTrue - pRecoveredGivenDrugFalse;
Console.WriteLine($"Effectiveness of drug (non-causal) = {effectivenessNonCausal.ToString("P2")}");
}
Console.WriteLine();
{
Console.WriteLine("Causal version...");
inference.Evidence.SetState(drugTrue, null, InterventionType.Do);
inference.Query(queryOptions, queryOutput);
var pRecoveredGivenDoDrugTrue = queryRecovered[recoveredTrue];
inference.Evidence.SetState(drugFalse, null, InterventionType.Do);
inference.Query(queryOptions, queryOutput);
var pRecoveredGivenDoDrugFalse = queryRecovered[recoveredTrue];
var effectivenessCausal = pRecoveredGivenDoDrugTrue - pRecoveredGivenDoDrugFalse;
Console.WriteLine($"Effectiveness of drug (causal) = {effectivenessCausal.ToString("P2")}");
}
}
}
}