Log-Likelihood analysis in C#
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogLikelihoodAnalysisExample.cs" company="Bayes Server">
// Copyright (C) Bayes Server. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using BayesServer.Analysis;
using BayesServer.Inference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BayesServer.HelpSamples
{
public static class LogLikelihoodAnalysisExample
{
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];
// You can either create some new evidence to analyze, or you can use
// inference.Evidence
// if you have evidence on an inference engine you want to analyze
var evidence = new Evidence(network);
// set some evidence
evidence.SetState(burningRegimen.States["Unstable", true]);
evidence.SetState(filterState.States["Defect", true]);
evidence.Set(filterEfficiency, -0.45);
evidence.Set(lightPenetrability, 2.0);
evidence.Set(metalsEmission, 6.3);
var evidenceToAnalyse = new Variable[evidence.Count];
evidence.GetVariables(evidenceToAnalyse);
var options = new LogLikelihoodAnalysisOptions();
options.SubsetMethod = LogLikelihoodAnalysisSubsetMethod.Exclude;
options.MaxEvidenceSubsetSize = 1;
var output = LogLikelihoodAnalysis.Calculate(
network,
evidence,
evidenceToAnalyse,
options);
Console.WriteLine($"None={output.Baseline.LogLikelihoodNone}\tAll={output.Baseline.LogLikelihoodAll}");
Console.WriteLine();
Console.WriteLine(string.Join('\t', evidenceToAnalyse.Select(v => v.Name)));
// In this example, we have choosen to order by descending log-likelihood, as we are using
// LogLikelihoodAnalysisSubsetMethod.Exclude and wish to understand which evidence
// causes the biggest decrease (e.g. for anomaly detection)
foreach (var item in output.Items.OrderByDescending(i => i.LogLikelihood))
{
var flags = string.Join('\t', item.EvidenceFlags.Select(f => f ? "T" : "F"));
Console.WriteLine($"{item.LogLikelihood}\t{flags}");
}
// Expected output
// None = 0 All = -25.7131596279843
// Burning Regimen Filter state Filter efficiency Light penetrability Metals emission
// -14.1361346392178 T T T F T
// - 17.0198859514185 T T F T T
// - 20.9544632365106 F T T T T
// - 23.6826206567332 T T T T F
// - 25.7131596279843 T F T T T
}
}
}