Joint and conditional Entropy in Java

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

package com.bayesserver.examples;

import com.bayesserver.*;
import com.bayesserver.inference.*;
import com.bayesserver.statistics.*;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws IOException, XMLStreamException, InconsistentEvidenceException {

        var network = new Network();

        // TODO change the path to the Asia network installed with the Bayes Server User Interface
        network.load("C:\\ProgramData\\Bayes Server 9.4\\Sample Networks\\Asia.bayes");

        var visitToAsia = network.getVariables().get("Visit to Asia", true);
        var hasLungCancer = network.getVariables().get("Has Lung Cancer", true);
        var tuberculosisOrCancer = network.getVariables().get("Tuberculosis or Cancer", true);
        var smoker = network.getVariables().get("Smoker", true);
        var hasTuberculosis = network.getVariables().get("Has Tuberculosis", true);
        var dyspnea = network.getVariables().get("Dyspnea", true);
        var xRayResult = network.getVariables().get("XRay Result", true);
        var hasBronchitis = network.getVariables().get("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.getEvidence().setState(smoker.getStates().get("True", true));


        var queryJoint = new Table(hasBronchitis, dyspnea);

        inf.getQueryDistributions().add(queryJoint);

        inf.query(qopt, qout);

        var jointEntropyGivenSmokerTrue = Entropy.calculate(queryJoint, LogarithmBase.NATURAL);

        var conditionOn = new ArrayList<VariableContext>();
        conditionOn.add(queryJoint.getSortedVariables().get(queryJoint.getSortedVariables().indexOf(hasBronchitis)));
        var conditionalEntropyGivenSmokerTrue = Entropy.calculate(queryJoint, conditionOn, LogarithmBase.NATURAL);

        System.out.println(String.format("H(Has Bronchitis, Dyspnea | Smoker = True) = %s NATS", jointEntropyGivenSmokerTrue));
        System.out.println(String.format("H(Has Bronchitis | Dyspnea, Smoker = True) = %s NATS", conditionalEntropyGivenSmokerTrue));

        //  Expected output

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

    }
}