This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
independence_cascade.py
54 lines (45 loc) · 1.57 KB
/
independence_cascade.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
This module defines functions relating to independence cascade.
Provided here are the independent cascade model by naive Monte-Carlo simulation
and the correlation robust influence maximisation cascade model
"""
# Python Standard library
from random import random, seed
from typing import Hashable
# packages
from igraph import Graph
from influence import det_inf_func
def ic_realisation(input_graph: Graph, randomness_seed: int) -> Graph:
"""Create a realisation out of an independence cascade model. Seeded."""
copy_graph = input_graph.copy()
seed(randomness_seed)
to_remove = [
(x[0], x[1]) for x in copy_graph.edges.data("weight") if x[2] < random()
]
copy_graph.remove_edges_from(to_remove)
return copy_graph
def mixic_realisation(
input_graph: Graph,
prob_mix: float,
p_1: float,
p_2: float,
randomness_seed: int = 1,
) -> Graph:
"""Return a mixture of two independent cascade models."""
copy_graph = input_graph.copy()
seed(randomness_seed)
to_remove = []
for edge in copy_graph.edges():
u_1 = random()
u_2 = random()
if u_1 > prob_mix:
if p_1 < u_2:
to_remove.append(edge)
else:
if p_2 < u_2:
to_remove.append(edge)
copy_graph.remove_edges_from(to_remove)
return copy_graph
def _calc_ic_inf(input_seed: int, input_graph: Graph, seed_set: list[Hashable]) -> int:
particular_realisation = ic_realisation(input_graph, randomness_seed=input_seed)
return len(det_inf_func(seed_set, particular_realisation))