Commit 1c155fb7 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

inference: Improve partition update

parent 25200e72
...@@ -1169,14 +1169,20 @@ public: ...@@ -1169,14 +1169,20 @@ public:
if (r != null_group) if (r != null_group)
{ {
dS += vt(_mrp[r] - kout, _mrm[r] - kin, _wr[r] - dwr ); auto mrp_r = _mrp[r];
dS -= vt(_mrp[r] , _mrm[r] , _wr[r] ); auto mrm_r = _mrm[r];
auto wr_r = _wr[r];
dS += vt(mrp_r - kout, mrm_r - kin, wr_r - dwr);
dS -= vt(mrp_r , mrm_r , wr_r );
} }
if (nr != null_group) if (nr != null_group)
{ {
dS += vt(_mrp[nr] + kout, _mrm[nr] + kin, _wr[nr] + dwnr); auto mrp_nr = _mrp[nr];
dS -= vt(_mrp[nr] , _mrm[nr] , _wr[nr] ); auto mrm_nr = _mrm[nr];
auto wr_nr = _wr[nr];
dS += vt(mrp_nr + kout, mrm_nr + kin, wr_nr + dwnr);
dS -= vt(mrp_nr , mrm_nr , wr_nr );
} }
return dS; return dS;
...@@ -1198,11 +1204,6 @@ public: ...@@ -1198,11 +1204,6 @@ public:
if (r == nr) if (r == nr)
return 0; return 0;
int kin = 0, kout = 0;
kout += out_degreeS()(v, _g, _eweight);
if (is_directed::apply<g_t>::type::value)
kin += in_degreeS()(v, _g, _eweight);
vector<int> deltap(num_vertices(_bg), 0); vector<int> deltap(num_vertices(_bg), 0);
int deltal = 0; int deltal = 0;
for (auto e : out_edges_range(v, _g)) for (auto e : out_edges_range(v, _g))
...@@ -1969,10 +1970,17 @@ public: ...@@ -1969,10 +1970,17 @@ public:
double p = 0; double p = 0;
size_t w = 0; size_t w = 0;
size_t kout = out_degreeS()(v, _g, _eweight); size_t kout = 0, kin = 0;
size_t kin = kout; degs_op(v, _vweight, _eweight, _degs, _g,
if (is_directed::apply<g_t>::type::value) [&] (size_t din, size_t dout, auto c)
kin = in_degreeS()(v, _g, _eweight); {
kout += dout * c;
if (graph_tool::is_directed(this->_g))
kin += din * c;
});
if (!graph_tool::is_directed(_g))
kin = kout;
m_entries.get_mes(_emat); m_entries.get_mes(_emat);
auto sum_prob = [&](auto& e, auto u) auto sum_prob = [&](auto& e, auto u)
...@@ -2089,7 +2097,7 @@ public: ...@@ -2089,7 +2097,7 @@ public:
return S * _vweight[v]; return S * _vweight[v];
} }
double get_deg_entropy(size_t v, typename degs_map_t::unchecked_t& degs) double get_deg_entropy(size_t v, const typename degs_map_t::unchecked_t& degs)
{ {
if (_ignore_degrees[v] == 1) if (_ignore_degrees[v] == 1)
return 0; return 0;
......
...@@ -44,7 +44,7 @@ void degs_op(size_t v, Vprop& vweight, Eprop& eweight, const simple_degs_t&, ...@@ -44,7 +44,7 @@ void degs_op(size_t v, Vprop& vweight, Eprop& eweight, const simple_degs_t&,
template <class Graph, class Vprop, class Eprop, class F> template <class Graph, class Vprop, class Eprop, class F>
void degs_op(size_t v, Vprop& vweight, Eprop& eweight, void degs_op(size_t v, Vprop& vweight, Eprop& eweight,
typename degs_map_t::unchecked_t& degs, Graph& g, F&& f) const typename degs_map_t::unchecked_t& degs, Graph& g, F&& f)
{ {
auto& ks = degs[v]; auto& ks = degs[v];
if (ks.empty()) if (ks.empty())
...@@ -484,13 +484,40 @@ public: ...@@ -484,13 +484,40 @@ public:
void change_vertex(size_t v, size_t r, bool deg_corr, Graph& g, void change_vertex(size_t v, size_t r, bool deg_corr, Graph& g,
VWeight& vweight, EWeight& eweight, Degs& degs, VWeight& vweight, EWeight& eweight, Degs& degs,
int diff) int diff)
{
int vw = vweight[v];
int dv = vw * diff;
if (_total[r] == 0 && dv > 0)
_actual_B++;
if (_total[r] == vw && dv < 0)
_actual_B--;
_total[r] += dv;
_N += dv;
assert(_total[r] >= 0);
if (deg_corr && _ignore_degree[v] != 1)
{ {
degs_op(v, vweight, eweight, degs, g, degs_op(v, vweight, eweight, degs, g,
[&](auto kin, auto kout, auto n) [&](auto kin, auto kout, auto n)
{ {
this->change_k(v, r, deg_corr, n, kin, kout, diff); int dk = diff * n;
if (_ignore_degree[v] == 2)
kout = 0;
auto& h = _hist[r];
auto deg = make_pair(kin, kout);
auto iter = h.insert({deg, 0}).first;
iter->second += dk;
if (iter->second == 0)
h.erase(iter);
_em[r] += dk * deg.first;
_ep[r] += dk * deg.second;
}); });
} }
}
template <class Graph, class VWeight, class EWeight, class Degs> template <class Graph, class VWeight, class EWeight, class Degs>
void remove_vertex(size_t v, size_t r, bool deg_corr, Graph& g, void remove_vertex(size_t v, size_t r, bool deg_corr, Graph& g,
...@@ -537,6 +564,11 @@ public: ...@@ -537,6 +564,11 @@ public:
} }
} }
void change_E(int dE)
{
_E += dE;
}
size_t get_N() size_t get_N()
{ {
return _N; return _N;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment