Skip to main content

Time Series Parameter learning in Python

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

# __author__ = 'Bayes Server'
# __version__= '0.2'

import pandas as pd
import jpype # pip install jpype1 (version 1.2.1 or later)
import jpype.imports
from jpype.types import *
from jpype import java, JImplements, JOverride

classpath = "lib/bayesserver-10.8.jar" # TODO download the Bayes Server Java API, and adjust the path

# Launch the JVM

import data_frame_utils as dfu

# import the Java modules
from com.bayesserver import *
from com.bayesserver.inference import *
from import *
from com.bayesserver.learning.parameters import *

# Uncomment the following line and change the license key, if you are using a licensed version
# 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 DistributionMonitoring.NONE

def create_network_structure():

network = Network()

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

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


# add temporal links
for order in range(1, 4):
network.getLinks().add(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 = ParameterLearning(network, RelevanceTreeInferenceFactory())
learning_options = 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 = DataTableDataReaderCommand(dt)

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

# here we map variables to database columns
# in this case the variables and database columns have the same name
temporal_variable_references = [
VariableReference(x1, ColumnValueType.VALUE, x1.getName()),
VariableReference(x2, 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 = DefaultEvidenceReaderCommand(

result = learning.learn(evidence_reader_command, learning_options)

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