graph_blockmodel_uncertain_util.hh 3.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_BLOCKMODEL_UNCERTAIN_UTIL_HH
#define GRAPH_BLOCKMODEL_UNCERTAIN_UTIL_HH

#include "config.h"

namespace graph_tool
{
using namespace boost;
using namespace std;

struct uentropy_args_t:
        public entropy_args_t
{
    uentropy_args_t(const entropy_args_t& ea)
        : entropy_args_t(ea){}
    bool latent_edges;
    bool density;
};

template <class T>
T logsum(T a, T b)
{
    return std::max(a, b) + log1p(exp(std::min(a, b) - std::max(a, b)));
}

template <class State, class... X>
34
double get_edge_prob(State& state, size_t u, size_t v, const uentropy_args_t& ea,
35
36
37
38
                     double epsilon, X... x)
{
    auto e = state.get_u_edge(u, v);
    size_t ew = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
39
    [[maybe_unused]] double old_x = 0;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    if (e != state._null_edge)
    {
        ew = state._eweight[e];
        if constexpr (sizeof...(X) > 0)
            old_x = state._xc[e];
    }

    for (size_t i = 0; i < ew; ++i)
        state.remove_edge(u, v);

    double S = 0;
    double delta = 1. + epsilon;
    size_t ne = 0;
    double L = -std::numeric_limits<double>::infinity();
    while (delta > epsilon || ne < 2)
    {
        double dS = state.add_edge_dS(u, v, x..., ea);
        state.add_edge(u, v, x...);
        S += dS;
        double old_L = L;
        L = logsum(L, -S);
        ne++;
        delta = abs(L-old_L);
    }

    L = (L > 0) ? -log1p(exp(-L)) : L - log1p(exp(L));

    for (int i = 0; i < int(ne - ew); ++i)
        state.remove_edge(u, v);
    for (int i = 0; i < int(ew - ne); ++i)
        if constexpr (sizeof...(X) > 0)
            state.add_edge(u, v, old_x);
        else
            state.add_edge(u, v);

    return L;
}

template <class State>
void get_edges_prob(State& state, python::object edges, python::object probs,
80
                    const uentropy_args_t& ea, double epsilon)
81
82
83
84
85
86
87
88
89
{
    multi_array_ref<uint64_t,2> es = get_array<uint64_t,2>(edges);
    multi_array_ref<double,1> eprobs = get_array<double,1>(probs);
    for (size_t i = 0; i < eprobs.shape()[0]; ++i)
        eprobs[i] = get_edge_prob(state, es[i][0], es[i][1], ea, epsilon);
}

template <class State>
void get_xedges_prob(State& state, python::object edges, python::object probs,
90
                    const uentropy_args_t& ea, double epsilon)
91
92
93
94
95
96
97
98
{
    multi_array_ref<double,2> es = get_array<double,2>(edges);
    multi_array_ref<double,1> eprobs = get_array<double,1>(probs);
    for (size_t i = 0; i < eprobs.shape()[0]; ++i)
        eprobs[i] = get_edge_prob(state, es[i][0], es[i][1], ea, epsilon,
                                  (es.shape()[1] > 2) ? es[i][2] : 0);
}

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
template <class State, class Graph, class EProp>
void set_state(State& state, Graph& u, EProp w)
{
    std::vector<std::pair<size_t, size_t>> us;
    for (auto v : vertices_range(state._u))
    {
        us.clear();
        for (auto e : out_edges_range(v, state._u))
        {
            auto w = target(e, state._u);
            if (w == v)
                continue;
            us.emplace_back(w, state._eweight[e]);
        }
        for (auto& uw : us)
        {
            for (size_t i = 0; i < uw.second; ++i)
                state.remove_edge(v, uw.first);
        }
        auto& e = state.template get_u_edge<false>(v, v);
        if (e == state._null_edge)
            continue;
        size_t x = state._eweight[e];
        for (size_t i = 0; i < x; ++i)
            state.remove_edge(v, v);
    }

    for (auto e : edges_range(u))
    {
        for (size_t i = 0; i < size_t(w[e]); ++i)
            state.add_edge(source(e, u), target(e, u));
    }
}

133
134
135
136

} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_UNCERTAIN_UTIL_HH