Source code for graph_tool.inference.mcmc

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# graph_tool -- a general graph manipulation python module
#
# Copyright (C) 2006-2026 Tiago de Paula Peixoto <tiago@skewed.de>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .. import Vector_size_t, Vector_double

import numpy as np
from . util import *
from . base_states import *

[docs] def mcmc_equilibrate(state, wait=1000, nbreaks=2, max_niter=np.inf, force_niter=None, epsilon=0, gibbs=False, multiflip=True, mcmc_args={}, entropy_args={}, history=False, callback=None, verbose=False): r"""Equilibrate a MCMC with a given starting state. Parameters ---------- state : Any state class (e.g. :class:`~graph_tool.inference.BlockState`) Initial state. This state will be modified during the algorithm. wait : ``int`` (optional, default: ``1000``) Number of iterations to wait for a record-breaking event. nbreaks : ``int`` (optional, default: ``2``) Number of iteration intervals (of size ``wait``) without record-breaking events necessary to stop the algorithm. max_niter : ``int`` (optional, default: ``numpy.inf``) Maximum number of iterations. force_niter : ``int`` (optional, default: ``None``) If given, will force the algorithm to run this exact number of iterations. epsilon : ``float`` (optional, default: ``0``) Relative changes in entropy smaller than epsilon will not be considered as record-breaking. gibbs : ``bool`` (optional, default: ``False``) If ``True``, each step will call ``state.gibbs_sweep`` instead of ``state.mcmc_sweep``. multiflip : ``bool`` (optional, default: ``True``) If ``True`` and ``state`` is an instance of :class:`~graph_tool.inference.MultiflipMCMCState`, each step will call ``state.multiflip_mcmc_sweep`` instead of ``state.mcmc_sweep``. mcmc_args : ``dict`` (optional, default: ``{}``) Arguments to be passed to ``state.mcmc_sweep`` (or ``state.gibbs_sweep``). entropy_args : ``dict`` (optional, default: ``{}``) Currently unused. The arguments passed to ``state.entropy()`` are taken from ``mcmc_args["entropy_args"]`` instead. history : ``bool`` (optional, default: ``False``) If ``True``, a list of tuples of the form ``(nattempts, nmoves, entropy)`` will be kept and returned, where ``entropy`` is the current entropy and ``nmoves`` is the number of vertices moved. callback : ``function`` (optional, default: ``None``) If given, this function will be called after each iteration. The function must accept the current state as an argument, and its return value must be either ``None`` or a (possibly empty) list of values that will be appended to the history, if ``history == True``. verbose : ``bool`` or ``tuple`` (optional, default: ``False``) If ``True``, progress information will be shown. Optionally, this accepts arguments of the type ``tuple`` of the form ``(level, prefix)`` where ``level`` is a positive integer that specifies the level of detail, and ``prefix`` is a string that is prepended to all output messages. Notes ----- The MCMC equilibration is attempted by keeping track of the maximum and minimum values, and waiting sufficiently long without a record-breaking event. This function calls ``state.mcmc_sweep`` (or ``state.gibbs_sweep``) at each iteration (e.g. :meth:`graph_tool.inference.BlockState.mcmc_sweep` and :meth:`graph_tool.inference.BlockState.gibbs_sweep`), and keeps track of the value of ``state.entropy(**args)`` with ``args`` corresponding to ``mcmc_args["entropy_args"]``. Returns ------- history : list of tuples of the form ``(nattempts, nmoves, entropy)`` Summary of the MCMC run. This is returned only if ``history == True``. entropy : ``float`` Current entropy value after run. This is returned only if ``history == False``. nattempts : ``int`` Number of node move attempts. nmoves : ``int`` Number of node moves. See Also -------- mcmc_anneal: Simulated annealing with an MCMC minimize_blockmodel_dl: Inference of the stochastic block model BlockState: Stochastic block model References ---------- .. [peixoto-efficient-2014] Tiago P. Peixoto, "Efficient Monte Carlo and greedy heuristic for the inference of stochastic block models", Phys. Rev. E 89, 012804 (2014), :doi:`10.1103/PhysRevE.89.012804`, :arxiv:`1310.4378` """ count = 0 break_count = 0 niter = 0 total_nmoves = 0 total_nattempts = 0 S = state.entropy(**mcmc_args.get("entropy_args", {})) min_S = max_S = S m_eps = 1e-6 hist = [] while count < wait: if gibbs: delta, nattempts, nmoves = state.gibbs_sweep(**mcmc_args) elif multiflip and hasattr(state, "multiflip_mcmc_sweep"): delta, nattempts, nmoves = state.multiflip_mcmc_sweep(**mcmc_args) else: delta, nattempts, nmoves = state.mcmc_sweep(**mcmc_args) S += delta niter += 1 total_nmoves += nmoves total_nattempts += nattempts if force_niter is not None: max_S = max((S, max_S)) min_S = min((S, min_S)) if niter >= force_niter: break else: if abs(delta) >= (S - delta) * epsilon: if S > max_S + m_eps: max_S = S count = 0 elif S < min_S - m_eps: min_S = S count = 0 else: count += 1 else: count += 1 if count >= wait: break_count += 1 if break_count < nbreaks: count = 0 min_S = max_S = S extra = [] if callback is not None: extra = callback(state) if extra is None: extra = [] if check_verbose(verbose): print((verbose_pad(verbose) + u"niter: %5d count: %4d breaks: %2d min_S: %#8.8g " + u"max_S: %#8.8g S: %#8.8g ΔS: %#12.6g moves: %5d %s") % (niter, count, break_count, min_S, max_S, S, delta, nmoves, str(extra) if len(extra) > 0 else "")) if history: hist.append(tuple([nattempts, nmoves, S] + extra)) if niter >= max_niter: break if history: return hist else: return (S, total_nattempts, total_nmoves)
[docs] def mcmc_anneal(state, beta_range=(1., 10.), niter=100, history=False, mcmc_equilibrate_args={}, verbose=False): r"""Equilibrate a MCMC at a specified target temperature by performing simulated annealing. Parameters ---------- state : Any state class (e.g. :class:`~graph_tool.inference.BlockState`) Initial state. This state will be modified during the algorithm. beta_range : ``tuple`` of two floats (optional, default: ``(1., 10.)``) Inverse temperature range. niter : ``int`` (optional, default: ``100``) Number of steps (in logspace) from the starting temperature to the final one. history : ``bool`` (optional, default: ``False``) If ``True``, a list of tuples of the form ``(nattempts, nmoves, beta, entropy)`` will be kept and returned. mcmc_equilibrate_args : ``dict`` (optional, default: ``{}``) Arguments to be passed to :func:`~graph_tool.inference.mcmc_equilibrate`. verbose : ``bool`` or ``tuple`` (optional, default: ``False``) If ``True``, progress information will be shown. Optionally, this accepts arguments of the type ``tuple`` of the form ``(level, prefix)`` where ``level`` is a positive integer that specifies the level of detail, and ``prefix`` is a string that is prepended to all output messages. Notes ----- This algorithm employs exponential cooling, where the value of beta is multiplied by a constant at each iteration, so that starting from ``beta_range[0]`` the value of ``beta_range[1]`` is reached after ``niter`` iterations. At each iteration, the function :func:`~graph_tool.inference.mcmc_equilibrate` is called with the current value of ``beta`` (via the ``mcmc_args`` parameter). Returns ------- history : list of tuples of the form ``(nattempts, nmoves, beta, entropy)`` Summary of the MCMC run. This is returned only if ``history == True``. entropy : ``float`` Current entropy value after run. This is returned only if ``history == False``. nattempts : ``int`` Number of node move attempts. nmoves : ``int`` Number of node moves. See Also -------- mcmc_equilibrate: Equilibration of an MCMC minimize_blockmodel_dl: Inference of the stochastic block model BlockState: Stochastic block model References ---------- .. [peixoto-efficient-2014] Tiago P. Peixoto, "Efficient Monte Carlo and greedy heuristic for the inference of stochastic block models", Phys. Rev. E 89, 012804 (2014), :doi:`10.1103/PhysRevE.89.012804`, :arxiv:`1310.4378` """ beta = beta_range[0] hist = ([], [], [], []) nattempts = 0 nmoves = 0 speed = np.exp((np.log(beta_range[1]) - np.log(beta_range[0])) / niter) mcmc_args = mcmc_equilibrate_args.get("mcmc_args", {}) while beta < beta_range[1] * speed: ret = mcmc_equilibrate(state, **dict(mcmc_equilibrate_args, mcmc_args=dict(mcmc_args, beta=beta), history=history, verbose=verbose_push(verbose, ("β: %#8.6g " % beta)))) if history: ret = list(zip(*ret)) hist[0].extend(ret[0]) hist[1].extend(ret[1]) hist[2].extend([beta] * len(ret[0])) hist[3].extend(ret[2]) S = ret[2][-1] else: S = ret[0] nattempts += ret[1] nmoves += ret[2] beta *= speed if history: return list(zip(*hist)) else: return S, nattempts, nmoves