Skip to main content

Joint and conditional Entropy in C#

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


using BayesServer.Inference;
using BayesServer.Inference.RelevanceTree;
using BayesServer.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BayesServer.HelpSamples
{
public static class EntropyExample
{
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(@"Asia.bayes");

var visitToAsia = network.Variables["Visit to Asia", true];
var hasLungCancer = network.Variables["Has Lung Cancer", true];
var tuberculosisOrCancer = network.Variables["Tuberculosis or Cancer", true];
var smoker = network.Variables["Smoker", true];
var hasTuberculosis = network.Variables["Has Tuberculosis", true];
var dyspnea = network.Variables["Dyspnea", true];
var xRayResult = network.Variables["XRay Result", true];
var hasBronchitis = network.Variables["Has Bronchitis", true];


var factory = new RelevanceTreeInferenceFactory();
var inf = factory.CreateInferenceEngine(network);
var qopt = factory.CreateQueryOptions();
var qout = factory.CreateQueryOutput();

// Set evidence, if any
inf.Evidence.SetState(smoker.States["True", true]);


var queryJoint = new Table(hasBronchitis, dyspnea);

inf.QueryDistributions.Add(queryJoint);

inf.Query(qopt, qout);

var jointEntropyGivenSmokerTrue = Entropy.Calculate(queryJoint, LogarithmBase.Natural);

var conditionOn = new List<VariableContext>();
conditionOn.Add(queryJoint.SortedVariables[queryJoint.SortedVariables.IndexOf(hasBronchitis)]);
var conditionalEntropyGivenSmokerTrue = Entropy.Calculate(queryJoint, conditionOn, LogarithmBase.Natural);

Console.WriteLine($"H(Has Bronchitis, Dyspnea | Smoker = True) = {jointEntropyGivenSmokerTrue} NATS");
Console.WriteLine($"H(Has Bronchitis | Dyspnea, Smoker = True) = {conditionalEntropyGivenSmokerTrue} NATS");

// Expected output

// H(Has Bronchitis, Dyspnea | Smoker = True) = 1.14347590829214 NATS
// H(Has Bronchitis | Dyspnea, Smoker = True) = 0.470464241282886 NATS


}
}
}