package com.bayesserver.examples;
import com.bayesserver.*;
import com.bayesserver.data.*;
import com.bayesserver.inference.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
var network = new Network();
network.load("Waste.bayes");
var variables = network.getVariables();
var burningRegimen = variables.get("Burning Regimen", true);
var wasteType = variables.get("Waste type", true);
var filterState = variables.get("Filter state", true);
var burningRegimenStable = burningRegimen.getStates().get("Stable", true);
var filterEfficiency = variables.get("Filter efficiency", true);
var dustEmission = variables.get("Dust emission", true);
var metalsInWaste = variables.get("Metals in waste", true);
var co2Concentration = variables.get("CO2 concentration", true);
var lightPenetrability = variables.get("Light penetrability", true);
var metalsEmission = variables.get("Metals emission", true);
var factory = new RelevanceTreeInferenceFactory();
var inference = factory.createInferenceEngine(network);
var queryOptions = factory.createQueryOptions();
var queryOutput = factory.createQueryOutput();
var queries = inference.getQueryDistributions();
var queryBurningRegimen = new Table(burningRegimen);
queries.add(queryBurningRegimen);
var queryLightPenetrability = new CLGaussian(lightPenetrability);
queries.add(queryLightPenetrability);
var queryJoint = new CLGaussian(new Variable[]{filterEfficiency, burningRegimen});
queries.add(queryJoint);
queryOptions.setLogLikelihood(true);
var evidenceReaderCommand = createEvidenceReaderCommand(network);
var readOptions = new DefaultReadOptions();
try(var evidenceReader = evidenceReaderCommand.executeReader())
{
while (evidenceReader.read(inference.getEvidence(), readOptions)) {
inference.query(queryOptions, queryOutput);
var logLikelihood = queryOutput.getLogLikelihood();
var probStable = queryBurningRegimen.get(burningRegimenStable);
var meanLightPenetrability = queryLightPenetrability.getMean(lightPenetrability);
var varianceLightPenetrability = queryLightPenetrability.getVariance(lightPenetrability);
var jointElement = queryJoint.getMean(filterEfficiency, burningRegimenStable);
System.out.printf(
"{%s}\t{%s}\t{%s}\t{%s}\t{%s}%n",
logLikelihood,
probStable,
meanLightPenetrability,
varianceLightPenetrability,
jointElement);
}
}
}
private static EvidenceReaderCommand createEvidenceReaderCommand(Network network) {
var dataReaderCommand = createDataReaderCommand();
var wasteType = network.getVariables().get("Waste type", true);
var co2Concentration = network.getVariables().get("CO2 concentration", true);
return new DefaultEvidenceReaderCommand(
dataReaderCommand,
Arrays.asList(
new VariableReference(wasteType, ColumnValueType.NAME, wasteType.getName()),
new VariableReference(co2Concentration, ColumnValueType.VALUE, co2Concentration.getName())
),
new ReaderOptions()
);
}
private static DataReaderCommand createDataReaderCommand() {
var table = new DataTable();
var wasteType = table.getColumns().add("Waste type", String.class);
var co2Concentration = table.getColumns().add("CO2 concentration", Double.class);
var rows = table.getRows();
rows.add("Industrial", -1.7);
rows.add("Household", -2.0);
rows.add(null, -1.7);
rows.add("Industrial", null);
rows.add("Industrial", 0.5);
return new DataTableDataReaderCommand(table);
}
}