Skip to content

Instantly share code, notes, and snippets.

@pbloem
Created November 11, 2020 12:16
Show Gist options
  • Save pbloem/776e8ac0bdfe43e5f3ef5da7ef6f424a to your computer and use it in GitHub Desktop.
Save pbloem/776e8ac0bdfe43e5f3ef5da7ef6f424a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: powerlaw in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (1.4.6)\r\n",
"Requirement already satisfied: numpy in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from powerlaw) (1.18.1)\r\n",
"Requirement already satisfied: mpmath in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from powerlaw) (1.1.0)\r\n",
"Requirement already satisfied: matplotlib in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from powerlaw) (3.1.3)\r\n",
"Requirement already satisfied: scipy in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from powerlaw) (1.4.1)\r\n",
"Requirement already satisfied: cycler>=0.10 in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from matplotlib->powerlaw) (0.10.0)\r\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from matplotlib->powerlaw) (1.1.0)\r\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from matplotlib->powerlaw) (2.4.6)\r\n",
"Requirement already satisfied: python-dateutil>=2.1 in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from matplotlib->powerlaw) (2.8.1)\r\n",
"Requirement already satisfied: six in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from cycler>=0.10->matplotlib->powerlaw) (1.14.0)\r\n",
"Requirement already satisfied: setuptools in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib->powerlaw) (46.0.0.post20200309)\r\n"
]
}
],
"source": [
"# documentation\n",
"# -- https://github.com/jeffalstott/powerlaw\n",
"# -- https://arxiv.org/pdf/1305.0215.pdf\n",
"\n",
"!pip install powerlaw"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# load the data \n",
"data = [1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 8, 8, 16]\n",
"# NB: these are the (ordered) sizes of all SCCs, not the frequencies per bin"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"alpha 2.342900692497361\n",
"xmin 3.0\n",
"D 0.14223515510867168\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Calculating best minimal value for power law fit\n"
]
}
],
"source": [
"import powerlaw\n",
"\n",
"results = powerlaw.Fit(data, discrete=True) # if data has less than \n",
"\n",
"print('alpha ', results.power_law.alpha) # exponent of the best fitting powerlaw distribution\n",
"print('xmin ', results.power_law.xmin) # value from which the powerlaw takes effect\n",
"print('D ', results.power_law.D) # goodness-of-fit: KS distance between the fitted law \n",
" # and the emprical distribution. The smaller, the more likely the powerlaw\n",
" # assumption is to be correct."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"likelihood ratio -0.3257441865401183\n",
"p value 0.5224495836658524\n"
]
}
],
"source": [
"# Likelihood ratio test with an alternative assumption for the distribution family (log normal, exponential, etc).\n",
"# -- we don't need this for the gamma/delta metrics, but it might be nice to run for the plot in 5.4\n",
"\n",
"R, p = results.distribution_compare('power_law', 'lognormal')\n",
"\n",
"print('likelihood ratio', R)\n",
"# far from zero means it's likely one or the other. Positive means it's a power law, negative lognormal.\n",
"print('p value', p)\n",
"# p value for the conclusion."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment