Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nuevo ejercicio #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notebook/problems.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells":[{"cell_type":"markdown","id":"a41f3622","metadata":{},"source":["# Probability exercises"]},{"cell_type":"markdown","id":"a6bdbd89","metadata":{},"source":["## Exercise 1 \n","\n","Two dices are thrown once and the total score is observed. Use a simulation to find the estimated probability that the total score is even or greater than 7. A simulation is a repetition of the same experiment multiple times to observe its behavior:\n","\n","- Run the experiment 1000 times (roll 2 dice 1000 times, and sum the number of both dices).\n","- Keep track of the number of times that the sum was either greater than 7 or an even number.\n","- Divide the number from step 2 by the number of iterations (1000)."]},{"cell_type":"code","execution_count":1,"id":"8939d892","metadata":{},"outputs":[],"source":["# TODO"]},{"cell_type":"markdown","id":"55732bca","metadata":{},"source":["## Exercise 2\n","\n","A box contains 10 white balls, 20 red balls and 30 green balls. If we take 5 balls from the box with replacement (we take the ball, observe what color it is and put it back into the box). We want to know the probability of:\n","\n","1. Take 3 white and 2 red.\n","2. All are the same color.\n","\n","Run the experiment 1000 times and calculate the above probabilities."]},{"cell_type":"code","execution_count":2,"id":"23244d20","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{0: 'White', 1: 'White', 2: 'White', 3: 'White', 4: 'White', 5: 'White', 6: 'White', 7: 'White', 8: 'White', 9: 'White', 10: 'Red', 11: 'Red', 12: 'Red', 13: 'Red', 14: 'Red', 15: 'Red', 16: 'Red', 17: 'Red', 18: 'Red', 19: 'Red', 20: 'Red', 21: 'Red', 22: 'Red', 23: 'Red', 24: 'Red', 25: 'Red', 26: 'Red', 27: 'Red', 28: 'Red', 29: 'Red', 30: 'Green', 31: 'Green', 32: 'Green', 33: 'Green', 34: 'Green', 35: 'Green', 36: 'Green', 37: 'Green', 38: 'Green', 39: 'Green', 40: 'Green', 41: 'Green', 42: 'Green', 43: 'Green', 44: 'Green', 45: 'Green', 46: 'Green', 47: 'Green', 48: 'Green', 49: 'Green', 50: 'Green', 51: 'Green', 52: 'Green', 53: 'Green', 54: 'Green', 55: 'Green', 56: 'Green', 57: 'Green', 58: 'Green', 59: 'Green'}\n"]}],"source":["ball_box = {}\n","\n","# Create the box of balls\n","for i in range(60):\n"," if i < 10:\n"," ball_box[i] = \"White\"\n"," elif (i > 9) and (i < 30):\n"," ball_box[i] = \"Red\"\n"," else:\n"," ball_box[i] = \"Green\"\n","\n","print(ball_box)\n"," \n","# TODO"]}],"metadata":{"interpreter":{"hash":"9248718ffe6ce6938b217e69dbcc175ea21f4c6b28a317e96c05334edae734bb"},"kernelspec":{"display_name":"Python 3.9.12 ('ML-BOOTCAMP')","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.11.4"}},"nbformat":4,"nbformat_minor":5}
{"cells":[{"cell_type":"markdown","id":"a41f3622","metadata":{},"source":["# Probability exercises"]},{"cell_type":"markdown","id":"a6bdbd89","metadata":{},"source":["## Exercise 1 \n","\n","Two dices are thrown once and the total score is observed. Use a simulation to find the estimated probability that the total score is even or greater than 7. A simulation is a repetition of the same experiment multiple times to observe its behavior:\n","\n","- Run the experiment 1000 times (roll 2 dice 1000 times, and sum the number of both dices).\n","- Keep track of the number of times that the sum was either greater than 7 or an even number.\n","- Divide the number from step 2 by the number of iterations (1000)."]},{"cell_type":"code","execution_count":4,"id":"8939d892","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["La probabilidad de sacar más de un 7 en la suma de los 2 dados es de 65.0%\n"]}],"source":["import numpy as np\n","import random\n","\n","def experimento_dados(n_lanzamientos = 1000):\n"," resultado = 0\n","\n"," for i in range(n_lanzamientos):\n","\n"," dado1 = random.randint(1,6)\n"," dado2 = random.randint(1,6)\n"," \n"," sucesos = dado1 + dado2\n"," if (sucesos %2 == 0) or sucesos > 7:\n"," resultado+=1\n"," return resultado/ n_lanzamientos\n","\n","print(f\"La probabilidad de sacar más de un 7 en la suma de los 2 dados es de {round(experimento_dados()*100,2)}%\")\n","\n","\n","\n","\n","\n","\n"]},{"cell_type":"markdown","id":"55732bca","metadata":{},"source":["## Exercise 2\n","\n","A box contains 10 white balls, 20 red balls and 30 green balls. If we take 5 balls from the box with replacement (we take the ball, observe what color it is and put it back into the box). We want to know the probability of:\n","\n","1. Take 3 white and 2 red.\n","2. All are the same color.\n","\n","Run the experiment 1000 times and calculate the above probabilities."]},{"cell_type":"code","execution_count":9,"id":"23244d20","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{0: 'White', 1: 'White', 2: 'White', 3: 'White', 4: 'White', 5: 'White', 6: 'White', 7: 'White', 8: 'White', 9: 'White', 10: 'Red', 11: 'Red', 12: 'Red', 13: 'Red', 14: 'Red', 15: 'Red', 16: 'Red', 17: 'Red', 18: 'Red', 19: 'Red', 20: 'Red', 21: 'Red', 22: 'Red', 23: 'Red', 24: 'Red', 25: 'Red', 26: 'Red', 27: 'Red', 28: 'Red', 29: 'Red', 30: 'Green', 31: 'Green', 32: 'Green', 33: 'Green', 34: 'Green', 35: 'Green', 36: 'Green', 37: 'Green', 38: 'Green', 39: 'Green', 40: 'Green', 41: 'Green', 42: 'Green', 43: 'Green', 44: 'Green', 45: 'Green', 46: 'Green', 47: 'Green', 48: 'Green', 49: 'Green', 50: 'Green', 51: 'Green', 52: 'Green', 53: 'Green', 54: 'Green', 55: 'Green', 56: 'Green', 57: 'Green', 58: 'Green', 59: 'Green'}\n","La probabilidad de 3 blancas y 2 rojas es de: 0.4%\n","La probabilidad de sacar todas las bolas del mismo color es de: 3.6%\n"]}],"source":["\n","ball_box = {}\n","\n","# Create the box of balls\n","for i in range(60):\n"," if i < 10:\n"," ball_box[i] = \"White\"\n"," elif (i > 9) and (i < 30):\n"," ball_box[i] = \"Red\"\n"," else:\n"," ball_box[i] = \"Green\"\n","\n","print(ball_box)\n"," \n","def bolas(n_simulaciones = 1000):\n"," resultados_1 = 0\n"," resultados_2 = 0\n","\n"," for i in range(n_simulaciones):\n"," colores = []\n","\n"," for i in range(5):\n"," colores.append(ball_box[np.random.randint(0, 59)])\n","\n"," colores = np.array(colores)\n"," \n"," bolas_blancas = sum(colores == \"White\")\n"," bolas_rojas = sum(colores == \"Red\")\n"," bolas_verdes = sum(colores == \"Green\")\n","\n"," \n"," if (bolas_blancas == 3) and (bolas_rojas == 2):\n"," resultados_1 += 1\n"," \n"," if (bolas_blancas == 5) or (bolas_rojas == 5) or (bolas_verdes == 5):\n"," resultados_2 += 1\n"," \n"," return resultados_1 / n_simulaciones, resultados_2 / n_simulaciones\n","\n","probabilidad = bolas(1000)\n","\n","print(f\"La probabilidad de 3 blancas y 2 rojas es de: {np.round(probabilidad[0] * 100, 2)}%\")\n","print(f\"La probabilidad de sacar todas las bolas del mismo color es de: {np.round(probabilidad[1] * 100, 2)}%\")"]}],"metadata":{"interpreter":{"hash":"9248718ffe6ce6938b217e69dbcc175ea21f4c6b28a317e96c05334edae734bb"},"kernelspec":{"display_name":"Python 3.9.12 ('ML-BOOTCAMP')","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.11.4"}},"nbformat":4,"nbformat_minor":5}