Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
nurfnick committed Nov 8, 2023
1 parent 9bee4f6 commit 477a78e
Showing 1 changed file with 323 additions and 0 deletions.
323 changes: 323 additions & 0 deletions Solutions_to_Selected_Problems/SimulationProject.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPYIF8u/qzvbHCNXsh/jbI+",
"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/github/nurfnick/Operations_Research/blob/main/Solutions_to_Selected_Problems/SimulationProject.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Solutions to Simulation Project"
],
"metadata": {
"id": "DyS8R-7sNWVp"
}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "zVyGjZFHNVc9",
"outputId": "fb6c7b91-4169-43cc-d93f-e47ad5fe6868"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"10"
]
},
"metadata": {},
"execution_count": 2
}
],
"source": [
"import random\n",
"\n",
"random.choices([10,9],[9,1])[0]"
]
},
{
"cell_type": "markdown",
"source": [
"Let's do it a bunch"
],
"metadata": {
"id": "wpyL5dEWNr5X"
}
},
{
"cell_type": "code",
"source": [
"cho = []\n",
"\n",
"for i in range(1000):\n",
" cho.append(random.choices([10,9],[9,1])[0])"
],
"metadata": {
"id": "KkAVPYezNmfp"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"sum(cho)/len(cho)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1TqqGqUqN47q",
"outputId": "88dd91cb-810d-4840-9ebb-a49ec4b809df"
},
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"9.896"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "markdown",
"source": [
"I meant for you to use a simple binomial approach. With success being $\\frac1{10}$ Let's see how I might simulate that."
],
"metadata": {
"id": "KFVNovugO44i"
}
},
{
"cell_type": "code",
"source": [
"random.choices([1,0],[1,9])[0]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NI3aPmz6N8BL",
"outputId": "42cf7ff0-c7c6-4c09-acca-b925f9cb8536"
},
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"source": [
"numtests = []\n",
"for i in range(1000):\n",
" test = []\n",
" while sum(test) ==0:\n",
" test.append(random.choices([1,0],[1,9])[0])\n",
" numtests.append(len(test))"
],
"metadata": {
"id": "MRHYESViPPoc"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"sum(numtests)/len(numtests)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TyZ3OysGPqI5",
"outputId": "d825151f-1589-40ac-e563-2baf78705eb3"
},
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"9.975"
]
},
"metadata": {},
"execution_count": 12
}
]
},
{
"cell_type": "code",
"source": [
"test.index(1)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2c-6OXUTP05n",
"outputId": "1d8c5601-8329-4408-d0b8-b05f071abc99"
},
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"9"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"rs = [10,10,10,10,10,10,10,10,10,9]\n",
"random.shuffle(rs)\n",
"rs"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "d2VTdmw0QvNB",
"outputId": "5493503e-d241-40e4-c6da-e33eb9df24c9"
},
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[10, 9, 10, 10, 10, 10, 10, 10, 10, 10]"
]
},
"metadata": {},
"execution_count": 27
}
]
},
{
"cell_type": "code",
"source": [
"rs.index(9)+1"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vl0LHOyZQ5BP",
"outputId": "3d44dbc3-584a-4c6f-c99d-fdf304b0e3a4"
},
"execution_count": 30,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"execution_count": 30
}
]
},
{
"cell_type": "code",
"source": [
"pos = []\n",
"\n",
"for i in range(1000):\n",
" random.shuffle(rs)\n",
" pos.append(rs.index(9)+1)"
],
"metadata": {
"id": "Sotpea4JQ_oI"
},
"execution_count": 34,
"outputs": []
},
{
"cell_type": "code",
"source": [
"sum(pos)/len(pos)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9_NNlvcvRjGY",
"outputId": "7a794b4e-d006-4f8d-e96b-d4273df27e33"
},
"execution_count": 35,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5.379"
]
},
"metadata": {},
"execution_count": 35
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "r3vvsxphRlYO"
},
"execution_count": null,
"outputs": []
}
]
}

0 comments on commit 477a78e

Please sign in to comment.