# Forecasting with Dynamical Bayesian Networks

I am trying to forecast some variables of a dataset (time series) with Dynamical Bayesian Networks (DBN) using pgmpy. I could be mistaken, but what is being called "forecasting" in the bibliography (like the paper "A Tool for Learning Dynamic Bayesian Networks for Forecasting", which uses a network very similar to mine) is actually using the DBN as a classifier, since it makes use of all of the variables of the timestamp $$t+1$$, except the one it wants to predict. Pgmpy documentation also uses this same logic of removing only the desired variable from the dataset to predict it:

    def predict(self, data, n_jobs=-1):
"""
Predicts states of all the missing variables.

Parameters
----------
data : pandas DataFrame object
A DataFrame object with column names same as the variables in the model.

Examples
--------
>>> import numpy as np
>>> import pandas as pd
>>> from pgmpy.models import BayesianModel
>>> values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 5)),
...                       columns=['A', 'B', 'C', 'D', 'E'])
>>> train_data = values[:800]
>>> predict_data = values[800:]
>>> model = BayesianModel([('A', 'B'), ('C', 'B'), ('C', 'D'), ('B', 'E')])
>>> model.fit(train_data)
>>> predict_data = predict_data.copy()
>>> predict_data.drop('E', axis=1, inplace=True)
>>> y_pred = model.predict(predict_data)
>>> y_pred
E
800 0
801 1
802 1
803 1
804 0
... ...
993 0
994 0
995 1
996 1
997 0
998 0
999 0
"""
from pgmpy.inference import VariableElimination

if set(data.columns) == set(self.nodes()):
raise ValueError("No variable missing in data. Nothing to predict")

elif set(data.columns) - set(self.nodes()):
raise ValueError("Data has variables which are not in the model")

data_unique = data.drop_duplicates()
missing_variables = set(self.nodes()) - set(data_unique.columns)
#         pred_values = defaultdict(list)
pred_values = []

# Send state_names dict from one of the estimated CPDs to the inference class.
model_inference = VariableElimination(self)
pred_values = Parallel(n_jobs=n_jobs)(
delayed(model_inference.map_query)(
variables=missing_variables,
evidence=data_point.to_dict(),
show_progress=False,
)
for index, data_point in tqdm(
data_unique.iterrows(), total=data_unique.shape[0]
)
)

df_results = pd.DataFrame(pred_values, index=data_unique.index)
data_with_results = pd.concat([data_unique, df_results], axis=1)
return data.merge(data_with_results, how="left").loc[:, missing_variables]


I tried to follow pgmpy logic, and it returns me a very good predictor. However, when I try to predict all of the variables in my timestamp $$t+1$$, it performs really poor. So, my question is: is there an algorithm or approach to forecasting with DBNs? Any paper or reference would be much appreciated.

• have you solved this problem? And I am also facing the problem of using DBN in pgmpy, maybe we can discuss by emails? my account is z_yichuan@163.com – 张一川 Feb 22 at 3:08