import jpype
import jpype.imports
from jpype.types import *
from math import sqrt
classpath = "lib/bayesserver-10.8.jar"
jpype.startJVM(classpath=[classpath])
from com.bayesserver import *
from com.bayesserver.inference import *
network = Network()
network.load("Waste.bayes")
variables = network.getVariables()
print("Network has {} variables".format(variables.size()))
wasteType = variables.get("Waste type", True)
co2Concentration = variables.get("CO2 concentration", True)
filterEfficiency = variables.get("Filter efficiency", True)
metalsInWaste = variables.get("Metals in waste", True)
metalsEmission = variables.get("Metals emission", True)
lightPenetrability = variables.get("Light penetrability", True)
factory = RelevanceTreeInferenceFactory()
inference = factory.createInferenceEngine(network)
queryOptions = factory.createQueryOptions()
queryOptions.setLogLikelihood(True)
queryOutput = factory.createQueryOutput()
evidence = inference.getEvidence()
industrial = wasteType.getStates().get("Industrial", True)
evidence.setState(industrial)
evidence.set(co2Concentration, -1.0)
queryDistributions = inference.getQueryDistributions()
filterState = variables.get("Filter state", True)
queryFilterState = Table(filterState)
queryDistributions.add(QueryDistribution(queryFilterState))
queryFilterEfficiency = CLGaussian(filterEfficiency)
queryDistributions.add(QueryDistribution(queryFilterEfficiency))
queryJoint = CLGaussian([lightPenetrability, metalsEmission])
queryDistributions.add(QueryDistribution(queryJoint))
inference.query(queryOptions, queryOutput)
print("Log-likelihood: {}".format(queryOutput.getLogLikelihood()))
filterEfficiencyMean = queryFilterEfficiency.getMean(filterEfficiency)
filterEfficiencyVariance = queryFilterEfficiency.getVariance(filterEfficiency)
print("{}, {}".format(filterEfficiencyMean, sqrt(filterEfficiencyVariance)))
for state in filterState.getStates():
print("Filter state: {} {}".format(state.getName(), queryFilterState.get([state])))
print("Joint covariance = {}".format(queryJoint.getCovariance(lightPenetrability, metalsEmission)))