import pandas as pd
import jpype
import jpype.imports
from jpype.types import *
classpath = "lib/bayesserver-10.8.jar"
jpype.startJVM(classpath=[classpath])
import data_frame_utils as dfu
from com.bayesserver import *
from com.bayesserver.inference import *
from com.bayesserver.learning.parameters import *
from com.bayesserver.data import *
from com.bayesserver.data.sampling import *
from jpype import java, JImplements, JOverride
def value_as_text(variable, evidence):
if evidence.getEvidenceType(variable) == EvidenceType.NONE:
return "(null)"
if variable.getValueType() == VariableValueType.CONTINUOUS:
return f"{evidence.get(variable)}"
else:
return variable.getStates().get(evidence.getState(variable)).getName()
def create_network():
network = Network()
node_gender = Node("Gender", ["Female", "Male"])
network.getNodes().add(node_gender)
node_height = Node("Height", VariableValueType.CONTINUOUS)
network.getNodes().add(node_height)
node_hair_length = Node("Hair Length", ["Short", "Medium", "Long"])
network.getNodes().add(node_hair_length)
network.getLinks().add(Link(node_gender, node_height))
network.getLinks().add(Link(node_gender, node_hair_length))
table_gender = node_gender.newDistribution().getTable()
table_gender.copyFrom([0.51, 0.49])
node_gender.setDistribution(table_gender)
table_hair_length = node_hair_length.newDistribution().getTable()
iterator_hair_length = TableIterator(table_hair_length, [node_gender, node_hair_length])
iterator_hair_length.copyFrom([0.1, 0.4, 0.5, 0.8, 0.15, 0.05])
node_hair_length.setDistribution(table_hair_length)
gaussian_height = node_height.newDistribution()
gaussian_height.setMean(0, 0, 162.56)
gaussian_height.setVariance(0, 0, 50.58)
gaussian_height.setMean(1, 0, 176.022)
gaussian_height.setVariance(1, 0, 50.58)
node_height.setDistribution(gaussian_height)
network.validate(ValidationOptions())
return network
network = create_network()
gender = network.getVariables().get("Gender")
height = network.getVariables().get("Height")
hairLength = network.getVariables().get("Hair Length")
fixedEvidence = DefaultEvidence(network)
fixedEvidence.setState(gender.getStates().get("Female", True))
sampler = DataSampler(network, fixedEvidence)
options = DataSamplingOptions()
random = RandomDefault(0)
sample = DefaultEvidence(network)
print("Gender\tHeight\tHair Length")
print("------------------------------")
for i in range(100):
try:
sampler.takeSample(sample, random, options)
print(f"{value_as_text(gender, sample)}\t{value_as_text(height, sample)}\t{value_as_text(hairLength, sample)}")
except InconsistentEvidenceException:
print("Inconsistent evidence exception was raised.")