InfluenceDiffusion 0.0.13__tar.gz → 0.0.15__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- influencediffusion-0.0.15/InfluenceDiffusion/Inference.py +116 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/Trace.py +4 -4
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/__init__.py +2 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/estimation_models/BaseWeightEstimator.py +6 -6
- influencediffusion-0.0.15/InfluenceDiffusion/estimation_models/CDFEstimation.py +73 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/estimation_models/OptimEstimation.py +89 -13
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/influence_models.py +3 -0
- influencediffusion-0.0.15/InfluenceDiffusion/plot_utils.py +94 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/utils.py +15 -2
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/PKG-INFO +39 -23
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/SOURCES.txt +3 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/requires.txt +1 -1
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/PKG-INFO +39 -23
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/README.md +27 -20
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/setup.py +2 -2
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/Graph.py +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/estimation_models/EMEstimation.py +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/estimation_models/__init__.py +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/weight_samplers.py +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/dependency_links.txt +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/top_level.txt +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/LICENSE +0 -0
- {influencediffusion-0.0.13 → influencediffusion-0.0.15}/setup.cfg +0 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import jax.numpy as jnp
|
|
3
|
+
import numpy as np
|
|
4
|
+
from typing import Dict, Callable
|
|
5
|
+
from functools import partial
|
|
6
|
+
from scipy.stats import norm
|
|
7
|
+
|
|
8
|
+
from .estimation_models.OptimEstimation import GLTWeightEstimator
|
|
9
|
+
from .utils import make_jax_cdf
|
|
10
|
+
|
|
11
|
+
__all__ = ["GLTInferenceModule"]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GLTInferenceModule:
|
|
15
|
+
def __init__(self, estimator: GLTWeightEstimator, vertex_2_jax_cdf: Dict[int, Callable] = None):
|
|
16
|
+
self.estimator = estimator
|
|
17
|
+
if vertex_2_jax_cdf is None:
|
|
18
|
+
self.vertex_2_jax_cdf = {vertex: make_jax_cdf(distrib)
|
|
19
|
+
for vertex, distrib in self.estimator.vertex_2_distrib.items()}
|
|
20
|
+
else:
|
|
21
|
+
self.vertex_2_jax_cdf = vertex_2_jax_cdf
|
|
22
|
+
self.vertex_2_parent_weight_cov = {}
|
|
23
|
+
|
|
24
|
+
def _get_vertex_activ_status_and_masks(self, vertex: int):
|
|
25
|
+
n_parents = self.estimator.graph.get_indegree(vertex)
|
|
26
|
+
if vertex in self.estimator._vertex_2_active_parent_mask_t:
|
|
27
|
+
t_active_masks = self.estimator._vertex_2_active_parent_mask_t[vertex]
|
|
28
|
+
tm1_active_masks = self.estimator._vertex_2_active_parent_mask_tm1[vertex]
|
|
29
|
+
else:
|
|
30
|
+
t_active_masks = jnp.empty(shape=(0, n_parents))
|
|
31
|
+
tm1_active_masks = jnp.empty(shape=(0, n_parents))
|
|
32
|
+
|
|
33
|
+
if vertex in self.estimator._failed_vertices_masks:
|
|
34
|
+
t_failed_masks = self.estimator._failed_vertices_masks[vertex]
|
|
35
|
+
tm1_failed_masks = jnp.zeros_like(t_failed_masks)
|
|
36
|
+
else:
|
|
37
|
+
t_failed_masks = jnp.empty(shape=(0, n_parents))
|
|
38
|
+
tm1_failed_masks = jnp.empty(shape=(0, n_parents))
|
|
39
|
+
ys = jnp.array([True] * len(t_active_masks) + [False] * len(t_failed_masks), dtype=bool)
|
|
40
|
+
masks_t = jnp.vstack([t_active_masks, t_failed_masks], dtype=jnp.float32)
|
|
41
|
+
masks_tm1 = jnp.vstack([tm1_active_masks, tm1_failed_masks], dtype=jnp.float32)
|
|
42
|
+
return ys, masks_t, masks_tm1
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def _vertex_ll(parent_weights, cdf, ys, masks_t, masks_tm1, eps=1e-6):
|
|
46
|
+
F_t = cdf(masks_t @ parent_weights)
|
|
47
|
+
F_tm1 = cdf(masks_tm1 @ parent_weights)
|
|
48
|
+
F_tm1 = jnp.where(F_tm1 < eps, 0, F_tm1)
|
|
49
|
+
F_t = jnp.where(F_t > 1. - eps, 1., F_t)
|
|
50
|
+
prob = jnp.clip(F_t - F_tm1, eps, 1. - eps)
|
|
51
|
+
return jnp.sum(jnp.log(jnp.where(ys, prob, 1. - prob)))
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def _vertex_conditional_activ_prob(weights, cdf, mask_t, mask_tm1, eps=1e-6):
|
|
55
|
+
F_t = cdf(mask_t @ weights)
|
|
56
|
+
F_tm1 = cdf(mask_tm1 @ weights)
|
|
57
|
+
F_t = jnp.clip(F_t, eps, 1. - eps)
|
|
58
|
+
F_tm1 = jnp.clip(F_tm1, eps, 1. - eps)
|
|
59
|
+
return 1. - (1. - F_t) / (1. - F_tm1)
|
|
60
|
+
|
|
61
|
+
def compute_vertex_conditional_activ_probs(self, vertex: int, masks_t, masks_tm1):
|
|
62
|
+
cdf = self.vertex_2_jax_cdf[vertex]
|
|
63
|
+
parent_mask = self.estimator.graph.get_parents_mask(vertex)
|
|
64
|
+
weights = jnp.array(self.estimator.weights_[parent_mask], dtype=jnp.float32)
|
|
65
|
+
return self._vertex_conditional_activ_prob(weights=weights, cdf=cdf, mask_t=masks_t, mask_tm1=masks_tm1)
|
|
66
|
+
|
|
67
|
+
def compute_vertex_parent_weight_cov(self, vertex, fisher_eps=1e-6):
|
|
68
|
+
if vertex in self.vertex_2_parent_weight_cov:
|
|
69
|
+
return self.vertex_2_parent_weight_cov[vertex]
|
|
70
|
+
cdf = self.vertex_2_jax_cdf[vertex]
|
|
71
|
+
parent_mask = self.estimator.graph.get_parents_mask(vertex)
|
|
72
|
+
weights = jnp.array(self.estimator.weights_[parent_mask], dtype=jnp.float32)
|
|
73
|
+
ys, masks_t, masks_tm1 = self._get_vertex_activ_status_and_masks(vertex=vertex)
|
|
74
|
+
hessian_fun = jax.hessian(partial(self._vertex_ll, cdf=cdf, ys=ys,
|
|
75
|
+
masks_t=masks_t, masks_tm1=masks_tm1))
|
|
76
|
+
fisher_info = -hessian_fun(weights)
|
|
77
|
+
cov = jnp.linalg.inv(fisher_info + fisher_eps * jnp.eye(len(fisher_info)))
|
|
78
|
+
self.vertex_2_parent_weight_cov[vertex] = cov
|
|
79
|
+
return cov
|
|
80
|
+
|
|
81
|
+
def compute_vertex_2_parent_weight_cov_dict(self):
|
|
82
|
+
return {vertex: self.compute_vertex_parent_weight_cov(vertex)
|
|
83
|
+
for vertex in self.estimator.informative_vertices}
|
|
84
|
+
|
|
85
|
+
def compute_parent_weight_conf_ints(self, vertex: int, alpha: float = 0.05):
|
|
86
|
+
weights = self.estimator.weights_[self.estimator.graph.get_parents_mask(vertex)]
|
|
87
|
+
lb, ub = self.estimator.vertex_2_distrib[vertex].support()
|
|
88
|
+
cov = self.compute_vertex_parent_weight_cov(vertex=vertex)
|
|
89
|
+
stds = jnp.sqrt(jnp.diag(cov))
|
|
90
|
+
quantile = norm.ppf(1 - alpha / 2)
|
|
91
|
+
return jnp.stack([jnp.clip(weights - quantile * stds, lb, None),
|
|
92
|
+
jnp.clip(weights + quantile * stds, None, ub)]).T
|
|
93
|
+
|
|
94
|
+
def compute_all_weight_conf_ints(self, alpha=0.05):
|
|
95
|
+
conf_ints = np.empty(shape=(self.estimator.graph.count_edges(), 2), dtype=np.float32)
|
|
96
|
+
for vertex in self.estimator.informative_vertices:
|
|
97
|
+
parent_conf_ints = self.compute_parent_weight_conf_ints(vertex=vertex, alpha=alpha)
|
|
98
|
+
mask = self.estimator.graph.get_parents_mask(vertex)
|
|
99
|
+
conf_ints[mask] = parent_conf_ints
|
|
100
|
+
return conf_ints
|
|
101
|
+
|
|
102
|
+
def compute_vertex_activation_prob_conf_ints(self, vertex: int, masks_t, masks_tm1, alpha=0.05):
|
|
103
|
+
cdf = self.vertex_2_jax_cdf[vertex]
|
|
104
|
+
weights = self.estimator.weights_[self.estimator.graph.get_parents_mask(vertex)]
|
|
105
|
+
cov = self.compute_vertex_parent_weight_cov(vertex=vertex)
|
|
106
|
+
quantile = norm.ppf(1 - alpha / 2)
|
|
107
|
+
conf_ints = []
|
|
108
|
+
for mask_t, mask_tm1 in zip(masks_t, masks_tm1):
|
|
109
|
+
prob, prob_grad = jax.value_and_grad(partial(self._vertex_conditional_activ_prob,
|
|
110
|
+
cdf=cdf, mask_t=mask_t, mask_tm1=mask_tm1))(weights)
|
|
111
|
+
prob_grad = prob_grad.reshape((-1, 1))
|
|
112
|
+
std = jnp.sqrt(prob_grad.T @ cov @ prob_grad)
|
|
113
|
+
conf_ints.append([jnp.clip(prob - quantile * std, 0., None).item(),
|
|
114
|
+
jnp.clip(prob + quantile * std, None, 1.).item()])
|
|
115
|
+
|
|
116
|
+
return np.stack(conf_ints)
|
|
@@ -78,10 +78,10 @@ class Trace(tuple):
|
|
|
78
78
|
if check_feasibility:
|
|
79
79
|
self.make_feasibility_check()
|
|
80
80
|
|
|
81
|
-
self.__cumulative_trace
|
|
82
|
-
self.__failed_vertices
|
|
83
|
-
self.__activated_vertices
|
|
84
|
-
self.__activation_time_dict
|
|
81
|
+
self.__cumulative_trace = None
|
|
82
|
+
self.__failed_vertices = None
|
|
83
|
+
self.__activated_vertices = None
|
|
84
|
+
self.__activation_time_dict = None
|
|
85
85
|
|
|
86
86
|
def make_feasibility_check(self) -> None:
|
|
87
87
|
"""Check if each vertex has at least one active parent at each time step."""
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
from .Graph import Graph
|
|
2
2
|
from .influence_models import InfluenceModel, ICM, LTM, GLTM
|
|
3
3
|
from .Trace import Trace, Traces, PseudoTraces
|
|
4
|
+
from .Inference import GLTInferenceModule
|
|
4
5
|
from .utils import invert_non_zeros, multiple_union, random_vector_inside_simplex, random_vector_on_simplex
|
|
6
|
+
from .plot_utils import plot_with_conf_intervals, plot_hist_with_normal_fit
|
|
5
7
|
from .weight_samplers import make_weighted_cascade_weights, make_random_weights_with_fixed_indeg, \
|
|
6
8
|
make_random_weights_with_indeg_constraint
|
|
@@ -8,7 +8,7 @@ from scipy.stats._distn_infrastructure import rv_frozen
|
|
|
8
8
|
|
|
9
9
|
from ..Trace import Traces, PseudoTraces
|
|
10
10
|
from ..Graph import Graph
|
|
11
|
-
from ..utils import multiple_union
|
|
11
|
+
from ..utils import multiple_union, random_vector_inside_simplex
|
|
12
12
|
|
|
13
13
|
__all__ = ["BaseWeightEstimator", "BaseGLTWEightEstimator", "BaseICWEightEstimator"]
|
|
14
14
|
|
|
@@ -361,10 +361,12 @@ class BaseGLTWEightEstimator(BaseWeightEstimator):
|
|
|
361
361
|
"""
|
|
362
362
|
|
|
363
363
|
def __init__(self, graph: Graph, n_jobs: int = None,
|
|
364
|
-
vertex_2_distrib: Dict[int, rv_frozen] = None):
|
|
364
|
+
vertex_2_distrib: Union[Dict[int, rv_frozen], rv_frozen] = None):
|
|
365
365
|
super().__init__(graph, n_jobs)
|
|
366
366
|
if vertex_2_distrib is None:
|
|
367
367
|
vertex_2_distrib = {v: uniform(0, 1) for v in graph.get_vertices()}
|
|
368
|
+
elif isinstance(vertex_2_distrib, rv_frozen):
|
|
369
|
+
vertex_2_distrib = {v: vertex_2_distrib for v in graph.get_vertices()}
|
|
368
370
|
self.vertex_2_distrib = vertex_2_distrib
|
|
369
371
|
|
|
370
372
|
def _generate_random_weights(self) -> np.ndarray:
|
|
@@ -380,10 +382,8 @@ class BaseGLTWEightEstimator(BaseWeightEstimator):
|
|
|
380
382
|
parent_mask = self.graph.get_parents_mask(vertex)
|
|
381
383
|
num_parents = self.graph.get_indegree(vertex, weighted=False)
|
|
382
384
|
support_ub = self.vertex_2_distrib[vertex].support()[1]
|
|
383
|
-
if support_ub == np.inf
|
|
384
|
-
|
|
385
|
-
else:
|
|
386
|
-
weights[parent_mask] = np.random.rand(num_parents) * support_ub / num_parents
|
|
385
|
+
support_ub = 1 if support_ub == np.inf else support_ub
|
|
386
|
+
weights[parent_mask] = random_vector_inside_simplex(num_parents, ub=support_ub)
|
|
387
387
|
return weights
|
|
388
388
|
|
|
389
389
|
def _check_init_weight_correctness(self, init_weights: Union[List, np.ndarray], eps: float = 1e-6) -> None:
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from typing import List, Tuple, Any
|
|
2
|
+
from scipy.stats._distn_infrastructure import rv_continuous
|
|
3
|
+
from scipy.interpolate import interp1d
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CensoredCDFEstimator(rv_continuous):
|
|
8
|
+
def __init__(self, support: Tuple[float, float] = (-np.inf, np.inf),
|
|
9
|
+
momtype=1,
|
|
10
|
+
a=None,
|
|
11
|
+
b=None,
|
|
12
|
+
xtol=1e-14,
|
|
13
|
+
badvalue=None,
|
|
14
|
+
name=None,
|
|
15
|
+
longname=None,
|
|
16
|
+
shapes=None,
|
|
17
|
+
extradoc=None,
|
|
18
|
+
seed=None):
|
|
19
|
+
|
|
20
|
+
super().__init__(momtype=momtype, a=a, b=b, xtol=xtol, badvalue=badvalue,
|
|
21
|
+
name=name, longname=longname, shapes=shapes, seed=seed)
|
|
22
|
+
self.support_ = support
|
|
23
|
+
|
|
24
|
+
def fit(self, intervals: List[Tuple[float, float]],
|
|
25
|
+
max_iter=50, tol=1e-4, verbose=False, verbose_interval=1):
|
|
26
|
+
|
|
27
|
+
self.qp_ints_ = self._extract_disjoint_intervals(intervals)
|
|
28
|
+
|
|
29
|
+
alphas = np.fromfunction(
|
|
30
|
+
np.vectorize(lambda i, j: self._if_sub_interval(intervals[i], self.qp_ints_[j])),
|
|
31
|
+
shape=(len(intervals), len(self.qp_ints_)), dtype=int)
|
|
32
|
+
|
|
33
|
+
self.qp_probs_ = np.ones(len(self.qp_ints_)) / len(self.qp_ints_)
|
|
34
|
+
|
|
35
|
+
for iteration in range(max_iter):
|
|
36
|
+
cur_probs = self.qp_probs_.copy()
|
|
37
|
+
ms = (alphas * cur_probs) / (alphas @ cur_probs).reshape(-1, 1)
|
|
38
|
+
self.qp_probs_ = ms.sum(0) / ms.sum()
|
|
39
|
+
diff = np.linalg.norm(cur_probs - self.qp_probs_)
|
|
40
|
+
if verbose and iteration % verbose_interval == 0:
|
|
41
|
+
print(f"Iteration: {iteration}, Probs diff l2-norm: {round(diff, int(2 - np.log10(tol)))}")
|
|
42
|
+
if diff < tol:
|
|
43
|
+
break
|
|
44
|
+
|
|
45
|
+
if len(self.qp_ints_[:, 0]) >= 2:
|
|
46
|
+
self._cdf_interpolator = interp1d(self.qp_ints_[:, 0], np.cumsum(self.qp_probs_),
|
|
47
|
+
bounds_error=False, fill_value=(0.0, 1.0))
|
|
48
|
+
else:
|
|
49
|
+
self._cdf_interpolator = lambda x: np.clip(x, 0, 1)
|
|
50
|
+
|
|
51
|
+
return self
|
|
52
|
+
|
|
53
|
+
def _cdf(self, x: Any, *args, **kwargs):
|
|
54
|
+
return self._cdf_interpolator(x)
|
|
55
|
+
|
|
56
|
+
def support(self):
|
|
57
|
+
return self.support_
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def _if_sub_interval(interval1: Tuple[float, float], interval2: Tuple[float, float]):
|
|
61
|
+
return (interval1[0] <= interval2[0]) and (interval1[1] >= interval2[1])
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def _extract_disjoint_intervals(intervals: List[Tuple[float, float]]):
|
|
65
|
+
assert len(intervals) > 0, "At least one interval should be provided"
|
|
66
|
+
assert all(interval[0] <= interval[1] for interval in intervals)
|
|
67
|
+
lefts, rights = zip(*intervals)
|
|
68
|
+
sort_endpoints = sorted([(left, "L") for left in lefts] + [(right, "R") for right in rights])
|
|
69
|
+
disjoint_intervals = []
|
|
70
|
+
for (ep, next_ep) in zip(sort_endpoints[:-1], sort_endpoints[1:]):
|
|
71
|
+
if ep[1] == "L" and next_ep[1] == "R":
|
|
72
|
+
disjoint_intervals.append((ep[0], next_ep[0]))
|
|
73
|
+
return np.array(disjoint_intervals)
|
|
@@ -2,22 +2,17 @@ import numpy as np
|
|
|
2
2
|
from typing import List, Union, Dict, Tuple
|
|
3
3
|
from scipy.optimize import LinearConstraint, minimize
|
|
4
4
|
from scipy.stats._distn_infrastructure import rv_frozen
|
|
5
|
+
from copy import copy
|
|
5
6
|
|
|
6
7
|
from ..Graph import Graph
|
|
7
8
|
from ..Trace import Traces, PseudoTraces
|
|
8
9
|
from .BaseWeightEstimator import BaseGLTWEightEstimator
|
|
10
|
+
from .CDFEstimation import CensoredCDFEstimator
|
|
9
11
|
|
|
10
|
-
__all__ = ["GLTGridSearchEstimator", "GLTWeightEstimator"]
|
|
12
|
+
__all__ = ["GLTGridSearchEstimator", "GLTWeightEstimator", "GLTWeightDistribEstimator"]
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
class GLTWeightEstimator(BaseGLTWEightEstimator):
|
|
14
|
-
"""GLTW Weight Estimator for modeling influence in networks.
|
|
15
|
-
|
|
16
|
-
Attributes
|
|
17
|
-
----------
|
|
18
|
-
vertex_2_distrib : Dict[int, rv_frozen]
|
|
19
|
-
Distribution mapping for vertices.
|
|
20
|
-
"""
|
|
21
16
|
|
|
22
17
|
def _compute_failed_vertex_ll(self, weights: np.ndarray, vertex: int) -> float:
|
|
23
18
|
"""Compute the log-likelihood for failed vertices.
|
|
@@ -99,8 +94,8 @@ class GLTWeightEstimator(BaseGLTWEightEstimator):
|
|
|
99
94
|
"""
|
|
100
95
|
return np.sum([
|
|
101
96
|
self._compute_vertex_nll(weights[self.graph.get_parents_mask(vertex)], vertex)
|
|
102
|
-
for vertex in self.informative_vertices
|
|
103
|
-
|
|
97
|
+
for vertex in self.informative_vertices]
|
|
98
|
+
)
|
|
104
99
|
|
|
105
100
|
def _compute_normalized_vertex_nll(self, weights: np.ndarray, vertex: int) -> float:
|
|
106
101
|
"""Compute the normalized negative log-likelihood for a vertex.
|
|
@@ -149,7 +144,8 @@ class GLTWeightEstimator(BaseGLTWEightEstimator):
|
|
|
149
144
|
for vertex in self.informative_vertices
|
|
150
145
|
}
|
|
151
146
|
|
|
152
|
-
def _optimize_vertex_parent_params(self, vertex: int, optimization_kwargs: Dict = None) ->
|
|
147
|
+
def _optimize_vertex_parent_params(self, vertex: int, optimization_kwargs: Dict = None) -> \
|
|
148
|
+
Tuple[np.ndarray, rv_frozen]:
|
|
153
149
|
"""Optimize parameters for a specific vertex.
|
|
154
150
|
|
|
155
151
|
Parameters
|
|
@@ -173,7 +169,8 @@ class GLTWeightEstimator(BaseGLTWEightEstimator):
|
|
|
173
169
|
)
|
|
174
170
|
return optimizer_output.x, self.vertex_2_distrib[vertex]
|
|
175
171
|
|
|
176
|
-
def _set_informative_vertices_parent_params(self, informative_vertices_params: List[Tuple[np.ndarray, rv_frozen]])
|
|
172
|
+
def _set_informative_vertices_parent_params(self, informative_vertices_params: List[Tuple[np.ndarray, rv_frozen]]) \
|
|
173
|
+
-> None:
|
|
177
174
|
"""Set the optimized parameters for informative vertices.
|
|
178
175
|
|
|
179
176
|
Parameters
|
|
@@ -257,7 +254,8 @@ class GLTGridSearchEstimator(GLTWeightEstimator):
|
|
|
257
254
|
for vertex_distribs in self.vertex_2_distrib_grid.values()
|
|
258
255
|
]), "All elements of the grid should be `rv_frozen`."
|
|
259
256
|
|
|
260
|
-
def _optimize_vertex_parent_params(self, vertex: int, optimization_kwargs: Dict = None) ->
|
|
257
|
+
def _optimize_vertex_parent_params(self, vertex: int, optimization_kwargs: Dict = None) -> \
|
|
258
|
+
Tuple[np.ndarray, rv_frozen]:
|
|
261
259
|
"""Optimize parameters for a specific vertex using grid search.
|
|
262
260
|
|
|
263
261
|
Parameters
|
|
@@ -292,3 +290,81 @@ class GLTGridSearchEstimator(GLTWeightEstimator):
|
|
|
292
290
|
best_nll = nll
|
|
293
291
|
|
|
294
292
|
return best_weights, best_distrib
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
class GLTWeightDistribEstimator(GLTWeightEstimator):
|
|
296
|
+
"""
|
|
297
|
+
Estimator of weights and vertex threshold distributions under the GLT diffusion model
|
|
298
|
+
"""
|
|
299
|
+
|
|
300
|
+
def __init__(self, graph: Graph,
|
|
301
|
+
threshold_support: Tuple[float, float] = (0, np.inf),
|
|
302
|
+
n_jobs: int = 1) -> None:
|
|
303
|
+
|
|
304
|
+
"""Initialize the GLTWeightDistribEstimator.
|
|
305
|
+
|
|
306
|
+
Parameters
|
|
307
|
+
----------
|
|
308
|
+
graph : Graph
|
|
309
|
+
The graph structure.
|
|
310
|
+
n_jobs : int, optional
|
|
311
|
+
Number of jobs for parallel processing.
|
|
312
|
+
"""
|
|
313
|
+
super().__init__(graph=graph, vertex_2_distrib=None, n_jobs=n_jobs)
|
|
314
|
+
self.threshold_support = threshold_support
|
|
315
|
+
|
|
316
|
+
def _construct_vertex_threshold_observed_intervals(self, vertex: int, parent_weights: np.array):
|
|
317
|
+
intervals = []
|
|
318
|
+
for mask_tm1, mask_t in zip(self._vertex_2_active_parent_mask_tm1[vertex],
|
|
319
|
+
self._vertex_2_active_parent_mask_t[vertex]):
|
|
320
|
+
lb = parent_weights[mask_tm1].sum()
|
|
321
|
+
ub = parent_weights[mask_t].sum()
|
|
322
|
+
intervals.append((lb, ub))
|
|
323
|
+
|
|
324
|
+
for failed_mask in self._failed_vertices_masks[vertex]:
|
|
325
|
+
lb = parent_weights[failed_mask].sum()
|
|
326
|
+
intervals.append((lb, np.inf))
|
|
327
|
+
return intervals
|
|
328
|
+
|
|
329
|
+
def _optimize_vertex_parent_params(self, vertex: int,
|
|
330
|
+
max_alternate_iter=2, tol=1e-5,
|
|
331
|
+
verbose=False,
|
|
332
|
+
optimization_kwargs: Dict = None) -> Tuple[np.ndarray, rv_frozen]:
|
|
333
|
+
"""Optimize parameters for a specific vertex by iteratively estimating the weights and vertex threshold CDF.
|
|
334
|
+
|
|
335
|
+
Parameters
|
|
336
|
+
----------
|
|
337
|
+
vertex : int
|
|
338
|
+
The vertex to optimize.
|
|
339
|
+
max_alternate_iter: int
|
|
340
|
+
Max number of weight & CDF estimation steps
|
|
341
|
+
verbose: bool
|
|
342
|
+
If verbose the NLL after iteration.
|
|
343
|
+
optimization_kwargs : Dict, optional
|
|
344
|
+
Additional optimization parameters.
|
|
345
|
+
|
|
346
|
+
Returns
|
|
347
|
+
-------
|
|
348
|
+
Tuple[np.ndarray, rv_frozen]
|
|
349
|
+
Weights and vertex threshold empirical CDF.
|
|
350
|
+
"""
|
|
351
|
+
parent_weights = self.weights_[self.graph.get_parents_mask(vertex)].copy()
|
|
352
|
+
for iteration in range(max_alternate_iter):
|
|
353
|
+
|
|
354
|
+
cdf_fit_intervals = self._construct_vertex_threshold_observed_intervals(vertex, parent_weights)
|
|
355
|
+
distrib = CensoredCDFEstimator(support=self.threshold_support)
|
|
356
|
+
self.vertex_2_distrib[vertex] = copy(distrib.fit(intervals=cdf_fit_intervals))
|
|
357
|
+
optimizer_output = minimize(
|
|
358
|
+
lambda weights: self._compute_normalized_vertex_nll(weights, vertex=vertex),
|
|
359
|
+
x0=parent_weights,
|
|
360
|
+
method='SLSQP',
|
|
361
|
+
constraints=self._weight_constraints[vertex],
|
|
362
|
+
options=optimization_kwargs)
|
|
363
|
+
diff = np.linalg.norm(optimizer_output.x - parent_weights)
|
|
364
|
+
parent_weights = optimizer_output.x.copy()
|
|
365
|
+
if verbose:
|
|
366
|
+
print(f"Alternating Iteration: {iteration}, l2-norm wights diff: {diff}")
|
|
367
|
+
if diff < tol:
|
|
368
|
+
break
|
|
369
|
+
|
|
370
|
+
return parent_weights, self.vertex_2_distrib[vertex]
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
__all__ = ["plot_with_conf_intervals", "plot_hist_with_normal_fit"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def plot_with_conf_intervals(x_true, x_pred, conf_intervals=None,
|
|
9
|
+
fontsize=12, figsize=(10, 8), color="blue", ax=None,
|
|
10
|
+
xlab="True activation probability",
|
|
11
|
+
ylab="Predicted activation probability"):
|
|
12
|
+
"""
|
|
13
|
+
Plot a scatter plot of `x_true` vs `x_pred` with confidence intervals as a filled area
|
|
14
|
+
and a diagonal line y=x.
|
|
15
|
+
|
|
16
|
+
:param x_true: Array-like, true values.
|
|
17
|
+
:param x_pred: Array-like, predicted values.
|
|
18
|
+
:param conf_intervals: 2D array of shape (2, len(x_pred)),
|
|
19
|
+
containing the lower and upper bounds of the confidence intervals.
|
|
20
|
+
If None, no confidence intervals are plotted.
|
|
21
|
+
:param fontsize: int, font size for axis labels. Default is 12.
|
|
22
|
+
:param figsize: tuple, size of the figure (width, height) in inches. Default is (10, 8).
|
|
23
|
+
:param color: str or tuple, color of the scatter points and confidence interval area. Default is "blue".
|
|
24
|
+
:param ax: matplotlib.axes.Axes, optional existing axes to plot on.
|
|
25
|
+
If None, a new figure and axes are created.
|
|
26
|
+
:param xlab: str, label for the x-axis. Default is "True activation probability".
|
|
27
|
+
:param ylab: str, label for the y-axis. Default is "Predicted activation probability".
|
|
28
|
+
"""
|
|
29
|
+
assert len(x_pred) == len(x_true), "x_pred and x_true must have the same length"
|
|
30
|
+
assert conf_intervals.shape[1] == len(x_pred), "conf_intervals must have the same second dim as x_pred"
|
|
31
|
+
assert conf_intervals.shape[0] == 2, "conf_intervals must have two rows for lower and upper bounds"
|
|
32
|
+
|
|
33
|
+
# Sort by x_true so fill_between works correctly
|
|
34
|
+
sort_idx = np.argsort(x_true)
|
|
35
|
+
x_true_sorted = x_true[sort_idx]
|
|
36
|
+
x_pred_sorted = x_pred[sort_idx]
|
|
37
|
+
|
|
38
|
+
if ax is None:
|
|
39
|
+
fig, ax = plt.subplots(figsize=figsize)
|
|
40
|
+
|
|
41
|
+
# Scatter plot
|
|
42
|
+
ax.scatter(x_true_sorted, x_pred_sorted, color=color)
|
|
43
|
+
|
|
44
|
+
# Filled confidence intervals
|
|
45
|
+
if conf_intervals is not None:
|
|
46
|
+
conf_lower_sorted = conf_intervals[0, sort_idx]
|
|
47
|
+
conf_upper_sorted = conf_intervals[1, sort_idx]
|
|
48
|
+
ax.fill_between(x_true_sorted, conf_lower_sorted, conf_upper_sorted,
|
|
49
|
+
color=color, alpha=0.2)
|
|
50
|
+
|
|
51
|
+
# Diagonal line y=x
|
|
52
|
+
min_x, max_x = np.min(x_true), np.max(x_true)
|
|
53
|
+
min_y, max_y = np.min(x_pred), np.max(x_pred)
|
|
54
|
+
ax.plot([min_x, max_x], [min_y, max_y], linestyle='--', color="black")
|
|
55
|
+
|
|
56
|
+
# Labels
|
|
57
|
+
ax.set_xlabel(xlab, fontsize=fontsize)
|
|
58
|
+
ax.set_ylabel(ylab, fontsize=fontsize)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def plot_hist_with_normal_fit(sample, true_value, true_std=None, n_bins=20):
|
|
62
|
+
"""
|
|
63
|
+
Plot a histogram of a sample with a fitted normal curve and a vertical line at the true value.
|
|
64
|
+
|
|
65
|
+
:param sample: Array-like, sample data points
|
|
66
|
+
:param true_value: Float, the true value
|
|
67
|
+
:param true_std: Float, the true std
|
|
68
|
+
:param n_bins: Int, the number of histogram bins
|
|
69
|
+
"""
|
|
70
|
+
from scipy.stats import norm
|
|
71
|
+
|
|
72
|
+
# Plot the histogram
|
|
73
|
+
plt.hist(sample, bins=n_bins, density=True, alpha=0.6, color='g', edgecolor='black')
|
|
74
|
+
|
|
75
|
+
xmin, xmax = plt.xlim()
|
|
76
|
+
x = np.linspace(xmin, xmax, 100)
|
|
77
|
+
mean, std = norm.fit(sample)
|
|
78
|
+
|
|
79
|
+
# Plot the fitted normal curve
|
|
80
|
+
p_fit = norm.pdf(x, loc=mean, scale=std)
|
|
81
|
+
plt.plot(x, p_fit, 'b', linewidth=2, label="Fitted Gaussian")
|
|
82
|
+
|
|
83
|
+
# Create the normal distribution's PDF
|
|
84
|
+
if true_std is not None:
|
|
85
|
+
p = norm.pdf(x, loc=true_value, scale=true_std)
|
|
86
|
+
plt.plot(x, p, 'r', linewidth=2, label="Theoretical Gaussian")
|
|
87
|
+
|
|
88
|
+
# Plot the vertical line at the true value
|
|
89
|
+
plt.axvline(true_value, color='black', linestyle='--', linewidth=1.5, label="True value")
|
|
90
|
+
|
|
91
|
+
# Add labels and title
|
|
92
|
+
plt.xlabel('Value')
|
|
93
|
+
plt.ylabel('Density')
|
|
94
|
+
plt.legend()
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import numpy as np
|
|
2
2
|
from typing import Iterable, Set
|
|
3
|
+
from scipy.stats._distn_infrastructure import rv_frozen
|
|
4
|
+
import jax.numpy as jnp
|
|
3
5
|
|
|
4
6
|
|
|
5
7
|
def multiple_union(set_list: Iterable[Set]):
|
|
@@ -16,7 +18,7 @@ def invert_non_zeros(array):
|
|
|
16
18
|
return out
|
|
17
19
|
|
|
18
20
|
|
|
19
|
-
def random_vector_inside_simplex(dim, ub=1):
|
|
21
|
+
def random_vector_inside_simplex(dim: int, ub: float = 1.):
|
|
20
22
|
U = np.random.uniform(low=0, high=ub, size=dim)
|
|
21
23
|
U_sorted = np.sort(U)
|
|
22
24
|
U_sorted = np.concatenate(([0], U_sorted))
|
|
@@ -24,7 +26,18 @@ def random_vector_inside_simplex(dim, ub=1):
|
|
|
24
26
|
return x
|
|
25
27
|
|
|
26
28
|
|
|
27
|
-
def random_vector_on_simplex(dim, ub=1):
|
|
29
|
+
def random_vector_on_simplex(dim: int, ub: float = 1.):
|
|
28
30
|
X = random_vector_inside_simplex(dim=dim, ub=1)
|
|
29
31
|
return X / np.sum(X) * ub
|
|
30
32
|
|
|
33
|
+
|
|
34
|
+
def make_jax_cdf(distrib: rv_frozen):
|
|
35
|
+
name = distrib.dist.name
|
|
36
|
+
args = distrib.args
|
|
37
|
+
kwargs = distrib.kwds
|
|
38
|
+
local_dic = {}
|
|
39
|
+
exec(f"jax_distrib=jax.scipy.stats.{name}", None, local_dic)
|
|
40
|
+
jax_distrib = local_dic["jax_distrib"]
|
|
41
|
+
if name == "expon":
|
|
42
|
+
return lambda x: 1. - jnp.exp(-x)
|
|
43
|
+
return lambda x: jax_distrib.cdf(x, *args, **kwargs)
|
{influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: InfluenceDiffusion
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.15
|
|
4
4
|
Summary: InfluenceDiffusion package
|
|
5
5
|
Author: Alexander Kagan
|
|
6
6
|
Author-email: <amkagan@umich.edu>
|
|
@@ -13,18 +13,27 @@ Classifier: Operating System :: MacOS :: MacOS X
|
|
|
13
13
|
Classifier: Operating System :: Microsoft :: Windows
|
|
14
14
|
Description-Content-Type: text/markdown
|
|
15
15
|
License-File: LICENSE
|
|
16
|
-
Requires-Dist: numpy
|
|
16
|
+
Requires-Dist: numpy<2
|
|
17
17
|
Requires-Dist: scipy
|
|
18
18
|
Requires-Dist: networkx
|
|
19
19
|
Requires-Dist: typing
|
|
20
20
|
Requires-Dist: joblib
|
|
21
|
+
Dynamic: author
|
|
22
|
+
Dynamic: author-email
|
|
23
|
+
Dynamic: classifier
|
|
24
|
+
Dynamic: description
|
|
25
|
+
Dynamic: description-content-type
|
|
26
|
+
Dynamic: keywords
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
Dynamic: requires-dist
|
|
29
|
+
Dynamic: summary
|
|
21
30
|
|
|
22
31
|
|
|
23
32
|
# InfluenceDiffusion
|
|
24
33
|
|
|
25
34
|
InfluenceDiffusion is a Python library that provides instruments for working with influence diffusion models on graphs. In particular, it contains implementations of
|
|
26
|
-
- Popular diffusion models such as Independent Cascade, (General) Linear Threshold, etc.
|
|
27
|
-
- Methods for estimating parameters of these models
|
|
35
|
+
- Popular diffusion models such as Independent Cascade, (General) Linear Threshold, etc.
|
|
36
|
+
- Methods for estimating parameters of these models and constructing the corresponding confidence intervals.
|
|
28
37
|
|
|
29
38
|
|
|
30
39
|
## Installation
|
|
@@ -40,36 +49,43 @@ pip install InfluenceDiffusion
|
|
|
40
49
|
```python
|
|
41
50
|
# Imports
|
|
42
51
|
import matplotlib.pyplot as plt
|
|
43
|
-
from networkx import
|
|
52
|
+
from networkx import connected_watts_strogatz_graph
|
|
53
|
+
from scipy.stats import beta
|
|
44
54
|
|
|
45
55
|
from InfluenceDiffusion.Graph import Graph # class inheriting from nx.DiGraph
|
|
46
|
-
from InfluenceDiffusion.
|
|
47
|
-
from InfluenceDiffusion.
|
|
56
|
+
from InfluenceDiffusion.Inference import GLTInferenceModule
|
|
57
|
+
from InfluenceDiffusion.influence_models import LTM
|
|
58
|
+
from InfluenceDiffusion.estimation_models.OptimEstimation import GLTWeightEstimator
|
|
48
59
|
from InfluenceDiffusion.weight_samplers import make_random_weights_with_indeg_constraint
|
|
60
|
+
from InfluenceDiffusion.plot_utils import plot_with_conf_intervals
|
|
49
61
|
|
|
50
|
-
# Sample an Erdos-Renyi graph
|
|
51
|
-
g_nx = erdos_renyi_graph(50, p=0.1, directed=True)
|
|
52
|
-
g = Graph(edge_list=g_nx.edges)
|
|
53
62
|
|
|
54
|
-
#
|
|
55
|
-
|
|
63
|
+
# Sample a connected Watts-Strogatz graph
|
|
64
|
+
random_state = 1
|
|
65
|
+
g = Graph(connected_watts_strogatz_graph(n=100, k=5, p=0.2, seed=random_state))
|
|
66
|
+
|
|
67
|
+
# Set ground-truth GLT model edge weights (in-degree of each node is at most 1)
|
|
68
|
+
weights = make_random_weights_with_indeg_constraint(g, indeg_ub=1, random_state=random_state)
|
|
56
69
|
g.set_weights(weights)
|
|
57
70
|
|
|
58
|
-
# Sample traces from
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
# Sample traces from the Beta(2, 1)-GLT model on this graph
|
|
72
|
+
threhsold_distrib = beta(2, 1)
|
|
73
|
+
gltm = LTM(g, threshold_generator=threhsold_distrib, random_state=random_state)
|
|
74
|
+
traces = gltm.sample_traces(1000)
|
|
61
75
|
|
|
62
76
|
# Estimate the weights using the traces
|
|
63
|
-
|
|
64
|
-
pred_weights =
|
|
77
|
+
gltm_estimator = GLTWeightEstimator(g, threhsold_distrib)
|
|
78
|
+
pred_weights = gltm_estimator.fit(traces)
|
|
79
|
+
|
|
80
|
+
# Compute 95% confidence intervals
|
|
81
|
+
glt_inferencer = GLTInferenceModule(gltm_estimator)
|
|
82
|
+
conf_ints = glt_inferencer.compute_all_weight_conf_ints(alpha=0.05)
|
|
65
83
|
|
|
66
84
|
# Compare with the ground-truth weights
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
plt.xlabel("True weights")
|
|
70
|
-
plt.ylabel("Predicted weights")
|
|
71
|
-
plt.show()
|
|
85
|
+
plot_with_conf_intervals(weights, pred_weights, conf_ints,
|
|
86
|
+
xlab="True weights", ylab="Predicted weights")
|
|
72
87
|
```
|
|
88
|
+

|
|
73
89
|
|
|
74
90
|
## License
|
|
75
91
|
|
{influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/SOURCES.txt
RENAMED
|
@@ -3,9 +3,11 @@ README.md
|
|
|
3
3
|
setup.cfg
|
|
4
4
|
setup.py
|
|
5
5
|
InfluenceDiffusion/Graph.py
|
|
6
|
+
InfluenceDiffusion/Inference.py
|
|
6
7
|
InfluenceDiffusion/Trace.py
|
|
7
8
|
InfluenceDiffusion/__init__.py
|
|
8
9
|
InfluenceDiffusion/influence_models.py
|
|
10
|
+
InfluenceDiffusion/plot_utils.py
|
|
9
11
|
InfluenceDiffusion/utils.py
|
|
10
12
|
InfluenceDiffusion/weight_samplers.py
|
|
11
13
|
InfluenceDiffusion.egg-info/PKG-INFO
|
|
@@ -14,6 +16,7 @@ InfluenceDiffusion.egg-info/dependency_links.txt
|
|
|
14
16
|
InfluenceDiffusion.egg-info/requires.txt
|
|
15
17
|
InfluenceDiffusion.egg-info/top_level.txt
|
|
16
18
|
InfluenceDiffusion/estimation_models/BaseWeightEstimator.py
|
|
19
|
+
InfluenceDiffusion/estimation_models/CDFEstimation.py
|
|
17
20
|
InfluenceDiffusion/estimation_models/EMEstimation.py
|
|
18
21
|
InfluenceDiffusion/estimation_models/OptimEstimation.py
|
|
19
22
|
InfluenceDiffusion/estimation_models/__init__.py
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: InfluenceDiffusion
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.15
|
|
4
4
|
Summary: InfluenceDiffusion package
|
|
5
5
|
Author: Alexander Kagan
|
|
6
6
|
Author-email: <amkagan@umich.edu>
|
|
@@ -13,18 +13,27 @@ Classifier: Operating System :: MacOS :: MacOS X
|
|
|
13
13
|
Classifier: Operating System :: Microsoft :: Windows
|
|
14
14
|
Description-Content-Type: text/markdown
|
|
15
15
|
License-File: LICENSE
|
|
16
|
-
Requires-Dist: numpy
|
|
16
|
+
Requires-Dist: numpy<2
|
|
17
17
|
Requires-Dist: scipy
|
|
18
18
|
Requires-Dist: networkx
|
|
19
19
|
Requires-Dist: typing
|
|
20
20
|
Requires-Dist: joblib
|
|
21
|
+
Dynamic: author
|
|
22
|
+
Dynamic: author-email
|
|
23
|
+
Dynamic: classifier
|
|
24
|
+
Dynamic: description
|
|
25
|
+
Dynamic: description-content-type
|
|
26
|
+
Dynamic: keywords
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
Dynamic: requires-dist
|
|
29
|
+
Dynamic: summary
|
|
21
30
|
|
|
22
31
|
|
|
23
32
|
# InfluenceDiffusion
|
|
24
33
|
|
|
25
34
|
InfluenceDiffusion is a Python library that provides instruments for working with influence diffusion models on graphs. In particular, it contains implementations of
|
|
26
|
-
- Popular diffusion models such as Independent Cascade, (General) Linear Threshold, etc.
|
|
27
|
-
- Methods for estimating parameters of these models
|
|
35
|
+
- Popular diffusion models such as Independent Cascade, (General) Linear Threshold, etc.
|
|
36
|
+
- Methods for estimating parameters of these models and constructing the corresponding confidence intervals.
|
|
28
37
|
|
|
29
38
|
|
|
30
39
|
## Installation
|
|
@@ -40,36 +49,43 @@ pip install InfluenceDiffusion
|
|
|
40
49
|
```python
|
|
41
50
|
# Imports
|
|
42
51
|
import matplotlib.pyplot as plt
|
|
43
|
-
from networkx import
|
|
52
|
+
from networkx import connected_watts_strogatz_graph
|
|
53
|
+
from scipy.stats import beta
|
|
44
54
|
|
|
45
55
|
from InfluenceDiffusion.Graph import Graph # class inheriting from nx.DiGraph
|
|
46
|
-
from InfluenceDiffusion.
|
|
47
|
-
from InfluenceDiffusion.
|
|
56
|
+
from InfluenceDiffusion.Inference import GLTInferenceModule
|
|
57
|
+
from InfluenceDiffusion.influence_models import LTM
|
|
58
|
+
from InfluenceDiffusion.estimation_models.OptimEstimation import GLTWeightEstimator
|
|
48
59
|
from InfluenceDiffusion.weight_samplers import make_random_weights_with_indeg_constraint
|
|
60
|
+
from InfluenceDiffusion.plot_utils import plot_with_conf_intervals
|
|
49
61
|
|
|
50
|
-
# Sample an Erdos-Renyi graph
|
|
51
|
-
g_nx = erdos_renyi_graph(50, p=0.1, directed=True)
|
|
52
|
-
g = Graph(edge_list=g_nx.edges)
|
|
53
62
|
|
|
54
|
-
#
|
|
55
|
-
|
|
63
|
+
# Sample a connected Watts-Strogatz graph
|
|
64
|
+
random_state = 1
|
|
65
|
+
g = Graph(connected_watts_strogatz_graph(n=100, k=5, p=0.2, seed=random_state))
|
|
66
|
+
|
|
67
|
+
# Set ground-truth GLT model edge weights (in-degree of each node is at most 1)
|
|
68
|
+
weights = make_random_weights_with_indeg_constraint(g, indeg_ub=1, random_state=random_state)
|
|
56
69
|
g.set_weights(weights)
|
|
57
70
|
|
|
58
|
-
# Sample traces from
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
# Sample traces from the Beta(2, 1)-GLT model on this graph
|
|
72
|
+
threhsold_distrib = beta(2, 1)
|
|
73
|
+
gltm = LTM(g, threshold_generator=threhsold_distrib, random_state=random_state)
|
|
74
|
+
traces = gltm.sample_traces(1000)
|
|
61
75
|
|
|
62
76
|
# Estimate the weights using the traces
|
|
63
|
-
|
|
64
|
-
pred_weights =
|
|
77
|
+
gltm_estimator = GLTWeightEstimator(g, threhsold_distrib)
|
|
78
|
+
pred_weights = gltm_estimator.fit(traces)
|
|
79
|
+
|
|
80
|
+
# Compute 95% confidence intervals
|
|
81
|
+
glt_inferencer = GLTInferenceModule(gltm_estimator)
|
|
82
|
+
conf_ints = glt_inferencer.compute_all_weight_conf_ints(alpha=0.05)
|
|
65
83
|
|
|
66
84
|
# Compare with the ground-truth weights
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
plt.xlabel("True weights")
|
|
70
|
-
plt.ylabel("Predicted weights")
|
|
71
|
-
plt.show()
|
|
85
|
+
plot_with_conf_intervals(weights, pred_weights, conf_ints,
|
|
86
|
+
xlab="True weights", ylab="Predicted weights")
|
|
72
87
|
```
|
|
88
|
+

|
|
73
89
|
|
|
74
90
|
## License
|
|
75
91
|
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
# InfluenceDiffusion
|
|
3
3
|
|
|
4
4
|
InfluenceDiffusion is a Python library that provides instruments for working with influence diffusion models on graphs. In particular, it contains implementations of
|
|
5
|
-
- Popular diffusion models such as Independent Cascade, (General) Linear Threshold, etc.
|
|
6
|
-
- Methods for estimating parameters of these models
|
|
5
|
+
- Popular diffusion models such as Independent Cascade, (General) Linear Threshold, etc.
|
|
6
|
+
- Methods for estimating parameters of these models and constructing the corresponding confidence intervals.
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
@@ -19,36 +19,43 @@ pip install InfluenceDiffusion
|
|
|
19
19
|
```python
|
|
20
20
|
# Imports
|
|
21
21
|
import matplotlib.pyplot as plt
|
|
22
|
-
from networkx import
|
|
22
|
+
from networkx import connected_watts_strogatz_graph
|
|
23
|
+
from scipy.stats import beta
|
|
23
24
|
|
|
24
25
|
from InfluenceDiffusion.Graph import Graph # class inheriting from nx.DiGraph
|
|
25
|
-
from InfluenceDiffusion.
|
|
26
|
-
from InfluenceDiffusion.
|
|
26
|
+
from InfluenceDiffusion.Inference import GLTInferenceModule
|
|
27
|
+
from InfluenceDiffusion.influence_models import LTM
|
|
28
|
+
from InfluenceDiffusion.estimation_models.OptimEstimation import GLTWeightEstimator
|
|
27
29
|
from InfluenceDiffusion.weight_samplers import make_random_weights_with_indeg_constraint
|
|
30
|
+
from InfluenceDiffusion.plot_utils import plot_with_conf_intervals
|
|
28
31
|
|
|
29
|
-
# Sample an Erdos-Renyi graph
|
|
30
|
-
g_nx = erdos_renyi_graph(50, p=0.1, directed=True)
|
|
31
|
-
g = Graph(edge_list=g_nx.edges)
|
|
32
32
|
|
|
33
|
-
#
|
|
34
|
-
|
|
33
|
+
# Sample a connected Watts-Strogatz graph
|
|
34
|
+
random_state = 1
|
|
35
|
+
g = Graph(connected_watts_strogatz_graph(n=100, k=5, p=0.2, seed=random_state))
|
|
36
|
+
|
|
37
|
+
# Set ground-truth GLT model edge weights (in-degree of each node is at most 1)
|
|
38
|
+
weights = make_random_weights_with_indeg_constraint(g, indeg_ub=1, random_state=random_state)
|
|
35
39
|
g.set_weights(weights)
|
|
36
40
|
|
|
37
|
-
# Sample traces from
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
# Sample traces from the Beta(2, 1)-GLT model on this graph
|
|
42
|
+
threhsold_distrib = beta(2, 1)
|
|
43
|
+
gltm = LTM(g, threshold_generator=threhsold_distrib, random_state=random_state)
|
|
44
|
+
traces = gltm.sample_traces(1000)
|
|
40
45
|
|
|
41
46
|
# Estimate the weights using the traces
|
|
42
|
-
|
|
43
|
-
pred_weights =
|
|
47
|
+
gltm_estimator = GLTWeightEstimator(g, threhsold_distrib)
|
|
48
|
+
pred_weights = gltm_estimator.fit(traces)
|
|
49
|
+
|
|
50
|
+
# Compute 95% confidence intervals
|
|
51
|
+
glt_inferencer = GLTInferenceModule(gltm_estimator)
|
|
52
|
+
conf_ints = glt_inferencer.compute_all_weight_conf_ints(alpha=0.05)
|
|
44
53
|
|
|
45
54
|
# Compare with the ground-truth weights
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
plt.xlabel("True weights")
|
|
49
|
-
plt.ylabel("Predicted weights")
|
|
50
|
-
plt.show()
|
|
55
|
+
plot_with_conf_intervals(weights, pred_weights, conf_ints,
|
|
56
|
+
xlab="True weights", ylab="Predicted weights")
|
|
51
57
|
```
|
|
58
|
+

|
|
52
59
|
|
|
53
60
|
## License
|
|
54
61
|
|
|
@@ -3,7 +3,7 @@ from pathlib import Path
|
|
|
3
3
|
this_directory = Path(__file__).parent
|
|
4
4
|
LONG_DESCRIPTION = (this_directory / "README.md").read_text()
|
|
5
5
|
|
|
6
|
-
VERSION = '0.0.
|
|
6
|
+
VERSION = '0.0.15'
|
|
7
7
|
DESCRIPTION = 'InfluenceDiffusion package'
|
|
8
8
|
|
|
9
9
|
setup(
|
|
@@ -15,7 +15,7 @@ setup(
|
|
|
15
15
|
long_description_content_type="text/markdown",
|
|
16
16
|
long_description=LONG_DESCRIPTION,
|
|
17
17
|
packages=find_packages(),
|
|
18
|
-
install_requires=["numpy",
|
|
18
|
+
install_requires=["numpy<2",
|
|
19
19
|
"scipy",
|
|
20
20
|
"networkx",
|
|
21
21
|
"typing",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion/weight_samplers.py
RENAMED
|
File without changes
|
|
File without changes
|
{influencediffusion-0.0.13 → influencediffusion-0.0.15}/InfluenceDiffusion.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|