Models — lgbn documentation

# Models

class lgbn.models.BayesianNetwork

  A Bayesian Network.

  A Bayesian network is a directed acyclic graph where each node is a random variable with a distribution conditional on the parent nodes.

  add_cpd(cpd: CPD) → None

    Add a conditional probability distribution to the network.

    Note

    Also adds the node and edges to the underlying networkx.DiGraph. If there already is a conditional probability distribution it is replaced and new edges are added, but previous edges not present in the new distribution will not be removed.

    Parameters:

      cpd (CPD) – A conditional probability distribution

  apply_op(op)

    Modify the network according to the given operation.

    An operation is an edge together with an action (add edge, remove edge, flip edge).

    Parameters:

      op – A tuple `(action, (u, v))` where action is one of +, - or F and (u, v) is an edge.

    Raises:

      - NotImplementedError – If the given action is not supported.

      - ValueError – If the edge is not in the network and operation requires editing it. (This is actually raised by networkx.DiGraph.remove_edge.)

  cpd_class

    The class used to instantiated CPDs when loading from a dict via .from_dict(data).

    alias of `CPD`

  cpds

    A dictionary mapping node identifiers to Conditional Probability Distributions.

    alias of `Dict`[`Any`, `CPD`]

  classmethod from_dict(data)

    Load Bayesian network from a dict.

    Parameters:

      data (Sequence[Dict[str, Any]]) – A list of dictionaries corresponding to CPDs. See CPD.to_dict for more information on the format.

    See also

    `to_dict`, `CPD.from_dict`

    Notes

    This method expects the data to be sorted by node in topological order, so that a node always comes before its children. This is the way to_dict generates dictionaries.

  to_dict()

    Serializes the Bayesian network into a list of dictionaries.

    Each dictionary is the result of serializing a CPD via CPD.to_dict.

  update_cpds_from_structure() → None

    Updates the parents attribute in each CPD to match the current graph structure.

    Tip

    Use this method after updating the network structure (e.g. via learning).

class lgbn.models.CPD(node: Any, parents: Optional[tuple[Any]] = None)

  A conditional probability distribution for a node.which also references the node’s parents.s

  In a BayesianNetwork the probability distributions of the nodes are specified via CPDs or Conditional Probability distributions. These classes represent the probability distributions of the random variables in the nodes of a Bayesian network, which in general are conditioned on the parent nodes.

  Note

  This is a base class which cannot actually be used.

  See also

  `LinearGaussianCPD`

    A conditional probability distribution for linear Gaussian Bayesian networks.

  classmethod from_dict(data: Dict[str, Any])

    Loads this conditional probability distribution from a dict.

  to_dict() → Dict[str, Any]

    Serializes this conditional probability distribution into a dict.

class lgbn.models.LinearGaussianBayesianNetwork

  A Bayesian network where every node has a Gaussian distribution, where the mean of each node is a linear combination of its parents plus a bias factor and the standard deviations of the nodes are independent.

  The joint distribution of these networks also a Gaussian distribution, the parameters of which can be obtained via the to_joint_gaussian method.

  cpd_class

    alias of `LinearGaussianCPD`

  to_joint_gaussian()

    Get the equivalent multivariate Gaussian distribution to this Bayesian network.

    Returns a scipy.stats.multivariate_normal frozen random variable which has attributes mean (vector of means) and cov (covariance matrix).

    Returns:

      A frozen random variable.

    Return type:

      scipy.stats.multivariate_normal

    Notes

    Linear Gaussian Bayesian networks have a joint probability distribution that is also normal, and thus this method is well defined. See p. 252 of [1] and pp. 370-371 of [2].

    References

    [2]

    C. M. Bishop, Pattern recognition and machine learning. New York: Springer, 2006.

class lgbn.models.LinearGaussianCPD(node: Any, mean: Optional[float] = 0, var: Optional[float] = 1, parents: Optional[tuple[Any]] = None, weights: Optional[tuple[float]] = None)

  A linear Gaussian conditional probability distribution.

  A linear Gaussian conditional probability distribution is normal distribution where the mean is a linear combination of the means of the parent nodes plus a bias term, i.e. if this node (X) has parents \(U_1, \ldots, U_k\) then

  \[p(X) = N(X \mid \mu + w_1 \mu_{U_1} + \ldots + w_k \mu_{U_k}, \sigma^2),\]

  where \(\mu\) is specified via the mean parameter, \(\sigma^2\) via the var parameter and \(w_1, \ldots, w_k\) via the weights parameter.

  mle(data: DataFrame)

    Find maximum likelihood estimate for mean, variance and weights given the data and the dependencies on the parents.

    Parameters:

      data (pandas.DataFrame) – A DataFrame with one row per observation and one column per variable.

    Returns:

      A new conditional probability distribution where the parameters are set to the ML estimates.

    Return type:

      LinearGaussianCPD

    Notes

    Maximum likelihood estimation of parameters is computed using the sufficient statistics approach described in section 17.2.4 of [1].

    References

    [1] (1,2)

    D. Koller and N. Friedman, Probabilistic graphical models: principles and techniques. Cambridge, MA: MIT Press, 2009.

  to_dict()

    Serializes this conditional probability distribution into a dict.