From 49472431dc47eab387264f26b53b2b4b983788b2 Mon Sep 17 00:00:00 2001 From: Benja022 <113633123+Benja022@users.noreply.github.com> Date: Sat, 30 Nov 2024 11:32:08 +0100 Subject: [PATCH 1/2] Solved Lab --- lab-intro-probability.ipynb | 215 +++++++++++++++++++++++++++++++----- 1 file changed, 189 insertions(+), 26 deletions(-) diff --git a/lab-intro-probability.ipynb b/lab-intro-probability.ipynb index 5893fc1..9b48c6d 100644 --- a/lab-intro-probability.ipynb +++ b/lab-intro-probability.ipynb @@ -38,11 +38,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La probabilidad de que falten 10 pasajeros o más es: 0.1853\n" + ] + } + ], "source": [ - "#code here" + "import scipy.stats as stats\n", + "\n", + "# Número de boletos vendidos\n", + "n = 460\n", + "# Probabilidad de que un pasajero no se presente\n", + "p = 0.03\n", + "# Número de pasajeros que se presentan\n", + "k = 450\n", + "\n", + "# Calculamos la probabilidad de que 10 o más pasajeros no se presenten\n", + "prob = 1 - stats.binom.cdf(k - 1, n, 1 - p)\n", + "\n", + "print(f\"La probabilidad de que falten 10 pasajeros o más es: {prob:.4f}\")" ] }, { @@ -72,11 +92,27 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the representative needs at least three attempts is: 0.4900\n" + ] + } + ], "source": [ - "#code here" + "# Probabilidad de éxito en cada intento\n", + "p = 0.3\n", + "# Número de intentos\n", + "k = 3\n", + "\n", + "# Calculamos la probabilidad de que se necesiten al menos 3 intentos\n", + "prob = (1 - p) ** (k - 1)\n", + "\n", + "print(f\"The probability that the representative needs at least three attempts is: {prob:.4f}\")" ] }, { @@ -107,11 +143,29 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of the website server being overwhelmed is: 0.0129\n" + ] + } + ], "source": [ - "#code here" + "import scipy.stats as stats\n", + "\n", + "# Tasa promedio de visitas por hora\n", + "lambda_ = 500\n", + "# Capacidad máxima del servidor\n", + "max_visits = 550\n", + "\n", + "# Calculamos la probabilidad de que el número de visitas sea mayor que 550\n", + "prob = 1 - stats.poisson.cdf(max_visits, lambda_)\n", + "\n", + "print(f\"The probability of the website server being overwhelmed is: {prob:.4f}\")" ] }, { @@ -123,11 +177,38 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability of the website server being overwhelmed at some point during a day is: 0.2677\n" + ] + } + ], "source": [ - "#code here" + "import scipy.stats as stats\n", + "\n", + "# Tasa promedio de visitas por hora\n", + "lambda_ = 500\n", + "# Capacidad máxima del servidor\n", + "max_visits = 550\n", + "\n", + "# Calculamos la probabilidad de que el número de visitas sea mayor que 550 en una hora\n", + "prob_overwhelmed_per_hour = 1 - stats.poisson.cdf(max_visits, lambda_)\n", + "\n", + "# Calculamos la probabilidad de que el servidor no se vea abrumado en una hora\n", + "prob_not_overwhelmed_per_hour = 1 - prob_overwhelmed_per_hour\n", + "\n", + "# Calculamos la probabilidad de que el servidor no se vea abrumado en ninguna de las 24 horas\n", + "prob_not_overwhelmed_per_day = prob_not_overwhelmed_per_hour ** 24\n", + "\n", + "# Calculamos la probabilidad de que el servidor se vea abrumado al menos una vez en 24 horas\n", + "prob_overwhelmed_per_day = 1 - prob_not_overwhelmed_per_day\n", + "\n", + "print(f\"The probability of the website server being overwhelmed at some point during a day is: {prob_overwhelmed_per_day:.4f}\")" ] }, { @@ -157,10 +238,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the next customer will arrive within the next 5 minutes is: 0.3935\n" + ] + } + ], + "source": [ + "import scipy.stats as stats\n", + "\n", + "# Tasa promedio de llegadas (por minuto)\n", + "lambda_ = 1 / 10\n", + "# Tiempo en minutos\n", + "t = 5\n", + "\n", + "# Calculamos la probabilidad de que el próximo cliente llegue dentro de los próximos 5 minutos\n", + "prob = stats.expon.cdf(t, scale=1/lambda_)\n", + "\n", + "print(f\"The probability that the next customer will arrive within the next 5 minutes is: {prob:.4f}\")" + ] }, { "cell_type": "markdown", @@ -173,10 +274,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that an employee can take a break is: 0.2231\n" + ] + } + ], + "source": [ + "import scipy.stats as stats\n", + "\n", + "# Tasa promedio de llegadas (por minuto)\n", + "lambda_ = 1 / 10\n", + "# Tiempo en minutos\n", + "t = 15\n", + "\n", + "# Calculamos la probabilidad de que no haya clientes durante 15 minutos\n", + "prob = stats.expon.cdf(t, scale=1/lambda_)\n", + "\n", + "# La probabilidad de que no haya clientes durante 15 minutos es 1 menos la probabilidad acumulada\n", + "prob_no_customers = 1 - prob\n", + "\n", + "print(f\"The probability that an employee can take a break is: {prob_no_customers:.4f}\")" + ] }, { "cell_type": "markdown", @@ -196,11 +320,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the bird's weight is between 140 and 160 grams is: 0.6827\n" + ] + } + ], "source": [ - "#code here" + "import scipy.stats as stats\n", + "\n", + "# Parámetros de la distribución normal\n", + "mu = 150\n", + "sigma = 10\n", + "\n", + "# Pesos entre los que queremos calcular la probabilidad\n", + "lower_bound = 140\n", + "upper_bound = 160\n", + "\n", + "# Calculamos la probabilidad de que el peso esté entre 140 y 160 gramos\n", + "prob = stats.norm.cdf(upper_bound, mu, sigma) - stats.norm.cdf(lower_bound, mu, sigma)\n", + "\n", + "print(f\"The probability that the bird's weight is between 140 and 160 grams is: {prob:.4f}\")" ] }, { @@ -219,17 +364,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The probability that the component fails within the first 30 hours is: 0.4512\n" + ] + } + ], "source": [ - "#code here" + "import scipy.stats as stats\n", + "\n", + "# Tasa promedio de fallos (por hora)\n", + "lambda_ = 1 / 50\n", + "# Tiempo en horas\n", + "t = 30\n", + "\n", + "# Calculamos la probabilidad de que el componente falle dentro de las primeras 30 horas\n", + "prob = stats.expon.cdf(t, scale=1/lambda_)\n", + "\n", + "print(f\"The probability that the component fails within the first 30 hours is: {prob:.4f}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -243,7 +406,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.12.4" } }, "nbformat": 4, From 440a7ea445f25d6ee171f6d0213a1f756e8cc65a Mon Sep 17 00:00:00 2001 From: Benja022 <113633123+Benja022@users.noreply.github.com> Date: Sat, 30 Nov 2024 11:45:32 +0100 Subject: [PATCH 2/2] Refactoring challenge 2 --- lab-intro-probability.ipynb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lab-intro-probability.ipynb b/lab-intro-probability.ipynb index 9b48c6d..ed137bf 100644 --- a/lab-intro-probability.ipynb +++ b/lab-intro-probability.ipynb @@ -92,14 +92,15 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The probability that the representative needs at least three attempts is: 0.4900\n" + "The probability that the representative needs at least three attempts is: 0.4900\n", + "The probability that the representative needs exactly three attempts is: 0.1470\n" ] } ], @@ -109,10 +110,14 @@ "# Número de intentos\n", "k = 3\n", "\n", - "# Calculamos la probabilidad de que se necesiten al menos 3 intentos\n", - "prob = (1 - p) ** (k - 1)\n", + "# Calculamos la probabilidad de que se necesiten al menos 3 intentos (fallar en los primeros 2 intentos)\n", + "prob_at_least_3_attempts = (1 - p) ** (k - 1)\n", "\n", - "print(f\"The probability that the representative needs at least three attempts is: {prob:.4f}\")" + "# Calculamos la probabilidad de que se necesiten exactamente 3 intentos (fallar en los primeros 2 intentos y luego tener éxito)\n", + "prob_exactly_3_attempts = (1 - p) ** (k - 1) * p\n", + "\n", + "print(f\"The probability that the representative needs at least three attempts is: {prob_at_least_3_attempts:.4f}\")\n", + "print(f\"The probability that the representative needs exactly three attempts is: {prob_exactly_3_attempts:.4f}\")" ] }, {