Skip to content

Commit 5f35ed7

Browse files
Merge pull request #28 from CSHVienna/rename_vars
Rename `m` to `k`
2 parents 42ab3b5 + 28781dc commit 5f35ed7

File tree

17 files changed

+217
-96
lines changed

17 files changed

+217
-96
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Create an undirected network with preferential attachment and homophily.
3939
.. code:: pycon
4040
4141
>>> from netin.models import PAHModel
42-
>>> m = PAHModel(n=200, m=2, f_m=0.2, h_MM=0.1, h_mm=0.9, seed=42)
42+
>>> m = PAHModel(n=200, k=2, f_m=0.2, h_MM=0.1, h_mm=0.9, seed=42)
4343
>>> m.simulate()
4444
4545

examples/notebooks/inference.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
{
4545
"cell_type": "code",
46-
"execution_count": 2,
46+
"execution_count": null,
4747
"metadata": {
4848
"ExecuteTime": {
4949
"end_time": "2024-09-11T10:26:35.083384Z",
@@ -53,7 +53,7 @@
5353
"outputs": [],
5454
"source": [
5555
"OUTPUT_DIR = 'inference_results'\n",
56-
"FIGSIZE = (6,3) "
56+
"FIGSIZE = (6,3)"
5757
]
5858
},
5959
{
@@ -65,7 +65,7 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": 3,
68+
"execution_count": null,
6969
"metadata": {
7070
"ExecuteTime": {
7171
"end_time": "2024-09-11T10:26:35.122852Z",
@@ -75,7 +75,7 @@
7575
"outputs": [],
7676
"source": [
7777
"N=1000\n",
78-
"m=2\n",
78+
"k=2\n",
7979
"f_m=0.5\n",
8080
"h_mm=0.2\n",
8181
"h_MM=0.2\n",
@@ -84,11 +84,11 @@
8484
},
8585
{
8686
"cell_type": "code",
87-
"execution_count": 4,
87+
"execution_count": null,
8888
"metadata": {},
8989
"outputs": [],
9090
"source": [
91-
"g = PAHModel(N=N, m=2, f_m=f_m, h_mm=h_mm, h_MM=h_MM, seed=seed)\n",
91+
"g = PAHModel(N=N, k=2, f_m=f_m, h_mm=h_mm, h_MM=h_MM, seed=seed)\n",
9292
"g = g.simulate()"
9393
]
9494
},
@@ -251,7 +251,7 @@
251251
},
252252
{
253253
"cell_type": "code",
254-
"execution_count": 13,
254+
"execution_count": null,
255255
"metadata": {
256256
"ExecuteTime": {
257257
"end_time": "2024-09-11T10:27:23.555466Z",
@@ -271,7 +271,7 @@
271271
}
272272
],
273273
"source": [
274-
"j.plot_evidences(bayes_factors=True, \n",
274+
"j.plot_evidences(bayes_factors=True,\n",
275275
" figsize=FIGSIZE, bboxx=1.4,\n",
276276
" output_dir=OUTPUT_DIR)"
277277
]

examples/notebooks/ranking.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@
6161
" return f\"{df.name}\\n{', '.join(g)}\"\n",
6262
"\n",
6363
"def create_pah(\n",
64-
" n: int, f_m: float, m: int,\n",
64+
" n: int, f_m: float, k: int,\n",
6565
" h_MM: float, h_mm: float,\n",
6666
" seed: Union[int, Any, None] = None):\n",
67-
" model = PAHModel(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
67+
" model = PAHModel(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
6868
" graph = model.simulate()\n",
6969
" df = stats.get_node_metadata_as_dataframe(\n",
7070
" graph=graph, include_graph_metadata=False)\n",
@@ -103,7 +103,7 @@
103103
"source": [
104104
"n = 500\n",
105105
"f_m = 0.5\n",
106-
"m = 2\n",
106+
"k = 2\n",
107107
"d = 0.005\n",
108108
"plo_M = 2.0\n",
109109
"plo_m = 2.0\n",
@@ -130,7 +130,7 @@
130130
"h_mm = 0.5\n",
131131
"\n",
132132
"# pah\n",
133-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
133+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
134134
"node_metadata_balance_pah.append(df)\n",
135135
"\n",
136136
"#daph\n",
@@ -155,7 +155,7 @@
155155
"h_mm = 0.9\n",
156156
"\n",
157157
"# pah\n",
158-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
158+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
159159
"node_metadata_balance_pah.append(df)\n",
160160
"\n",
161161
"#daph\n",
@@ -180,7 +180,7 @@
180180
"h_mm = 0.1\n",
181181
"\n",
182182
"# pah\n",
183-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
183+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
184184
"node_metadata_balance_pah.append(df)\n",
185185
"\n",
186186
"#daph\n",
@@ -205,7 +205,7 @@
205205
"h_mm = 0.1\n",
206206
"\n",
207207
"# pah\n",
208-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
208+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
209209
"node_metadata_balance_pah.append(df)\n",
210210
"\n",
211211
"#daph\n",
@@ -230,7 +230,7 @@
230230
"h_mm = 0.9\n",
231231
"\n",
232232
"# pah\n",
233-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
233+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
234234
"node_metadata_balance_pah.append(df)\n",
235235
"\n",
236236
"#daph\n",
@@ -274,7 +274,7 @@
274274
"h_mm = 0.5\n",
275275
"\n",
276276
"# pah\n",
277-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
277+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
278278
"node_metadata_unbalance_pah.append(df)\n",
279279
"\n",
280280
"#daph\n",
@@ -299,7 +299,7 @@
299299
"h_mm = 0.9\n",
300300
"\n",
301301
"# pah\n",
302-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
302+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
303303
"node_metadata_unbalance_pah.append(df)\n",
304304
"\n",
305305
"#daph\n",
@@ -324,7 +324,7 @@
324324
"h_mm = 0.1\n",
325325
"\n",
326326
"# pah\n",
327-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
327+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
328328
"node_metadata_unbalance_pah.append(df)\n",
329329
"\n",
330330
"#daph\n",
@@ -349,7 +349,7 @@
349349
"h_mm = 0.1\n",
350350
"\n",
351351
"# pah\n",
352-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
352+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
353353
"node_metadata_unbalance_pah.append(df)\n",
354354
"\n",
355355
"#daph\n",
@@ -374,7 +374,7 @@
374374
"h_mm = 0.9\n",
375375
"\n",
376376
"# pah\n",
377-
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
377+
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
378378
"node_metadata_unbalance_pah.append(df)\n",
379379
"\n",
380380
"#daph\n",

examples/undirected/barabasi_albert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
def run():
55
n = 4000
6-
m = 2
6+
k = 2
77
seed = 1234
88

99
model = BarabasiAlbertModel(n=n,
10-
m=m,
10+
k=k,
1111
seed=seed)
1212
timer = SimulationTimer(model)
1313
model.simulate()

examples/undirected/homophily.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
def run():
55
n = 4000
6-
m = 2
6+
k = 2
77
f_m = 0.1
88
h_MM = 0.5
99
h_mm = 0.5
1010
seed = 1234
1111

12-
model = HomophilyModel(n=n, m=m, f_m=f_m,
12+
model = HomophilyModel(n=n, k=k, f_m=f_m,
1313
h_mm=h_mm, h_MM=h_MM,
1414
seed=seed)
1515
timer = SimulationTimer(model)

examples/undirected/pa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
def run():
66
n = 4000
7-
m = 2
7+
k = 2
88
f_m = 0.1
99
seed = 1234
1010

1111
model = PAModel(n=n,
12-
m=m,
12+
k=k,
1313
f_m=f_m,
1414
seed=seed)
1515
timer = SimulationTimer(model)

examples/undirected/pah.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
def run():
55
n = 4000
6-
m = 2
6+
k = 2
77
f_m = 0.1
88
h_MM = 0.5
99
h_mm = 0.5
1010
seed = 1234
1111

12-
model = PAHModel(n=n, m=m, f_m=f_m,
12+
model = PAHModel(n=n, k=k, f_m=f_m,
1313
h_mm=h_mm, h_MM=h_MM,
1414
seed=seed)
1515
timer = SimulationTimer(model)

examples/undirected/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def run():
1111
lfm_l = CompoundLFM.PAH
1212
lfm_g = CompoundLFM.PAH
1313
seed = 1234
14-
model = PATCHModel(n=n, m=k, f_m=f_m,
14+
model = PATCHModel(n=n, k=k, f_m=f_m,
1515
tau=tau,
1616
lfm_tc=lfm_l, lfm_global=lfm_g,
1717
h_mm=h_mm, h_MM=h_MM,

examples/undirected/testing.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from typing import Dict, Tuple
2+
from netin.graphs import Graph
3+
from netin.utils.constants import CLASS_ATTRIBUTE
4+
from netin.models import PAHModel
5+
6+
from sympy import Eq
7+
from sympy import solve
8+
from sympy import symbols
9+
import powerlaw
10+
11+
import numpy as np
12+
13+
def cmp_new(a_h: np.ndarray, a_pa: np.ndarray) -> np.ndarray:
14+
a_p = (a_h / a_h.sum()) * (a_pa / a_pa.sum())
15+
return a_p / a_p.sum()
16+
17+
def cmp_old(a_h: np.ndarray, a_pa: np.ndarray) -> np.ndarray:
18+
a_p = a_h * a_pa
19+
return a_p / a_p.sum()
20+
21+
def calculate_edge_type_counts(graph: Graph) -> Dict[str, int]:
22+
minority_nodes = graph.get_node_class(CLASS_ATTRIBUTE).get_class_values()
23+
counter = {
24+
"MM": 0,
25+
"Mm": 0,
26+
"mM": 0,
27+
"mm": 0
28+
}
29+
for source, target in graph.edges():
30+
counter[f"{minority_nodes[source]}{minority_nodes[target]}"] += 1
31+
return counter
32+
33+
def calculate_degree_powerlaw_exponents(graph: Graph) -> Tuple[float, float]:
34+
degrees = graph.degrees()
35+
minority_nodes = graph.get_node_class(CLASS_ATTRIBUTE)
36+
37+
dM = degrees[minority_nodes.get_majority_mask()]
38+
dm = degrees[minority_nodes.get_minority_mask()]
39+
40+
fit_M = powerlaw.Fit(data=dM, discrete=True, xmin=min(dM), xmax=max(dM), verbose=False)
41+
fit_m = powerlaw.Fit(data=dm, discrete=True, xmin=min(dm), xmax=max(dm), verbose=False)
42+
43+
pl_M = fit_M.power_law.alpha
44+
pl_m = fit_m.power_law.alpha
45+
return pl_M, pl_m
46+
47+
def infer_homophily_values(graph: Graph) -> Tuple[float, float]:
48+
"""
49+
Infers analytically the homophily values for the majority and minority classes.
50+
51+
Returns
52+
-------
53+
h_MM: float
54+
homophily within majority group
55+
56+
h_mm: float
57+
homophily within minority group
58+
"""
59+
60+
f_m = np.mean(graph.get_node_class(CLASS_ATTRIBUTE))
61+
f_M = 1 - f_m
62+
63+
e = calculate_edge_type_counts(graph)
64+
e_MM = e['MM']
65+
e_mm = e['mm']
66+
M = e['MM'] + e['mm'] + e['Mm'] + e['mM']
67+
68+
p_MM = e_MM / M
69+
p_mm = e_mm / M
70+
71+
pl_M, pl_m = calculate_degree_powerlaw_exponents(graph)
72+
b_M = -1 / (pl_M + 1)
73+
b_m = -1 / (pl_m + 1)
74+
75+
# equations
76+
hmm, hMM, hmM, hMm = symbols('hmm hMM hmM hMm')
77+
eq1 = Eq((f_m * f_m * hmm * (1 - b_M)) / ((f_m * hmm * (1 - b_M)) + (f_M * hmM * (1 - b_m))), p_mm)
78+
eq2 = Eq(hmm + hmM, 1)
79+
80+
eq3 = Eq((f_M * f_M * hMM * (1 - b_m)) / ((f_M * hMM * (1 - b_m)) + (f_m * hMm * (1 - b_M))), p_MM)
81+
eq4 = Eq(hMM + hMm, 1)
82+
83+
solution = solve((eq1, eq2, eq3, eq4), (hmm, hmM, hMM, hMm))
84+
h_MM, h_mm = solution[hMM], solution[hmm]
85+
return h_MM, h_mm
86+
87+
def main():
88+
n = 2000
89+
k = 2
90+
f_m = .2
91+
h = .8
92+
93+
a_h_M_infer, a_h_m_infer = [], []
94+
for i in range(50):
95+
model = PAHModel(n=n, f_m=f_m, k=k, h_mm=h, h_MM=h, seed=i)
96+
g = model.simulate()
97+
98+
print(calculate_edge_type_counts(g))
99+
h_M_inf, h_m_inf = infer_homophily_values(g)
100+
101+
a_h_M_infer.append(h_M_inf)
102+
a_h_m_infer.append(h_m_inf)
103+
104+
a_h_M_infer = np.asarray(a_h_M_infer)
105+
a_h_m_infer = np.asarray(a_h_m_infer)
106+
107+
print("Minority")
108+
print(np.mean(a_h_m_infer))
109+
110+
print("Majority")
111+
print(np.mean(a_h_M_infer))
112+
113+
a_h = np.asarray([.2, .2, .8, .2, .8])
114+
a_pa = np.asarray([4, 8, 2, 2, 3])
115+
print(
116+
cmp_new(a_h, a_pa),
117+
cmp_old(a_h, a_pa)
118+
)
119+
120+
if __name__ == "__main__":
121+
main()

0 commit comments

Comments
 (0)