Skip to main content

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 EntropyExample {

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

Network 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");

Variable visitToAsia = network.getVariables().get("Visit to Asia", true);
Variable hasLungCancer = network.getVariables().get("Has Lung Cancer", true);
Variable tuberculosisOrCancer = network.getVariables().get("Tuberculosis or Cancer", true);
Variable smoker = network.getVariables().get("Smoker", true);
Variable hasTuberculosis = network.getVariables().get("Has Tuberculosis", true);
Variable dyspnea = network.getVariables().get("Dyspnea", true);
Variable xRayResult = network.getVariables().get("XRay Result", true);
Variable hasBronchitis = network.getVariables().get("Has Bronchitis", true);


InferenceFactory factory = new RelevanceTreeInferenceFactory();
Inference inf = factory.createInferenceEngine(network);
QueryOptions qopt = factory.createQueryOptions();
QueryOutput qout = factory.createQueryOutput();

// Set evidence, if any
inf.getEvidence().setState(smoker.getStates().get("True", true));


Table queryJoint = new Table(hasBronchitis, dyspnea);

inf.getQueryDistributions().add(queryJoint);

inf.query(qopt, qout);

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

ArrayList<VariableContext> conditionOn = new ArrayList<VariableContext>();
conditionOn.add(queryJoint.getSortedVariables().get(queryJoint.getSortedVariables().indexOf(hasBronchitis)));
double 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

}
}