Skip to content

Instantly share code, notes, and snippets.

@josh-hernandez-exe
Last active October 5, 2020 18:14
Show Gist options
  • Save josh-hernandez-exe/2987a0feb052bde7f9945a902ac2b277 to your computer and use it in GitHub Desktop.
Save josh-hernandez-exe/2987a0feb052bde7f9945a902ac2b277 to your computer and use it in GitHub Desktop.
625.603 Discussion Assignment 3
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction\n",
"\n",
"Using question 3.9.17, we explore many of the theorems we have at our disposal to evaluate expectation values and variances of either single and multivariate probability density functions. Three variants to solve the question are explored focusing on Theorems 3.8.3, 3.9.1, and 3.9.5.\n",
"\n",
"As a supplement, [`sympy`](https://sympy.org) (python library for symbolic computing) will used verify all algebraic steps and use to demonstrate how you would go about doing so with `sympy`.\n",
"\n",
"In my [previous report](https://gist.github.com/josh-hernandez-exe/c331d6cb5cfb5127df6db126fc555c47) I explain the pros of symbolic computation, pros and cons of `sympy` and python, as well as alternative symbolic computation platforms.\n",
"\n",
"Below is just some helpful modules to load and other boilerplate that you can ignore."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [
{
"data": {
"application/javascript": [
"// This is some random code to get Latex Equation numbeing turned on inside of Jupyter Lab\n",
"MathJax.Hub.Config({\n",
" TeX: { equationNumbers: { autoNumber: \"AMS\" } }\n",
"});\n"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%javascript\n",
"// This is some random code to get Latex Equation numbeing turned on inside of Jupyter Lab\n",
"MathJax.Hub.Config({\n",
" TeX: { equationNumbers: { autoNumber: \"AMS\" } }\n",
"});"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from sympy.abc import * # this loads most commonly used variable assignments in math\n",
"\n",
"# Loading this second is deliberate\n",
"# So we have access to the singleton variable `S` which in sympy contains many useful\n",
"# singlton variables\n",
"from sympy import * # this loads all top level sympy functions for convience"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conveniences\n",
"\n",
"Before we begin, I'm going to state some calculations that will frequently come up. This will get some algebraic clutter out of the way. These will be solved as an [appendix](#appendix).\n",
"\n",
"\\begin{equation}\n",
" \\lim_{x\\to\\infty} \\left( x^n e^{-\\lambda \\ x} \\right) = 0\n",
" \\label{eq:limexp}\n",
"\\end{equation}\n",
"\n",
"\\begin{equation}\n",
" \\int_{0}^{\\infty} e^{-\\lambda \\ x} = \\frac{1}{\\lambda}\n",
" \\label{eq:intexp}\n",
"\\end{equation}\n",
"\n",
"\\begin{equation}\n",
" \\int_{0}^{\\infty} x e^{-\\lambda \\ x} = \\frac{1}{\\lambda^2}\n",
" \\label{eq:intxexp}\n",
"\\end{equation}\n",
"\n",
"\\begin{equation}\n",
" \\int_{0}^{\\infty} x^2 e^{-\\lambda \\ x} = \\frac{2}{\\lambda^3}\n",
" \\label{eq:intxsqexp}\n",
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question Statement\n",
"\n",
"Suppose we have a multivariate pdf (probability density function)\n",
"$$f_{X,Y}(x,y) = \\lambda^{2}e^{-\\lambda(x+y)}$$\n",
"which is defined for $x,y\\in[0,\\infty)$. We wish to calculate $\\mathrm{Var}(X+Y)$.\n",
"\n",
"It's important to notice that $\\lambda$ must be a positive number, otherwise $f_{X,Y}$ couldn't be a valid pdf.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Method 1: Theorem 3.9.5\n",
"\n",
"This is probably the most straight forward approach. The theorem says:\n",
"$$\n",
"\\mathrm{Var}(aX+bY) = a^2\\mathrm{Var}(X) + b^2\\mathrm{Var}(Y) + 2ab\\ \\mathrm{Cov}(X,Y)\n",
"$$\n",
"In our case $a=1$ and $b=1$, so it simplifies down to\n",
"$$\n",
"\\mathrm{Var}(X+Y) = \\mathrm{Var}(X) + \\mathrm{Var}(Y) + \\mathrm{Cov}(X,Y)\n",
"$$\n",
"\n",
"Which means we need to calculate $\\mathrm{Var}(X)$, $\\mathrm{Var}(Y)$, and $\\mathrm{Cov}(X,Y)$.\n",
"\n",
"How can we calculate these? Well the variance from Definition 3.6.1, we require $\\mathrm{E}[X]$. Since we have a multivariate pdf, we will have to calculate the marginal pdf by applying Theorem 3.7.2.\n",
"\n",
"Conveniently $f_{X,Y}$ is symmetric in $X$ and $Y$, so we only demonstrate for $f_{X}$.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
"f_{X}(x) \n",
" &= \\int_{-\\infty}^{\\infty} f_{X,Y}(x,y) \\mathrm{d}y & \\mathrm{J1} \\\\\n",
" &= \\int_{0}^{\\infty} \\lambda^{2}e^{-\\lambda(x+y)} \\mathrm{d}y & \\mathrm{J2} \\\\\n",
" &= \\lambda^{2} e^{-\\lambda\\cdot x} \\int_{0}^{\\infty} e^{-\\lambda\\cdot y} \\mathrm{d}y & \\mathrm{J3} \\\\\n",
" &= \\lambda^{2} e^{-\\lambda\\cdot x} \\left[ \\frac{1}{\\lambda} \\right] & \\mathrm{J4} \\\\\n",
" &= \\lambda e^{-\\lambda\\cdot x} \\\\\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying definition 3.6.1 for a marginal pdf.\n",
"* $\\mathrm{J2}$: Substituting $f_{X,Y}$ for the current problem, and adjusting the bounds since $x\\in[0,\\infty)$ for our given multivariate pdf.\n",
"* $\\mathrm{J3}$: Since $x$ is a constant relative to the integral with respect to $y$.\n",
"* $\\mathrm{J4}$: Applying integral Eq $\\eqref{eq:intexp}$\n",
"\n",
"\n",
"Great, so we have:\n",
"\n",
"\\begin{equation}\n",
" f_{X}(x) = \\lambda e^{-\\lambda\\cdot x}\n",
"\\label{eq:f_x}\n",
"\\end{equation}\n",
"\n",
"Notice that this is defined only for $x\\in[0,\\infty)$ much the same as in $f_{X,Y}$.\n",
"\n",
"Lets verify computationally. Here is a link to the documentation for the `sympy` function [`integrate`](https://docs.sympy.org/latest/modules/integrals/integrals.html#sympy.integrals.integrals.integrate) if you want to learn more about it. As we will be using it a lot to verify our algebra."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Some setup needed\n",
"\n",
"# we need to apply the assumption `positive` othewise some integrals get out hand trying to handle other cases\n",
"lamda = symbols(\"\\lambda\", positive=True)\n",
"\n",
"# Define our multivariate function here\n",
"f_xy = lamda**2 * exp(-lamda*(x+y))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\lambda^{2} e^{- \\lambda \\left(x + y\\right)}$"
],
"text/plain": [
"\\lambda**2*exp(-\\lambda*(x + y))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_xy"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# This could be all in one line, but I often break out a function call on multiple lines for clarify\n",
"f_x = integrate(\n",
" f_xy,\n",
" (y,0,oo), # Note that the oo variable is a stand in for infinity and was defined by sympy\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\lambda e^{- \\lambda x}$"
],
"text/plain": [
"\\lambda*exp(-\\lambda*x)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great! So we validated our result. We'll now quickly do the same for $Y$."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"f_y = integrate(\n",
" f_xy,\n",
" (x,0,oo),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\lambda e^{- \\lambda y}$"
],
"text/plain": [
"\\lambda*exp(-\\lambda*y)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now need the expectation value $E[X]$.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
"E[X] \n",
" &= \\int_{-\\infty}^{\\infty} x \\cdot f_{X}(x) \\mathrm{d}x & \\mathrm{J1} \\\\\n",
" &= \\int_{0}^{\\infty} x \\cdot \\lambda e^{-\\lambda\\cdot x} \\mathrm{d}x & \\mathrm{J2} \\\\\n",
" &= \\lambda\\int_{0}^{\\infty} x \\cdot e^{-\\lambda\\cdot x} \\mathrm{d}x \\\\\n",
" &= \\lambda\n",
" \\left(\n",
" \\frac{1}{\\lambda^2}\n",
" \\right)\n",
" & \\mathrm{J3} \\\\\n",
" &= \\frac{1}{\\lambda} \\\\\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: The definition of an expectation value (definition 3.5.1)\n",
"* $\\mathrm{J2}$: Substituting the marginal pdf $f_{X}$ that we calculated earlier (Eq \\eqref{eq:f_x}), and adjusting the bounds since $x\\in[0,\\infty)$.\n",
"* $\\mathrm{J3}$: From Eq $\\eqref{eq:intxexp}$ we know $\\int_{0}^{\\infty} x e^{-\\lambda \\ x} = \\frac{1}{\\lambda^2}$.\n",
"\n",
"So we have calculated \n",
"\n",
"\\begin{equation}\n",
" E[X]=\\frac{1}{\\lambda} \\\\\n",
" \\label{eq:e_x}\n",
" E[Y]=\\frac{1}{\\lambda}\n",
"\\end{equation}\n",
"\n",
"Lets check computationally."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"e_x = integrate(\n",
" x*f_x,\n",
" (x,0,oo),\n",
")\n",
"e_y = integrate(\n",
" y*f_y,\n",
" (y,0,oo),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda}$"
],
"text/plain": [
"1/\\lambda"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_x"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda}$"
],
"text/plain": [
"1/\\lambda"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So now that we have the expectation values. We can now calculate the variance.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
"\\mathrm{Var}(X)\n",
" &= \\mathrm{E}[X^2] - \\left(\\mathrm{E}[X] \\right)^2 & \\mathrm{J1} \\\\\n",
" &= \\mathrm{E}[X^2] - \\left(\\frac{1}{\\lambda} \\right)^2 & \\mathrm{J2} \\\\ \\\\\n",
" &= \\left(\n",
" \\int_{-\\infty}^{\\infty}\n",
" x^2 f_{X}(x)\n",
" \\mathrm{d}x\n",
" \\right)\n",
" - \\frac{1}{\\lambda^2} & \\mathrm{J3} \\\\ \\\\\n",
" &= \\left(\n",
" \\int_{0}^{\\infty}\n",
" x^2 \\lambda e^{-\\lambda\\cdot x}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" - \\frac{1}{\\lambda^2} & \\mathrm{J4} \\\\ \\\\\n",
" &= \\lambda\n",
" \\left(\n",
" \\int_{0}^{\\infty}\n",
" x^2 e^{-\\lambda\\cdot x}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" - \\frac{1}{\\lambda^2} \\\\ \\\\\n",
" &= \\lambda\n",
" \\left(\n",
" \\frac{2}{\\lambda^3}\n",
" \\right)\n",
" - \\frac{1}{\\lambda^2} & \\mathrm{J5} \\\\ \\\\\n",
" &= \\frac{2}{\\lambda^2} \n",
" - \\frac{1}{\\lambda^2} \\\\\n",
" &= \\frac{1}{\\lambda^2} \\\\\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: By Theorem 3.6.1\n",
" * Literally states $\\mathrm{Var}(X) = \\mathrm{E}[X^2] - \\left(\\mathrm{E}[X] \\right)^2$\n",
"* $\\mathrm{J2}$: By using the expectation value we calculated prior at Eq \\eqref{eq:e_x}\n",
"* $\\mathrm{J3}$: Technically we are applying Theorem 3.5.3\n",
" * $\\mathrm{E}[g(Y)] = \\int_{-\\infty}^{\\infty}g(y)f_{Y}(y)\\mathrm{d}y$\n",
"* $\\mathrm{J4}$: Substituting the marginal pdf we calculated at Eq $\\eqref{eq:f_x}$\n",
"* $\\mathrm{J5}$: From Eq $\\eqref{eq:intxsqexp}$ we know $\\int_{0}^{\\infty} x^2 e^{-\\lambda \\ x} = \\frac{2}{\\lambda^3}$\n",
"\n",
"Remember the symmetry that the $X$ and $Y$ have in their joint pdf $f_{X,Y}$. So the calculation would of been exactly the same for $Y$.\n",
"\n",
"So we have calculated \n",
"\n",
"\\begin{equation}\n",
" \\mathrm{Var}(X)=\\frac{1}{\\lambda^2} \\\\\n",
" \\label{eq:var_x}\n",
" \\mathrm{Var}(Y)=\\frac{1}{\\lambda^2} \\\\\n",
"\\end{equation}\n",
"\n",
"\n",
"Again lets check computationally."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"e_x_sqr = integrate(\n",
" x**2*f_x,\n",
" (x,0,oo),\n",
")\n",
"e_y_sqr = integrate(\n",
" y**2*f_y,\n",
" (y,0,oo),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda^{2}}$"
],
"text/plain": [
"\\lambda**(-2)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The variance of X applying Theorem 3.6.1\n",
"e_x_sqr - e_x **2"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda^{2}}$"
],
"text/plain": [
"\\lambda**(-2)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The variance of Y applying Theorem 3.6.1\n",
"e_y_sqr - e_y **2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are almost there! We just need to calculate $\\mathrm{Cov}(X,Y)$. But we have a few tricks up our sleeve. Notice that we can write $f_{X,Y}$ as a product of two exponentials, one of each of the variables.\n",
"\n",
"<a id=\"xy_independence\"></a>$\\label{sec:xy_independence}$\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" f_{X,Y}(x,y)\n",
" &= \\lambda^2 \\cdot e^{-\\lambda\\cdot(x+y)} \\\\\n",
" &= \\left(\\lambda \\cdot e^{-\\lambda\\cdot x}\\right)\\left(\\lambda \\cdot e^{-\\lambda\\cdot y}\\right) \\\\\n",
" &= f_{X}(x) \\cdot f_{Y}(y)\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"So we can can write $f_{X,Y}(x,y)$ as the product $f_{X}(x) \\cdot f_{Y}(y)$. Why is this amazing? Consider the statement of Theorem 3.7.4.\n",
"\n",
"| *The continuous variables $X$ and $Y$ are independent **if and only if** there are functions $g(x)$ and $h(y)$ such that $f_{X,Y}(x,y) = g(x)h(x)$.*\n",
"\n",
"Which is exactly what we have. Because of the \"if and only if* finding a pair of functions that satisfies the separability equation is enough to know with sure confidence that $X$ and $Y$ are independent variables in our given problem.\n",
"\n",
"Knowing that $X$ and $Y$ are independent variables Theorem 3.9.4 and conclude that\n",
"\n",
"\\begin{equation}\n",
" \\mathrm{Cov}(X,Y) = 0\n",
" \\label{eq:cov_xy}\n",
"\\end{equation}\n",
"\n",
"With everything in place we can now carry out the requested computation\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\mathrm{Var}(X+Y) \n",
" &= \\mathrm{Var}(X) + \\mathrm{Var}(Y) + 2 \\cdot \\mathrm{Cov}(X,Y)\n",
" & \\mathrm{J1} \\\\\n",
" &= \\mathrm{Var}(X) + \\mathrm{Var}(Y) + 0\n",
" & \\mathrm{J2} \\\\\n",
" &= \\frac{1}{\\lambda^2} + \\frac{1}{\\lambda^2}\n",
" & \\mathrm{J3} \\\\\n",
" &= \\frac{2}{\\lambda^2} \\\\ \n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying Theorem 3.9.5\n",
" * $\\mathrm{Var}(aX+bY) = a^2\\mathrm{Var}(X) + b^2\\mathrm{Var}(Y) + 2ab\\ \\mathrm{Cov}(X,Y)$\n",
"* $\\mathrm{J2}$: Since we know $X$ and $Y$ are independent variables we apply Eq $\\eqref{eq:cov_xy}$\n",
"* $\\mathrm{J3}$: Plugging in the value of the variance we calculated from Eq $\\eqref{eq:cov_xy}$\n",
"\n",
"We thus have calculated the value asked of us from the problem.\n",
"\n",
"\\begin{equation}\n",
" \\mathrm{Var}(X+Y)=\\frac{2}{\\lambda^2} \\\\\n",
" \\label{eq:var_x_p_y}\n",
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Method 1 Computational Checks\n",
"\n",
"So with finishing method 1, there are some steps that we can verify with other methods. In particular I'm going to verify the variances and expectation value of the product $X\\cdot Y$ (a necessary step to calculating the variance in general) by their definitions without the use of additional theorems."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"var_x = integrate(\n",
" (x-e_x)**2 * f_x, # Variance: Definition 3.6.1\n",
" (x,0,oo),\n",
")\n",
"var_y = integrate(\n",
" (y-e_y)**2 * f_y, # Variance: Definition 3.6.1\n",
" (y,0,oo),\n",
")\n",
"e_xy = integrate(\n",
" x*y*f_xy, # E[XY] and apply Theorem 3.9.1 with g(x,y) = x*y\n",
" (x,0,oo),\n",
" (y,0,oo),\n",
")\n",
"print(simplify(Eq(\n",
" var_x,\n",
" e_x_sqr - e_x**2,\n",
")))\n",
"print(simplify(Eq(\n",
" var_y,\n",
" e_y_sqr - e_y**2,\n",
")))\n",
"print(simplify(Eq(\n",
" e_xy,\n",
" e_x*e_y,\n",
")))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda^{2}}$"
],
"text/plain": [
"\\lambda**(-2)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_x"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda^{2}}$"
],
"text/plain": [
"\\lambda**(-2)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_y"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda^{2}}$"
],
"text/plain": [
"\\lambda**(-2)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_xy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The latter three `print` statements to test the equalities. In `sympy` if you simplify a relation (like an equality for example), it will algebraically test the relation. We could not use the `==` operator in `python` and this oddity is explained in their [gotchas](https://docs.sympy.org/latest/gotchas.html#equals-signs) section.\n",
"\n",
"That aside, all the values are as we expect!\n",
"\n",
"One more tangent before moving on the next method. There is another way to calculate $\\mathrm{E}[XY]$ and that is to define a new random variable $V=X\\cdot Y$ and apply Theorem 3.8.5\n",
"$$\n",
" f_{V}(v)=\\int_{-\\infty}^{\\infty} \\frac{1}{|x|} f_{X}(x) f_{Y}(v/x) \\mathrm{d}x\n",
"$$\n",
"This gets ugly, but possible with symbolic computation."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Let V = X*Y\n",
"# we need to constrant the variable to be positive otherwise integration gets really out of hand\n",
"v = symbols(\"v\",positive=True)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"f_v = integrate(\n",
" (1/x)*f_x*f_y.subs(y,v/x), # don't need abs since x > 0 in this integral\n",
" # v/x \\in [0, inf] doesn't imply any bound on integration\n",
" (x,0,oo),\n",
")\n",
"e_v = integrate(\n",
" v*f_v,\n",
" (v,0,oo),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\lambda^{2} {G_{2, 0}^{0, 2}\\left(\\begin{matrix} 1, 1 & \\\\ & \\end{matrix} \\middle| {\\frac{1}{\\lambda^{2} v}} \\right)}$"
],
"text/plain": [
"\\lambda**2*meijerg(((1, 1), ()), ((), ()), 1/(\\lambda**2*v))"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Meijer G-Function\n",
"f_v"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1}{\\lambda^{2}}$"
],
"text/plain": [
"\\lambda**(-2)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_v"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The pdf of the new random variable $V$ is described by some special function that is far beyond the scope of this report. But the expectation value of $V$ matches the value we previously calculated for $\\mathrm{E}[XY]$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Method 2: Theorem 3.8.3\n",
"\n",
"With this method we define a new random variable $W=X+Y$ which we can then find the variance of using single variate methods. Recall from our previous method we discovered [independence](#xy_independence) [$\\ref{sec:xy_independence}$] of $X$ and $Y$. As a result we can apply Theorem 3.8.3 and define the pdf of this new variable will be defined as\n",
"\n",
"\\begin{equation}\n",
" f_W(w)\n",
" = \\int_{\\infty}^{\\infty}f_{X}(x)f_{Y}(w-x)\\mathrm{d}x\n",
" \\label{eq:pdf_sum}\n",
"\\end{equation}\n",
"\n",
"So lets calculate that pdf\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" f_W(w)\n",
" &= \\int_{\\infty}^{\\infty}\n",
" f_{X}(x)f_{Y}(w-x)\n",
" \\mathrm{d}x\n",
" & \\mathrm{J1} \\\\ \\\\\n",
" &= \\int_{0}^{w}\n",
" \\left(\n",
" \\lambda e^{-\\lambda x}\n",
" \\right)\n",
" \\left(\n",
" \\lambda e^{-\\lambda (w-x)}\n",
" \\right)\n",
" \\mathrm{d}x\n",
" & \\mathrm{J2} \\\\ \\\\\n",
" &= \\lambda^2 \\int_{0}^{w}\n",
" e^{-\\lambda x}\n",
" e^{-\\lambda w + \\lambda x}\n",
" \\mathrm{d}x\n",
" \\\\ \\\\\n",
" &= \\lambda^2 \\int_{0}^{w}\n",
" e^{-\\lambda w + 0}\n",
" \\mathrm{d}x\n",
" \\\\ \\\\\n",
" &= \\lambda^2 e^{-\\lambda w}\n",
" \\int_{0}^{w}\n",
" \\mathrm{d}x\n",
" & \\mathrm{J3} \\\\ \\\\\n",
" &= \\lambda^2 e^{-\\lambda w} w \\\\\n",
" &= \\lambda^2 w e^{-\\lambda w}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying Theorem 3.8.3\n",
"* $\\mathrm{J2}$: Substituting the marginal pdfs for $X$ and $Y$.\n",
" * Also note that the term $f_{Y}(w-x)$ enforces that $w-x \\ge 0$ and thus that $w\\ge x$\n",
"* $\\mathrm{J3}$: Since $e^{-\\lambda w}$ is independent of the integral\n",
"\n",
"So the pdf of our new variable $W$\n",
"\\begin{equation}\n",
" f_{W}(w)=\\lambda^2 w e^{-\\lambda w}\n",
" \\label{eq:pdf_w}\n",
"\\end{equation}\n",
"\n",
"Like usual lets check computationally."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"f_w = integrate(\n",
" f_x*f_y.subs(y,w-x),\n",
" (x,0,w)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\lambda^{2} w e^{- \\lambda w}$"
],
"text/plain": [
"\\lambda**2*w*exp(-\\lambda*w)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_w"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets now calculate the expectation value.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\mathrm{E}[W]\n",
" &= \\int_{-\\infty}^{\\infty}\n",
" w \\cdot f_{W}(w)\n",
" \\mathrm{d}w\n",
" & \\mathrm{J1} \\\\ \\\\\n",
" &= \\int_{0}^{\\infty}\n",
" w \\cdot\\lambda^2 w e^{-\\lambda w}\n",
" \\mathrm{d}w \n",
" & \\mathrm{J2} \\\\ \\\\\n",
" &= \\lambda^3\n",
" \\int_{0}^{\\infty}\n",
" w^2 e^{-\\lambda w}\n",
" \\mathrm{d}w \n",
" \\\\ \\\\\n",
" &= \\lambda^2\n",
" \\left(\n",
" \\frac{2}{\\lambda^3}\n",
" \\right)\n",
" & \\mathrm{J3} \\\\ \\\\\n",
" &= \\frac{2}{\\lambda}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying definition 3.5.1.\n",
"* $\\mathrm{J2}$: Our calculated pdf for $W=X+Y$ from Eq $\\eqref{eq:pdf_w}$\n",
" * Notice that $w\\ge 0$, which makes sense as it is a sum of two variables that can only take on positive values.\n",
"* $\\mathrm{J3}$: From Eq $\\eqref{eq:intxsqexp}$ we know $\\int_{0}^{\\infty} x^2 e^{-\\lambda \\ x} = \\frac{2}{\\lambda^3}$\n",
"\n",
"Thus we have\n",
"\\begin{equation}\n",
" \\mathrm{E}[W]=\\frac{2}{\\lambda}\n",
" \\label{eq:e_w}\n",
"\\end{equation}\n",
"\n",
"We verify computationally again."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"e_w = integrate(\n",
" w*f_w,\n",
" (w,0,oo)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2}{\\lambda}$"
],
"text/plain": [
"2/\\lambda"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_w"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets now calculate $\\mathrm{E}[W^2]$ to calculate variance.\n",
"<a id='e_wsqr'></a>$\\label{sec:e_wsqr}$\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\mathrm{E}[W^2]\n",
" &= \\int_{-\\infty}^{\\infty}\n",
" w^2 f_{W}(w)\n",
" \\mathrm{d}w\n",
" & \\mathrm{J1} \\\\ \\\\\n",
" &= \\int_{0}^{\\infty}\n",
" w^2\n",
" \\left(\n",
" \\lambda^2 w e^{-\\lambda w}\n",
" \\right)\n",
" \\mathrm{d}w\n",
" & \\mathrm{J2} \\\\ \\\\\n",
" &= \\lambda^2\\int_{0}^{\\infty}\n",
" w^3 e^{-\\lambda w}\n",
" \\mathrm{d}w\n",
" \\\\ \\\\\n",
" &= \\lambda^2\n",
" \\left(\n",
" \\frac{6}{\\lambda^4}\n",
" \\right)\n",
" & \\mathrm{J3} \\\\ \\\\\n",
" &= \\frac{6}{\\lambda^2}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: By Theorem 3.5.3 : $\\mathrm{E}[g(X)] = \\int_{-\\infty}^{\\infty}g(x)f_{X}(x)\\mathrm{d}x$\n",
"* $\\mathrm{J2}$: Our calculated pdf for $W=X+Y$ from Eq $\\eqref{eq:pdf_w}$\n",
"* $\\mathrm{J3}$: This integral is calculated in the [appendix](#Integral-needed-for-E[W2]\\mathrm{E}[W^2]-for-Method-2).\n",
" * $\\int_{0}^{\\infty}x^3 e^{-\\lambda x}\\mathrm{d}x = \\frac{6}{\\lambda^4}$\n",
"\n",
"Thus\n",
"\\begin{equation}\n",
" \\mathrm{E}[W^2] = \\frac{6}{\\lambda^2}\n",
" \\label{eq:e_wsqr}\n",
"\\end{equation}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we can now calculate the variance\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\mathrm{Var}(W)\n",
" &= \\mathrm{E}[W^2] - \\left( \\mathrm{E}[W] \\right)^2\n",
" & \\mathrm{J1} \\\\\n",
" &= \\left(\n",
" \\frac{6}{\\lambda^2}\n",
" \\right)\n",
" -\n",
" \\left(\n",
" \\frac{2}{\\lambda}\n",
" \\right)^2\n",
" & \\mathrm{J2} \\\\\n",
" &= \\frac{6}{\\lambda^2}\n",
" -\n",
" \\frac{4}{\\lambda^2}\n",
" \\\\\n",
" &= \\frac{2}{\\lambda^2}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying Theorem 3.6.1\n",
"* $\\mathrm{J2}$: Substituting in our values for $\\mathrm{E}[W^2]$ from Eq $\\eqref{eq:e_wsqr}$ and $\\mathrm{E}[W]$ from $\\eqref{eq:e_w}$.\n",
"\n",
"Thus we have\n",
"$$\n",
" \\mathrm{Var}(W)=\\mathrm{Var}(X+Y)=\\frac{2}{\\lambda^2} \\\\\n",
"$$\n",
"\n",
"We verify computationally."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"e_w_sqr = integrate(\n",
" w**2*f_w,\n",
" (w,0,oo)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2}{\\lambda^{2}}$"
],
"text/plain": [
"2/\\lambda**2"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_w_sqr - e_w**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Method 2 Computational Checks\n",
"\n",
"Here we will check the variance by applying the definition. But we will only check computationally."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"var_w = integrate(\n",
" (w-e_w)**2 * f_w,\n",
" (w,0,oo)\n",
")\n",
"print(simplify(Eq(\n",
" var_w,\n",
" # Theorem 3.6.1\n",
" e_w_sqr - e_w**2,\n",
")))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2}{\\lambda^{2}}$"
],
"text/plain": [
"2/\\lambda**2"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_w"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Potential Method 3 : Theorem 3.9.1\n",
"\n",
"This method is the most direct and is probably the shortest, but requires some extrapolation. Lets review the key theorem statement.\n",
"\n",
"\\begin{equation}\n",
" \\mathrm{E}[g(X,Y)]\n",
" = \\int_{-\\infty}^{\\infty}\n",
" \\int_{-\\infty}^{\\infty}\n",
" g(x,y) f_{X,Y}(x,y)\n",
" \\mathrm{d}x\n",
" \\mathrm{d}y\n",
"\\end{equation}\n",
"\n",
"We can define a function, $g$, on our random variables $X$ and $Y$ such that $g(x,y)=x+y$ then directly apply the theorem. So that gives us the expected value.\n",
"\n",
"We don't have anything from the text book about this, but we think we can extend this idea to the variance. If any readers discover any issues, please let the author know.\n",
"\n",
"$$\n",
" \\mathrm{Var}(g(X,Y) = \\mathrm{E}[{g^2(X,Y)}] - \\left(\\mathrm{E}[g(X,Y)]\\right)^2\n",
"$$\n",
"\n",
"Where $g^2(X,Y) = (g(X,Y))^2$ and\n",
"$$\n",
" \\mathrm{E}[{g^2(X,Y)}]\n",
" = \\int_{-\\infty}^{\\infty}\n",
" \\int_{-\\infty}^{\\infty}\n",
" g^2(x,y) f_{X,Y}(x,y)\n",
" \\mathrm{d}x\n",
" \\mathrm{d}y\n",
"$$\n",
"\n",
"In our particular case the bounds would be from $0$ to $\\infty$ due to the restrictions our $f_{X,Y}$ impose.\n",
"\n",
"Due to experimentalism of this method, we will not invest in a detailed explanation, but show the computational steps."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"g = x+y\n",
"\n",
"e_gxy = integrate(\n",
" g*f_xy,\n",
" (x,0,oo),\n",
" (y,0,oo),\n",
")\n",
"\n",
"e_gxy_sqr = integrate(\n",
" g**2 * f_xy,\n",
" (x,0,oo),\n",
" (y,0,oo),\n",
")\n",
"\n",
"var_gxy = integrate(\n",
" (e_gxy-g)**2 *f_xy,\n",
" (x,0,oo),\n",
" (y,0,oo),\n",
")\n",
"\n",
"print(simplify(Eq(\n",
" var_w,\n",
" # Theorem 3.6.1\n",
" e_gxy_sqr - e_gxy**2,\n",
")))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2}{\\lambda^{2}}$"
],
"text/plain": [
"2/\\lambda**2"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e_gxy_sqr - e_gxy**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So from the computational results, you can see that the agreed with our previous results on $\\mathrm{Var}(X+Y)$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conclusion\n",
"\n",
"We have demonstrated three methods (two fully justified and one partially justified) to solve question 3.9.17. As a result we have seen the applications of many theorems (in particular 3.8.3, 3.9.1, and 3.9.5).\n",
"\n",
"In addition we have shown how one can weave symbolic computation into ones work flow in checking ones work.\n",
"\n",
"An online version (and possibly updated) version of this report can be found [here](https://gist.github.com/josh-hernandez-exe/2987a0feb052bde7f9945a902ac2b277)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Appendix\n",
"<a id='appendix'></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Limit from Eq $\\eqref{eq:limexp}$\n",
"<a id='appendix_limexp'></a>\n",
"\n",
"We wish to show\n",
"\\begin{equation*}\n",
" \\lim_{x\\to\\infty}\n",
" x^{n} \\cdot e^{-\\lambda \\cdot x}\n",
" = 0\n",
" \\label{appendix:limexp}\n",
"\\end{equation*}\n",
"\n",
"Let $n$ be an integer such that $n>0$ and $\\lambda$ a positive parameter.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\lim_{x\\to\\infty}\n",
" x^{n} \\cdot e^{-\\lambda \\cdot x}\n",
" &= \\lim_{x\\to\\infty}\n",
" \\frac{x^{n}}{e^{\\lambda \\cdot x}} \\\\\n",
" &= \\lim_{x\\to\\infty}\n",
" \\frac{\n",
" \\frac{\\mathrm{d} x^{n} }{\\mathrm{d} x}\n",
" }{\n",
" \\frac{\\mathrm{d} e^{\\lambda \\cdot x} }{\\mathrm{d} x}\n",
" } & \\mathrm{J1} \\\\\n",
" &= \\lim_{x\\to\\infty}\n",
" \\frac{\n",
" n \\cdot x^{n-1}\n",
" }{\n",
" \\lambda e^{\\lambda \\cdot x}\n",
" } \\\\\n",
" &= \\lim_{x\\to\\infty}\n",
" \\frac{\n",
" \\frac{\\mathrm{d} n \\cdot x^{n-1} }{\\mathrm{d} x}\n",
" }{\n",
" \\frac{\\mathrm{d} \\lambda e^{\\lambda \\cdot x} }{\\mathrm{d} x}\n",
" } & \\mathrm{J2} \\\\\n",
" &= \\lim_{x\\to\\infty}\n",
" \\frac{\n",
" n!\n",
" }{\n",
" \\lambda^{n} e^{\\lambda \\cdot x}\n",
" } & \\mathrm{J3} \\\\\n",
" &= \\frac{n!}{\\lambda^{n}}\\lim_{x\\to\\infty}\n",
" \\frac{\n",
" 1\n",
" }{\n",
" e^{\\lambda \\cdot x}\n",
" } & \\mathrm{J4} \\\\\n",
" &= \\frac{n!}{\\lambda^{n}} \\cdot 0 & \\mathrm{J5} \\\\\n",
" &= 0 \\\\\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: The limit is in indeterminate form ($\\frac{\\infty}{\\infty}$), so we can apply [L'Hopital's Rule](https://en.wikipedia.org/wiki/L%27H%C3%B4pital%27s_rule).\n",
"* $\\mathrm{J2}$: We have an indeterminate form again, and will apply L'Hopital's Rule. We will in fact have an indeterminate form until make $n$ application of L'Hopital's Rule in total.\n",
"* $\\mathrm{J3}$: Multiple things:\n",
" * Notice that $\\frac{\\mathrm{d}^n}{\\mathrm{d} x^n} x^{n} = \\frac{\\mathrm{d}^{n-1} }{\\mathrm{d} x^{n-1}} n x^{n-1} = \\frac{\\mathrm{d}^{n-2}}{\\mathrm{d} x^{n-2}} n (n-1) x^{n-2} = \\cdots = \\frac{\\mathrm{d}}{\\mathrm{d}x} n(n-1)(n-2) \\cdots 3\\cdot 2 x = n!$\n",
" * Similarly $\\frac{\\mathrm{d}^n}{\\mathrm{d} x^n} e^{\\lambda \\cdot x} = \\frac{\\mathrm{d}^{n-1}}{\\mathrm{d} x^{n-1}} \\lambda e^{\\lambda \\cdot x} = \\frac{\\mathrm{d}^{n-2}}{\\mathrm{d} x^{n-2}} \\lambda^2 e^{\\lambda \\cdot x} = \\cdots = \\lambda^n e^{\\lambda \\cdot x}$\n",
" * So at each application of L'Hopital's Rule we have an indeterminate form until the $n^{\\mathrm{th}}$ application.\n",
"* $\\mathrm{J4}$: Moving all values constant with respect to the limit to outside the limit.\n",
"* $\\mathrm{J5}$: Since $\\lim_{x\\to\\infty}\\frac{1}{e^{\\lambda \\cdot x}} = 0$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Integral from Eq $\\eqref{eq:intexp}$\n",
"<a id='appendix_intexp'></a>\n",
"\n",
"We want to show\n",
"\\begin{equation*}\n",
" \\int_{0}^{\\infty} e^{-\\lambda \\ x} = \\frac{1}{\\lambda}\n",
"\\end{equation*}\n",
"\n",
"\n",
"Let $\\lambda$ a positive parameter.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\int_{0}^{\\infty} e^{-\\lambda \\ x}\n",
" &= \\left[\n",
" \\frac{e^{-\\lambda \\ x}}{-\\lambda}\n",
" \\right]_{x=0}^{x\\to\\infty}\n",
" & \\mathrm{J1} \\\\\n",
" &= \\frac{1}{-\\lambda}\n",
" \\left[\n",
" \\left(\n",
" \\lim_{x\\to\\infty}e^{-\\lambda \\ x}\n",
" \\right)\n",
" -\n",
" \\left(\n",
" e^{-\\lambda \\ 0}\n",
" \\right)\n",
" \\right]\n",
" \\\\\n",
" &= \\frac{1}{-\\lambda}\n",
" \\left[\n",
" 0\n",
" -\n",
" 1\n",
" \\right]\n",
" & \\mathrm{J2} \\\\\n",
" &= \\frac{1}{\\lambda}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying the antiderivative of $e^{ax}$ \n",
"* $\\mathrm{J2}$: Since $\\lim_{x\\to\\infty}e^{-x} = 0$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Integral from Eq $\\eqref{eq:intxexp}$\n",
"<a id='appendix_intxexp'></a>\n",
"\n",
"We want to show\n",
"\\begin{equation*}\n",
" \\int_{0}^{\\infty} x e^{-\\lambda \\ x} = \\frac{1}{\\lambda^2}\n",
"\\end{equation*}\n",
"\n",
"\n",
"Let $\\lambda$ a positive parameter.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\int_{0}^{\\infty} x e^{-\\lambda \\ x}\n",
" &= \\left[\n",
" \\left(x \\cdot \n",
" \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}\n",
" \\right)\n",
" \\bigg|_{x=0}^{x\\to\\infty}\n",
" - \\int_{0}^{\\infty} \\frac{e^{-\\lambda\\cdot x}}{-\\lambda} \\mathrm{d}x \\right] & \\mathrm{J1} \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\n",
" \\left[\n",
" \\left(x \\cdot e^{-\\lambda\\cdot x} \\right)\n",
" \\bigg|_{x=0}^{x\\to\\infty}\n",
" - \\int_{0}^{\\infty} e^{-\\lambda\\cdot x} \\mathrm{d}x\n",
" \\right] \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\n",
" \\left[\n",
" \\left(\n",
" \\left[\n",
" \\lim_{x\\to\\infty} x \\cdot e^{-\\lambda\\cdot x}\n",
" \\right]\n",
" -\n",
" \\left[\n",
" 0 \\cdot e^{-\\lambda\\cdot 0}\n",
" \\right]\n",
" \\right)\n",
" -\n",
" \\int_{0}^{\\infty} e^{-\\lambda\\cdot x} \\mathrm{d}x\n",
" \\right] \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\n",
" \\left[\n",
" \\left(\n",
" 0\n",
" -\n",
" 0\n",
" \\right)\n",
" -\n",
" \\int_{0}^{\\infty} e^{-\\lambda\\cdot x} \\mathrm{d}x\n",
" \\right] & \\mathrm{J2} \\\\ \\\\\n",
" &= \\frac{1}{\\lambda}\n",
" \\int_{0}^{\\infty} e^{-\\lambda\\cdot x} \\mathrm{d}x\n",
" & \\mathrm{J2} \\\\ \\\\\n",
" &= \\frac{1}{\\lambda}\n",
" \\left[\n",
" \\frac{1}{\\lambda}\n",
" \\right]\n",
" & \\mathrm{J3} \\\\ \\\\\n",
" &= \\frac{1}{\\lambda^2}\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"\n",
"Justifications:\n",
"\n",
"* $\\mathrm{J1}$: Applying integration by parts ($\\int f\\mathrm{d}g = fg - \\int g \\mathrm{d}f$)\n",
" * with $f=x$ and $\\mathrm{d}g=e^{-\\lambda\\cdot x}\\mathrm{d}x$\n",
" * thus $\\mathrm{d}f=\\mathrm{d}x$ and $g=\\frac{e^{-\\lambda\\cdot x}}{-\\lambda}$\n",
"* $\\mathrm{J2}$: Since [previously](#appendix_limexp) we have shown that $\\lim_{x\\to\\infty}x^n e^{-\\lambda x} =0$\n",
"* $\\mathrm{J3}$: Since [previously](#appendix_intexp) we have shown $\\int_{0}^{\\infty} e^{-\\lambda \\ x} = \\frac{1}{\\lambda}$\n",
"* $\\mathrm{J4}$: Since $\\lim_{x\\to\\infty}e^{-x} = 0$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Integral from Eq $\\eqref{eq:intxsqexp}$\n",
"<a id='appendix_intxsqexp'></a>\n",
"\n",
"We want to show\n",
"\n",
"$$\n",
"\\int_{0}^{\\infty} x^2 e^{-\\lambda \\ x} = \\frac{2}{\\lambda^3}\n",
"$$\n",
"\n",
"Let $\\lambda$ a positive parameter.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\int_{0}^{\\infty} x^2 e^{-\\lambda \\ x}\n",
" &= \\left(\n",
" \\left[\n",
" x^2 \\cdot \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}\n",
" \\right]_{x=0}^{x\\to\\infty}\n",
" -\n",
" \\int_{0}^{\\infty}\n",
" \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}\n",
" \\cdot\n",
" 2x\n",
" \\mathrm{d}x\n",
" \\right)\n",
" & \\mathrm{J1} \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" x^2 \\cdot e^{-\\lambda\\cdot x}\n",
" \\right]_{x=0}^{x\\to\\infty}\n",
" -\n",
" 2\n",
" \\int_{0}^{\\infty}\n",
" x\n",
" e^{-\\lambda\\cdot x}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" x^2 \\cdot e^{-\\lambda\\cdot x}\n",
" \\right]_{x=0}^{x\\to\\infty}\n",
" -\n",
" 2\n",
" \\frac{1}{\\lambda^2}\n",
" \\right)\n",
" & \\mathrm{J2} \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" \\left(\n",
" \\lim_{x\\to\\infty}\n",
" x^2 \\cdot e^{-\\lambda\\cdot x}\n",
" \\right)\n",
" -\n",
" \\left(\n",
" 0^2 \\cdot e^{-\\lambda\\cdot 0}\n",
" \\right)\n",
" \\right]\n",
" -\n",
" \\frac{2}{\\lambda^2}\n",
" \\right)\n",
" \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" 0\n",
" -\n",
" 0\n",
" \\right]\n",
" -\n",
" \\frac{2}{\\lambda^2}\n",
" \\right)\n",
" & \\mathrm{J3} \\\\ \\\\\n",
" &= \\frac{2}{\\lambda^3} \\\\ \\\\\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"\n",
"* $\\mathrm{J1}$: Apply integration by parts $\\int f \\mathrm{d}g = fg -\\int g \\mathrm{d}f$\n",
" * $f = x^2$ and $\\mathrm{d}g = e^{-\\lambda\\cdot x}$\n",
" * $\\mathrm{d}f = 2x\\mathrm{d}x$ and $g = \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}$\n",
"* $\\mathrm{J2}$: Since [previously](#appendix_intxexp) we have shown $\\int_{0}^{\\infty} x e^{-\\lambda \\ x} = \\frac{1}{\\lambda^2}$\n",
"* $\\mathrm{J3}$: Since [previously](#appendix_limexp) we have shown that $\\lim_{x\\to\\infty}x^n e^{-\\lambda x} =0$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Integral needed for $\\mathrm{E}[W^2]$ for [Method 2](#e_wsqr)\n",
"\n",
"<a id=\"appendix_intxcubeexp\"></a>\n",
"\n",
"We want to show\n",
"$$\n",
"\\int_{0}^{\\infty} x^3 e^{-\\lambda \\ x} = \\frac{6}{\\lambda^4}\n",
"$$\n",
"\n",
"This integral result was used [here](#e_wsqr) [$\\ref{sec:e_wsqr}$]. Let $\\lambda$ a positive parameter.\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" \\int_{0}^{\\infty} x^3 e^{-\\lambda \\ x}\n",
" &= \\left(\n",
" \\left[\n",
" x^3 \\cdot \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}\n",
" \\right]_{x=0}^{x\\to\\infty}\n",
" -\n",
" \\int_{0}^{\\infty}\n",
" \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}\n",
" \\cdot\n",
" 3x^{2}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" & \\mathrm{J1} \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" x^3 \\cdot e^{-\\lambda\\cdot x}\n",
" \\right]_{x=0}^{x\\to\\infty}\n",
" -\n",
" \\int_{0}^{\\infty}\n",
" e^{-\\lambda\\cdot x}\n",
" \\cdot\n",
" 3x^{2}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" \\left(\n",
" \\lim_{x\\to\\infty}\n",
" x^3 \\cdot e^{-\\lambda\\cdot x}\n",
" \\right)\n",
" -\n",
" \\left(\n",
" 0^3 \\cdot e^{-\\lambda\\cdot 0}\n",
" \\right)\n",
" \\right]\n",
" -\n",
" \\int_{0}^{\\infty}\n",
" e^{-\\lambda\\cdot x}\n",
" \\cdot\n",
" 3x^{2}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" \\\\ \\\\\n",
" &= \\frac{1}{-\\lambda}\\left(\n",
" \\left[\n",
" 0\n",
" -\n",
" 0\n",
" \\right]\n",
" -\n",
" \\int_{0}^{\\infty}\n",
" e^{-\\lambda\\cdot x}\n",
" \\cdot\n",
" 3x^{2}\n",
" \\mathrm{d}x\n",
" \\right)\n",
" & \\mathrm{J2} \\\\ \\\\\n",
" &= \\frac{3}{\\lambda}\n",
" \\int_{0}^{\\infty}\n",
" x^{2}\n",
" e^{-\\lambda\\cdot x}\n",
" \\mathrm{d}x\n",
" \\\\ \\\\\n",
" &= \\frac{1}{\\lambda}\n",
" \\left(\n",
" \\frac{2}{\\lambda^3}\n",
" \\right)\n",
" & \\mathrm{J3} \\\\ \\\\\n",
" &= \\frac{6}{\\lambda^4} \\\\ \\\\\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Justifications:\n",
"\n",
"\n",
"* $\\mathrm{J1}$: Apply integration by parts $\\int f \\mathrm{d}g = fg -\\int g \\mathrm{d}f$\n",
" * $f = x^3$ and $\\mathrm{d}g = e^{-\\lambda\\cdot x}$\n",
" * $\\mathrm{d}f = 3x^2\\mathrm{d}x$ and $g = \\frac{e^{-\\lambda\\cdot x}}{-\\lambda}$\n",
"* $\\mathrm{J2}$: Since [previously](#appendix_limexp) we have shown that $\\lim_{x\\to\\infty}x^n e^{-\\lambda x} =0$\n",
"* $\\mathrm{J3}$: Since [previously](#appendix_intxsqexp) we have shown $\\int_{0}^{\\infty} x^2 e^{-\\lambda \\ x} = \\frac{2}{\\lambda^3}$"
]
}
],
"metadata": {
"authors": [
{
"name": "Josh Hernandez"
}
],
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
},
"title": "Question 3.9.17"
},
"nbformat": 4,
"nbformat_minor": 4
}
@sciencecasey
Copy link

Amazing report, Josh!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment