diff --git a/src/graph/inference/uncertain/graph_blockmodel_measured.hh b/src/graph/inference/uncertain/graph_blockmodel_measured.hh index 4be76217f16e61b4d7acf8ef8ba43ddd97034777..2ba02e0f5ec557b98244c62eaefbe4c185dcc70b 100644 --- a/src/graph/inference/uncertain/graph_blockmodel_measured.hh +++ b/src/graph/inference/uncertain/graph_blockmodel_measured.hh @@ -161,6 +161,19 @@ struct Measured return _get_edge(u, v, _g, _edges); } + double get_MP(size_t T, size_t M, bool complete = true) + { + double S = 0; + S += lbeta(M - T + _alpha, T + _beta); + S += lbeta(_X - T + _mu, _N - _X - (M - T) + _nu); + if (complete) + { + S -= lbeta(_alpha, _beta); + S -= lbeta(_mu, _nu); + } + return S; + } + double entropy() { double S = 0; @@ -172,8 +185,7 @@ struct Measured } S += (_NP - gE) * lbinom(_n_default, _x_default); - S += lbeta(_T + _alpha, _M - _T + _beta) - lbeta(_alpha, _beta); - S += lbeta(_X - _T + _mu, _N - _X - (_M - _T) + _nu) - lbeta(_mu, _nu); + S+= get_MP(_T, _M); if (_E_prior) S += _E * _pe - lgamma_fast(_E + 1) - exp(_pe); @@ -183,14 +195,9 @@ struct Measured double get_dS(int dT, int dM) { - size_t nT = _T + dT; - size_t nM = _M + dM; - // FIXME: Can be faster! - double Si = (lbeta(_T + _alpha, _M - _T + _beta) + - lbeta(_X - _T + _mu, _N - _X - (_M - _T) + _nu)); - double Sf = (lbeta(nT + _alpha, nM - nT + _beta) + - lbeta(_X - nT + _mu, _N - _X - (nM - nT) + _nu)); + double Si = get_MP(_T, _M, false); + double Sf = get_MP(_T + dT, _M + dM, false); return -(Sf - Si); } diff --git a/src/graph_tool/inference/uncertain_blockmodel.py b/src/graph_tool/inference/uncertain_blockmodel.py index b2db658f0645d7195cbb7ee764e1eba2df250b83..73f2bde60c6e3e02531d3c3695ed774a9bb7e49b 100644 --- a/src/graph_tool/inference/uncertain_blockmodel.py +++ b/src/graph_tool/inference/uncertain_blockmodel.py @@ -83,7 +83,7 @@ class UncertainBaseState(object): self.nbstate = None self.bstate = bstate - edges = self.u.get_edges() + edges = self.g.get_edges() edges = numpy.concatenate((edges, numpy.ones(edges.shape, dtype=edges.dtype) * (N + 1))) @@ -366,8 +366,8 @@ class MeasuredBlockState(UncertainBaseState): x_default=self.x_default, fp_params=dict(alpha=self.alpha, beta=self.beta), fn_params=dict(mu=self.mu, nu=self.nu), phi=self.phi, - bstate=(self.nbstate.copy() if self.nbstate is not None - else self.bstate.copy()), self_loops=self.self_loops) + bstate=(self.nbstate if self.nbstate is not None + else self.bstate), self_loops=self.self_loops) def __setstate__(self, state): self.__init__(**state) @@ -398,7 +398,7 @@ class MeasuredBlockState(UncertainBaseState): """Get gamma distribution parameters for the posterior probability of missing edges.""" T = self._state.get_T() M = self._state.get_M() - return T + self.alpha, M - T + self.beta + return M - T + self.alpha, M + self.beta def get_q_posterior(self): """Get gamma distribution parameters for the posterior probability of spurious edges."""