Hands-on: Plotting a Degree Distribution Without Lying
Plotting a degree distribution badly is the most common mistake in network analysis. This notebook is mostly about not making it.
NoteWhere the code lives
This course keeps its lecture note free of runnable code. Everything you execute lives in a marimo notebook, so it stays interactive and reactive instead of being a wall of output frozen into a web page.
Run this notebook — it runs in your browser, with nothing to install and nothing to sign into. The first load takes a few seconds while Python itself is downloaded.
Prefer to work locally? The notebook is in the repository at notebooks/m04-friendship-paradox/coding.py. Run it with marimo edit notebooks/m04-friendship-paradox/coding.py.
What you will build
- Generate a scale-free network and plot its degree distribution on linear axes, log-log axes, and as a CCDF.
- Estimate the power-law exponent from the CCDF slope — remembering that the slope is 1 - \gamma, not -\gamma (why).
- Demonstrate the friendship paradox by sampling edge endpoints rather than nodes.
- Compare the degree distribution of nodes with that of their neighbours.
- Identify hubs, remove them, and measure the damage.
- Measure degree assortativity across different network types.
What to watch for
- Compute the gap between the mean node degree and the mean friend degree, then check it against \text{Var}(k)/\langle k \rangle. They should match exactly.
- Try the same plot with different bin widths. The PDF changes shape; the CCDF does not. That is the whole argument for the CCDF.