Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename m to k #28

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Create an undirected network with preferential attachment and homophily.
.. code:: pycon

>>> from netin.models import PAHModel
>>> m = PAHModel(n=200, m=2, f_m=0.2, h_MM=0.1, h_mm=0.9, seed=42)
>>> m = PAHModel(n=200, k=2, f_m=0.2, h_MM=0.1, h_mm=0.9, seed=42)
>>> m.simulate()


Expand Down
16 changes: 8 additions & 8 deletions examples/notebooks/inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-11T10:26:35.083384Z",
Expand All @@ -53,7 +53,7 @@
"outputs": [],
"source": [
"OUTPUT_DIR = 'inference_results'\n",
"FIGSIZE = (6,3) "
"FIGSIZE = (6,3)"
]
},
{
Expand All @@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-11T10:26:35.122852Z",
Expand All @@ -75,7 +75,7 @@
"outputs": [],
"source": [
"N=1000\n",
"m=2\n",
"k=2\n",
"f_m=0.5\n",
"h_mm=0.2\n",
"h_MM=0.2\n",
Expand All @@ -84,11 +84,11 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g = PAHModel(N=N, m=2, f_m=f_m, h_mm=h_mm, h_MM=h_MM, seed=seed)\n",
"g = PAHModel(N=N, k=2, f_m=f_m, h_mm=h_mm, h_MM=h_MM, seed=seed)\n",
"g = g.simulate()"
]
},
Expand Down Expand Up @@ -251,7 +251,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-11T10:27:23.555466Z",
Expand All @@ -271,7 +271,7 @@
}
],
"source": [
"j.plot_evidences(bayes_factors=True, \n",
"j.plot_evidences(bayes_factors=True,\n",
" figsize=FIGSIZE, bboxx=1.4,\n",
" output_dir=OUTPUT_DIR)"
]
Expand Down
26 changes: 13 additions & 13 deletions examples/notebooks/ranking.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
" return f\"{df.name}\\n{', '.join(g)}\"\n",
"\n",
"def create_pah(\n",
" n: int, f_m: float, m: int,\n",
" n: int, f_m: float, k: int,\n",
" h_MM: float, h_mm: float,\n",
" seed: Union[int, Any, None] = None):\n",
" model = PAHModel(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
" model = PAHModel(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
" graph = model.simulate()\n",
" df = stats.get_node_metadata_as_dataframe(\n",
" graph=graph, include_graph_metadata=False)\n",
Expand Down Expand Up @@ -103,7 +103,7 @@
"source": [
"n = 500\n",
"f_m = 0.5\n",
"m = 2\n",
"k = 2\n",
"d = 0.005\n",
"plo_M = 2.0\n",
"plo_m = 2.0\n",
Expand All @@ -130,7 +130,7 @@
"h_mm = 0.5\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_balance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -155,7 +155,7 @@
"h_mm = 0.9\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_balance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -180,7 +180,7 @@
"h_mm = 0.1\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_balance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -205,7 +205,7 @@
"h_mm = 0.1\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_balance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -230,7 +230,7 @@
"h_mm = 0.9\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_balance_pah.append(df)\n",
"\n",
"#daph\n",
Expand Down Expand Up @@ -274,7 +274,7 @@
"h_mm = 0.5\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_unbalance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -299,7 +299,7 @@
"h_mm = 0.9\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_unbalance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -324,7 +324,7 @@
"h_mm = 0.1\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_unbalance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -349,7 +349,7 @@
"h_mm = 0.1\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_unbalance_pah.append(df)\n",
"\n",
"#daph\n",
Expand All @@ -374,7 +374,7 @@
"h_mm = 0.9\n",
"\n",
"# pah\n",
"df = create_pah(n=n, f_m=f_m, m=m, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"df = create_pah(n=n, f_m=f_m, k=k, h_MM=h_MM, h_mm=h_mm, seed=seed)\n",
"node_metadata_unbalance_pah.append(df)\n",
"\n",
"#daph\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/undirected/barabasi_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

def run():
n = 200
m = 2
k = 2
seed = 1234

model = BarabasiAlbertModel(n=n,
m=m,
k=k,
seed=seed)
timer = SimulationTimer(model)
model.simulate()
Expand Down
4 changes: 2 additions & 2 deletions examples/undirected/homophily.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

def run():
n = 4000
m = 2
k = 2
f_m = 0.1
h_MM = 0.5
h_mm = 0.5
seed = 1234

model = HomophilyModel(n=n, m=m, f_m=f_m,
model = HomophilyModel(n=n, k=k, f_m=f_m,
h_mm=h_mm, h_MM=h_MM,
seed=seed)
timer = SimulationTimer(model)
Expand Down
4 changes: 2 additions & 2 deletions examples/undirected/pa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

def run():
n = 4000
m = 2
k = 2
f_m = 0.1
seed = 1234

model = PAModel(n=n,
m=m,
k=k,
f_m=f_m,
seed=seed)
timer = SimulationTimer(model)
Expand Down
4 changes: 2 additions & 2 deletions examples/undirected/pah.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

def run():
n = 4000
m = 2
k = 2
f_m = 0.1
h_MM = 0.5
h_mm = 0.5
seed = 1234

model = PAHModel(n=n, m=m, f_m=f_m,
model = PAHModel(n=n, k=k, f_m=f_m,
h_mm=h_mm, h_MM=h_MM,
seed=seed)
timer = SimulationTimer(model)
Expand Down
2 changes: 1 addition & 1 deletion examples/undirected/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run():
lfm_g = CompoundLFM.PAH
seed = 1234

model = PATCHModel(n=n, m=k, f_m=f_m,
model = PATCHModel(n=n, k=k, f_m=f_m,
p_tc=p_tc,
lfm_local=lfm_l, lfm_global=lfm_g,
lfm_params={'h_mm': h_mm, 'h_MM': h_MM},
Expand Down
121 changes: 121 additions & 0 deletions examples/undirected/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from typing import Dict, Tuple
from netin.graphs import Graph
from netin.utils.constants import CLASS_ATTRIBUTE
from netin.models import PAHModel

from sympy import Eq
from sympy import solve
from sympy import symbols
import powerlaw

import numpy as np

def cmp_new(a_h: np.ndarray, a_pa: np.ndarray) -> np.ndarray:
a_p = (a_h / a_h.sum()) * (a_pa / a_pa.sum())
return a_p / a_p.sum()

def cmp_old(a_h: np.ndarray, a_pa: np.ndarray) -> np.ndarray:
a_p = a_h * a_pa
return a_p / a_p.sum()

def calculate_edge_type_counts(graph: Graph) -> Dict[str, int]:
minority_nodes = graph.get_node_class(CLASS_ATTRIBUTE).get_class_values()
counter = {
"MM": 0,
"Mm": 0,
"mM": 0,
"mm": 0
}
for source, target in graph.edges():
counter[f"{minority_nodes[source]}{minority_nodes[target]}"] += 1
return counter

def calculate_degree_powerlaw_exponents(graph: Graph) -> Tuple[float, float]:
degrees = graph.degrees()
minority_nodes = graph.get_node_class(CLASS_ATTRIBUTE)

dM = degrees[minority_nodes.get_majority_mask()]
dm = degrees[minority_nodes.get_minority_mask()]

fit_M = powerlaw.Fit(data=dM, discrete=True, xmin=min(dM), xmax=max(dM), verbose=False)
fit_m = powerlaw.Fit(data=dm, discrete=True, xmin=min(dm), xmax=max(dm), verbose=False)

pl_M = fit_M.power_law.alpha
pl_m = fit_m.power_law.alpha
return pl_M, pl_m

def infer_homophily_values(graph: Graph) -> Tuple[float, float]:
"""
Infers analytically the homophily values for the majority and minority classes.

Returns
-------
h_MM: float
homophily within majority group

h_mm: float
homophily within minority group
"""

f_m = np.mean(graph.get_node_class(CLASS_ATTRIBUTE))
f_M = 1 - f_m

e = calculate_edge_type_counts(graph)
e_MM = e['MM']
e_mm = e['mm']
M = e['MM'] + e['mm'] + e['Mm'] + e['mM']

p_MM = e_MM / M
p_mm = e_mm / M

pl_M, pl_m = calculate_degree_powerlaw_exponents(graph)
b_M = -1 / (pl_M + 1)
b_m = -1 / (pl_m + 1)

# equations
hmm, hMM, hmM, hMm = symbols('hmm hMM hmM hMm')
eq1 = Eq((f_m * f_m * hmm * (1 - b_M)) / ((f_m * hmm * (1 - b_M)) + (f_M * hmM * (1 - b_m))), p_mm)
eq2 = Eq(hmm + hmM, 1)

eq3 = Eq((f_M * f_M * hMM * (1 - b_m)) / ((f_M * hMM * (1 - b_m)) + (f_m * hMm * (1 - b_M))), p_MM)
eq4 = Eq(hMM + hMm, 1)

solution = solve((eq1, eq2, eq3, eq4), (hmm, hmM, hMM, hMm))
h_MM, h_mm = solution[hMM], solution[hmm]
return h_MM, h_mm

def main():
n = 2000
k = 2
f_m = .2
h = .8

a_h_M_infer, a_h_m_infer = [], []
for i in range(50):
model = PAHModel(n=n, f_m=f_m, k=k, h_mm=h, h_MM=h, seed=i)
g = model.simulate()

print(calculate_edge_type_counts(g))
h_M_inf, h_m_inf = infer_homophily_values(g)

a_h_M_infer.append(h_M_inf)
a_h_m_infer.append(h_m_inf)

a_h_M_infer = np.asarray(a_h_M_infer)
a_h_m_infer = np.asarray(a_h_m_infer)

print("Minority")
print(np.mean(a_h_m_infer))

print("Majority")
print(np.mean(a_h_M_infer))

a_h = np.asarray([.2, .2, .8, .2, .8])
a_pa = np.asarray([4, 8, 2, 2, 3])
print(
cmp_new(a_h, a_pa),
cmp_old(a_h, a_pa)
)

if __name__ == "__main__":
main()
Loading