6. Calculations

6.1. Factor

class pybn.operations.Factor[source]

Factor

Factors are used to represent the CPDs in the Bayesian network. As such, the core functionality, which are implemented, are the factor product, factor marginalization and factor reduction operations.

Attributes :
  • name (str): Name of the factor
  • var (list): List of variables in the factor, e.g. [1 2 3]
  • card (list): List of cardinalities corresponding to .var, e.g. [2 2 2]
  • val (list): Value table of size prod(card)

6.1.1. Factor Product

class pybn.operations.FactorProduct[source]

Factor Product Computes the product of two factors.

C.FactorProduct(A,B) computes the product between two factors, A and B, where each factor is defined over a set of variables with given dimension. The factor data structure has the following fields:

  • .var Vector of variables in the factor, e.g. [1 2 3]
  • .card Vector of cardinalities corresponding to .var, e.g. [2 2 2]
  • .val Value table of size prod(.card)
Args :
  • A (Factor): Factor A
  • B (Factor): Factor B
Returns :
  • C (Factor): Return factor C

6.1.2. Factor Marginalization

class pybn.operations.FactorMarginalization[source]

Factor Marginalization Sums given variables out of a factor.

B = FactorMarginalization(A,V) computes the factor with the variables in V summed out. The factor data structure has the following fields:

  • .var Vector of variables in the factor, e.g. [1 2 3]
  • .card Vector of cardinalities corresponding to .var, e.g. [2 2 2]
  • .val Value table of size prod(.card)

The resultant factor should have at least one variable remaining or this function will throw an error.

Args :
  • A (Factor): Factor A
  • V (list): variables summed out
Returns :
  • B (Factor): Return factor B

6.1.3. Observe Evidence

class pybn.operations.ObserveEvidence[source]

Observe Evidence Modify a vector of factors given some evidence.

F = ObserveEvidence(F, E) sets all entries in the vector of factors, F, that are not consistent with the evidence, E, to zero. F is a vector of factors, each a data structure with the following fields:

  • .var Vector of variables in the factor, e.g. [1 2 3]
  • .card Vector of cardinalities corresponding to .var, e.g. [2 2 2]
  • .val Value table of size prod(.card)
Args :
  • F (Factor): Factor F
  • E (tuple): E is an N-by-2 matrix, where each row consists of a variable/value pair.

Variables are in the first column and values are in the second column.

Returns :
  • F (Factor): Return factor F

6.1.4. Compute Joint Distribution

class pybn.operations.ComputeJointDistribution[source]

Compute Joint Distribution computes the joint distribution defined by a set of given factors

Joint = ComputeJointDistribution(F) computes the joint distribution defined by a set of given factors

Joint is a factor that encapsulates the joint distribution given by F F is a vector of factors (struct array) containing the factors defining the distribution

6.1.5. Compute Marginal

class pybn.operations.ComputeMarginal[source]

Compute Marginal Computes the marginal over a set of given variables.

M = ComputeMarginal(V, F, E) computes the marginal over variables V in the distribution induced by the set of factors F, given evidence E

Args :
  • V (list): V is a list containing the variables in the marginal e.g. [1 2 3] for X_1, X_2 and X_3.
  • F (list): F is a list of factors containing the factors defining the distribution
  • E (tuple): E is an N-by-2 matrix, each row being a variable/value pair.

Variables are in the first column and values are in the second column.

If there is no evidence, pass in the empty matrix [] for E.

Returns :
  • M (Factor): M is a factor containing the marginal over variables V

6.1.6. Assignment to Index

class pybn.operations.AssignmentToIndex[source]

AssignmentToIndex Convert assignment to index.

I = AssignmentToIndex(A, D) converts an assignment, A, over variables with cardinality D to an index into the .val vector for a factor. If A is a matrix then the function converts each row of A to an index.

Args :
  • A (list): assignment A.
  • D (list): cardinality of the assignment
Returns :
  • I (list): Returns a list I with indices correlated to the assignment

6.1.7. Index to Assignment

class pybn.operations.IndexToAssignment[source]

IndexToAssignment Convert index to variable assignment.

A = IndexToAssignment(I, D) converts an index, I, into the .val vector into an assignment over variables with cardinality D. If I is a vector, then the function produces a matrix of assignments, one assignment per row.

Args :
  • I (list): List of indices
  • D (list): cardinality of the assignment
Returns :
  • A (list): Returns the assignment A related to the indices I.