Skip to content

Commit e01525c

Browse files
correct computation of in posterior predictive checking section
1 parent 65f4cd9 commit e01525c

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

examples/spatial/nyc_bym.ipynb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,36 @@
838838
},
839839
{
840840
"cell_type": "code",
841-
"execution_count": 16,
841+
"execution_count": 6,
842842
"id": "8f8459d1-648d-48d6-ab24-1ec030dea611",
843843
"metadata": {},
844-
"outputs": [],
844+
"outputs": [
845+
{
846+
"ename": "ModuleNotFoundError",
847+
"evalue": "No module named 'pymc'",
848+
"output_type": "error",
849+
"traceback": [
850+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
851+
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
852+
"Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpymc\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpm\u001b[39;00m \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Assuming BYM_model is the model you have defined\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# Wrapping mixture and mu in pm.Deterministic in the model definition\u001b[39;00m\n",
853+
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pymc'"
854+
]
855+
}
856+
],
845857
"source": [
846-
"phi_pred = idata.posterior.phi.mean((\"chain\", \"draw\")).values\n",
847-
"beta0_pred = idata.posterior.beta0.mean((\"chain\", \"draw\")).values\n",
848-
"sigma_pred = idata.posterior.sigma.mean((\"chain\", \"draw\")).values\n",
849-
"y_predict = np.exp(log_E + beta0_pred + sigma_pred * (1 / scaling_factor) * phi_pred)"
858+
"import pymc as pm\n",
859+
"import numpy as np\n",
860+
"# Wrapping mixture and mu in pm.Deterministic in the model definition\n",
861+
"with BYM_model:\n",
862+
" mixture = pm.Deterministic('mixture', some_mixture_expression)\n",
863+
" mu = pm.Deterministic('mu', some_mu_expression)\n",
864+
"\n",
865+
"# Use pm.do to condition on rho=1 and sample posterior predictive\n",
866+
"with pm.do(BYM_model, {'rho': 1.0}):\n",
867+
" y_predict_rho_1 = pm.sample_posterior_predictive(idata, var_names=['mixture', 'mu'], predictions=True, extend_inferencedata=False)\n",
868+
"\n",
869+
"# Compute the mean of the predictions\n",
870+
"y_predict = y_predict_rho_1.predictions.mu.mean(dim=['chain', 'draw'])\n"
850871
]
851872
},
852873
{
@@ -1265,7 +1286,7 @@
12651286
"name": "python",
12661287
"nbconvert_exporter": "python",
12671288
"pygments_lexer": "ipython3",
1268-
"version": "3.11.0"
1289+
"version": "3.12.0"
12691290
},
12701291
"myst": {
12711292
"substitutions": {

examples/spatial/nyc_bym.myst.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,20 @@ The payoff of all this work is that we can now visualize what it means to decomp
383383
We'll extract the means of several parameters to generate predictions. In the first case, we'll visualize only the predictions that come from the spatial component of the model. In other words, we are assuming $\rho = 1$ and we ignore $\theta$ and social fragmentation.
384384

385385
```{code-cell} ipython3
386-
phi_pred = idata.posterior.phi.mean(("chain", "draw")).values
387-
beta0_pred = idata.posterior.beta0.mean(("chain", "draw")).values
388-
sigma_pred = idata.posterior.sigma.mean(("chain", "draw")).values
389-
y_predict = np.exp(log_E + beta0_pred + sigma_pred * (1 / scaling_factor) * phi_pred)
386+
import pymc as pm
387+
import numpy as np
388+
# Wrapping mixture and mu in pm.Deterministic in the model definition
389+
with BYM_model:
390+
mixture = pm.Deterministic('mixture', some_mixture_expression)
391+
mu = pm.Deterministic('mu', some_mu_expression)
392+
393+
# Use pm.do to condition on rho=1 and sample posterior predictive
394+
with pm.do(BYM_model, {'rho': 1.0}):
395+
y_predict_rho_1 = pm.sample_posterior_predictive(idata, var_names=['mixture', 'mu'], predictions=True, extend_inferencedata=False)
396+
397+
# Compute the mean of the predictions
398+
y_predict = y_predict_rho_1.predictions.mu.mean(dim=['chain', 'draw'])
399+
390400
```
391401

392402
Then we'll overlay our predictions onto the same {ref}`adjacency map we built earlier <adjacency-map>`.

0 commit comments

Comments
 (0)