Time Series Parameter learning in Python

This example makes use of the Python code in Data Frame Utils.

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

import pandas as pd
import data_frame_utils as dfu
from jpype import *  # pip install jpype1==0.7.5


classpath = 'C:\\Program Files\\Bayes Server\\Bayes Server 9.2\\API\\Java\\bayesserver-9.2.jar'

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

bayes = JPackage("com.bayesserver")
bayes_data = bayes.data
bayes_inference = bayes.inference
bayes_parameters = bayes.learning.parameters

# Uncomment the following 2 lines and change the license key, if you are using a licensed version
# License = JClass("com.bayesserver.License")
# License.validate("xxx")

class ConsoleParameterLearningProgress(object):

    def update(self, info):
        See ParameterLearningProgress in the Bayes Server .NET or Java API docs for details.
        print('Iterations: ', info.getIterationCount(), ', log-likelihood: ', info.getLogLikelihood())

    def getDistributionMonitoring(self):
        See ParameterLearningProgress in the Bayes Server .NET or Java API docs for details.
        return bayes_parameters.DistributionMonitoring.NONE

def create_network_structure():

    network = bayes.Network()

    x1 = bayes.Variable('X1', bayes.VariableValueType.CONTINUOUS)
    x2 = bayes.Variable('X2', bayes.VariableValueType.CONTINUOUS)

    # add a temporal (time series) node, with two continuous variables
    node_x = bayes.Node("X", [x1, x2])


    # add temporal links
    for order in range(1, 4):
        network.getLinks().add(bayes.Link(node_x, node_x, order))

    # at this point the Dynamic Bayesian network structure is fully specified

    return network

def learn_parameters():

    # we manually construct the network here, but it could be loaded from a file

    network = create_network_structure()

    x1 = network.getVariables().get("X1")
    x2 = network.getVariables().get("X2")

    # now learn the parameters from the data in Walkthrough 3 - Time Series network

    # This example uses Sql Server as the data source and assumes the data has been copied to
    # a table called TimeSeriesWalkthrough
    # We will use the RelevanceTree algorithm here, as it is optimized for parameter learning

    learning = bayes_parameters.ParameterLearning(network, bayes_inference.RelevanceTreeInferenceFactory())
    learning_options = bayes_parameters.ParameterLearningOptions()
    progress = ConsoleParameterLearningProgress()

    df = pd.read_csv('data/time_series_tutorial.csv') #  Note: Saved as csv from the Tutorial data installed with Bayes Server
    dt = dfu.to_data_table(df)
    temporal_data_reader_command = bayes_data.DataTableDataReaderCommand(dt)

    temporal_reader_options = bayes_data.TemporalReaderOptions('Case', 'Time', bayes_data.TimeValueType.INDEX)

    # here we map variables to database columns
    # in this case the variables and database columns have the same name
    temporal_variable_references = [
                        bayes_data.VariableReference(x1, bayes_data.ColumnValueType.VALUE, x1.getName()),
                        bayes_data.VariableReference(x2, bayes_data.ColumnValueType.VALUE, x2.getName())

    # note that although this example only has temporal data
    # we could have included additional non temporal variables and data

    evidence_reader_command = bayes_data.DefaultEvidenceReaderCommand(

    result = learning.learn(evidence_reader_command, learning_options)

    print("Log likelihood = ", result.getLogLikelihood())