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();
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();
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));
}
}