Skip to main content

Batch query Java

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

// TODO download the network from the Bayes Server User Interface (or Bayes Server Online)
// and adjust the following path
network.load("Waste.bayes");

var variables = network.getVariables();
// discrete
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);

// continuous
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);

// Create an inference engine to calculate the queries.
// The same inference engine can be re-used for multiple rows of data.
// Although not shown here, you can also create multiple inference engines,
// to take advantage of multiple threads.

var factory = new RelevanceTreeInferenceFactory();
var inference = factory.createInferenceEngine(network);
var queryOptions = factory.createQueryOptions();
var queryOutput = factory.createQueryOutput();

// add some marginal queries
var queries = inference.getQueryDistributions();
var queryBurningRegimen = new Table(burningRegimen);
queries.add(queryBurningRegimen);
var queryLightPenetrability = new CLGaussian(lightPenetrability);
queries.add(queryLightPenetrability);
// You can also add joint queries over > 1 variable
var queryJoint = new CLGaussian(new Variable[]{filterEfficiency, burningRegimen});
queries.add(queryJoint);

// we will also query the log-likelihood
queryOptions.setLogLikelihood(true);


var evidenceReaderCommand = createEvidenceReaderCommand(network);

var readOptions = new DefaultReadOptions();

// Iterate through the data, perform queries for each row.
// In this example, the output is simply written to the console, but the output
// could be streamed into a database or another data store or endpoint.

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

/**
Create an evidence reader command, than maps data to variables.

This can connect to a database, a DataTable or you can create a custom reader.
**/
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()
);

}

/**
Manually create some data here to keep the example self contained.
This could also be queried from a database,
or another data source.
**/
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);

// you can use null for missing data

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

// Expected output ...
//-1.56469966974624 0.934046351645668 1.57952317582283 0.419529880001972 -3.725
//-0.247575914981347 0.98111967122024 1.52305983561012 0.351488920422928 -3.065
//-0.311935701250374 0.934046351645668 1.52238029582283 0.380432496848107 -3.25357124
//-1.25276396849587 0.85 1.5375 0.43747475 -3.725
//-7.21683608441226 1.11889682547702E-11 1.11250000000559 0.420474750002601 -3.725
}