diff --git a/notebooks/T8 - Monte-Carlo & ensembles.ipynb b/notebooks/T8 - Monte-Carlo & ensembles.ipynb index 2f4f65b..219de09 100644 --- a/notebooks/T8 - Monte-Carlo & ensembles.ipynb +++ b/notebooks/T8 - Monte-Carlo & ensembles.ipynb @@ -123,7 +123,7 @@ { "cell_type": "code", "execution_count": null, - "id": "311ffb67", + "id": "84bec352", "metadata": {}, "outputs": [], "source": [ @@ -136,22 +136,13 @@ "# Using a loop (\"slow\")\n", "E = np.zeros((xDim, N))\n", "for n in range(N):\n", - " E[:, n] = mu + L@Z[:, n]\n", - "\n", - "# Comparison to true values\n", - "fig, (ax1, ax2) = plt.subplots(ncols=2)\n", - "kws = dict(vmin=C.min(), vmax=C.max(), cmap=\"Blues\")\n", - "cc = ax1.matshow(C, **kws)\n", - "cc = ax2.matshow(np.cov(E), **kws)\n", - "fig.colorbar(cc, ax=(ax1, ax2), orientation='horizontal')\n", - "with np.printoptions(precision=1):\n", - " print(\"Estimated mean =\", np.mean(E, axis=1))" + " E[:, n] = mu + L@Z[:, n]" ] }, { "cell_type": "code", "execution_count": null, - "id": "fae3cf5b", + "id": "4d254ff9", "metadata": {}, "outputs": [], "source": [ @@ -160,11 +151,24 @@ }, { "cell_type": "markdown", - "id": "f6c91f60", + "id": "3f46e7f6", "metadata": {}, "source": [ - "Note that the estimates are not exact. \n", - "They contain some amount of random error, a.k.a. ***sampling error***." + "The following prints some numbers that can be used to ascertain if you got it right.\n", + "Note that the estimates will never be exact:\n", + "they contain some amount of random error, a.k.a. ***sampling error***." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "311ffb67", + "metadata": {}, + "outputs": [], + "source": [ + "with np.printoptions(precision=1):\n", + " print(\"Estimated mean =\", np.mean(E, axis=1))\n", + " print(\"Estimated cov =\", np.cov(E), sep=\"\\n\")" ] }, { @@ -233,7 +237,7 @@ }, { "cell_type": "markdown", - "id": "b541c271", + "id": "471bc6ef", "metadata": {}, "source": [ "It can be shown that the above estimators are ***consistent and unbiased***.\n", @@ -303,7 +307,7 @@ }, { "cell_type": "markdown", - "id": "e5cc312d", + "id": "67b387e8", "metadata": {}, "source": [ "**Exc (optional) -- Error notions:**\n", @@ -317,7 +321,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4417a07b", + "id": "a38ee124", "metadata": {}, "outputs": [], "source": [ diff --git a/notebooks/scripts/T8 - Monte-Carlo & ensembles.py b/notebooks/scripts/T8 - Monte-Carlo & ensembles.py index caf2626..6d74f48 100644 --- a/notebooks/scripts/T8 - Monte-Carlo & ensembles.py +++ b/notebooks/scripts/T8 - Monte-Carlo & ensembles.py @@ -99,22 +99,18 @@ def pdf_reconstructions(seed=5, nbins=10, bw=.3): for n in range(N): E[:, n] = mu + L@Z[:, n] -# Comparison to true values -fig, (ax1, ax2) = plt.subplots(ncols=2) -kws = dict(vmin=C.min(), vmax=C.max(), cmap="Blues") -cc = ax1.matshow(C, **kws) -cc = ax2.matshow(np.cov(E), **kws) -fig.colorbar(cc, ax=(ax1, ax2), orientation='horizontal') -with np.printoptions(precision=1): - print("Estimated mean =", np.mean(E, axis=1)) - - # + # show_answer('Gaussian sampling', 'b') # - -# Note that the estimates are not exact. -# They contain some amount of random error, a.k.a. ***sampling error***. +# The following prints some numbers that can be used to ascertain if you got it right. +# Note that the estimates will never be exact: +# they contain some amount of random error, a.k.a. ***sampling error***. + +with np.printoptions(precision=1): + print("Estimated mean =", np.mean(E, axis=1)) + print("Estimated cov =", np.cov(E), sep="\n") + # **Exc -- Moment estimation code:** Above, we used numpy's (`np`) functions to compute the sample-estimated mean and covariance matrix, # $\bx$ and $\barC$,