Sensitivity analysis in Python

# __author__ = 'Bayes Server'
# __version__= '0.1'

from jpype import *  # pip install jpype1   (for Java 7, pip install jpype1 0.6.3, and remove convertStrings=False)

# TODO change path to Bayes Server jar file
classpath = 'C:\\Program Files\\Bayes Server\\Bayes Server 8.19\\API\\Java\\bayesserver-8.19.jar'

startJVM(getDefaultJVMPath(), '-Djava.class.path=%s' % classpath, convertStrings=False)

bayes = JPackage('com.bayesserver')
bayes_data = bayes.data
bayes_inference = bayes.inference
bayes_analysis = bayes.analysis

# // TODO change path to Asia network
network_path = 'C:\\ProgramData\\Bayes Server 8.19\\Sample Networks\\Asia.bayes'

network = bayes.Network()
network.load(network_path)

variables = network.getVariables()

visit_to_asia = variables.get('Visit to Asia', True)
has_lung_cancer = variables.get('Has Lung Cancer', True)
tuberculosis_or_cancer = variables.get('Tuberculosis or Cancer', True)
smoker = variables.get('Smoker', True)
has_tuberculosis = variables.get('Has Tuberculosis', True)
dyspnea = variables.get('Dyspnea', True)
xray_result = variables.get('XRay Result', True)
has_bronchitis = variables.get('Has Bronchitis', True)

xRayResultAbnormal = xray_result.getStates().get('Abnormal', True)
smokerFalse = smoker.getStates().get('False', True)
hasLungCancerFalse = has_lung_cancer.getStates().get('False', True)

evidence = bayes_inference.DefaultEvidence(network)

# TODO set any evidence here if you need to...

sensitivity = bayes_analysis.SensitivityToParameters(network, bayes_inference.RelevanceTreeInferenceFactory())

parameter = bayes_analysis.ParameterReference(has_lung_cancer.getNode(), [smokerFalse, hasLungCancerFalse])

oneWay = sensitivity.oneWay(
    evidence,
    xRayResultAbnormal,
    parameter
)

print('Parameter value = {}'.format(oneWay.getParameterValue()))
print('Sensitivity value = {}'.format(oneWay.getSensitivityValue()))
print('P(Abnormal | e) = {}'.format(oneWay.getProbabilityHypothesisGivenEvidence()))
print('Alpha = {}'.format(oneWay.getAlpha()))
print('Beta = {}'.format(oneWay.getBeta()))
print('Delta = {}'.format(oneWay.getDelta()))
print('Gamma = {}'.format(oneWay.getGamma()))

print('Eval(0.2) = {}'.format(oneWay.evaluate(0.2)))
print('Eval\'(0.2) = {}'.format(oneWay.evaluateDeriv(0.2)))


#   Expected output...

#   Parameter value = 0.99
#   Sensitivity value = -0.460164
#   P(Abnormal | e) = 0.11029004
#   Alpha = -0.460164
#   Beta = 0.5658524
#   Delta = 1
#   Gamma = 0
#   Eval(0.2) = 0.4738196
#   Eval'(0.2) = -0.460164