{ "cells": [ { "cell_type": "code", "execution_count": 96, "id": "d10fb797-f9d4-40c7-ba48-08e4ece334f9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Data for sum\n", " x P(X) x * P(X)\n", "0 10 0.125000 1.250000\n", "1 11 0.125000 1.375000\n", "2 9 0.115741 1.041667\n", "3 12 0.115741 1.388889\n", "4 8 0.097222 0.777778\n", "5 13 0.097222 1.263889\n", "6 7 0.069444 0.486111\n", "7 14 0.069444 0.972222\n", "8 6 0.046296 0.277778\n", "9 15 0.046296 0.694444\n", "10 5 0.027778 0.138889\n", "11 16 0.027778 0.444444\n", "12 4 0.013889 0.055556\n", "13 17 0.013889 0.236111\n", "14 3 0.004630 0.013889\n", "15 18 0.004630 0.083333\n", "\n", "Expectation for sum: 10.5\n", "\n", "Data for product\n", " x P(X) x * P(X)\n", "0 12 0.069444 0.833333\n", "1 24 0.069444 1.666667\n", "2 30 0.055556 1.666667\n", "3 60 0.055556 3.333333\n", "4 36 0.055556 2.000000\n", "5 18 0.041667 0.750000\n", "6 72 0.041667 3.000000\n", "7 6 0.041667 0.250000\n", "8 48 0.041667 2.000000\n", "9 20 0.041667 0.833333\n", "10 8 0.032407 0.259259\n", "11 90 0.027778 2.500000\n", "12 40 0.027778 1.111111\n", "13 16 0.027778 0.444444\n", "14 10 0.027778 0.277778\n", "15 4 0.027778 0.111111\n", "16 15 0.027778 0.416667\n", "17 120 0.027778 3.333333\n", "18 32 0.013889 0.444444\n", "19 25 0.013889 0.347222\n", "20 108 0.013889 1.500000\n", "21 100 0.013889 1.388889\n", "22 96 0.013889 1.333333\n", "23 3 0.013889 0.041667\n", "24 80 0.013889 1.111111\n", "25 75 0.013889 1.041667\n", "26 150 0.013889 2.083333\n", "27 144 0.013889 2.000000\n", "28 5 0.013889 0.069444\n", "29 180 0.013889 2.500000\n", "30 50 0.013889 0.694444\n", "31 9 0.013889 0.125000\n", "32 45 0.013889 0.625000\n", "33 2 0.013889 0.027778\n", "34 54 0.013889 0.750000\n", "35 1 0.004630 0.004630\n", "36 125 0.004630 0.578704\n", "37 64 0.004630 0.296296\n", "38 27 0.004630 0.125000\n", "39 216 0.004630 1.000000\n", "Expectation for product 42.875\n" ] } ], "source": [ "import pandas as pd\n", " \n", "dice1 = [1, 2, 3, 4, 5, 6]\n", "dice2 = [1, 2, 3, 4, 5, 6]\n", "dice3 = [1, 2, 3, 4, 5, 6]\n", "\n", "\n", "data = [[d1, d2, d3, d1 + d2 + d3, d1 * d2 * d3] for d1 in dice1 for d2 in dice2 for d3 in dice3]\n", "\n", "df = pd.DataFrame(data, columns=[\"dice1\", \"dice2\", \"dice3\", \"sum_of_dots\", \"product_of_dots\"])\n", "total_count = len(dice1) * len(dice2) * len(dice3)\n", "\n", "# Expectation of sum of number of dots on the three rolls\n", "sum_pmf = [(x[0], count_x / (total_count * 1.0)) for x, count_x in df.value_counts([\"sum_of_dots\"]).items()]\n", "sum_pmf_df = pd.DataFrame(sum_pmf, columns=[\"x\", \"P(X)\"])\n", "sum_pmf_df[\"x * P(X)\"] = sum_pmf_df[\"x\"] * sum_pmf_df[\"P(X)\"] \n", "expectation_sum = sum_pmf_df[\"x * P(X)\"].sum()\n", "\n", "# Expectation of product of number of dots on the three rolls\n", "product_pmf = [(x[0], count_x / (total_count * 1.0)) for x, count_x in df.value_counts([\"product_of_dots\"]).items()]\n", "product_pmf_df = pd.DataFrame(product_pmf, columns=[\"x\", \"P(X)\"])\n", "product_pmf_df[\"x * P(X)\"] = product_pmf_df[\"x\"] * product_pmf_df[\"P(X)\"] \n", "expectation_product = product_pmf_df[\"x * P(X)\"].sum()\n", "\n", "print(\"\\nData for sum\")\n", "print(sum_pmf_df)\n", "print(\"\\nExpectation for sum: {}\".format(expectation_sum))\n", "\n", "print(\"\\nData for product\")\n", "print(product_pmf_df)\n", "print(\"Expectation for product {}\".format(expectation_product))" ] }, { "cell_type": "code", "execution_count": 78, "id": "301ba7da-d523-48eb-8a6d-25e8d6fa5ffe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
xP(X)
0100.125000
1110.125000
290.115741
3120.115741
480.097222
\n", "
" ], "text/plain": [ " x P(X)\n", "0 10 0.125000\n", "1 11 0.125000\n", "2 9 0.115741\n", "3 12 0.115741\n", "4 8 0.097222" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": 62, "id": "42954464-bab8-44fc-9087-8c4f07bac679", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": 66, "id": "4de2391b-e130-405b-bc26-b04584b2ed9c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 70, "id": "56b9fd02-79ec-4c7d-9eca-17c04a7df44d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 71, "id": "09e2316e-e242-48ab-9803-ef9e9212ea94", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 72, "id": "45a28a91-dc74-469b-8a1c-caa43ed0b907", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2268\n" ] } ], "source": [ "print(expectation)" ] }, { "cell_type": "code", "execution_count": null, "id": "b4b63417-d541-44d1-98ad-2027865e1818", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }