Skip to content

Commit

Permalink
Print, don't plot, the covar
Browse files Browse the repository at this point in the history
  • Loading branch information
patnr committed Sep 20, 2023
1 parent 2dbd8af commit d7a1cdb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
40 changes: 22 additions & 18 deletions notebooks/T8 - Monte-Carlo & ensembles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "311ffb67",
"id": "84bec352",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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": [
Expand All @@ -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\")"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -303,7 +307,7 @@
},
{
"cell_type": "markdown",
"id": "e5cc312d",
"id": "67b387e8",
"metadata": {},
"source": [
"**Exc (optional) -- Error notions:**\n",
Expand All @@ -317,7 +321,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4417a07b",
"id": "a38ee124",
"metadata": {},
"outputs": [],
"source": [
Expand Down
20 changes: 8 additions & 12 deletions notebooks/scripts/T8 - Monte-Carlo & ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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$,
Expand Down

0 comments on commit d7a1cdb

Please sign in to comment.