Skip to main content

Impact analysis in C#

using BayesServer.Analysis;
using BayesServer.Inference;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;

namespace BayesServer.HelpSamples
{
public static class ImpactAnalysisExample
{
public static void Main()
{
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(@"Waste.bayes");

// discrete
var burningRegimen = network.Variables["Burning Regimen", true];
var wasteType = network.Variables["Waste type", true];
var filterState = network.Variables["Filter state", true];

// continuous
var filterEfficiency = network.Variables["Filter efficiency", true];
var dustEmission = network.Variables["Dust emission", true];
var metalsInWaste = network.Variables["Metals in waste", true];
var co2Concentration = network.Variables["CO2 concentration", true];
var lightPenetrability = network.Variables["Light penetrability", true];
var metalsEmission = network.Variables["Metals emission", true];

var filterStateIntact = filterState.States["Intact", true];
var wasteTypeIndustrial = wasteType.States["Industrial", true];

var evidence = new Evidence(network);
evidence.Set(lightPenetrability, 0.2);
evidence.Set(co2Concentration, -1.0);
evidence.SetState(wasteTypeIndustrial);

var options = new ImpactOptions();
options.SubsetMethod = ImpactSubsetMethod.Exclude;
options.MaxEvidenceSubsetSize = 1;

var evidenceToAnalyse = new Variable[] { lightPenetrability, co2Concentration, wasteType };

var output = Impact.Calculate(
network,
filterState,
filterStateIntact,
evidence,
evidenceToAnalyse,
options
);

Console.WriteLine($"P(Intact) (all evdience) = {output.Hypothesis.StateProbabilityAll}");
Console.WriteLine($"P(Intact) (no evdience) = {output.Hypothesis.StateProbabilityNone}");

Console.WriteLine();

foreach (var item in output.Items)
{
Console.WriteLine(string.Join(',', item.EvidenceFlags));
Console.WriteLine("\tP(Intact)\t" + item.StateProbability);
}

// Expected output...

// P(Intact)(all evidence) = 0.8806625135255
// P(Intact)(no evidence) = 0.95

// False,True,True
// P(Intact) 0.95
// True,False,True
// P(Intact) 0.447566691128873
// True,True,False
// P(Intact) 0.858460027839224


}
}
}