inference

Example setup

For the illustartion below, we set up the same example as in cpm.Cpm.

import numpy as np
from mbnpy import cpm, variable, inference

# Define variables and CPMs
varis = {}
varis['x1'] = variable.Variable('x1', ['fail', 'surv'])
varis['x2'] = variable.Variable('x2', ['f', 's'])
varis['x3'] = variable.Variable('x3', ['f', 's'])

cpms = {}
cpms['x1'] = cpm.Cpm([varis['x1']], no_child=1, C=np.array([[0], [1]], dtype=int), p = np.array([0.1, 0.9]))
cpms['x2'] = cpm.Cpm([varis['x2']], no_child=1, C=np.array([[0], [1]], dtype=int), p = np.array([0.2, 0.8]))

# 'x3' is a series system of 'x1' and 'x2'
cpms['x3'] = cpm.Cpm([varis['x3'], varis['x1'], varis['x2']], no_child=1,
                    C=np.array([[0, 0, 2], [0, 1, 0], [1, 1, 1]], dtype=int), p = np.array([1.0, 1.0, 1.0]))

Methods

inference.condition(cpms, cnd_vars, cnd_states)

Condition the CPMs on the given variables and states.

param cpms:

List or dictionary of CPMs.

type cpms:

list or dict of Cpm

param cnd_vars:

List of variables to condition on.

type cnd_vars:

list of Variable

param cnd_states:

List of states to condition on.

type cnd_states:

list of int

return:

Conditioned CPMs.

rtype:

list or dict of Cpm

Example:

cpm_x3_0 = inference.condition([cpms['x3']], [varis['x3']], [0])
print(cpm_x3_0)

Output:

[Cpm(variables=['x3', 'x1', 'x2'],
no_child=1,
C=[[0 0 2]
[0 1 0]],
p=[[1.]
[1.]],
)]

Notice that the previous last row of the C matrix and p vector are removed.

isinscope

inference.isinscope(idx, cpms):

Check if variables are in the scope of a given set of CPMs.

param idx:

list of variable names

type idx:

list of str

param cpms:

list or dictionary of CPMs.

type cpms:

list or dict of Cpm

return:

True if any of the variables are in the scope of the CPMs.

rtype:

list of bool

Example:

isin = inference.isinscope(['x1'], cpms)
print(isin)

Output:

[[ True]
 [False]
 [ True]]

variable_elim

inference.variable_elim(cpms, var_elim, prod=True)

Perform variable elimination on the given CPMs.

param cpms:

List or dictionary of CPMs.

type cpms:

list or dict of Cpm

param var_elim:

List of variables to eliminate in the order of elimination.

type var_elim:

list of Variable

param prod:

If True, the Cpm is returned as a product of the remaining CPMs. If False, a list of CPMs are returned after variable elimination.

return:

Resulting CPM.

rtype:

Cpm or list of Cpm

Example:

M_x3 = inference.variable_elim(cpms, [varis['x1'], varis['x2']])
print(M_x3)

Output:

Cpm(variables=['x3'],
    no_child=1,
    C=[[0]
    [1]],
    p=[[0.28]
    [0.72]],
    )

The CPM represents \(P(X_3) = \sum_{X_2} P(X_2) \cdot \sum_{X_1} P(X_3| X_1, X_2) \cdot P(X_1)\).

get_elimination_order

inference.get_elimination_order(cpms)

Get the ancestry order of variables for the given cpms. The ordering priorities are (i) ancestors first, (ii) fewest parents first.

param cpms:

List or dictionary of CPMs.

type cpms:

list or dict of Cpm

return:

List of Variable objects in the order of elimination.

rtype:

list of Variable

Example:

ve_order = inference.get_elimination_order(cpms)
print([v.name for v in ve_order])

Output:

['x1', 'x2', 'x3']

The output indicates that ‘x1’ and ‘x2’ should be eliminated before ‘x3’.

get_inf_vars

inference.get_inf_vars(cpms, varis, ve_ord=None)

Get the list of variables in the scope of variables in varis.

param cpms:

List or dictionary of CPMs.

type cpms:

list or dict of Cpm

param varis:

A list of variable names or Variable objects or a single variable name or Variable object whose marginal distributions are of interest.

type varis:

A list of str/Variable object or str/Variable object

param ve_ord:

variable names or Variable objects in the order of elimination. If provided, the function returns the variables in the given order.

type ve_ord:

list of str/Variable object

return:

List of variable names in the scope of the given variables.

rtype:

list of str

Example:

Below, an irrelevant variable ‘x4’ is added:

varis['x4'] = variable.Variable('x4', ['f', 's'])
cpms['x4'] = cpm.Cpm([varis['x4']], no_child=1, C = np.array([[0], [1]], dtype=int), p = np.array([0.3, 0.7]))

Then, the function is applied:

varis_inf = inference.get_inf_vars(cpms, 'x3', ['x1', 'x2'])
print(varis_inf)

Output:

['x1', 'x2', 'x3']

Notice that ‘x4’ is not included in the list. With the list that includes only relevant variables to obtain the marginal distribution of ‘x3’, one can perform variable elimination:

M_x3 = inference.variable_elim([cpms[x] for x in varis_inf], [varis['x1'], varis['x2']])
print(M_x3)

Output:

Cpm(variables=['x3'],
    no_child=1,
    C=[[0]
    [1]],
    p=[[0.28]
    [0.72]],
    )

where the result is the same as the previous example with inference.variable_elim. By eliminating irrelevant variables, one can save computational time.

prod_Msys_and_Mcomps

prod_Msys_and_Mcomps(Msys, Mcomps_list)

Compute the product of a system CPM and the list of component CPMs: \(P(X_{sys},X_1,...,X_n) = P(X_{sys}|X_1,...,X_n) \cdot P(X_1) \cdot ... \cdot P(X_n)\). The function is faster than product by exploiting the knowledge on which one is a system CPM and which ones are component CPMs.

param Msys:

System CPM.

type Msys:

Cpm

param Mcomps_list:

List of component CPMs.

type Mcomps_list:

list of Cpm

return:

Product of the system and component CPMs.

rtype:

Cpm

Example:

Mprod = inference.prod_Msys_and_Mcomps(cpms['x3'], [cpms['x1'], cpms['x2']])
print(Mprod)

Output:

Cpm(variables=['x3', 'x1', 'x2'],
    no_child=3,
    C=[[0 0 2]
    [0 1 0]
    [1 1 1]],
    p=[[0.1 ]
    [0.18]
    [0.72]],
    )

cal_first_order_sobol

cal_first_order_sobol(cpms, input_vars, target_var, elim_order=None)

Calculate the first-order Sobol indices for a given target variable based on the provided conditional probability models (CPMs) and input variables.

The calculation is performed by variable elimination.

Parameters:
  • cpms (dict[str, Cpm] or list[Cpm]) – list or dictionary of CPMs.

  • input_vars (list[str | Variable] or dict[str, Variable]) – Input variables (as names or Variable objects).

  • target_var (str or Variable) – The target variable of interest.

  • elim_order (list[str | Variable], optional) – Optional elimination order of variables.

Returns:

First-order Sobol indices for the input variables.

Return type:

list[float]

Raises:
  • TypeError – If inputs are of incorrect types.

  • ValueError – If a specified variable is not found in the CPMs.

Example:

varis = {}
varis['x'] = variable.Variable(name="x", values=[0, 1])
varis['y'] = variable.Variable(name="y", values=['a', 'b'])
varis['z'] = variable.Variable(name="z", values=['a', 'b', 'c'])

cpms = {}
cpms['x'] = cpm.Cpm(variables=[varis['x']], no_child=1, C=[[0],[1]], p=[0.5, 0.5])
cpms['y'] = cpm.Cpm(variables=[varis['y']], no_child=1, C=[[0],[1]], p=[0.1, 0.9])
cpms['z'] = cpm.Cpm(variables=[varis['z'], varis['x'], varis['y']], no_child=1, C=[[0, 0, 0],[1, 0, 0],[2, 0, 0],
                                                                                [0, 1, 0],[1, 1, 0],[2, 1, 0],
                                                                                [0, 0, 1],[1, 0, 1],[2, 0, 1],
                                                                                [0, 1, 1],[1, 1, 1],[2, 1, 1]],
                                                                                p = [0.1, 0.2, 0.7, 0.2, 0.3, 0.5, 0.3, 0.4, 0.3, 0.7, 0.2, 0.1])

sobol_indices = inference.cal_first_order_sobol(cpms, [varis['x'], varis['y']], varis['z'])
print(sobol_indices)

Output:

[0.124, 0.0774]