Skip to content

Instantly share code, notes, and snippets.

@alanzchen
Created July 8, 2021 03:40
Show Gist options
  • Save alanzchen/b694898342b7d92822f83d2578055a50 to your computer and use it in GitHub Desktop.
Save alanzchen/b694898342b7d92822f83d2578055a50 to your computer and use it in GitHub Desktop.
Biased Coin implementation in Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"treatment_groups = [0 for i in range(5)]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"for i in range(75*5):\n",
" g = random.choice(list(range(5)))\n",
" treatment_groups[g] += 1"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[72, 74, 88, 70, 71]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"treatment_groups"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"treatment_groups = [0 for i in range(5)]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def biased_coin(groups, current_assignment):\n",
" total_weight = sum([75-i for i in current_assignment])\n",
" weights = [(75-i)/total_weight for i in current_assignment]\n",
" return random.choices(list(range(5)), weights)[0]"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [],
"source": [
"treatment_groups = [0 for i in range(5)]\n",
"for i in range(75*5):\n",
" g = biased_coin(list(range(5)), treatment_groups)\n",
" treatment_groups[g] += 1"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[75, 75, 75, 75, 75]"
]
},
"execution_count": 141,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"treatment_groups"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [],
"source": [
"treatment_groups = [0 for i in range(5)]"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [],
"source": [
"def biased_coin2(groups, current_assignment):\n",
" total_weight = sum([75-i for i in current_assignment])\n",
" weights = [75-i for i in current_assignment]\n",
" cum_weights = [sum(weights[:i+1]) for i in range(5)]\n",
" i = random.randint(0, total_weight)\n",
" for j in range(5):\n",
" if i <= cum_weights[j]:\n",
" return j"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
"treatment_groups = [0 for i in range(5)]\n",
"for i in range(75*5):\n",
" g = biased_coin2(list(range(5)), treatment_groups)\n",
" treatment_groups[g] += 1"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[75, 75, 75, 75, 75]"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"treatment_groups"
]
},
{
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment