{ "cells": [ { "cell_type": "markdown", "id": "12870de5", "metadata": {}, "source": [ "# Mixing Matrix and Benchmarks" ] }, { "cell_type": "code", "execution_count": null, "id": "01f6500f", "metadata": {}, "outputs": [], "source": [ "from netseg import freeman\n", "import numpy as np \n" ] }, { "cell_type": "markdown", "id": "dcb5e8d2", "metadata": {}, "source": [ "## Different Ways to Use `netseg`\n", "\n", "Most of the measures in `netseg` can directly be used with a mixing matrix. These measures have an argument that accept a mixing matrix as total edge counts across groups. For example," ] }, { "cell_type": "code", "execution_count": null, "id": "208ea7de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.float64(0.7948717948717948)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "membership = [0 if i < 100 else 1 for i in range(200)]\n", "freeman(membership= membership, mixing_matrix= np.array([[140, 16],[16,140]]))" ] }, { "cell_type": "markdown", "id": "e825be10", "metadata": {}, "source": [ "## Benchmarks\n", "\n", "`netseg` provides scalable, easy-to-use segregation measures for computational social scientists. By leveraging NumPy's vectorization and igraph's C backend, the package builds upon foundational community implementations to optimize performance for large networks. It consolidates several individually developed metrics into a unified toolkit. \n", "\n", "The benchmarks below show the scaling of `netseg` alongside the existing packages that inspired this work. It is important to notice that only challenging metrics are considered since some of the metrics are trivial and require obtaining a mixing matrix, which does not require extensive optimization and all of the Python and R packages, more or less, will have similar times if they are develop with care. For assortativity coefficient, `netseg` will perform better than `networkx` but `igraph` will perform orders of magnitude better than `netseg`.\n", "\n", "All results were obtained on a MacBook Air M3. Times are reported in seconds. Python benchmarks were timed using `perf_counter` and R benchmarks were timed using `system.time()`. Note that if you perform these tests yourself, although the difference ratio will be the same more or less, the slope of the curve might change between time-points according to your L2 cache size.\n", "\n", "For the Random Walk Controversy (RWC) metric, the number of simulations was set to 10,000. For both RWC and Dipole Moment, `k_top` was set to .3% of the total number of nodes in the graph. Finally, for the previous best implementation of RWC, the number of parallel workers was set to 8.\n", "\n", "\n", "![performed benchmarks for dprwcbp](figures/benchmarks.png)\n" ] } ], "metadata": { "kernelspec": { "display_name": "netseg", "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.14.4" } }, "nbformat": 4, "nbformat_minor": 5 }