|
57 | 57 | "from IPython.display import HTML\n",
|
58 | 58 | "from matplotlib import animation as ani\n",
|
59 | 59 | "from matplotlib import pyplot as plt\n",
|
| 60 | + "from theano import shared\n", |
60 | 61 | "from theano import tensor as tt\n",
|
61 | 62 | "\n",
|
62 | 63 | "print(f\"Running on PyMC3 v{pm.__version__}\")"
|
|
1681 | 1682 | "with pm.Model() as model:\n",
|
1682 | 1683 | " alpha = pm.Normal(\"alpha\", 0.0, 5.0, shape=K)\n",
|
1683 | 1684 | " beta = pm.Normal(\"beta\", 0.0, 5.0, shape=(1, K))\n",
|
1684 |
| - " x = pm.Data(\"x\", std_range)\n", |
| 1685 | + " x = pm.Data('x', std_range)\n", |
1685 | 1686 | " v = norm_cdf(alpha + pm.math.dot(x, beta))\n",
|
1686 | 1687 | " w = pm.Deterministic(\"w\", stick_breaking(v))"
|
1687 | 1688 | ]
|
|
1699 | 1700 | "tags": []
|
1700 | 1701 | },
|
1701 | 1702 | "source": [
|
1702 |
| - "We have defined `x` as a `pm.Data` container variable in order to use `PyMC3`'s posterior prediction capabilities later.\n", |
| 1703 | + "We have defined `x_lidar` as a [theano shared](https://theano-pymc.readthedocs.io/en/latest/library/compile/shared.html) variable in order to use `PyMC3`'s posterior prediction capabilities later.\n", |
1703 | 1704 | "\n",
|
1704 | 1705 | "While the dependent density regression model theoretically has infinitely many components, we must truncate the model to finitely many components (in this case, twenty) in order to express it using `PyMC3`. After sampling from the model, we will verify that truncation did not unduly influence our results.\n",
|
1705 | 1706 | "\n",
|
|
1785 | 1786 | "source": [
|
1786 | 1787 | "with model:\n",
|
1787 | 1788 | " tau = pm.Gamma(\"tau\", 1.0, 1.0, shape=K)\n",
|
1788 |
| - " y = pm.Data(\"y\", std_logratio)\n", |
| 1789 | + " y = pm.Data('y', std_logratio)\n", |
1789 | 1790 | " obs = pm.NormalMixture(\"obs\", w, mu, tau=tau, observed=y)"
|
1790 | 1791 | ]
|
1791 | 1792 | },
|
|
1941 | 1942 | }
|
1942 | 1943 | ],
|
1943 | 1944 | "source": [
|
1944 |
| - "trace.posterior[\"w\"].shape # (n_chains, n_samples, N, K)" |
| 1945 | + "trace.posterior['w'].shape # (n_chains, n_samples, N, K)" |
1945 | 1946 | ]
|
1946 | 1947 | },
|
1947 | 1948 | {
|
|
1983 | 1984 | "source": [
|
1984 | 1985 | "fig, ax = plt.subplots(figsize=(8, 6))\n",
|
1985 | 1986 | "\n",
|
1986 |
| - "w_posterior = trace.posterior[\"w\"].data\n", |
| 1987 | + "w_posterior = trace.posterior['w'].data\n", |
1987 | 1988 | "# reshape to (n_chains*n_samples, N, K)\n",
|
1988 | 1989 | "w_posterior = w_posterior.reshape(-1, w_posterior.shape[2], w_posterior.shape[3])\n",
|
1989 | 1990 | "ax.bar(np.arange(K) + 1, w_posterior.mean(axis=0).max(axis=0))\n",
|
|
2077 | 2078 | "lidar_pp_x = np.linspace(std_range.min() - 0.05, std_range.max() + 0.05, 100)\n",
|
2078 | 2079 | "\n",
|
2079 | 2080 | "with model:\n",
|
2080 |
| - " pm.set_data({\"x\": lidar_pp_x[:, np.newaxis]})\n", |
| 2081 | + " pm.set_data({'x': lidar_pp_x[:, np.newaxis]})\n", |
2081 | 2082 | " pp_trace = pm.sample_posterior_predictive(trace, PP_SAMPLES, random_seed=SEED)"
|
2082 | 2083 | ]
|
2083 | 2084 | },
|
|
0 commit comments