NormalBPState#
- class graph_tool.dynamics.NormalBPState(g, x=1, mu=0, theta=1, m_m=None, m_s=None, vm_m=None, vm_s=None, marginal_init=False, frozen=None, converge=True)[git][source]#
Bases:
BPBaseStateBelief-propagation equations for the multivariate Normal distribution.
- Parameters:
- g
Graph Graph to be used for the dynamics.
- x
floatorEdgePropertyMap(optional, default:1.) Inverse covariance couplings. If a
EdgePropertyMapis given, it needs to be of typedouble. If a scalar is given, this will determine the value for every edge.- mu
floatorVertexPropertyMap(optional, default:0.) Node means. If a
VertexPropertyMapis given, it needs to be of typedouble. If a scalar is given, this will determine the value for every vertex.- theta
floator iterable orVertexPropertyMap(optional, default:1.) Diagonal of the inverse covariance matrix. If
VertexPropertyMap, this needs to be of typedouble. If a scalar is given, this will determine the value for every vertex.- m_m
EdgePropertyMap(optional, default:None) If provided, it should be an
EdgePropertyMapof typevector<double>, containing the edge messages for the means.- m_s
EdgePropertyMap(optional, default:None) If provided, it should be an
EdgePropertyMapof typevector<double>, containing the edge messages for the variances.- vm_m
VertexPropertyMap(optional, default:None) If provided, it should be an
VertexPropertyMapof typedouble, containing the node marginal means.- vm_s
VertexPropertyMap(optional, default:None) If provided, it should be an
VertexPropertyMapof typedouble, containing the node marginal variances.The values of
m_mandm_smust be in the format of the attributes of the same name of an existing state (two values per edge), otherwise aValueErroris raised.- marginal_init
boolean(optional, default:False) If
True, the messages will be initialized from the node marginals.- frozen
VertexPropertyMap(optional, default:None) If provided, it should be an
VertexPropertyMapof typebool, where a valueTruemeans that a vertex is not a variable, but a fixed field.- converge
boolean(optional, default:True) If
True, the functionconverge()will be called just after construction.
- g
Notes
This implements BP equations [mezard_information_2009] for the multivariate Normal distribution given by
\[P(\boldsymbol s | \boldsymbol A, \boldsymbol x, \boldsymbol \mu, \boldsymbol\theta) = \frac{\exp\left(-\frac{1}{2}(\boldsymbol s-\boldsymbol\mu)^{\intercal} \boldsymbol X (\boldsymbol s - \boldsymbol\mu)\right)} {Z(\boldsymbol X)}\]where \(X_{ij}=A_{ij}x_{ij}\) for \(i\neq j\), \(X_{ii}=\theta_i\), and \(Z(\boldsymbol X) = (2\pi)^{N/2}\left|\boldsymbol X\right|^{-1/2}\). The energy is therefore quadratic in \(\boldsymbol s\), with the linear term \(\boldsymbol h = \boldsymbol X\boldsymbol\mu\), i.e.
\[h_i = \theta_i\mu_i + \sum_jA_{ij}x_{ij}\mu_j.\]The frozen vertices are not variables, and the values of their states are their fixed marginal means given by
vm_m. The energy and the partition function then include only the terms that involve at least one variable.The BP equations consist in the Bethe approximation
\[\log Z(\boldsymbol X) = \sum_i\log Z_i - \sum_{i<j}A_{ij}\log Z_{ij}\]with \(Z_{ij}=Z_j/Z_{j\to i}=Z_i/Z_{i\to j}\), obtained from the message-passing equations
\[\begin{split}\begin{aligned} m_{i\to j} &= \frac{h_i - \sum_{k\in \partial i\setminus j}A_{ik}x_{ik}m_{k\to i}} {\theta_i - \sum_{k\in \partial i\setminus j}A_{ik}x_{ik}^2\sigma_{k\to i}^2},\\ \sigma_{i\to j}^2 &= \frac{1}{\theta_i - \sum_{k\in \partial i\setminus j}A_{ik}x_{ik}^2\sigma_{k\to i}^2}, \end{aligned}\end{split}\]with
\[\begin{split}\begin{aligned} \log Z_{i\to j} &= \frac{\beta_{i\to j}^2}{4\alpha_{i\to j}} - \frac{1}{2}\log\alpha_{i\to j} + \frac{1}{2}\log\pi - \frac{\theta_i\mu_i^2}{2}\\ \log Z_{i} &= \frac{\beta_{i}^2}{4\alpha_{i}} - \frac{1}{2}\log\alpha_{i} + \frac{1}{2}\log\pi - \frac{\theta_i\mu_i^2}{2} \end{aligned}\end{split}\]where
\[\begin{split}\begin{aligned} \alpha_{i\to j} &= \frac{\theta_i - \sum_{k\in \partial i\setminus j}A_{ik}x_{ik}^2\sigma_{k\to i}^2}{2}\\ \beta_{i\to j} &= h_i - \sum_{k\in \partial i\setminus j}A_{ik}x_{ik}m_{k\to i}\\ \alpha_{i} &= \frac{\theta_i - \sum_{j\in \partial i}A_{ij}x_{ij}^2\sigma_{j\to i}^2}{2}\\ \beta_{i} &= h_i - \sum_{j\in \partial i}A_{ij}x_{ij}m_{j\to i}, \end{aligned}\end{split}\]and the constant terms \(A_{ij}x_{ij}\mu_i\mu_j\) of the energy are subtracted from \(\log Z(\boldsymbol X)\) for every edge.
From these equations, the marginal node probability densities are normal distributions with mean and variance given by
\[\begin{split}\begin{aligned} m_i &= \frac{h_i - \sum_{j}A_{ij}x_{ij}m_{j\to i}} {\theta_i - \sum_{j}A_{ij}x_{ij}^2\sigma_{j\to i}^2},\\ \sigma_i^2 &= \frac{1}{\theta_i - \sum_{j}A_{ij}x_{ij}^2\sigma_{j\to i}^2}. \end{aligned}\end{split}\]References
[mezard_information_2009]Marc Mézard, and Andrea Montanari, “Information, physics, and computation”, Oxford University Press, 2009. https://web.stanford.edu/~montanar/RESEARCH/book.html
Examples
>>> g = gt.GraphView(gt.collection.data["polblogs"].copy(), directed=False) >>> gt.remove_parallel_edges(g) >>> g = gt.extract_largest_component(g, prune=True) >>> state = gt.NormalBPState(g, x=1/200, mu=g.vp.value.t(lambda x: arctanh((2*x-1)*.9))) >>> s = state.marginal_sample() >>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=s, ... output="bp-normal.svg") <...>
Marginal sample of a multivariate normal distribution.#
Methods
converge([epsilon, max_niter, update_marginals])Calls
iterate()until delta falls belowepsilonor the number of iterations exceedsmax_niter.copy(**kwargs)Return a copy of the state.
iterate([niter, parallel, update_marginals])Updates messages synchronously (or asynchronously if
parallel=False), niter number of times.log_Z()Obtains the log-partition function (a.k.a.
log_prob(s)Obtains the log-probability of state
s(aVertexPropertyMap), computed as the negative energy ofsminus the logarithm of the partition function.Obtains the marginal log-probability of state
s(aVertexPropertyMap).marginal_sample([update_marginals])Samples a state from the marginal distribution.
Obtains the energy (Hamiltonian) of state
s(aVertexPropertyMap).Update the node marginals from the current messages.
- converge(epsilon=1e-08, max_niter=1000, update_marginals=True, **kwargs)[git]#
Calls
iterate()until delta falls belowepsilonor the number of iterations exceedsmax_niter.If
update_marginals=True, the functionupdate_marginals()is called at the end.The remaining keyword arguments are passed to
iterate().This function returns a tuple
(niter, delta), with the number of iterations performed and the delta of the last iteration.
- copy(**kwargs)[git]#
Return a copy of the state. The keyword arguments override the constructor parameters.
- iterate(niter=1, parallel=True, update_marginals=True)[git]#
Updates messages synchronously (or asynchronously if
parallel=False), niter number of times. This function returns the iteration delta of the last iteration.If
update_marginals=True, this function callsupdate_marginals()at the end.Parallel implementation.
If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.
- log_prob(s)[git]#
Obtains the log-probability of state
s(aVertexPropertyMap), computed as the negative energy ofsminus the logarithm of the partition function.If
sis vector valued, it’s assumed to correspond to multiple states, and the total log-probability sum is returned.
- marginal_log_prob(s)[git]#
Obtains the marginal log-probability of state
s(aVertexPropertyMap).If
sis vector valued, it’s assumed to correspond to multiple states, and the total marginal log-probability sum is returned.
- marginal_sample(update_marginals=True)[git][source]#
Samples a state from the marginal distribution. This function returns a
VertexPropertyMapof type given byval_type.If
update_marginals=True, this function callsupdate_marginals()before sampling.
- sample_energy(s)[git]#
Obtains the energy (Hamiltonian) of state
s(aVertexPropertyMap).If
sis vector valued, it’s assumed to correspond to multiple states, and the total energy sum is returned.