Commit aebfabed authored by Tiago Peixoto's avatar Tiago Peixoto

Add 'edges_only' options to UncertainBlockModel

parent 1d09752d
...@@ -38,11 +38,25 @@ template <class State> ...@@ -38,11 +38,25 @@ template <class State>
class SBMEdgeSampler class SBMEdgeSampler
{ {
public: public:
SBMEdgeSampler(State& state) SBMEdgeSampler(State& state, bool edges_only=false)
: _state(state), : _state(state),
_v_in_sampler(graph_tool::is_directed(state._g) ? _v_in_sampler(graph_tool::is_directed(state._g) ?
__v_in_sampler : _v_out_sampler) __v_in_sampler : _v_out_sampler),
_edges_only(edges_only)
{ {
_N = num_vertices(state._g);
for (auto e : edges_range(state._g))
{
size_t u = source(e, state._g);
size_t v = target(e, state._g);
_edges.push_back(get_edge(u, v));
_edge_pos[_edges.back()] = _edges.size() - 1;
}
if (_edges_only)
return;
for (auto me : edges_range(state._bg)) for (auto me : edges_range(state._bg))
{ {
auto r = source(me, state._bg); auto r = source(me, state._bg);
...@@ -87,15 +101,6 @@ public: ...@@ -87,15 +101,6 @@ public:
auto B = _groups.size(); auto B = _groups.size();
_NB = B * B; _NB = B * B;
_N = num_vertices(state._g);
for (auto e : edges_range(state._g))
{
size_t u = source(e, state._g);
size_t v = target(e, state._g);
_edges.push_back(get_edge(u, v));
_edge_pos[_edges.back()] = _edges.size() - 1;
}
} }
std::tuple<size_t, size_t> get_edge(size_t u, size_t v) std::tuple<size_t, size_t> get_edge(size_t u, size_t v)
...@@ -109,6 +114,9 @@ public: ...@@ -109,6 +114,9 @@ public:
template <bool add> template <bool add>
void update_edge(size_t u, size_t v, size_t m) void update_edge(size_t u, size_t v, size_t m)
{ {
if (_edges_only)
return;
if (add) if (add)
{ {
if (m == 0) if (m == 0)
...@@ -203,6 +211,22 @@ public: ...@@ -203,6 +211,22 @@ public:
// std::uniform_int_distribution<size_t> sample(0, _N-1); // std::uniform_int_distribution<size_t> sample(0, _N-1);
// return {sample(rng), sample(rng)}; // return {sample(rng), sample(rng)};
if (_edges_only)
{
std::bernoulli_distribution coin(_edges.size() /
double(_edges.size() + _N));
if (coin(rng))
{
return uniform_sample(_edges, rng);
}
else
{
std::uniform_int_distribution<size_t> vsample(0, _N-1);
auto v = vsample(rng);
return {v, v};
}
}
if (!_edges.empty()) if (!_edges.empty())
{ {
std::bernoulli_distribution coin(.5); std::bernoulli_distribution coin(.5);
...@@ -231,7 +255,8 @@ public: ...@@ -231,7 +255,8 @@ public:
double log_prob(size_t u, size_t v, size_t m, int delta) double log_prob(size_t u, size_t v, size_t m, int delta)
{ {
// return 0; if (_edges_only)
return 0;
auto& g = _state._g; auto& g = _state._g;
size_t r = _state._b[u]; size_t r = _state._b[u];
...@@ -344,6 +369,8 @@ private: ...@@ -344,6 +369,8 @@ private:
size_t _NB = 0; size_t _NB = 0;
size_t _E = 0; size_t _E = 0;
size_t _N = 0; size_t _N = 0;
bool _edges_only;
}; };
......
...@@ -39,6 +39,7 @@ typedef std::vector<size_t> vlist_t; ...@@ -39,6 +39,7 @@ typedef std::vector<size_t> vlist_t;
((state, &, State&, 0)) \ ((state, &, State&, 0)) \
((beta,, double, 0)) \ ((beta,, double, 0)) \
((entropy_args,, uentropy_args_t, 0)) \ ((entropy_args,, uentropy_args_t, 0)) \
((edges_only,, bool, 0)) \
((verbose,, bool, 0)) \ ((verbose,, bool, 0)) \
((niter,, size_t, 0)) ((niter,, size_t, 0))
...@@ -62,7 +63,7 @@ struct MCMC ...@@ -62,7 +63,7 @@ struct MCMC
sizeof...(Ts)>* = nullptr> sizeof...(Ts)>* = nullptr>
MCMCUncertainState(ATs&&... as) MCMCUncertainState(ATs&&... as)
: MCMCUncertainStateBase<Ts...>(as...), : MCMCUncertainStateBase<Ts...>(as...),
_edge_sampler(_state._block_state), _edge_sampler(_state._block_state, _edges_only),
_vlist(num_vertices(_state._u)) _vlist(num_vertices(_state._u))
{ {
} }
......
...@@ -159,6 +159,7 @@ class UncertainBaseState(object): ...@@ -159,6 +159,7 @@ class UncertainBaseState(object):
kwargs = kwargs.copy() kwargs = kwargs.copy()
beta = kwargs.get("beta", 1.) beta = kwargs.get("beta", 1.)
niter = kwargs.get("niter", 1) niter = kwargs.get("niter", 1)
edges_only = kwargs.pop("edges_only", False)
verbose = kwargs.get("verbose", False) verbose = kwargs.get("verbose", False)
slist = self.slist slist = self.slist
tlist = self.tlist tlist = self.tlist
......
Markdown is supported
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