Skip to content

Instantly share code, notes, and snippets.

@mfbalin
Created July 15, 2023 20:43
Show Gist options
  • Save mfbalin/096dcad5e3b1f6a59ff7ff2f9f541618 to your computer and use it in GitHub Desktop.
Save mfbalin/096dcad5e3b1f6a59ff7ff2f9f541618 to your computer and use it in GitHub Desktop.
LaborReplacement.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOTE3QxnuvKMrzWIGc+94PM",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/mfbalin/096dcad5e3b1f6a59ff7ff2f9f541618/laborreplacement.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "6jh4PjthSDrb"
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def invcdf(u, n, s):\n",
" return s * (1 - (1 - u) ** (1 / n))\n",
"\n",
"def get(n, seed):\n",
" rng = np.random.default_rng(seed)\n",
" numbers = [0]\n",
" sum = 1.0\n",
" rem = sum\n",
" for n in range(n, 0, -1):\n",
" x = invcdf(rng.random(), n, rem)\n",
" numbers.append(x + numbers[-1])\n",
" rem -= x\n",
" return numbers[1:]"
]
},
{
"cell_type": "code",
"source": [
"from itertools import repeat\n",
"\n",
"fanout = 10\n",
"trials = 10000\n",
"P = [0.1, 0.2, 0.3, 0.4]\n",
"for z in range(20):\n",
" R = np.zeros(len(P), dtype=np.int64)\n",
" for i in range(z * trials, (z + 1) * trials):\n",
" A = []\n",
" for j, p in enumerate(P):\n",
" A += list(zip([r_t / p for r_t in get(fanout, i * len(P) + j)], repeat(j)))\n",
" A = sorted(A)[:fanout]\n",
" i, c = np.unique([i for r, i in A], return_counts=True)\n",
" R[i] += c\n",
" print(R / R.sum())"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Y6Tek2MFSFmt",
"outputId": "820950ac-1816-45d7-e31d-5050bce5b6ff"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[0.09875 0.19661 0.29995 0.40469]\n",
"[0.09871 0.19899 0.29906 0.40324]\n",
"[0.09832 0.19805 0.29959 0.40404]\n",
"[0.09808 0.19704 0.30089 0.40399]\n",
"[0.09801 0.198 0.30085 0.40314]\n",
"[0.09779 0.19795 0.3031 0.40116]\n",
"[0.09825 0.19793 0.29992 0.4039 ]\n",
"[0.09866 0.19855 0.29957 0.40322]\n",
"[0.09757 0.19807 0.3007 0.40366]\n",
"[0.09706 0.19917 0.29897 0.4048 ]\n",
"[0.09853 0.19722 0.29857 0.40568]\n",
"[0.09835 0.19805 0.29955 0.40405]\n",
"[0.09588 0.19735 0.30219 0.40458]\n",
"[0.09746 0.19887 0.30023 0.40344]\n",
"[0.09787 0.19775 0.30004 0.40434]\n",
"[0.09533 0.19804 0.3002 0.40643]\n",
"[0.10017 0.20008 0.29685 0.4029 ]\n",
"[0.0977 0.19713 0.3008 0.40437]\n",
"[0.0972 0.19699 0.30176 0.40405]\n",
"[0.0979 0.19775 0.29741 0.40694]\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment