Walkthrough 3 - Time series

In this walkthrough we will build a model of the multivariate time series data tabulated in the Data section.

The data consists of a single time series over the two continuous variables X1 and X2. A chart of X1 and X2 is shown below.

Data can be modeled that contains more that one time series. See Data for more information.

The following concepts will be covered:

  • Temporal nodes (i.e. Dynamic Bayesian networks)
  • Nodes with multiple variables
  • Creating a Data Connection
  • Learning parameters from data
  • Time Series predictions

Time series walkthrought X1, X2 chart

Bayes Server must be installed, before starting this walkthrough. An evaluation version can be downloaded from the Downloads page

Creating the model

We will create the Dynamic Bayesian network shown below.

Time series DBN

Add the nodes

  • To add the temporal X node, click the Node drop down on the Network tab, Editing group, and select the Temporal menu item, to create a new node. This will launch the New node window.

  • Enter X in the Name text box.

  • This node will contain two continuous variables, so in the Variable section, click the Multiple tab.

  • Click the Add Continuous toolbar button twice, to add two new continuous variables to the node.

  • Rename the new variables to the following, by clicking on the name of each new variable, and typing the new name.

    • X1
    • X2

    The New Node window should look like this:

    Time series - new node

  • Click the OK button to create the new node.

Note that we could have added the nodes automatically using the Add nodes from data feature.

Add the links

  • To add the first temporal link, click the Link button on the Network tab, Editing group. This will launch the New link window.

  • Select X as the From node.

  • Also select X as the To node.

  • Enter the value 1, in the Temporal Order text box.

  • The New Link window should look like this:

    Time series - new link

  • Click the OK button to create the new link.

  • To add the second, third and fourth temporal links, repeat the steps to create the first link, using the values in the table below.

    From To Temporal Order
    X X 2
    X X 3
    X X 4

You can either evaluate a time series model on unseen data with different Temporal orders to determine the best model, or you can use Structural learning.

To understand the structure of a Dynamic Bayesian network, click the Unroll button on the Network tab, Formation group, to see what the equivalent Bayesian network would look like. Note that unrolling is not required to work with Dynamic Bayesian networks.

Learning the distributions

We could now enter the distribution parameters for the nodes manually using the Distribution editor, however in this walkthrough we will learn the parameter values from data.

For convenience, we will use Microsoft Excel as the data source, however another database can be substituted.

Although Microsoft Excel is a convenient way of storing data, in practice we recommend using a database as the data source.

Adding a data connection

Note: You can skip this step, and instead use the pre-installed Walkthrough data connection.

  • Select the data (including the header) in the data section and copy it to the clipboard (Ctrl+C).
  • Open Microsoft Excel and paste the data into a new Microsoft Excel spreadsheet (Ctrl+V).
  • Save the new spreadsheet.
  • In Bayes Server, click the Data Connections button on the Data tab, Data Sources group. This will launch the Data connection manager.
  • Click the New button on the toolbar. This will launch the Data connection editor.
  • In the Standard list of data providers, select the appropriate Excel Driver for the version of Microsoft Excel you are using.
  • Next to the File Name text box, click the Ellipsis (...) button, and select the Microsoft Excel spreadsheet created in an earlier step.
  • Click the Test Connection button, to ensure the new data connection is working.
  • Click OK to add the new Data Connection.

Parameter learning

  • Click the Parameter Learning button, on the Data tab, Learning group. This will launch the Data tables window.

  • In the Data Connection drop down, select the new Data Connection created in an earlier step, or the Walkthrough data connection if you skipped that step. This should enable the Temporal Data drop down.

  • In the Temporal Data drop down, select the worksheet that contains the data. (If the data is on the first worksheet, select Sheet1$). If you are using the pre-installed Walkthrough data connection, select Walkthrough 3 - Time series.

  • Click the OK button. This will launch the Data map window.

  • In the Data map window, ensure that the variables X1 and X2 have automatically been mapped to columns X1 and X2 respectively.

  • Select the Case data column in the Case Id Column drop down, if it has not already been automatically mapped.

This step is optional as we only have a single time series.

  • Select the Time data column in the Time Column drop down, if it has not already been automatically mapped.

    The window should look like this:

    Time series - data map

  • Click the OK button. This will launch the Parameter learning window.

  • Click the Start button on the Parameter Learning tab, Learning group. Click OK on the message box displayed when learning has completed.

  • Click the Apply button on the Parameter Learning tab, Close group. This will launch the Candidate Networks window.

  • Click the OK button in the Candidate Networks window.

  • The network distributions have now been learned.

Time Series Prediction

Enter evidence

Before predicting future values, we will enter some evidence.

  • Ensure Data explorer is open by clicking the Data Explorer button on the View toolbar tab, Explorers group.

You can enter evidence manually using the Evidence window, however we will use Data Explorer here for convenience.

  • Click in Data Explorer to ensure it has focus.

    A contextual tab should appear on the toolbar, with the title Data Explorer. If the tab's contents are not visible, click on the Data Explorer tab.

  • Click the Load button on the Data Explorer tab, Data group. This will launch the Data tables window.

  • In the Data Connection drop down, select the Data Connection created earlier. This should enable the Temporal Data drop down.

  • In the Temporal Data drop down, select the worksheet that contains the data. (If the data is on the first worksheet, select Sheet1$).

  • Click the OK button. This will launch the Data map window.

  • In the Data map window, ensure that the variables X1 and X2 have automatically been mapped to columns X1 and X2 respectively.

  • Select the Case data column in the Case Id Column drop down, if it has not already been automatically mapped.

  • Select the Time data column in the Time Column drop down, if it has not already been automatically mapped. The window should look like this:

    Time series - data map

  • Click the OK button. This will load the data into Data explorer.

    Data explorer should look like this.

    Time series - data explorer

    Data Explorer loads data into memory, so should not be used for large data sets.

  • Select the only case in the top pane of Data Explorer with id 0, and click the Apply button in the Data Explorer tab, Apply group. This will set evidence on the network.

    Notice that the X node now has an ! symbol, indicating that the node has evidence.

    Instead of clicking the Apply button, you can double click the case in the top pane of Data Explorer to apply evidence.

  • To check that the evidence has been set correctly, and to see how you might enter evidence manually, click the Evidence button in the Query tab, Evidence group. This will launch the Evidence window.

    Double clicking a node, also opens the Evidence window.

  • Click the Discard button on the Evidence tab, Close group, to close the evidence window.

Time Series Query

In the Query tab, Temporal group, ensure that the minimum and maximum temporal times are set as follows.

Time Time Type
Min 1 Relative
Max 25 Relative

Relative means that times starts from the maximum evidence time.

Predicting values 25 steps into the future is usually a bit ambitious, however for this walkthrough it shows the trend well.

  • Click the Plot button on the Query tab, Temporal group. This will launch the Plot Variable Selection window.

  • With the Query Variables tab selected, select both X1 and X2 variables, and click the > button for the Y variables.

    The window should look like this.

    Time series - prediction query variables

  • With the Evidence Variables tab selected, select both X1 and X2 variables, and click the > button for the Y variables.

    The window should look like this.

    Time series - prediction evidence variables

  • Click the OK button.

    A time series prediction will be performed, and a chart generated.

To return a table of values instead of a chart, click the Marginals button on the Query tab, Temporal group.

The resulting chart should look like this:

Time series - prediction chart

More complex models

The network created in this walkthrough can be extended by adding additional nodes, to model more complex data.

For example, discrete nodes could be added, that represent different regimes/contexts, allowing the Gaussian distributions to vary. This could be a node that is mapped to data, or a Mixture node, as was used in Walkthrough 2 - Mixture model.

Data

Case Time X1 X2
0 0 2.318745783 20.46006463
0 1 5.933818502 17.67586928
0 2 7.504462149 15.1712915
0 3 8.851967996 13.46621753
0 4 12.60817163 12.45778021
0 5 13.53426343 11.28238594
0 6 14.08388741 11.36542721
0 7 13.3766438 11.32797101
0 8 15.68526939 10.8970169
0 9 15.11022927 14.1524426
0 10 13.20119013 14.26016682
0 11 9.494001085 16.08702792
0 12 8.304703769 18.63487888
0 13 6.593647849 22.21299995
0 14 4.118074113 23.21864609
0 15 2.111678085 26.50607334
0 16 2.476215264 26.69056302
0 17 -0.231077926 28.01168052
0 18 0.087771397 30.26062659
0 19 1.315852301 29.6079087
0 20 2.810231967 30.05485261
0 21 2.552855638 26.86816415
0 22 4.900436683 25.76875809
0 23 7.913171459 23.26273577
0 24 9.737370988 21.25918873
0 25 12.66258445 19.13469172
0 26 15.64544348 15.71762796
0 27 20.58203988 15.42579064
0 28 22.86777997 10.63982879
0 29 23.57926162 10.27021685
0 30 27.02910891 9.817333981
0 31 25.58107867 9.061899103
0 32 26.94255966 9.584602154
0 33 27.16040423 9.442765674
0 34 27.04495789 10.51641975
0 35 24.90688221 11.75130635
0 36 23.98954889 14.35173433
0 37 22.28610935 16.40809747
0 38 19.78816701 17.39167485
0 39 16.91941516 19.96761072
0 40 17.29332335 22.57813982
0 41 14.01409456 24.24254295
0 42 13.87122505 25.42334936
0 43 13.44463368 27.31677309
0 44 14.83781008 26.23227138
0 45 15.68276374 26.56970664
0 46 15.13583588 27.03137041
0 47 16.43886985 25.07092718
0 48 20.7979978 21.18958008
0 49 23.48791794 20.8674994
0 50 26.72710284 18.26751923
0 51 30.28903693 15.61215805
0 52 30.51520286 13.53912436
0 53 35.46528278 10.30766893
0 54 37.84679647 9.496972103
0 55 38.08298043 6.199712849
0 56 40.39095686 5.590196774
0 57 40.07846242 6.959696004
0 58 40.08610277 7.152498131
0 59 40.05179382 6.732313676
0 60 38.15557619 10.37673309
0 61 34.99846217 10.03214111
0 62 34.6842111 13.81602026
0 63 31.58501235 14.62634965
0 64 29.21931264 18.37806697
0 65 28.79983427 19.48749404
0 66 27.62649704 22.95728553
0 67 24.91441719 21.98144367
0 68 27.14508364 23.12311523
0 69 26.36169102 25.70715991
0 70 25.42262786 22.91309475
0 71 26.68629076 24.76868418
0 72 29.69233279 21.08297546
0 73 30.99710698 19.63166555
0 74 34.02657121 19.00319207
0 75 39.27729696 13.73228031
0 76 39.67147284 11.96785476
0 77 44.18986124 10.81560819
0 78 45.904526 7.002139952
0 79 49.52983788 5.68254081
0 80 51.30622996 4.286459901
0 81 50.67634617 4.42147376
0 82 53.11851589 3.747526155
0 83 51.48325685 3.070023993
0 84 50.63623668 4.0580626
0 85 51.8926161 5.507954661
0 86 48.70427797 9.596489359
0 87 47.51607953 11.35891616
0 88 46.67047755 13.64482398
0 89 43.05034733 15.68684769
0 90 41.62091455 17.72461027
0 91 39.80023298 19.58704864
0 92 39.29655794 20.00115014
0 93 38.93020152 23.13560737
0 94 38.88239321 21.07024372
0 95 39.63691083 22.20872048
0 96 40.17065414 21.28034691
0 97 42.31931906 20.13211517
0 98 43.81657518 17.76721478
0 99 48.32749664 15.14488814