Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mccurcio/9d6a2073f0bc047d08871309e397d248 to your computer and use it in GitHub Desktop.
Save mccurcio/9d6a2073f0bc047d08871309e397d248 to your computer and use it in GitHub Desktop.
Simulate a Weighted Probability of Numbers Given Known Weights
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "1d9b7e27",
"metadata": {},
"source": [
"[5,2,6,8]\n",
"\n",
"#returning a number from this list with some prob\n",
"\n",
"if prob(5) = 0.2, then if I draw 1000 numbers from this list on average, I will pick the '5' 200 times.\n",
"\n",
"prob(5) = 5/(5+2+6+8), There fore the number is the weight"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "dff45e64",
"metadata": {
"trusted": false
},
"outputs": [],
"source": [
"prob_5 = 5/(5+2+6+8)\n",
"\n",
"prob_2 = 2/(5+2+6+8)\n",
"\n",
"prob_6 = 6/(5+2+6+8)\n",
"\n",
"prob_8 = 8/(5+2+6+8)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "141ac88c",
"metadata": {
"trusted": false
},
"outputs": [],
"source": [
"# Calc Cum DF\n",
"\n",
"cum_prob_5 = prob_5\n",
"\n",
"cum_prob_2 = prob_2 + prob_5\n",
"\n",
"cum_prob_6 = prob_6 + prob_2 + prob_5\n",
"\n",
"cum_prob_8 = prob_8 + prob_6 + prob_2 + prob_5"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dcd054ef",
"metadata": {
"trusted": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.23809523809523808 0.3333333333333333 0.6190476190476191 1.0\n"
]
}
],
"source": [
"print(cum_prob_5, cum_prob_2, cum_prob_6,cum_prob_8)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6b88c24b",
"metadata": {
"trusted": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.348270124550372"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from scipy import stats\n",
"\n",
"stats.uniform.rvs()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5ee9083c",
"metadata": {
"trusted": false
},
"outputs": [],
"source": [
"ht = []\n",
"\n",
"for i in range(100):\n",
" u=stats.uniform.rvs()\n",
" if u < 0.238095:\n",
" ht.append('5')\n",
" elif u < 0.33333:\n",
" ht.append('2')\n",
" elif u < 0.61904:\n",
" ht.append('6')\n",
" else:\n",
" ht.append('8')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "663c70b2",
"metadata": {
"trusted": false
},
"outputs": [
{
"data": {
"text/plain": [
"22"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ht_5s = ht.count('5')\n",
"ht_2s = ht.count('2')\n",
"ht_6s = ht.count('6')\n",
"ht_8s = ht.count('8')\n",
"\n",
"ht_5s"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "63bd4af2",
"metadata": {
"trusted": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.22"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ht_5s/100"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "804c5b2f",
"metadata": {
"trusted": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ht_2s/100"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "be7ccb7b",
"metadata": {
"trusted": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.32"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ht_6s/100"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a151740a",
"metadata": {
"trusted": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.36"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ht_8s/100"
]
}
],
"metadata": {
"gist": {
"data": {
"description": "Simulate a Weighted Probability of Numbers Given Known Weights",
"public": true
},
"id": ""
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.10"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment