File size: 5,473 Bytes
bcf16ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
{
"cells": [
{
"cell_type": "markdown",
"id": "20fce00f",
"metadata": {},
"source": [
"# ML, Data Analysis\n",
"### Probability: law of total expectation \n",
"\n",
"The Law of Total Expectation is a fundamental rule in probability that relates the expected value of a random variable to its conditional expectations. For two random variables $X$ and $Y$, the law states that:\n",
"<br>$E[X]=E[E[X|Y]]$\n",
"<hr>\n",
"\n",
"For a **discrete random variable** $Y$, the law of total expectation becomes:\n",
"<br>$E[X]=∑_y P(Y=y)\\cdot E[X|Y=y]$\n",
"<br>where $P(Y=y)$ is the probability of the random variable $Y$ takes the value $y$. Moreover, $E[X|Y=y]$ is the expected value of $X$ given that $Y=y$.\n",
"<hr>\n",
"\n",
"For a **continuous random variable** $Y$, the law of total expectation becomes:\n",
"<br>$E[X]=\\int_{-\\infty}^{\\infty} E[X|Y=y]\\cdot f_Y(y)dy$\n",
"<br>where $f_Y(y)$ is the probability (density) function (PDF) of $Y$.\n",
"<hr>\n",
"\n",
"**Contents:**\n",
" - Using the law of total expectation for a discrete random variable $Y$\n",
" - Using the law of total expectation for a continuous random variable $Y$\n",
"<hr>\n",
"https://github.com/ostad-ai/Machine-Learning\n",
"<br> Explanation: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/background-knowledge"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f0c89242",
"metadata": {},
"outputs": [],
"source": [
"# Import thr required module\n",
"import numpy as np\n",
"# Import the required function for integration\n",
"from scipy.integrate import quad"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ac135579",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Expected number of ice creams sold a day: 36.5\n"
]
}
],
"source": [
"# Example: discrete random variable\n",
"# Y represents the type of weather: sunny, cloudy, rainy, or snowy.\n",
"# X represents the number of ice creams sold in a day, which depends on the weather.\n",
"# We want to compute the expected number of ice creams sold E[X]\n",
"\n",
"# Define the probabilities of each weather type\n",
"weather_probabilities = {\n",
" \"sunny\": 0.5, # Probability of sunny weather\n",
" \"cloudy\": 0.3, # Probability of cloudy weather\n",
" \"rainy\": 0.1, # Probability of rainy weather\n",
" \"snowy\": 0.1 # Probability of snowy weather\n",
"}\n",
"\n",
"# Define the conditional expectations E[X | Y = y] for each weather type\n",
"conditional_expectations = {\n",
" \"sunny\": 55, # Expected ice creams sold if sunny\n",
" \"cloudy\": 25, # Expected ice creams sold if cloudy\n",
" \"rainy\": 10, # Expected ice creams sold if rainy\n",
" \"snowy\": 5 # Expected ice creams sold if snowy\n",
"}\n",
"\n",
"# Compute the total expectation E[X] using the Law of Total Expectation\n",
"total_expectation = 0\n",
"for weather, prob in weather_probabilities.items():\n",
" total_expectation += prob * conditional_expectations[weather]\n",
"\n",
"# Print the result\n",
"print(f\"Expected number of ice creams sold a day: {total_expectation}\")"
]
},
{
"cell_type": "markdown",
"id": "7dd1a6a5",
"metadata": {},
"source": [
"<hr style=\"height:5px; background-color:green\">"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d70cec57",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Expected number of ice creams sold a day: 40.00000000382662\n"
]
}
],
"source": [
"# Example: continuous random variable\n",
"\n",
"# X is the number of ice creams sold, which depends on the temperature Y. \n",
"# The relationship is given in function conditional_expectation.\n",
"# Y is a continuous random variable representing the temperature\n",
"# (in Celsius) on a given day, with a uniform distribution between 0 and 30.\n",
"\n",
"# Define the PDF of Y\n",
"def pdf_Y(y):\n",
" return 1/30 if 0 <= y <= 30 else 0\n",
"\n",
"# Define the conditional expectation E[X | Y = y]\n",
"def conditional_expectation(y):\n",
" return 10 + 2 * y\n",
"\n",
"# Define the integrand for the Law of Total Expectation\n",
"def integrand(y):\n",
" return conditional_expectation(y) * pdf_Y(y)\n",
"\n",
"# Compute the expected value using numerical integration\n",
"expected_value, _ = quad(integrand, -np.inf, np.inf)\n",
"\n",
"print(f\"Expected number of ice creams sold a day: {expected_value}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d47e820d",
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|