Skip to content
Open
Show file tree
Hide file tree
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
76 changes: 5 additions & 71 deletions lecturebook/lecture04/hands-on-04.2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@
]
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:6: SyntaxWarning: invalid escape sequence '\\p'\n",
"<>:6: SyntaxWarning: invalid escape sequence '\\p'\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/2154087378.py:6: SyntaxWarning: invalid escape sequence '\\p'\n",
" ax.set_ylabel(\"$\\phi(z)$\")\n"
]
},
{
"data": {
"image/svg+xml": [
Expand Down Expand Up @@ -727,7 +717,7 @@
"output_type": "display_data"
}
],
"source": "fig, ax = plt.subplots()\nimport numpy as np\nzs = np.linspace(-4.0, 4.0, 100)\nax.plot(zs, Z.pdf(zs))\nax.set_xlabel(\"$z$\")\nax.set_ylabel(\"$\\phi(z)$\")\nsns.despine(trim=True);"
"source": "fig, ax = plt.subplots()\nimport numpy as np\nzs = np.linspace(-4.0, 4.0, 100)\nax.plot(zs, Z.pdf(zs))\nax.set_xlabel(\"$z$\")\nax.set_ylabel(r\"$\\phi(z)$\")\nsns.despine(trim=True);"
},
{
"cell_type": "markdown",
Expand All @@ -749,16 +739,6 @@
]
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:4: SyntaxWarning: invalid escape sequence '\\P'\n",
"<>:4: SyntaxWarning: invalid escape sequence '\\P'\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/625513495.py:4: SyntaxWarning: invalid escape sequence '\\P'\n",
" ax.set_ylabel(\"$\\Phi(z)$\")\n"
]
},
{
"data": {
"image/svg+xml": [
Expand Down Expand Up @@ -1396,7 +1376,7 @@
"output_type": "display_data"
}
],
"source": "fig, ax = plt.subplots()\nax.plot(zs, Z.cdf(zs))\nax.set_xlabel(\"$z$\")\nax.set_ylabel(\"$\\Phi(z)$\")\nsns.despine(trim=True);"
"source": "fig, ax = plt.subplots()\nax.plot(zs, Z.cdf(zs))\nax.set_xlabel(\"$z$\")\nax.set_ylabel(r\"$\\Phi(z)$\")\nsns.despine(trim=True);"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -1600,24 +1580,6 @@
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:5: SyntaxWarning: invalid escape sequence '\\p'\n",
"<>:6: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:8: SyntaxWarning: invalid escape sequence '\\p'\n",
"<>:5: SyntaxWarning: invalid escape sequence '\\p'\n",
"<>:6: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:8: SyntaxWarning: invalid escape sequence '\\p'\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/2312143129.py:5: SyntaxWarning: invalid escape sequence '\\p'\n",
" ax.plot(zs, Z.pdf(zs), label=\"$\\phi(x)$\")\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/2312143129.py:6: SyntaxWarning: invalid escape sequence '\\m'\n",
" ax.plot(zs, st.norm(loc=1.0).pdf(zs), label=\"$N(x|\\mu=1,\\sigma^2=1)$\")\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/2312143129.py:8: SyntaxWarning: invalid escape sequence '\\p'\n",
" ax.set_ylabel(\"$\\phi(x)$\")\n"
]
},
{
"data": {
"image/svg+xml": [
Expand Down Expand Up @@ -2997,7 +2959,7 @@
"output_type": "display_data"
}
],
"source": "# Plot the PDF of a standard normal and the PDF of a non-zero mean, unit variance normal\n# with an arrow indicating the change from the first to the second.\nfig, ax = plt.subplots()\nzs = np.linspace(-4.0, 4.0, 100)\nax.plot(zs, Z.pdf(zs), label=\"$\\phi(x)$\")\nax.plot(zs, st.norm(loc=1.0).pdf(zs), label=\"$N(x|\\mu=1,\\sigma^2=1)$\")\nax.set_xlabel(\"$x$\")\nax.set_ylabel(\"$\\phi(x)$\")\nax.legend(loc=\"best\", frameon=False)\nax.annotate(\"\", xytext=(0.0, 0.4), xy=(1.0, 0.4), arrowprops=dict(arrowstyle=\"->\",\n color=sns.color_palette()[2],\n lw=2))\nplt.title(\"The mean translates the standard normal PDF\")\nsns.despine(trim=True);\n"
"source": "# Plot the PDF of a standard normal and the PDF of a non-zero mean, unit variance normal\n# with an arrow indicating the change from the first to the second.\nfig, ax = plt.subplots()\nzs = np.linspace(-4.0, 4.0, 100)\nax.plot(zs, Z.pdf(zs), label=r\"$\\phi(x)$\")\nax.plot(zs, st.norm(loc=1.0).pdf(zs), label=r\"$N(x|\\mu=1,\\sigma^2=1)$\")\nax.set_xlabel(\"$x$\")\nax.set_ylabel(r\"$\\phi(x)$\")\nax.legend(loc=\"best\", frameon=False)\nax.annotate(\"\", xytext=(0.0, 0.4), xy=(1.0, 0.4), arrowprops=dict(arrowstyle=\"->\",\n color=sns.color_palette()[2],\n lw=2))\nplt.title(\"The mean translates the standard normal PDF\")\nsns.despine(trim=True);\n"
},
{
"cell_type": "markdown",
Expand All @@ -3016,20 +2978,6 @@
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:4: SyntaxWarning: invalid escape sequence '\\p'\n",
"<>:5: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:4: SyntaxWarning: invalid escape sequence '\\p'\n",
"<>:5: SyntaxWarning: invalid escape sequence '\\m'\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/775679383.py:4: SyntaxWarning: invalid escape sequence '\\p'\n",
" ax.plot(zs, Z.pdf(zs), label=\"$\\phi(x)$\")\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/775679383.py:5: SyntaxWarning: invalid escape sequence '\\m'\n",
" ax.plot(zs, st.norm(loc=0.0, scale=np.sqrt(2.0)).pdf(zs), label=\"$N(x|\\mu=0,\\sigma^2=2)$\")\n"
]
},
{
"data": {
"image/svg+xml": [
Expand Down Expand Up @@ -4504,7 +4452,7 @@
"output_type": "display_data"
}
],
"source": "# Plot the PDF of a standard normal and the pdf of a zero mean Normal with variance two\nfig, ax = plt.subplots()\nzs = np.linspace(-4.0, 4.0, 100)\nax.plot(zs, Z.pdf(zs), label=\"$\\phi(x)$\")\nax.plot(zs, st.norm(loc=0.0, scale=np.sqrt(2.0)).pdf(zs), label=\"$N(x|\\mu=0,\\sigma^2=2)$\")\nax.set_xlabel(\"$x$\")\nax.set_ylabel(\"$p(x)$\")\nax.legend(loc=\"best\", frameon=False)\nplt.title(\"The variance scales the standard normal PDF\")\nsns.despine(trim=True);"
"source": "# Plot the PDF of a standard normal and the pdf of a zero mean Normal with variance two\nfig, ax = plt.subplots()\nzs = np.linspace(-4.0, 4.0, 100)\nax.plot(zs, Z.pdf(zs), label=r\"$\\phi(x)$\")\nax.plot(zs, st.norm(loc=0.0, scale=np.sqrt(2.0)).pdf(zs), label=r\"$N(x|\\mu=0,\\sigma^2=2)$\")\nax.set_xlabel(\"$x$\")\nax.set_ylabel(r\"$p(x)$\")\nax.legend(loc=\"best\", frameon=False)\nplt.title(\"The variance scales the standard normal PDF\")\nsns.despine(trim=True);"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -5521,20 +5469,6 @@
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:2: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:3: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:2: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:3: SyntaxWarning: invalid escape sequence '\\m'\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/1046293625.py:2: SyntaxWarning: invalid escape sequence '\\m'\n",
" ax.hist(x_samples, density=True, alpha=0.5, label=\"Samples from $X = \\mu + \\sigma Z$\")\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68902/1046293625.py:3: SyntaxWarning: invalid escape sequence '\\m'\n",
" ax.plot(xs, X.pdf(xs), label=\"PDF of $N(\\mu, \\sigma^2)$\")\n"
]
},
{
"data": {
"image/svg+xml": [
Expand Down Expand Up @@ -6884,7 +6818,7 @@
"output_type": "display_data"
}
],
"source": "fig, ax = plt.subplots()\nax.hist(x_samples, density=True, alpha=0.5, label=\"Samples from $X = \\mu + \\sigma Z$\")\nax.plot(xs, X.pdf(xs), label=\"PDF of $N(\\mu, \\sigma^2)$\")\nax.set_xlabel(\"$x$\")\nax.set_ylabel(\"$p(x)$\")\nplt.legend(loc=\"best\", frameon=False)\nsns.despine(trim=True);"
"source": "fig, ax = plt.subplots()\nax.hist(x_samples, density=True, alpha=0.5, label=r\"Samples from $X = \\mu + \\sigma Z$\")\nax.plot(xs, X.pdf(xs), label=r\"PDF of $N(\\mu, \\sigma^2)$\")\nax.set_xlabel(\"$x$\")\nax.set_ylabel(\"$p(x)$\")\nplt.legend(loc=\"best\", frameon=False)\nsns.despine(trim=True);"
},
{
"cell_type": "markdown",
Expand Down
17 changes: 5 additions & 12 deletions lecturebook/lecture04/reading-04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3481,19 +3481,12 @@
"iopub.status.busy": "2025-06-16T15:03:23.011568Z",
"iopub.status.idle": "2025-06-16T15:03:23.125520Z",
"shell.execute_reply": "2025-06-16T15:03:23.125261Z"
}
},
"tags": [
"hide-input"
]
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:7: SyntaxWarning: invalid escape sequence '\\m'\n",
"<>:7: SyntaxWarning: invalid escape sequence '\\m'\n",
"/var/folders/3n/r5vj11ss7lzcdl10vfhb_mw00000gs/T/ipykernel_68920/3227439545.py:7: SyntaxWarning: invalid escape sequence '\\m'\n",
" plt.axvline(x=0, color='r', linestyle='--', label='$\\mathbb{E}[X]$')\n"
]
},
{
"data": {
"image/svg+xml": [
Expand Down Expand Up @@ -4815,7 +4808,7 @@
"output_type": "display_data"
}
],
"source": "# Plot the pdf of a mixture of two normal random variables with different means but the same variance\nfrom scipy.stats import norm\nx = np.linspace(-10, 10, 200)\ny = 0.5 * norm.pdf(x, loc=-5) + 0.5 * norm.pdf(x, loc=5)\nplt.plot(x, y)\n# Add the expected value\nplt.axvline(x=0, color='r', linestyle='--', label='$\\mathbb{E}[X]$')\nplt.xlabel('$x$')\nplt.ylabel('$f_X(x)$')\nsns.despine(trim=True)\nplt.title(\"Example of a case in which the expected value is not representative of the distribution\");"
"source": "# Plot the pdf of a mixture of two normal random variables with different means but the same variance\nfrom scipy.stats import norm\nx = np.linspace(-10, 10, 200)\ny = 0.5 * norm.pdf(x, loc=-5) + 0.5 * norm.pdf(x, loc=5)\nplt.plot(x, y)\n# Add the expected value\nplt.axvline(x=0, color='r', linestyle='--', label=r'$\\mathbb{E}[X]$')\nplt.legend()\nplt.xlabel('$x$')\nplt.ylabel('$f_X(x)$')\nsns.despine(trim=True)\nplt.title(\"Example of a case in which the expected value is not representative of the distribution\");"
},
{
"cell_type": "markdown",
Expand Down