Skip to content

Instantly share code, notes, and snippets.

@thejevans
Last active March 4, 2022 20:32
Show Gist options
  • Save thejevans/986acbb8c82db735fbf3768ddc84ccfc to your computer and use it in GitHub Desktop.
Save thejevans/986acbb8c82db735fbf3768ddc84ccfc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "ac1c0408-d967-4d03-bb15-cc77e52e6fa0",
"metadata": {},
"source": [
"## Part B\n",
"### 0: \n",
"John Evans, ...\n",
"### 1:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "09d4b9a5-18dd-477b-85a0-5aaa98cb18be",
"metadata": {},
"outputs": [],
"source": [
"from itertools import permutations\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib_inline.backend_inline\n",
"import numpy as np\n",
"import networkx as nx\n",
"\n",
"# uncomment this before exporting to pdf\n",
"#matplotlib_inline.backend_inline.set_matplotlib_formats('svg', 'pdf')\n",
"\n",
"rng = np.random.default_rng()\n",
"\n",
"n_a = 200\n",
"k_a = 2\n",
"n_b = 50\n",
"k_b = 8\n",
"realizations = 10"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "619b1ccf-55d0-4b6c-ad6f-cb143800216c",
"metadata": {},
"outputs": [],
"source": [
"stubs = np.repeat(np.arange(n_b), k_b)\n",
"\n",
"# this allows for double links. unsure if that's okay.\n",
"a_links = rng.permuted(np.tile(stubs, (realizations, 1)), axis=1)\n",
"a_links = np.reshape(a_links, (realizations, n_a, k_a))\n",
"\n",
"# likely a way to do this without loops\n",
"a_proj = np.empty((realizations, n_a, n_a), dtype=int)\n",
"for i in range(realizations): \n",
" for j, k in permutations(range(n_a), 2):\n",
" a_proj[i, j, k] = np.any(np.isin(a_links[i, j], a_links[i, k]))\n",
"\n",
"a_proj_k = np.tensordot(a_proj, np.ones(n_a), axes=([2], [0]))\n",
"a_proj_c = np.array([\n",
" list(nx.clustering(nx.from_numpy_array(proj)).values())\n",
" for proj in a_proj\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0064667e-762a-4ec9-bb52-3fe1794cd6df",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAANtUlEQVR4nO3cf6zdd13H8eeLNgM3cQN6h9J23EYK0ixjGzcTJSLJpukGaVXUrGEJxGX9a4qyaIozi46YMGc0/lF/TMGRBTbGFKyu0OGcYoxbesd+uLYU6hhrC2xlzhFdpKu+/eN8p4fbe3vPbc/t994Pz0dy0/P9nk/O952tffbb7znfk6pCkrT8vaTvASRJ42HQJakRBl2SGmHQJakRBl2SGrGyrwOvWrWqJicn+zq8JC1LDz744DeramK253oL+uTkJNPT030dXpKWpSRfnes5L7lIUiMMuiQ1wqBLUiMMuiQ1wqBLUiMMuiQ1wqBLUiMMuiQ1wqBLUiN6u1NUkvo0ue3u3o79xIfesSiv6xm6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSIwy6JDXCoEtSI0YKepKNSfYnOZBk2yzPn5fkviQPJXk0yRXjH1WSdCLzBj3JCmA7cDmwAdiSZMOMZb8B3FlVFwFXAn847kElSSc2yhn6JcCBqnq8qo4CdwCbZ6wp4Pu6x2cDXxvfiJKkUYwS9NXAwaHtQ92+Yb8JXJXkELAT+MXZXijJ1iTTSaaPHDlyEuNKkuYyrjdFtwC3VtUa4ArgtiTHvXZV3VJVU1U1NTExMaZDS5JgtKAfBtYOba/p9g27GrgToKr+GXgZsGocA0qSRjNK0HcD65OsS3IGgzc9d8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LEkr0pyJnAF33nzWt9eXVVf7x5/A3h1n8MsM78AfKbvIV6U5LeTHATeTT9n6MdJshk4XFWPjPu1l1vQVwIXA39UVRcB/0k//xyeVXfj1Sbgk33PAtBd+93M4C/C1wBnJbmq36kGZ5oMvpHzHuCzwMPAf/c501y6G+T8bO8IklwPHAM+1vcsL6qq66tqLYOZru17nu4E5tdZpL9cllvQDwGHquqBbvsuBoFfKi4HvlBVT/U9SOcy4CtVdaSqXgD+EvjRnmcCoKo+XFVvrqq3Ac8yuPa6VDyV5AcAul+f7nmeJS/Je4F3Au9eoneJfwx4V99DAD/I4ATrkSRPMLg89YUk3z+OF19WQa+qbwAHk7yh23UpsLfHkWbawhK53NJ5EnhLkjO7rzO+lCXwJjJAknO7X89jcP384/1O9B2Gv8riPcBf9TjLkpdkI4PrwZuq6vm+53lRkvVDm5uBL/Y1y4uq6l+q6tyqmqyqSQYnqRd3bRvLAZbVD3AhMA08CnwaeEXfM3VzncXgC8nO7nuWGXP9FoPfyI8BtwEv7Xumbq5/ZPCX8SPApT3OcTuD9xde6P5wXc3gq5/vBb4M/C3wyiUy1093j78NPAXsWiJzHWDw9dkPdz9/vETm+ovu9/2jwF8Dq5fCXDOefwJYNa7jeeu/JDViWV1ykSTNzaBLUiMMuiQ1wqBLUiMMuiQ1wqBLUiMMuiQ14n8B/o767Y0oo84AAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAN60lEQVR4nO3df6xk9V3G8fcjW1KpWKh7WyuLXTSAYlMDXhVtLBTaZgMNq9UYiChb0U0apbXBErCJGP+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amqTgXeOV72xZklXHuDnoLkcCzwiacAWHJM6jzgoRnm66vLKQ4+zOLeN0k2snhI5bEZZuyj0ykcknwfcCxw+4zz9dVlfk8AZwMk+X4WC3w005TT6fK3t3G/VxNXADtnnHGt7QZ+YfxplNOBL1XV09M+2GF3SbU6wCkAkvw2sFBVu4G3JjkPeJ7FNyS2rVvgVeo4v48Bb0jyIIsvT99RVV9Yv9TddZwfLJbD9TV+678VHed3KYuHvd7O4hua21qYZ8e5nQn8TpICbgN+Zd0CTyHJLhbnsHH8HsWVwAsAqur9LL5ncQ7wKPBl4M29xmvgeZckLeNwPIQiSYcEC1ySGmWBS1KjLHBJapQFLkmNssAlqVEWuCQ16v8ACq9ULIbt+ssAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.hist(a_proj_k.flatten(), density=True)\n",
"plt.show()\n",
"plt.hist(a_proj_c.flatten(), density=True)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "9599b8db-abfe-4859-9a90-80fa1edeaa60",
"metadata": {},
"source": [
"### 2:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "937067f8-bbae-4f5b-8f3d-91be48700baa",
"metadata": {},
"outputs": [],
"source": [
"n_ws = 500\n",
"ws_nn = 4\n",
"probs = [0, 0.01, 0.05, 0.1, 1]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3eae029a-f641-4e64-9896-14760eada847",
"metadata": {},
"outputs": [],
"source": [
"# leave the main diagonal = 1 for now to make rewiring easier later\n",
"ws_base = np.identity(n_ws)\n",
"\n",
"for i in range(ws_nn//2):\n",
" ws_base += np.roll(np.identity(n_ws), i + 1, axis=1)\n",
" ws_base += np.roll(np.identity(n_ws), -i - 1, axis=1)\n",
"\n",
"ws = np.tile(ws_base, (len(probs), 1, 1))\n",
"\n",
"ws_rewires = []\n",
"for i, p in enumerate(probs):\n",
" rewired_edges = []\n",
" \n",
" for j in range(n_ws):\n",
" rewires = rng.binomial(1, p, size=ws_nn + 1)\n",
" possible_nodes = np.nonzero(ws[i, j, :] == 0)[0]\n",
" rewire_nodes = rng.choice(possible_nodes, ws_nn + 1, replace=False)\n",
" ws[i, j, j] = 0\n",
" \n",
" for k, (rewire, node) in enumerate(zip(rewires, rewire_nodes)):\n",
" neighbor = (j - k + ws_nn//2 + 1) % n_ws\n",
" if rewire and ws[i, j, neighbor] != 0:\n",
" rewired_edges.append((j, node))\n",
" ws[i, j, neighbor] = 0\n",
" ws[i, neighbor, j] = 0\n",
" ws[i, j, node] = 1\n",
" ws[i, node, j] = 1\n",
"\n",
" ws_rewires.append(rewired_edges)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a21b646d-d083-49d1-884f-5d0d203f5488",
"metadata": {},
"outputs": [],
"source": [
"edge_bets = [nx.edge_betweenness_centrality(nx.from_numpy_array(net)) for net in ws]\n",
"\n",
"neighbor_avg_edge_bets = []\n",
"shortcut_avg_edge_bets = []\n",
"total_avg_edge_bets = []\n",
"\n",
"for edge_bet, rewires in zip(edge_bets, ws_rewires):\n",
" total = list(edge_bet.values())\n",
" shortcut = [bet for edge, bet in edge_bet.items() if edge in rewires or edge[::-1] in rewires]\n",
" neighbor = [bet for edge, bet in edge_bet.items() if edge not in rewires and edge[::-1] not in rewires]\n",
" total_avg_edge_bets.append((np.average(total), np.std(total)))\n",
"\n",
" if len(shortcut):\n",
" shortcut_avg_edge_bets.append((np.average(shortcut), np.std(shortcut)))\n",
" else:\n",
" shortcut_avg_edge_bets.append((0, 0))\n",
"\n",
" if len(neighbor):\n",
" neighbor_avg_edge_bets.append((np.average(neighbor), np.std(neighbor)))\n",
" else:\n",
" neighbor_avg_edge_bets.append((0, 0))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "5731b266-9260-4550-b012-77e1f426254c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"edge betweenness\n",
"probability total\t neighbors\t shortcuts\n",
"p = 0.00 0.0629+/-0.0619 0.0629+/-0.0619 0.0000+/-0.0000\n",
"p = 0.01 0.0166+/-0.0235 0.0152+/-0.0204 0.1039+/-0.0310\n",
"p = 0.05 0.0081+/-0.0098 0.0065+/-0.0071 0.0279+/-0.0141\n",
"p = 0.10 0.0068+/-0.0057 0.0054+/-0.0043 0.0158+/-0.0057\n",
"p = 1.00 0.0048+/-0.0014 0.0000+/-0.0000 0.0048+/-0.0014\n"
]
}
],
"source": [
"print('edge betweenness')\n",
"print('probability total\\t neighbors\\t shortcuts')\n",
"for i in range(len(probs)):\n",
" print(''.join([\n",
" f'p = {probs[i]:.2f}',\n",
" f' {total_avg_edge_bets[i][0]:.4f}+/-{total_avg_edge_bets[i][1]:.4f}',\n",
" f' {neighbor_avg_edge_bets[i][0]:.4f}+/-{neighbor_avg_edge_bets[i][1]:.4f}',\n",
" f' {shortcut_avg_edge_bets[i][0]:.4f}+/-{shortcut_avg_edge_bets[i][1]:.4f}',\n",
" ]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "985ecf41-8d00-407c-8d70-b9c273431cfa",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment