Inference is same as asking conditional probability questions to the models., variables=None, evidence=None, to_df=True, elimination_order='greedy', joint=True, groupby=None, verbose=3)

Inference using using Variable Elimination.

The basic concept of variable elimination is same as doing marginalization over Joint Distribution. But variable elimination avoids computing the Joint Distribution by doing marginalization over much smaller factors. So basically if we want to eliminate X from our distribution, then we compute the product of all the factors involving X and marginalize over them, thus allowing us to work on much smaller factors.

  • model (dict) – Contains model.

  • variables (List, optional) –

    For exact inference, P(variables | evidence). The default is None.
    • [‘Name_of_node_1’]

    • [‘Name_of_node_1’, ‘Name_of_node_2’]

  • evidence (dict, optional) –

    For exact inference, P(variables | evidence). The default is None.
    • {‘Rain’:1}

    • {‘Rain’:1, ‘Sprinkler’:0, ‘Cloudy’:1}

  • to_df (Bool, (default is True)) – The output is converted in the dataframe [query.df]. Enabling this function may heavily impact the processing speed.

  • elimination_order (str or list (default='greedy')) – Order in which to eliminate the variables in the algorithm. If list is provided, should contain all variables in the model except the ones in variables. str options are: greedy, WeightedMinFill, MinNeighbors, MinWeight, MinFill. Please refer for details.

  • joint (boolean (default: True)) – If True, returns a Joint Distribution over variables. If False, returns a dict of distributions over each of the variables.

  • groupby (list of strings (default: None)) – The query is grouped on the variable name by taking the maximum P value for each catagory.

  • verbose (int, optional) – Print progress to screen. The default is 3. 0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE

Return type:

query inference object.


>>> import bnlearn as bn
>>> # Load example data
>>> model = bn.import_DAG('sprinkler')
>>> bn.plot(model)
>>> # Do the inference
>>> query =, variables=['Wet_Grass'], evidence={'Rain':1, 'Sprinkler':0, 'Cloudy':1})
>>> print(query)
>>> query.df
>>> query =, variables=['Wet_Grass','Rain'], evidence={'Sprinkler':1})
>>> print(query)
>>> query.df