# Source code for bayespy.inference.vmp.nodes.stochastic

################################################################################
# Copyright (C) 2013-2014 Jaakko Luttinen
#
################################################################################

import numpy as np

from bayespy.utils import misc

import h5py

from .node import Node

[docs]class Distribution(): """ A base class for the VMP formulas of variables. Sub-classes implement distribution specific computations. If a sub-class maps the plates differently, it needs to overload the following methods: * compute_weights_to_parent * plates_to_parent * plates_from_parent """
[docs] def compute_message_to_parent(self, parent, index, u_self, *u_parents): """ Compute the message to a parent node. """ raise NotImplementedError()
[docs] def compute_weights_to_parent(self, index, weights): """ Maps the mask to the plates of a parent. """ # Sub-classes may need to overwrite this method return weights
[docs] def plates_to_parent(self, index, plates): """ Resolves the plate mapping to a parent. Given the plates of the node's moments, this method returns the plates that the message to a parent has for the parent's distribution. """ return plates
[docs] def plates_from_parent(self, index, plates): """ Resolve the plate mapping from a parent. Given the plates of a parent's moments, this method returns the plates that the moments has for this distribution. """ return plates
[docs] def random(self, *params, plates=None): """ Draw a random sample from the distribution. """ raise NotImplementedError()
[docs]class Stochastic(Node): """ Base class for nodes that are stochastic. u observed Sub-classes must implement: _compute_message_to_parent(parent, index, u_self, *u_parents) _update_distribution_and_lowerbound(self, m, *u) lowerbound(self) _compute_dims initialize_from_prior() If you want to be able to observe the variable: _compute_fixed_moments_and_f Sub-classes may need to re-implement: 1. If they manipulate plates: _compute_weights_to_parent(index, weights) _compute_plates_to_parent(self, index, plates) _compute_plates_from_parent(self, index, plates) """ # Sub-classes must over-write this _distribution = None
[docs] def __init__(self, *args, initialize=True, dims=None, **kwargs): self._id = Node._id_counter Node._id_counter += 1 super().__init__(*args, dims=dims, **kwargs) # Initialize moment array axes = len(self.plates)*(1,) self.u = [misc.nans(axes+dim) for dim in dims] # Not observed self.observed = False self.ndims = [len(dim) for dim in self.dims] if initialize: self.initialize_from_prior()
[docs] def get_pdf_nodes(self): return (self,) + super().get_pdf_nodes()
def _get_pdf_nodes_conditioned_on_parents(self): return (self,) def _get_id_list(self): """ Returns the stochastic ID list. This method is used to check that same stochastic nodes are not direct parents of a node several times. It is only valid if there are intermediate stochastic nodes. To put it another way: each ID corresponds to one factor q(..) in the posterior approximation. Different IDs mean different factors, thus they mean independence. The parents must have independent factors. Stochastic nodes should return their unique ID. Deterministic nodes should return the IDs of their parents. Constant nodes should return empty list of IDs. """ return [self._id] def _compute_plates_to_parent(self, index, plates): return self._distribution.plates_to_parent(index, plates) def _compute_plates_from_parent(self, index, plates): return self._distribution.plates_from_parent(index, plates) def _compute_weights_to_parent(self, index, weights): return self._distribution.compute_weights_to_parent(index, weights)
[docs] def get_moments(self): # Just for safety, do not return a reference to the moment list of this # node but instead create a copy of the list. return [ui for ui in self.u]