Skip to content

Instantly share code, notes, and snippets.

@daviddavo
Last active February 8, 2022 11:17
Show Gist options
  • Select an option

  • Save daviddavo/b1a6ca47a1cd579a03db71c5c6f78983 to your computer and use it in GitHub Desktop.

Select an option

Save daviddavo/b1a6ca47a1cd579a03db71c5c6f78983 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{"metadata":{"interpreter":{"hash":"0175641433de90fb173cab95d8303f4dccda59c5916a086d658e1a39fa8ff50e"},"kernelspec":{"name":"python3","display_name":"Python 3 (ipykernel)","language":"python"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install git+https://github.com/mmngreco/IneqPy.git\n!pip install git+https://github.com/GRASIA/inequality_coefficients.git@develop\n!pip install pytest","metadata":{"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"Collecting git+https://github.com/mmngreco/IneqPy.git\n Cloning https://github.com/mmngreco/IneqPy.git to /tmp/pip-req-build-llgvdr4l\n Running command git clone --filter=blob:none -q https://github.com/mmngreco/IneqPy.git /tmp/pip-req-build-llgvdr4l\n Resolved https://github.com/mmngreco/IneqPy.git to commit b6e06dcf60d1b08db03f9e055bf03510baa221c6\n Installing build dependencies ... \u001b[?25ldone\n\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n\u001b[?25hCollecting numpy\n Downloading numpy-1.21.5-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.7 MB)\n |████████████████████████████████| 15.7 MB 4.9 MB/s \n\u001b[?25hCollecting numba\n Downloading numba-0.55.1-1-cp37-cp37m-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (3.3 MB)\n |████████████████████████████████| 3.3 MB 88.9 MB/s \n\u001b[?25hCollecting pandas\n Downloading pandas-1.3.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.3 MB)\n |████████████████████████████████| 11.3 MB 43.1 MB/s \n\u001b[?25hRequirement already satisfied: setuptools in /srv/conda/envs/notebook/lib/python3.7/site-packages (from numba->IneqPy==0.3.0rc1) (60.5.0)\nCollecting llvmlite<0.39,>=0.38.0rc1\n Downloading llvmlite-0.38.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.5 MB)\n |████████████████████████████████| 34.5 MB 46.9 MB/s \n\u001b[?25hRequirement already satisfied: python-dateutil>=2.7.3 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pandas->IneqPy==0.3.0rc1) (2.8.2)\nRequirement already satisfied: pytz>=2017.3 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pandas->IneqPy==0.3.0rc1) (2021.3)\nRequirement already satisfied: six>=1.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas->IneqPy==0.3.0rc1) (1.16.0)\nBuilding wheels for collected packages: IneqPy\n Building wheel for IneqPy (pyproject.toml) ... \u001b[?25ldone\n\u001b[?25h Created wheel for IneqPy: filename=IneqPy-0.3.0rc1-py3-none-any.whl size=24808 sha256=dd946420eef5297ec1c8837380126421e71f2a494963a3a086002955c19ef456\n Stored in directory: /tmp/pip-ephem-wheel-cache-1h7c_i39/wheels/df/0f/c9/e03ae8c469662dc7d825356452091115c7e2175ea9e40e6f08\nSuccessfully built IneqPy\nInstalling collected packages: numpy, llvmlite, pandas, numba, IneqPy\nSuccessfully installed IneqPy-0.3.0rc1 llvmlite-0.38.0 numba-0.55.1 numpy-1.21.5 pandas-1.3.5\nCollecting git+https://github.com/GRASIA/inequality_coefficients.git@develop\n Cloning https://github.com/GRASIA/inequality_coefficients.git (to revision develop) to /tmp/pip-req-build-s92sj7xn\n Running command git clone --filter=blob:none -q https://github.com/GRASIA/inequality_coefficients.git /tmp/pip-req-build-s92sj7xn\n Running command git checkout -b develop --track origin/develop\n Switched to a new branch 'develop'\n Branch 'develop' set up to track remote branch 'develop' from 'origin'.\n Resolved https://github.com/GRASIA/inequality_coefficients.git to commit f0086deca0ef363a319fce9be6d470539935003f\n Preparing metadata (setup.py) ... \u001b[?25ldone\n\u001b[?25hRequirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.7/site-packages (from inequality-coefficients==1.2.4) (1.21.5)\nBuilding wheels for collected packages: inequality-coefficients\n Building wheel for inequality-coefficients (setup.py) ... \u001b[?25ldone\n\u001b[?25h Created wheel for inequality-coefficients: filename=inequality_coefficients-1.2.4-py3-none-any.whl size=5597 sha256=42a78e849803c56c644a8a9b2581869aba6d44a4791eb17f52cb8a7ac4ad429d\n Stored in directory: /tmp/pip-ephem-wheel-cache-d5bml7nf/wheels/16/cc/cc/c148069caf1cf5369ebe36af2f9846be13c4a1be9f4c7dff6d\nSuccessfully built inequality-coefficients\nInstalling collected packages: inequality-coefficients\nSuccessfully installed inequality-coefficients-1.2.4\nCollecting pytest\n Downloading pytest-7.0.0-py3-none-any.whl (296 kB)\n |████████████████████████████████| 296 kB 4.6 MB/s \n\u001b[?25hRequirement already satisfied: attrs>=19.2.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pytest) (21.4.0)\nCollecting pluggy<2.0,>=0.12\n Downloading pluggy-1.0.0-py2.py3-none-any.whl (13 kB)\nCollecting py>=1.8.2\n Downloading py-1.11.0-py2.py3-none-any.whl (98 kB)\n |████████████████████████████████| 98 kB 14.4 MB/s \n\u001b[?25hCollecting tomli>=1.0.0\n Downloading tomli-2.0.1-py3-none-any.whl (12 kB)\nRequirement already satisfied: importlib-metadata>=0.12 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pytest) (4.10.1)\nCollecting iniconfig\n Downloading iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB)\nRequirement already satisfied: packaging in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pytest) (21.3)\nRequirement already satisfied: typing-extensions>=3.6.4 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from importlib-metadata>=0.12->pytest) (4.0.1)\nRequirement already satisfied: zipp>=0.5 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from importlib-metadata>=0.12->pytest) (3.7.0)\nRequirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from packaging->pytest) (3.0.7)\nInstalling collected packages: tomli, py, pluggy, iniconfig, pytest\nSuccessfully installed iniconfig-1.1.1 pluggy-1.0.0 py-1.11.0 pytest-7.0.0 tomli-2.0.1\n","output_type":"stream"}]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport pytest\n\nfrom timeit import timeit\nimport ineqpy\nimport inequality_coefficients as ineq","metadata":{"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"arrays = []\nall_ones = np.full(1000, 1)\nfor _ in range(1000):\n arrays.append(np.random.rand(1000))\n g1 = ineq.gini(arrays[-1])\n g2 = ineqpy.inequality.gini(arrays[-1])\n assert pytest.approx(g1, 5) == g2, \"%f should be approx %f to 5 places\" % (g1, g2)\n\n t1 = ineq.theil(arrays[-1])\n t2 = ineqpy.inequality.theil(arrays[-1], all_ones)\n assert pytest.approx(t1, 5) == t2, \"%f should be approx %f to 5 places\" % (t1, t2)\n\nprint(\"All OK!\")","metadata":{"trusted":true},"execution_count":3,"outputs":[{"name":"stdout","text":"All OK!\n","output_type":"stream"}]},{"cell_type":"code","source":"%%timeit\n[ineq.gini(a) for a in arrays]","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"643 ms ± 25.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n","output_type":"stream"}]},{"cell_type":"code","source":"%%timeit\n[ineqpy.inequality.gini(a) for a in arrays]","metadata":{"trusted":true},"execution_count":5,"outputs":[{"name":"stdout","text":"290 ms ± 32.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n","output_type":"stream"}]},{"cell_type":"code","source":"%%timeit\n[ineq.theil(a) for a in arrays]","metadata":{"trusted":true},"execution_count":6,"outputs":[{"name":"stdout","text":"1.84 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n","output_type":"stream"}]},{"cell_type":"code","source":"%%timeit\n[ineqpy.inequality.theil(a, all_ones) for a in arrays]","metadata":{"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"125 ms ± 6.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n","output_type":"stream"}]}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment